finally fixed bug in i2c address scan check routine

was checking if set instead of negated. oops.
This commit is contained in:
true 2024-08-08 03:35:03 -07:00
parent d32442da9a
commit 3eb211fb54
2 changed files with 8 additions and 12 deletions

View File

@ -80,12 +80,10 @@ int8_t i2c_read_addr1b(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len)
*data++ = I2C_ReceiveData(I2C1);
len--;
if (!len) {
I2C_GenerateSTOP(I2C1, ENABLE);
}
}
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}
@ -145,7 +143,7 @@ void i2c_write_reg_8b(uint8_t addr, uint8_t reg, const uint8_t dat)
i2c_write_addr1b(addr, reg, &dat, 1);
}
uint8_t i2c_addr_scan(uint8_t addr)
int8_t i2c_addr_scan(uint8_t addr)
{
uint8_t found = 1;
uint32_t event;
@ -164,30 +162,28 @@ uint8_t i2c_addr_scan(uint8_t addr)
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--);
if (!timeout) return -2;
I2C_Send7bitAddress(I2C1, addr, (addr & 1));
I2C_Send7bitAddress(I2C1, (addr & 0xfe), (addr & 0x01));
timeout = I2C_TIMEOUT_ACK_POLL;
if (addr & 1) event = I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED;
else event = I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED;
while (I2C_CheckEvent(I2C1, event) && timeout--) {
while ((I2C_CheckEvent(I2C1, event) == NoREADY) && timeout--) {
if (I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)) {
found = 0;
break;
}
}
if (!timeout) {
found = 0;
}
// reset flags; it might be in a fucked state
I2C1->STAR1 = 0;
// send a stop to make sure anything listening knows to stfu
I2C_GenerateSTOP(I2C1, ENABLE);
// reset flags; it might be in a fucked state
if (!found) {
I2C1->STAR1 = 0;
return 0;
}

View File

@ -19,7 +19,7 @@ int8_t i2c_write_addr1b(uint8_t addr, uint8_t reg, const uint8_t *data, uint8_t
uint8_t i2c_read_reg_8b(uint8_t addr, uint8_t reg);
void i2c_write_reg_8b(uint8_t addr, uint8_t reg, const uint8_t dat);
uint8_t i2c_addr_scan(uint8_t addr);
int8_t i2c_addr_scan(uint8_t addr);