int i2c_slave_read(i2c_t *obj, char *data, int length) { uint8_t dummy_read, count; uint8_t * ptr; // set rx mode obj->i2c->C1 &= ~I2C_C1_TX_MASK; // first dummy read dummy_read = obj->i2c->D; if(i2c_wait_end_rx_transfer(obj)) { return 0; } // read address dummy_read = obj->i2c->D; if(i2c_wait_end_rx_transfer(obj)) { return 0; } // read (length - 1) bytes for (count = 0; count < (length - 1); count++) { data[count] = obj->i2c->D; if(i2c_wait_end_rx_transfer(obj)) { return 0; } } // read last byte ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count]; *ptr = obj->i2c->D; return (length) ? (count + 1) : 0; }
int i2c_slave_read(i2c_t *obj, char *data, int length) { uint8_t dummy_read; uint8_t *ptr; int count; // set rx mode i2c_hal_set_direction(obj->instance, kI2CTransmit); // first dummy read dummy_read = i2c_hal_read(obj->instance); if (i2c_wait_end_rx_transfer(obj)) return 0; // read address dummy_read = i2c_hal_read(obj->instance); if (i2c_wait_end_rx_transfer(obj)) return 0; // read (length - 1) bytes for (count = 0; count < (length - 1); count++) { data[count] = i2c_hal_read(obj->instance); if (i2c_wait_end_rx_transfer(obj)) return count; } // read last byte ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count]; *ptr = i2c_hal_read(obj->instance); return (length) ? (count + 1) : 0; }
int i2c_slave_read(i2c_t *obj, char *data, int length) { uint8_t dummy_read; uint8_t *ptr; int count; uint32_t i2c_addrs[] = I2C_BASE_ADDRS; // set rx mode I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CSend); // first dummy read dummy_read = I2C_HAL_ReadByte(i2c_addrs[obj->instance]); if (i2c_wait_end_rx_transfer(obj)) return 0; // read address dummy_read = I2C_HAL_ReadByte(i2c_addrs[obj->instance]); if (i2c_wait_end_rx_transfer(obj)) return 0; // read (length - 1) bytes for (count = 0; count < (length - 1); count++) { data[count] = I2C_HAL_ReadByte(i2c_addrs[obj->instance]); if (i2c_wait_end_rx_transfer(obj)) return count; } // read last byte ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count]; *ptr = I2C_HAL_ReadByte(i2c_addrs[obj->instance]); return (length) ? (count + 1) : 0; }
static int i2c_do_read(i2c_t *obj, char * data, int last) { if (last) i2c_send_nack(obj); else i2c_send_ack(obj); *data = (obj->i2c->D & 0xFF); // start rx transfer and wait the end of the transfer return i2c_wait_end_rx_transfer(obj); }
static int i2c_do_read(i2c_t *obj, char * data, int last) { if (last) { i2c_hal_send_nak(obj->instance); } else { i2c_hal_send_ack(obj->instance); } *data = (i2c_hal_read(obj->instance) & 0xFF); // start rx transfer and wait the end of the transfer return i2c_wait_end_rx_transfer(obj); }
static int i2c_do_read(i2c_t *obj, char * data, int last) { uint32_t i2c_addrs[] = I2C_BASE_ADDRS; if (last) { I2C_HAL_SendNak(i2c_addrs[obj->instance]); } else { I2C_HAL_SendAck(i2c_addrs[obj->instance]); } *data = (I2C_HAL_ReadByte(i2c_addrs[obj->instance]) & 0xFF); // start rx transfer and wait the end of the transfer return i2c_wait_end_rx_transfer(obj); }
int i2c_slave_write(i2c_t *obj, const char *data, int length) { int i, count = 0; // set tx mode i2c_hal_set_direction(obj->instance, kI2CTransmit); for (i = 0; i < length; i++) { if (i2c_do_write(obj, data[count++]) == 2) return i; } // set rx mode i2c_hal_set_direction(obj->instance, kI2CReceive); // dummy rx transfer needed // otherwise the master cannot generate a stop bit i2c_hal_read(obj->instance); if (i2c_wait_end_rx_transfer(obj) == 2) return count; return count; }
int i2c_slave_write(i2c_t *obj, const char *data, int length) { int i, count = 0; uint32_t i2c_addrs[] = I2C_BASE_ADDRS; // set tx mode I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CSend); for (i = 0; i < length; i++) { if (i2c_do_write(obj, data[count++]) == 2) return i; } // set rx mode I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CReceive); // dummy rx transfer needed // otherwise the master cannot generate a stop bit I2C_HAL_ReadByte(i2c_addrs[obj->instance]); if (i2c_wait_end_rx_transfer(obj) == 2) return count; return count; }
int i2c_slave_write(i2c_t *obj, const char *data, int length) { uint32_t i, count = 0; // set tx mode obj->i2c->C1 |= I2C_C1_TX_MASK; for (i = 0; i < length; i++) { if(i2c_do_write(obj, data[count++]) == 2) { return 0; } } // set rx mode obj->i2c->C1 &= ~I2C_C1_TX_MASK; // dummy rx transfer needed // otherwise the master cannot generate a stop bit obj->i2c->D; if(i2c_wait_end_rx_transfer(obj) == 2) { return 0; } return count; }