67 S_SLAVE_TRANSMIT_MODE,
68 S_SLAVE_LOW_BYTE_ADDRESS_DETECT,
76#define I2C2_TRANSMIT_REG I2C2TRN
77#define I2C2_RECEIVE_REG I2C2RCV
79#define I2C2_MASK_REG I2C2MSK
80#define I2C2_ADDRESS_REG I2C2ADD
84#define I2C2_GENERAL_CALL_ENABLE_BIT I2C2CONLbits.GCEN
85#define I2C2_10_BIT_ADDRESS_ENABLE_BIT I2C2CONLbits.A10M
86#define I2C2_RELEASE_SCL_CLOCK_CONTROL_BIT I2C2CONLbits.SCLREL
91#define I2C2_READ_NOT_WRITE_STATUS_BIT I2C2STATbits.R_W
92#define I2C2_DATA_NOT_ADDRESS_STATUS_BIT I2C2STATbits.D_A
93#define I2C2_RECEIVE_OVERFLOW_STATUS_BIT I2C2STATbits.I2COV
94#define I2C2_GENERAL_CALL_ADDRESS_STATUS_BIT I2C2STATbits.GCSTAT
95#define I2C2_ACKNOWLEDGE_STATUS_BIT I2C2STATbits.ACKSTAT
96#define I2C2_STOP_STATUS_BIT I2C2STATbits.P
98#define EMULATE_EEPROM_SIZE 64
103static inline void __attribute__ ((always_inline)) I2C2_TransmitProcess(
void);
104static inline void __attribute__ ((always_inline)) I2C2_ReceiveProcess(
void);
110static I2C_SLAVE_STATES i2c2_slave_state;
111static uint8_t *p_i2c2_write_pointer;
112static uint8_t *p_i2c2_read_pointer;
124void I2C2_Initialize(
void)
133 I2C2_SlaveAddressSet(0x20);
135 I2C2_SlaveAddressMaskSet(0x00);
138 i2c2_slave_state = S_SLAVE_IDLE;
140 I2C2_ReadPointerSet(NULL);
141 I2C2_WritePointerSet(NULL);
145 I2C2CONHbits.PCIE = 1;
150void I2C2_SlaveTasks (
void )
152 static bool prior_address_match =
false;
153 static bool not_busy =
true;
159 switch (i2c2_slave_state)
162 case S_SLAVE_RECEIVE_MODE:
177 ( (I2C2_10_BIT_ADDRESS_ENABLE_BIT == 0) &&
178 (I2C2_DATA_NOT_ADDRESS_STATUS_BIT == 0)
182 ( (I2C2_GENERAL_CALL_ENABLE_BIT == 1) &&
183 (I2C2_GENERAL_CALL_ADDRESS_STATUS_BIT == 1)
187 if (I2C2_READ_NOT_WRITE_STATUS_BIT == 0)
195 I2C2_ReceiveProcess();
196 i2c2_slave_state = S_SLAVE_RECEIVE_MODE;
203 dummy = I2C2_RECEIVE_REG;
216 I2C2_TransmitProcess();
217 i2c2_slave_state = S_SLAVE_TRANSMIT_MODE;
225 ( (I2C2_10_BIT_ADDRESS_ENABLE_BIT == 1) &&
226 (I2C2_DATA_NOT_ADDRESS_STATUS_BIT == 0)
230 if (I2C2_READ_NOT_WRITE_STATUS_BIT == 0)
234 prior_address_match =
false;
235 i2c2_slave_state = S_SLAVE_LOW_BYTE_ADDRESS_DETECT;
240 if (prior_address_match ==
true)
258 I2C2_TransmitProcess();
259 i2c2_slave_state = S_SLAVE_TRANSMIT_MODE;
266 prior_address_match =
false;
267 i2c2_slave_state = S_SLAVE_LOW_BYTE_ADDRESS_DETECT;
273 dummy = I2C2_RECEIVE_REG;
278 if (i2c2_slave_state == S_SLAVE_RECEIVE_MODE)
281 if (I2C2_STOP_STATUS_BIT == 1)
285 else if (I2C2_DATA_NOT_ADDRESS_STATUS_BIT == 1)
288 if (I2C2_RECEIVE_OVERFLOW_STATUS_BIT != 1)
290 I2C2_ReceiveProcess();
300 dummy = I2C2_RECEIVE_REG;
301 I2C2_RECEIVE_OVERFLOW_STATUS_BIT = 0;
309 case S_SLAVE_LOW_BYTE_ADDRESS_DETECT:
322 prior_address_match =
true;
327 dummy = I2C2_RECEIVE_REG;
330 i2c2_slave_state = S_SLAVE_RECEIVE_MODE;
334 case S_SLAVE_TRANSMIT_MODE:
343 if (I2C2_ACKNOWLEDGE_STATUS_BIT == 0)
349 I2C2_TransmitProcess();
355 i2c2_slave_state = S_SLAVE_IDLE;
368 IFS2bits.SI2C2IF = 0;
372void I2C2_ReadPointerSet(uint8_t *p)
374 p_i2c2_read_pointer = p;
377void I2C2_WritePointerSet(uint8_t *p)
379 p_i2c2_write_pointer = p;
382uint8_t *I2C2_ReadPointerGet(
void)
384 return (p_i2c2_read_pointer);
387uint8_t *I2C2_WritePointerGet(
void)
389 return (p_i2c2_write_pointer);
392void I2C2_SlaveAddressMaskSet(
395 I2C2_MASK_REG = mask;
398void I2C2_SlaveAddressSet(
404 I2C2_10_BIT_ADDRESS_ENABLE_BIT =
true;
409 I2C2_10_BIT_ADDRESS_ENABLE_BIT =
false;
411 i2c2_slave_state = S_SLAVE_IDLE;
412 I2C2_ADDRESS_REG = address;
416static inline void __attribute__ ((always_inline)) I2C2_TransmitProcess(
void)
421 if (p_i2c2_read_pointer == NULL)
424 I2C2_TRANSMIT_REG = *p_i2c2_read_pointer;
427 I2C2_RELEASE_SCL_CLOCK_CONTROL_BIT = 1;
431static inline void __attribute__ ((always_inline)) I2C2_ReceiveProcess(
void)
436 if (p_i2c2_write_pointer == NULL)
439 *p_i2c2_write_pointer = I2C2_RECEIVE_REG;
bool I2C2_StatusCallback(I2C2_SLAVE_DRIVER_STATUS status)