mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
Merge pull request #1872 from thomaseichinger/stm32f1-i2c-multibyte-fix
cpu/stm32f1: fix i2c master read for N=2 and N>2 bytes
This commit is contained in:
commit
56062b0f23
@ -161,7 +161,7 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
|
|||||||
DEBUG("Send Slave address and wait for ADDR == 1\n");
|
DEBUG("Send Slave address and wait for ADDR == 1\n");
|
||||||
_start(i2c, address, I2C_FLAG_READ);
|
_start(i2c, address, I2C_FLAG_READ);
|
||||||
DEBUG("Set POS bit\n");
|
DEBUG("Set POS bit\n");
|
||||||
i2c->CR1 |= I2C_CR1_POS;
|
i2c->CR1 |= (I2C_CR1_POS | I2C_CR1_ACK);
|
||||||
DEBUG("Crit block: Clear ADDR bit and clear ACK flag\n");
|
DEBUG("Crit block: Clear ADDR bit and clear ACK flag\n");
|
||||||
state = disableIRQ();
|
state = disableIRQ();
|
||||||
_clear_addr(i2c);
|
_clear_addr(i2c);
|
||||||
@ -192,9 +192,9 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
|
|||||||
_start(i2c, address, I2C_FLAG_READ);
|
_start(i2c, address, I2C_FLAG_READ);
|
||||||
_clear_addr(i2c);
|
_clear_addr(i2c);
|
||||||
|
|
||||||
while (i < (length - 2)) {
|
while (i < (length - 3)) {
|
||||||
DEBUG("Wait until byte was received\n");
|
DEBUG("Wait until byte was received\n");
|
||||||
while (!(i2c->SR1 & I2C_SR1_BTF));
|
while (!(i2c->SR1 & I2C_SR1_RXNE));
|
||||||
DEBUG("Copy byte from DR\n");
|
DEBUG("Copy byte from DR\n");
|
||||||
data[i++] = (char)i2c->DR;
|
data[i++] = (char)i2c->DR;
|
||||||
}
|
}
|
||||||
@ -205,12 +205,17 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
|
|||||||
DEBUG("Disable ACK\n");
|
DEBUG("Disable ACK\n");
|
||||||
i2c->CR1 &= ~(I2C_CR1_ACK);
|
i2c->CR1 &= ~(I2C_CR1_ACK);
|
||||||
|
|
||||||
DEBUG("Crit block: set STOP and read second last byte\n");
|
DEBUG("Crit block: set STOP and read N-2 byte\n");
|
||||||
state = disableIRQ();
|
state = disableIRQ();
|
||||||
i2c->CR1 |= (I2C_CR1_STOP);
|
|
||||||
data[i++] = (char)i2c->DR;
|
data[i++] = (char)i2c->DR;
|
||||||
|
i2c->CR1 |= (I2C_CR1_STOP);
|
||||||
restoreIRQ(state);
|
restoreIRQ(state);
|
||||||
|
|
||||||
|
DEBUG("Read N-1 byte\n");
|
||||||
|
data[i++] = (char)i2c->DR;
|
||||||
|
|
||||||
while (!(i2c->SR1 & I2C_SR1_RXNE));
|
while (!(i2c->SR1 & I2C_SR1_RXNE));
|
||||||
|
DEBUG("Read last byte\n");
|
||||||
data[i++] = (char)i2c->DR;
|
data[i++] = (char)i2c->DR;
|
||||||
|
|
||||||
DEBUG("wait for STOP bit to be cleared again\n");
|
DEBUG("wait for STOP bit to be cleared again\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user