Update .clang-format and apply
Update .clang-format for LLVM 14.0, available on Ubuntu 22.04. There is still plenty that clang-format sucks at or does wrong, so either add some more blocks to disable it, or just put up with it. Signed-off-by: Tim Crawford <tcrawford@system76.com>
This commit is contained in:
committed by
Jeremy Soller
parent
c3267fc4ad
commit
e032c5f0f2
@ -8,9 +8,9 @@
|
||||
#include <common/i2c.h>
|
||||
#include <common/macro.h>
|
||||
|
||||
#define TIMEOUT (F_CPU/1000)
|
||||
#define TIMEOUT (F_CPU / 1000)
|
||||
|
||||
int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
||||
int16_t i2c_start(struct I2C *i2c, uint8_t addr, bool read) {
|
||||
uint32_t count;
|
||||
|
||||
// reset TWI control register
|
||||
@ -19,11 +19,14 @@ int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
||||
TWCR = BIT(TWINT) | BIT(TWSTA) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||
count -= 1;
|
||||
if (count == 0)
|
||||
return -1;
|
||||
|
||||
// check if the start condition was successfully transmitted
|
||||
if((TWSR & 0xF8) != TW_START) return -1;
|
||||
if ((TWSR & 0xF8) != TW_START)
|
||||
return -1;
|
||||
|
||||
// load slave addr into data register
|
||||
TWDR = ((addr << 1) | read);
|
||||
@ -31,22 +34,25 @@ int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||
count -= 1;
|
||||
if (count == 0)
|
||||
return -1;
|
||||
|
||||
// check if the device has acknowledged the READ / WRITE mode
|
||||
uint8_t twst = TW_STATUS & 0xF8;
|
||||
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) return -1;
|
||||
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void i2c_stop(struct I2C * i2c) {
|
||||
void i2c_stop(struct I2C *i2c) {
|
||||
// transmit STOP condition
|
||||
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO);
|
||||
}
|
||||
|
||||
int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) {
|
||||
int16_t i2c_write(struct I2C *i2c, uint8_t *data, uint16_t length) {
|
||||
uint16_t i;
|
||||
for (i = 0; i < length; i++) {
|
||||
// load data into data register
|
||||
@ -55,17 +61,20 @@ int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) {
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||
count -= 1;
|
||||
// timed out
|
||||
if (count == 0) return -1;
|
||||
if (count == 0)
|
||||
return -1;
|
||||
// failed to receive ack
|
||||
if((TWSR & 0xF8) != TW_MT_DATA_ACK) return -1;
|
||||
if ((TWSR & 0xF8) != TW_MT_DATA_ACK)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) {
|
||||
int16_t i2c_read(struct I2C *i2c, uint8_t *data, uint16_t length) {
|
||||
uint16_t i;
|
||||
for (i = 0; i < length; i++) {
|
||||
if ((i + 1) < length) {
|
||||
@ -77,8 +86,10 @@ int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) {
|
||||
}
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||
count -= 1;
|
||||
if (count == 0)
|
||||
return -1;
|
||||
// return received data from TWDR
|
||||
data[i] = TWDR;
|
||||
}
|
||||
|
Reference in New Issue
Block a user