I2C code formatting, README fix
This commit is contained in:
parent
cb4ea206fa
commit
080974ccfd
3 changed files with 80 additions and 71 deletions
|
|
@ -2,11 +2,12 @@
|
||||||
|
|
||||||
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].
|
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].
|
||||||
|
|
||||||
### Usage
|
### Basic usage
|
||||||
|
|
||||||
````
|
```C
|
||||||
#include <i2c.h>
|
#include <i2c.h>
|
||||||
|
|
||||||
|
#define BUS (0)
|
||||||
#define SCL_PIN (0)
|
#define SCL_PIN (0)
|
||||||
#define SDA_PIN (2)
|
#define SDA_PIN (2)
|
||||||
|
|
||||||
|
|
@ -14,19 +15,19 @@ uint8_t slave_addr = 0x20;
|
||||||
uint8_t reg_addr = 0x1f;
|
uint8_t reg_addr = 0x1f;
|
||||||
uint8_t reg_data;
|
uint8_t reg_data;
|
||||||
|
|
||||||
i2c_init(SCL_PIN, SDA_PIN);
|
i2c_init(BUS, SCL_PIN, SDA_PIN);
|
||||||
|
|
||||||
// Write 1 byte to slave register
|
// Write 1 byte to slave register
|
||||||
int err = i2c_slave_write(slave_addr, ®_addr, &data, 1);
|
int err = i2c_slave_write(BUS, slave_addr, ®_addr, &data, 1);
|
||||||
if (err != 0)
|
if (err != 0)
|
||||||
{
|
{
|
||||||
// do something with error
|
// do something with error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue write to slave, sending reg_addr, followed by reading 1 byte
|
// Issue write to slave, sending reg_addr, followed by reading 1 byte
|
||||||
err = i2c_slave_read(slave_addr, ®_addr, ®_data, 1);
|
err = i2c_slave_read(BUS, slave_addr, ®_addr, ®_data, 1);
|
||||||
|
|
||||||
````
|
```
|
||||||
|
|
||||||
For details please see `extras/i2c/i2c.h`.
|
For details please see `extras/i2c/i2c.h`.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,10 +22,13 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "i2c.h"
|
||||||
|
|
||||||
#include <esp8266.h>
|
#include <esp8266.h>
|
||||||
#include <espressif/esp_misc.h> // sdk_os_delay_us
|
#include <espressif/esp_misc.h> // sdk_os_delay_us
|
||||||
#include <espressif/esp_system.h>
|
#include <espressif/esp_system.h>
|
||||||
#include "i2c.h"
|
#include <FreeRTOS.h>
|
||||||
|
#include <task.h>
|
||||||
|
|
||||||
//#define I2C_DEBUG true
|
//#define I2C_DEBUG true
|
||||||
|
|
||||||
|
|
@ -37,6 +40,15 @@
|
||||||
|
|
||||||
#define CLK_STRETCH (10)
|
#define CLK_STRETCH (10)
|
||||||
|
|
||||||
|
// Following array contain delay values for different frequencies
|
||||||
|
// Warning: 1 is minimal, that mean at 80MHz clock, frequency max is 320kHz
|
||||||
|
const static uint8_t i2c_freq_array[][2] = {
|
||||||
|
[I2C_FREQ_80K] = {255, 35},
|
||||||
|
[I2C_FREQ_100K] = {100, 20},
|
||||||
|
[I2C_FREQ_400K] = {10, 1},
|
||||||
|
[I2C_FREQ_500K] = {6, 1}
|
||||||
|
};
|
||||||
|
|
||||||
static uint8_t freq; // Store CPU frequency for optimisation speed in delay function (Warning: Don't change CPU frequency during a transaction)
|
static uint8_t freq; // Store CPU frequency for optimisation speed in delay function (Warning: Don't change CPU frequency during a transaction)
|
||||||
static i2c_bus_description_t i2c_bus[MAX_I2C_BUS];
|
static i2c_bus_description_t i2c_bus[MAX_I2C_BUS];
|
||||||
|
|
||||||
|
|
@ -136,7 +148,8 @@ void i2c_start(uint8_t bus)
|
||||||
(void) read_sda(bus);
|
(void) read_sda(bus);
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
uint32_t clk_stretch = CLK_STRETCH;
|
uint32_t clk_stretch = CLK_STRETCH;
|
||||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
while (read_scl(bus) == 0 && clk_stretch--)
|
||||||
|
;
|
||||||
// Repeated start setup time, minimum 4.7us
|
// Repeated start setup time, minimum 4.7us
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
}
|
}
|
||||||
|
|
@ -158,7 +171,8 @@ bool i2c_stop(uint8_t bus)
|
||||||
clear_sda(bus);
|
clear_sda(bus);
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
// Clock stretching
|
// Clock stretching
|
||||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
while (read_scl(bus) == 0 && clk_stretch--)
|
||||||
|
;
|
||||||
// Stop bit setup time, minimum 4us
|
// Stop bit setup time, minimum 4us
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
// SCL is high, set SDA from 0 to 1
|
// SCL is high, set SDA from 0 to 1
|
||||||
|
|
@ -185,7 +199,8 @@ static void i2c_write_bit(uint8_t bus, bool bit)
|
||||||
}
|
}
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
// Clock stretching
|
// Clock stretching
|
||||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
while (read_scl(bus) == 0 && clk_stretch--)
|
||||||
|
;
|
||||||
// SCL is high, now data is valid
|
// SCL is high, now data is valid
|
||||||
// If SDA is high, check that nobody else is driving SDA
|
// If SDA is high, check that nobody else is driving SDA
|
||||||
if (bit && read_sda(bus) == 0) {
|
if (bit && read_sda(bus) == 0) {
|
||||||
|
|
@ -204,7 +219,8 @@ static bool i2c_read_bit(uint8_t bus)
|
||||||
(void) read_sda(bus);
|
(void) read_sda(bus);
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
// Clock stretching
|
// Clock stretching
|
||||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
while (read_scl(bus) == 0 && clk_stretch--)
|
||||||
|
;
|
||||||
// SCL is high, now data is valid
|
// SCL is high, now data is valid
|
||||||
bit = read_sda(bus);
|
bit = read_sda(bus);
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
|
|
|
||||||
|
|
@ -28,40 +28,31 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <FreeRTOS.h>
|
|
||||||
#include <task.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
/*
|
|
||||||
* Define i2c bus max number
|
* Define i2c bus max number
|
||||||
*/
|
*/
|
||||||
|
#ifndef MAX_I2C_BUS
|
||||||
#define MAX_I2C_BUS 2
|
#define MAX_I2C_BUS 2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum
|
||||||
/*
|
{
|
||||||
* following array contain value for different frequency
|
|
||||||
* Warning : 1 is minimal, that mean at 80MHz clock, frequency max is 320kHz
|
|
||||||
* Array format is { {160MHz, 80MHz} , {160MHz, 80MHz} , ... }
|
|
||||||
*/
|
|
||||||
#define NB_FREQ_AVAILABLE 4
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
I2C_FREQ_80K = 0,
|
I2C_FREQ_80K = 0,
|
||||||
I2C_FREQ_100K,
|
I2C_FREQ_100K,
|
||||||
I2C_FREQ_400K,
|
I2C_FREQ_400K,
|
||||||
I2C_FREQ_500K,
|
I2C_FREQ_500K,
|
||||||
} i2c_freq_t;
|
} i2c_freq_t;
|
||||||
|
|
||||||
const static uint8_t i2c_freq_array[NB_FREQ_AVAILABLE][2] = { {255,35}, {100,20}, {10,1}, {6,1} } ;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Device descriptor
|
* Device descriptor
|
||||||
*/
|
*/
|
||||||
typedef struct i2c_dev {
|
typedef struct i2c_dev
|
||||||
|
{
|
||||||
uint8_t bus;
|
uint8_t bus;
|
||||||
uint8_t addr;
|
uint8_t addr;
|
||||||
} i2c_dev_t;
|
} i2c_dev_t;
|
||||||
|
|
@ -69,10 +60,11 @@ typedef struct i2c_dev {
|
||||||
/**
|
/**
|
||||||
* Bus settings
|
* Bus settings
|
||||||
*/
|
*/
|
||||||
typedef struct i2c_bus_description {
|
typedef struct i2c_bus_description
|
||||||
uint8_t g_scl_pin; // Scl pin
|
{
|
||||||
uint8_t g_sda_pin; // Sda pin
|
uint8_t g_scl_pin; ///< SCL pin
|
||||||
uint8_t frequency; // frequency selection
|
uint8_t g_sda_pin; ///< SDA pin
|
||||||
|
uint8_t frequency; ///< Frequency
|
||||||
bool started;
|
bool started;
|
||||||
bool flag;
|
bool flag;
|
||||||
bool force;
|
bool force;
|
||||||
|
|
@ -83,7 +75,7 @@ typedef struct i2c_bus_description {
|
||||||
// Based on https://en.wikipedia.org/wiki/I²C#Example_of_bit-banging_the_I.C2.B2C_Master_protocol
|
// Based on https://en.wikipedia.org/wiki/I²C#Example_of_bit-banging_the_I.C2.B2C_Master_protocol
|
||||||
// With calling overhead, we end up at ~320kbit/s
|
// With calling overhead, we end up at ~320kbit/s
|
||||||
|
|
||||||
//Level 0 API
|
/// Level 0 API
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init bitbanging I2C driver on given pins
|
* Init bitbanging I2C driver on given pins
|
||||||
|
|
@ -137,7 +129,7 @@ bool i2c_stop(uint8_t bus);
|
||||||
*/
|
*/
|
||||||
bool i2c_status(uint8_t bus);
|
bool i2c_status(uint8_t bus);
|
||||||
|
|
||||||
//Level 1 API (Don't need functions above)
|
/// Level 1 API (Don't need functions above)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function will allow you to force a transmission I2C, cancel current transmission.
|
* This function will allow you to force a transmission I2C, cancel current transmission.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue