diff options
Diffstat (limited to 'vlib/vlib/i2c.c')
-rw-r--r-- | vlib/vlib/i2c.c | 67 |
1 files changed, 38 insertions, 29 deletions
diff --git a/vlib/vlib/i2c.c b/vlib/vlib/i2c.c index 83a702ed..97f5bb21 100644 --- a/vlib/vlib/i2c.c +++ b/vlib/vlib/i2c.c @@ -19,7 +19,7 @@ static inline void i2c_delay (i2c_bus_t * b, f64 timeout) { - vlib_main_t * vm = vlib_get_main(); + vlib_main_t *vm = vlib_get_main (); vlib_time_wait (vm, timeout); } @@ -31,7 +31,7 @@ i2c_wait_for_scl (i2c_bus_t * b) while (t < b->hold_time) { int sda, scl; - i2c_delay(b, b->rise_fall_time); + i2c_delay (b, b->rise_fall_time); b->get_bits (b, &scl, &sda); if (scl) @@ -87,7 +87,7 @@ i2c_write_bit (i2c_bus_t * b, int sda) } static void -i2c_read_bit (i2c_bus_t * b, int * sda) +i2c_read_bit (i2c_bus_t * b, int *sda) { int scl; @@ -116,7 +116,7 @@ i2c_write_byte (i2c_bus_t * b, u8 data) b->put_bits (b, 0, 1); i2c_delay (b, b->rise_fall_time); - i2c_read_bit(b, &sda); + i2c_read_bit (b, &sda); if (sda) b->timeout = 1; @@ -171,35 +171,37 @@ vlib_i2c_xfer (i2c_bus_t * bus, i2c_msg_t * msgs) int i; vec_foreach (msg, msgs) - { - i2c_start (bus); - i2c_write_byte (bus, (msg->addr << 1) + (msg->flags == I2C_MSG_FLAG_READ)); - - if (msg->flags & I2C_MSG_FLAG_READ) - for (i=0; i< msg->len; i++) - { - i2c_read_byte (bus, &msg->buffer[i], /* ack */ i + 1 != msg->len); - if (bus->timeout) - goto done; - } - - else - for (i=0; i< msg->len; i++) - { - i2c_write_byte (bus, msg->buffer[i]); - if (bus->timeout) - goto done; - } - } + { + i2c_start (bus); + i2c_write_byte (bus, + (msg->addr << 1) + (msg->flags == I2C_MSG_FLAG_READ)); + + if (msg->flags & I2C_MSG_FLAG_READ) + for (i = 0; i < msg->len; i++) + { + i2c_read_byte (bus, &msg->buffer[i], /* ack */ i + 1 != msg->len); + if (bus->timeout) + goto done; + } + + else + for (i = 0; i < msg->len; i++) + { + i2c_write_byte (bus, msg->buffer[i]); + if (bus->timeout) + goto done; + } + } done: - i2c_stop(bus); + i2c_stop (bus); } void -vlib_i2c_read_eeprom (i2c_bus_t * bus, u8 i2c_addr, u16 start_addr, u16 length, u8 * data) +vlib_i2c_read_eeprom (i2c_bus_t * bus, u8 i2c_addr, u16 start_addr, + u16 length, u8 * data) { - i2c_msg_t * msg = 0; + i2c_msg_t *msg = 0; u8 start_address[1]; vec_validate (msg, 1); @@ -207,7 +209,7 @@ vlib_i2c_read_eeprom (i2c_bus_t * bus, u8 i2c_addr, u16 start_addr, u16 length, start_address[0] = start_addr; msg[0].addr = i2c_addr; msg[0].flags = I2C_MSG_FLAG_WRITE; - msg[0].buffer = (u8 *) &start_address; + msg[0].buffer = (u8 *) & start_address; msg[0].len = 1; msg[1].addr = i2c_addr; @@ -215,8 +217,15 @@ vlib_i2c_read_eeprom (i2c_bus_t * bus, u8 i2c_addr, u16 start_addr, u16 length, msg[1].buffer = data; msg[1].len = length; - vlib_i2c_xfer(bus, msg); + vlib_i2c_xfer (bus, msg); vec_free (msg); } +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ |