Rev 275 | Blame | Last modification | View Log | Download | RSS feed
#include "defines.h"#include "I2C2.h"static I2C2_DATA *i2c_data_p;// Set up the data structures for the base_I2C.code// Should be called once before any i2c routines are calledvoid I2C2_Init(I2C2_DATA *data) {i2c_data_p = data;i2c_data_p->buffer_in_len = 0;i2c_data_p->buffer_in_len_tmp = 0;i2c_data_p->buffer_in_read_ind = 0;i2c_data_p->buffer_in_write_ind = 0;i2c_data_p->buffer_out_ind = 0;i2c_data_p->buffer_out_len = 0;i2c_data_p->operating_mode = 0;i2c_data_p->operating_state = I2C_IDLE;i2c_data_p->return_status = 0;i2c_data_p->slave_in_last_byte = 0;i2c_data_p->slave_sending_data = 0;i2c_data_p->master_dest_addr = 0;i2c_data_p->master_status = I2C_MASTER_IDLE;// Enable I2C interruptPIE4bits.SSP2IE = 1;}// Setup the PIC to operate as a master.void I2C2_Configure_Master(uint8_t speed) {i2c_data_p->operating_mode = I2C_MODE_MASTER;I2C_2_CLK_TRIS = 1;I2C_2_DAT_TRIS = 1;SSP2STAT = 0x0;SSP2CON1 = 0x0;SSP2CON2 = 0x0;SSP2CON3 = 0x0;SSP2CON1bits.SSPM = 0x8; // I2C Master Modeif (speed == 0x01) {SSP2ADD = 0x13; // Operate at 400KHz (32MHz)SSP2STATbits.SMP = 1; // Disable Slew Rate Control} else if (speed == 0x02) {SSP2ADD = 0x07; // Operate at 1Mhz (32Mhz)SSP2STATbits.SMP = 1; // Disable Slew Rate Control} else {SSP2ADD = 0x4F; // Operate at 100KHz (32MHz)SSP2STATbits.SMP = 0; // Enable Slew Rate Control}SSP2CON1bits.SSPEN = 1; // Enable MSSP2 Module}// Sends length number of bytes in msg to specified address (no R/W bit)void I2C2_Master_Send(uint8_t address, uint8_t length, uint8_t *msg) {uint8_t i;if (length == 0)return;// Copy message to send into buffer and save length/addressfor (i = 0; i < length; i++) {i2c_data_p->buffer_in[i] = msg[i];}i2c_data_p->buffer_in_len = length;i2c_data_p->master_dest_addr = address;i2c_data_p->buffer_in_read_ind = 0;i2c_data_p->buffer_in_write_ind = 0;// Change status to 'next' operationi2c_data_p->operating_state = I2C_SEND_ADDR;i2c_data_p->master_status = I2C_MASTER_SEND;// Generate start conditionSSP2CON2bits.SEN = 1;}// Reads length number of bytes from address (no R/W bit)void I2C2_Master_Recv(uint8_t address, uint8_t length) {if (length == 0)return;// Save length and address to get data fromi2c_data_p->buffer_in_len = length;i2c_data_p->master_dest_addr = address;i2c_data_p->buffer_in_read_ind = 0;i2c_data_p->buffer_in_write_ind = 0;// Change status to 'next' operationi2c_data_p->operating_state = I2C_SEND_ADDR;i2c_data_p->master_status = I2C_MASTER_RECV;// Generate start conditionSSP2CON2bits.SEN = 1;}// Writes msg to address then reads length number of bytes from addressvoid I2C2_Master_Restart(uint8_t address, uint8_t msg, uint8_t length) {uint8_t c;if (length == 0) {c = msg;I2C2_Master_Send(address, 1, &c);return;}// Save length and address to get data fromi2c_data_p->buffer_in[0] = msg;i2c_data_p->buffer_in_len = length;i2c_data_p->master_dest_addr = address;i2c_data_p->buffer_in_read_ind = 0;i2c_data_p->buffer_in_write_ind = 0;// Change status to 'next' operationi2c_data_p->operating_state = I2C_SEND_ADDR;i2c_data_p->master_status = I2C_MASTER_RESTART;// Generate start conditionSSP2CON2bits.SEN = 1;}// Setup the PIC to operate as a slave. The address must not include the R/W bitvoid I2C2_Configure_Slave(uint8_t addr) {i2c_data_p->operating_mode = I2C_MODE_SLAVE;// Ensure the two lines are set for input (we are a slave)I2C_2_CLK_TRIS = 1;I2C_2_DAT_TRIS = 1;SSP2ADD = addr << 1; // Set the slave addressSSP2STAT = 0x0;SSP2CON1 = 0x0;SSP2CON2 = 0x0;SSP2CON3 = 0x0;SSP2CON1bits.SSPM = 0xE; // Enable Slave 7-bit w/ start/stop interruptsSSP2STATbits.SMP = 1; // Slew OffSSP2CON2bits.SEN = 1; // Enable clock-stretchingSSP1CON3bits.PCIE = 1; // Interrupt on stop conditionSSP1CON3bits.SCIE = 0; // Disable interrupt on start conditionSSP2CON1bits.SSPEN = 1; // Enable MSSP2 Module}void I2C2_Interrupt_Handler() {// Call interrupt depending on which mode we are operating inif (i2c_data_p->operating_mode == I2C_MODE_MASTER) {I2C2_Interrupt_Master();} else if (i2c_data_p->operating_mode == I2C_MODE_SLAVE) {I2C2_Interrupt_Slave();}}// An internal subroutine used in the master version of the i2c_interrupt_handlervoid I2C2_Interrupt_Master() {// If we are in the middle of sending dataif (i2c_data_p->master_status == I2C_MASTER_SEND) {switch (i2c_data_p->operating_state) {case I2C_IDLE:break;case I2C_SEND_ADDR:// Send the address with read bit seti2c_data_p->operating_state = I2C_CHECK_ACK_SEND;SSP2BUF = (i2c_data_p->master_dest_addr << 1) | 0x0;break;case I2C_CHECK_ACK_SEND:// Check if ACK is received or notif (!SSP2CON2bits.ACKSTAT) {// If an ACK is received, send next byte of dataif (i2c_data_p->buffer_in_read_ind < i2c_data_p->buffer_in_len) {SSP2BUF = i2c_data_p->buffer_in[i2c_data_p->buffer_in_read_ind];i2c_data_p->buffer_in_read_ind++;} else {// If no more data is to be sent, send stop biti2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_SEND_OK;}} else {// If a NACK is received, stop transmission and send errori2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_SEND_FAIL;}break;}// If we are in the middle of receiving data} else if (i2c_data_p->master_status == I2C_MASTER_RECV) {switch (i2c_data_p->operating_state) {case I2C_IDLE:break;case I2C_SEND_ADDR:// Send address with write bit seti2c_data_p->operating_state = I2C_CHECK_ACK_RECV;uint8_t tmp = (i2c_data_p->master_dest_addr << 1);tmp |= 0x01;SSP2BUF = tmp;break;case I2C_CHECK_ACK_RECV:// Check if ACK is receivedif (!SSP2CON2bits.ACKSTAT) {// If an ACK is received, set module to receive 1 byte of datai2c_data_p->operating_state = I2C_RCV_DATA;SSP2CON2bits.RCEN = 1;} else {// If a NACK is received, stop transmission and send errori2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_RECV_FAIL;}break;case I2C_RCV_DATA:// On receive, save byte into buffer// TODO: Handle I2C buffer overflowi2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = SSP2BUF;i2c_data_p->buffer_in_write_ind++;if (i2c_data_p->buffer_in_write_ind < i2c_data_p->buffer_in_len) {// If we still need to read, send an ACK to the slavei2c_data_p->operating_state = I2C_REQ_DATA;SSP2CON2bits.ACKDT = 0; // ACKSSP2CON2bits.ACKEN = 1;} else {// If we are done reading, send an NACK to the slavei2c_data_p->operating_state = I2C_SEND_STOP;SSP2CON2bits.ACKDT = 1; // NACKSSP2CON2bits.ACKEN = 1;}break;case I2C_REQ_DATA:// Set module to receive one byte of datai2c_data_p->operating_state = I2C_RCV_DATA;SSP2CON2bits.RCEN = 1;break;case I2C_SEND_STOP:// Send the stop bit and copy message to send to Main()i2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_RECV_OK;break;}} else if (i2c_data_p->master_status == I2C_MASTER_RESTART) {switch (i2c_data_p->operating_state) {case I2C_IDLE:break;case I2C_SEND_ADDR:// Send the address with read bit seti2c_data_p->operating_state = I2C_CHECK_ACK_SEND;SSP2BUF = (i2c_data_p->master_dest_addr << 1) | 0x0;break;case I2C_CHECK_ACK_SEND:// Check if ACK is received or notif (!SSP2CON2bits.ACKSTAT) {// If an ACK is received, send first byte of dataSSP2BUF = i2c_data_p->buffer_in[0];i2c_data_p->operating_state = I2C_CHECK_ACK_RESTART;} else {// If a NACK is received, stop transmission and send errori2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_SEND_FAIL;}break;case I2C_CHECK_ACK_RESTART:if (!SSP2CON2bits.ACKSTAT) {SSP2CON2bits.RSEN = 1;i2c_data_p->operating_state = I2C_SEND_ADDR_2;} else {// If a NACK is received, stop transmission and send errori2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_SEND_FAIL;}break;case I2C_SEND_ADDR_2:// Send the address with read bit seti2c_data_p->operating_state = I2C_CHECK_ACK_RECV;uint8_t tmp = (i2c_data_p->master_dest_addr << 1);tmp |= 0x01;SSP2BUF = tmp;break;case I2C_CHECK_ACK_RECV:// Check if ACK is receivedif (!SSP2CON2bits.ACKSTAT) {// If an ACK is received, set module to receive 1 byte of datai2c_data_p->operating_state = I2C_RCV_DATA;SSP2CON2bits.RCEN = 1;} else {// If a NACK is received, stop transmission and send errori2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_RECV_FAIL;}break;case I2C_RCV_DATA:// On receive, save byte into buffer// TODO: Handle I2C buffer overflowi2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = SSP2BUF;i2c_data_p->buffer_in_write_ind++;if (i2c_data_p->buffer_in_write_ind < i2c_data_p->buffer_in_len) {// If we still need to read, send an ACK to the slavei2c_data_p->operating_state = I2C_REQ_DATA;SSP2CON2bits.ACKDT = 0; // ACKSSP2CON2bits.ACKEN = 1;} else {// If we are done reading, send an NACK to the slavei2c_data_p->operating_state = I2C_SEND_STOP;SSP2CON2bits.ACKDT = 1; // NACKSSP2CON2bits.ACKEN = 1;}break;case I2C_REQ_DATA:// Set module to receive one byte of datai2c_data_p->operating_state = I2C_RCV_DATA;SSP2CON2bits.RCEN = 1;break;case I2C_SEND_STOP:// Send the stop biti2c_data_p->operating_state = I2C_IDLE;SSP2CON2bits.PEN = 1;i2c_data_p->master_status = I2C_MASTER_IDLE;i2c_data_p->return_status = I2C_RECV_OK;break;}}}void I2C2_Interrupt_Slave() {uint8_t received_data;uint8_t data_read_from_buffer = 0;uint8_t data_written_to_buffer = 0;uint8_t overrun_error = 0;// Clear SSP2OV (overflow bit)if (SSP2CON1bits.SSPOV == 1) {SSP2CON1bits.SSPOV = 0;// We failed to read the buffer in time, so we know we// can't properly receive this message, just put us in the// a state where we are looking for a new messagei2c_data_p->operating_state = I2C_IDLE;overrun_error = 1;i2c_data_p->return_status = I2C_ERR_OVERRUN;}// Read SPPxBUF if it is fullif (SSP2STATbits.BF == 1) {received_data = SSP2BUF;// DBG_PRINT_I2C("I2C: data read from buffer: %x\r\n", SSP2BUF);data_read_from_buffer = 1;}if (!overrun_error) {switch (i2c_data_p->operating_state) {case I2C_IDLE:// {// // Ignore anything except a start// if (SSP2STATbits.S == 1) {// i2c_data_p->buffer_in_len_tmp = 0;// i2c_data_p->operating_state = I2C_STARTED;// }// break;// }// case I2C_STARTED:{// In this case, we expect either an address or a stop bitif (SSP2STATbits.P == 1) {// Return to idle modei2c_data_p->operating_state = I2C_IDLE;} else if (data_read_from_buffer) {if (SSP2STATbits.D_nA == 0) {// Address receivedif (SSP2STATbits.R_nW == 0) {// Slave write modei2c_data_p->operating_state = I2C_RCV_DATA;} else {// Slave read modei2c_data_p->operating_state = I2C_SEND_DATA;// Process the first byte immediatly if sending datagoto send;}} else {i2c_data_p->operating_state = I2C_IDLE;i2c_data_p->return_status = I2C_ERR_NOADDR;}}break;}send:case I2C_SEND_DATA:{if (!i2c_data_p->slave_sending_data) {// If we are not currently sending data, figure out what to reply withif (I2C2_Process_Receive(i2c_data_p->slave_in_last_byte)) {// Data exists to be returned, send first byteSSP2BUF = i2c_data_p->buffer_out[0];i2c_data_p->buffer_out_ind = 1;i2c_data_p->slave_sending_data = 1;data_written_to_buffer = 1;} else {// Unknown requesti2c_data_p->slave_sending_data = 0;i2c_data_p->operating_state = I2C_IDLE;}} else {// Sending remaining data back to masterif (i2c_data_p->buffer_out_ind < i2c_data_p->buffer_out_len) {SSP2BUF = i2c_data_p->buffer_out[i2c_data_p->buffer_out_ind];i2c_data_p->buffer_out_ind++;data_written_to_buffer = 1;} else {// Nothing left to sendi2c_data_p->slave_sending_data = 0;i2c_data_p->operating_state = I2C_IDLE;}}break;}case I2C_RCV_DATA:{// We expect either data or a stop bit or a (if a restart, an addr)if (SSP2STATbits.P == 1) {// Stop bit detected, we need to check to see if we also read dataif (data_read_from_buffer) {if (SSP2STATbits.D_nA == 1) {// Data received with stop bit// TODO: Handle I2C buffer overflowi2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = received_data;if (i2c_data_p->buffer_in_write_ind == MAXI2C2BUF-1) {i2c_data_p->buffer_in_write_ind = 0;} else {i2c_data_p->buffer_in_write_ind++;}i2c_data_p->buffer_in_len_tmp++;// Save the last byte receivedi2c_data_p->slave_in_last_byte = received_data;i2c_data_p->return_status = I2C_DATA_AVAL;} else {i2c_data_p->operating_state = I2C_IDLE;i2c_data_p->return_status = I2C_ERR_NODATA;}}i2c_data_p->buffer_in_len += i2c_data_p->buffer_in_len_tmp;i2c_data_p->operating_state = I2C_IDLE;} else if (data_read_from_buffer) {if (SSP2STATbits.D_nA == 1) {// Data receivedi2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = received_data;if (i2c_data_p->buffer_in_write_ind == MAXI2C2BUF-1) {i2c_data_p->buffer_in_write_ind = 0;} else {i2c_data_p->buffer_in_write_ind++;}i2c_data_p->buffer_in_len_tmp++;// Save the last byte receivedi2c_data_p->slave_in_last_byte = received_data;i2c_data_p->return_status = I2C_DATA_AVAL;} else {// Restart bit detectedif (SSP2STATbits.R_nW == 1) {i2c_data_p->buffer_in_len += i2c_data_p->buffer_in_len_tmp;i2c_data_p->operating_state = I2C_SEND_DATA;// Process the first byte immediatly if sending datagoto send;} else {// Bad to recv an address again, we aren't readyi2c_data_p->operating_state = I2C_IDLE;i2c_data_p->return_status = I2C_ERR_NODATA;}}}break;}}}// Release the clock stretching bit (if we should)if (data_read_from_buffer || data_written_to_buffer) {// Release the clockif (SSP2CON1bits.CKP == 0) {SSP2CON1bits.CKP = 1;}}}/* Returns 0 if I2C module is currently busy, otherwise returns status code */uint8_t I2C2_Get_Status() {if (i2c_data_p->operating_mode == I2C_MODE_MASTER) {if (i2c_data_p->master_status != I2C_MASTER_IDLE || i2c_data_p->buffer_in_len == 0) {return 0;} else {return i2c_data_p->return_status;}} else {if (i2c_data_p->operating_state != I2C_IDLE || i2c_data_p->buffer_in_len == 0) {return 0;} else {return i2c_data_p->return_status;}}}uint8_t I2C2_Buffer_Len() {return i2c_data_p->buffer_in_len;}/* Returns 0 if I2C module is currently busy, otherwise returns buffer length */uint8_t I2C2_Read_Buffer(uint8_t *buffer) {uint8_t i = 0;while (i2c_data_p->buffer_in_len != 0) {buffer[i] = i2c_data_p->buffer_in[i2c_data_p->buffer_in_read_ind];i++;if (i2c_data_p->buffer_in_read_ind == MAXI2C2BUF-1) {i2c_data_p->buffer_in_read_ind = 0;} else {i2c_data_p->buffer_in_read_ind++;}i2c_data_p->buffer_in_len--;}return i;}/* Put data to be returned here */uint8_t I2C2_Process_Receive(uint8_t c) {uint8_t ret = 0;return ret;}