lpc176x: Add i2c_read functionality

This adds the i2c_read command to the lp176x MCU.

Signed-off-by: Michael Kurz <michi.kurz@gmail.com>
This commit is contained in:
Michael Kurz 2021-04-21 00:11:26 +02:00 committed by KevinOConnor
parent dd7085cbca
commit 308f0338cf
1 changed files with 34 additions and 1 deletions

View File

@ -100,6 +100,20 @@ i2c_send_byte(LPC_I2C_TypeDef *i2c, uint8_t b, uint32_t timeout)
return i2c->I2STAT; return i2c->I2STAT;
} }
static uint8_t
i2c_read_byte(LPC_I2C_TypeDef *i2c, uint32_t timeout, uint8_t remaining)
{
if (remaining == 0)
i2c->I2CONCLR = IF_ACK | IF_IRQ;
else {
i2c->I2CONSET = IF_ACK;
i2c->I2CONCLR = IF_IRQ;
}
i2c_wait(i2c, IF_IRQ, timeout);
uint8_t b = i2c->I2DAT;
return b;
}
static void static void
i2c_stop(LPC_I2C_TypeDef *i2c, uint32_t timeout) i2c_stop(LPC_I2C_TypeDef *i2c, uint32_t timeout)
{ {
@ -125,5 +139,24 @@ void
i2c_read(struct i2c_config config, uint8_t reg_len, uint8_t *reg i2c_read(struct i2c_config config, uint8_t reg_len, uint8_t *reg
, uint8_t read_len, uint8_t *read) , uint8_t read_len, uint8_t *read)
{ {
shutdown("i2c_read not supported on lpc176x"); LPC_I2C_TypeDef *i2c = config.i2c;
uint32_t timeout = timer_read_time() + timer_from_us(5000);
uint8_t addr = config.addr | 0x01;
if (reg_len != 0) {
// write the register
i2c_start(i2c, timeout);
i2c_send_byte(i2c, config.addr, timeout);
while(reg_len--)
i2c_send_byte(i2c, *reg++, timeout);
i2c_stop(i2c, timeout);
}
// start/re-start and read data
i2c_start(i2c, timeout);
i2c_send_byte(i2c, addr, timeout);
while(read_len--) {
*read = i2c_read_byte(i2c, timeout, read_len);
read++;
}
i2c_stop(i2c, timeout);
} }