2015-08-25 12:45:48 +00:00
|
|
|
# Yet another I2C driver for the ESP8266
|
|
|
|
|
|
|
|
This time a driver for the excellent esp-open-rtos. This is a bit banging I2C driver based on the Wikipedia pesudo C code [1].
|
|
|
|
|
2017-10-12 12:42:34 +00:00
|
|
|
### Basic usage
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-10-12 12:42:34 +00:00
|
|
|
```C
|
2015-08-25 12:45:48 +00:00
|
|
|
#include <i2c.h>
|
|
|
|
|
2017-10-12 12:42:34 +00:00
|
|
|
#define BUS (0)
|
2015-08-25 12:45:48 +00:00
|
|
|
#define SCL_PIN (0)
|
|
|
|
#define SDA_PIN (2)
|
|
|
|
|
|
|
|
uint8_t slave_addr = 0x20;
|
|
|
|
uint8_t reg_addr = 0x1f;
|
|
|
|
uint8_t reg_data;
|
|
|
|
|
2017-10-12 12:42:34 +00:00
|
|
|
i2c_init(BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-03-21 21:14:06 +00:00
|
|
|
// Write 1 byte to slave register
|
2017-10-12 12:42:34 +00:00
|
|
|
int err = i2c_slave_write(BUS, slave_addr, ®_addr, &data, 1);
|
2017-03-21 21:14:06 +00:00
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
// do something with error
|
|
|
|
}
|
2015-08-25 12:45:48 +00:00
|
|
|
|
|
|
|
// Issue write to slave, sending reg_addr, followed by reading 1 byte
|
2017-10-12 12:42:34 +00:00
|
|
|
err = i2c_slave_read(BUS, slave_addr, ®_addr, ®_data, 1);
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-10-12 12:42:34 +00:00
|
|
|
```
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-03-21 21:14:06 +00:00
|
|
|
For details please see `extras/i2c/i2c.h`.
|
|
|
|
|
2015-08-25 12:45:48 +00:00
|
|
|
The driver is released under the MIT license.
|
|
|
|
|
|
|
|
[1] https://en.wikipedia.org/wiki/I²C#Example_of_bit-banging_the_I.C2.B2C_Master_protocol
|