inline int i2c_start(i2c_t *obj) { int status = 0; // 8.1 Before master mode can be entered, I2CON must be initialised to: // - I2EN STA STO SI AA - - // - 1 0 0 0 x - - // if AA = 0, it can't enter slave mode i2c_conclr(obj, 1, 1, 1, 1); // The master mode may now be entered by setting the STA bit // this will generate a start condition when the bus becomes free i2c_conset(obj, 1, 0, 0, 1); i2c_wait_SI(obj); status = i2c_status(obj); // Clear start bit now transmitted, and interrupt bit i2c_conclr(obj, 1, 0, 0, 0); return status; }
int i2c_slave_read(i2c_t *obj, char *data, int length) { int count = 0; int status; do { i2c_clear_SI(obj); i2c_wait_SI(obj); status = i2c_status(obj); if((status == 0x80) || (status == 0x90)) { data[count] = I2C_DAT(obj) & 0xFF; } count++; } while (((status == 0x80) || (status == 0x90) || (status == 0x060) || (status == 0x70)) && (count < length)); if(status != 0xA0) { i2c_stop(obj); } i2c_clear_SI(obj); return count; }