tock / libtock-rs

Rust userland library for Tock
Apache License 2.0
163 stars 109 forks source link

api: add i2c master/slave support | examples: add i2c master/slave samples #523

Closed twilfredo closed 10 months ago

twilfredo commented 10 months ago

Overview

Adds new I2C master/slave api to libtock-rs. The changes were tested using the samples running on 2x Particle Boron boards. Where one was a master and the other was a slave device, that would ping-pong data across. Additional testing was performed by communicating with an MCP9808 device as an I2C bus Master.

Building and Testing

The samples can be built by running the below from the top-dir:

$ make nrf52840 EXAMPLE=i2c_slave_send_recv
$ make nrf52840 EXAMPLE=i2c_master_write_read

After flashing each sample to the respective master and slave device. The following serial output can be observed. Note you may have to reset the devices after flashing.

Master Log (i2c_master_write_read.rs)

i2c-master: write-read operation 1
2 bytes read from slave | data received (0h): [c1, c5]

i2c-master: write-read operation 2
2 bytes read from slave | data received (0h): [c2, ca]

i2c-master: write-read operation 3
2 bytes read from slave | data received (0h): [c3, cf]

Slave Log (i2c_slave_send_recv.rs)

i2c-slave: operation 1
4 bytes received from master | buf: [5e, bc, 1a, 78, 0, 0, 0, 0]
2 bytes read by master | data sent: [2, a, 0, 0, 0, 0, 0, 0]
i2c-slave: operation 2
4 bytes received from master | buf: [60, c0, 20, 80, 0, 0, 0, 0]
2 bytes read by master | data sent: [3, f, 0, 0, 0, 0, 0, 0]
i2c-slave: operation 3
4 bytes received from master | buf: [62, c4, 26, 88, 0, 0, 0, 0]
2 bytes read by master | data sent: [4, 14, 0, 0, 0, 0, 0, 0]
i2c-slave: operation 4
4 bytes received from master | buf: [64, c8, 2c, 90, 0, 0, 0, 0]
2 bytes read by master | data sent: [5, 19, 0, 0, 0, 0, 0, 0]
i2c-slave: operation 5
4 bytes received from master | buf: [66, cc, 32, 98, 0, 0, 0, 0]
2 bytes read by master | data sent: [6, 1e, 0, 0, 0, 0, 0, 0]

NOTE: These are two different logs, so the bytes aren't matched... I only have one UART converter >,>

TODO/Help

This api is all synchronous, we need to look at adding async support. It looks like we need to improve the kernel i2c_master_slave_driver.rs for this, as it seems to support only a single subscriber currently. What's a good place to get started? Or is there something I missed here?

Cheers

twilfredo commented 10 months ago

@alistair23

twilfredo commented 10 months ago

ping!