Initial i2c bus upgrade
This commit is contained in:
parent
9523e872f8
commit
0654299a17
5 changed files with 191 additions and 161 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
/* Test code for DS3231 high precision RTC module
|
/* Test code for DS3231 high precision RTC module
|
||||||
*
|
*
|
||||||
* Part of esp-open-rtos
|
* Part of esp-open-rtos
|
||||||
* Copyright (C) 2016 Bhuvanchandra DV <bhuvanchandra.dv@gmail.com>
|
* Copyright (C) 2016 Bhuvanchandra DV <bhuvanchandra.dv@gmail.com>
|
||||||
|
|
@ -35,7 +35,8 @@ void user_init(void)
|
||||||
printf("SDK version : %s\n", sdk_system_get_sdk_version());
|
printf("SDK version : %s\n", sdk_system_get_sdk_version());
|
||||||
printf("GIT version : %s\n", GITSHORTREV);
|
printf("GIT version : %s\n", GITSHORTREV);
|
||||||
|
|
||||||
ds3231_Init(scl, sda);
|
i2c_init(0,scl,sda,I2C_FREQ_400K);
|
||||||
|
ds3231_Init(0);
|
||||||
|
|
||||||
xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL);
|
xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@
|
||||||
|
|
||||||
#include "i2c/i2c.h"
|
#include "i2c/i2c.h"
|
||||||
|
|
||||||
|
static uint8_t _bus;
|
||||||
|
|
||||||
/* Convert normal decimal to binary coded decimal */
|
/* Convert normal decimal to binary coded decimal */
|
||||||
static inline uint8_t decToBcd(uint8_t dec)
|
static inline uint8_t decToBcd(uint8_t dec)
|
||||||
{
|
{
|
||||||
|
|
@ -30,7 +32,7 @@ static inline uint8_t bcdToDec(uint8_t bcd)
|
||||||
*/
|
*/
|
||||||
static inline int ds3231_send(uint8_t reg, uint8_t *data, uint8_t len)
|
static inline int ds3231_send(uint8_t reg, uint8_t *data, uint8_t len)
|
||||||
{
|
{
|
||||||
return i2c_slave_write(DS3231_ADDR, ®, data, len);
|
return i2c_slave_write(_bus, DS3231_ADDR, ®, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read a number of bytes from the rtc over i2c
|
/* Read a number of bytes from the rtc over i2c
|
||||||
|
|
@ -38,7 +40,7 @@ static inline int ds3231_send(uint8_t reg, uint8_t *data, uint8_t len)
|
||||||
*/
|
*/
|
||||||
static inline int ds3231_recv(uint8_t reg, uint8_t *data, uint8_t len)
|
static inline int ds3231_recv(uint8_t reg, uint8_t *data, uint8_t len)
|
||||||
{
|
{
|
||||||
return i2c_slave_read(DS3231_ADDR, ®, data, len);
|
return i2c_slave_read(_bus, DS3231_ADDR, ®, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ds3231_setTime(struct tm *time)
|
int ds3231_setTime(struct tm *time)
|
||||||
|
|
@ -281,10 +283,11 @@ bool ds3231_getTime(struct tm *time)
|
||||||
//applyTZ(time);
|
//applyTZ(time);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ds3231_Init(uint8_t scl, uint8_t sda)
|
void ds3231_Init(uint8_t bus)
|
||||||
{
|
{
|
||||||
i2c_init(scl, sda);
|
_bus = bus;
|
||||||
|
//i2c_init(0, scl, sda);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -186,7 +186,7 @@ bool ds3231_getTempFloat(float *temp);
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getTime(struct tm *time);
|
bool ds3231_getTime(struct tm *time);
|
||||||
void ds3231_Init(uint8_t scl, uint8_t sda);
|
void ds3231_Init(uint8_t bus);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
248
extras/i2c/i2c.c
248
extras/i2c/i2c.c
|
|
@ -1,18 +1,18 @@
|
||||||
/*
|
/*
|
||||||
* The MIT License (MIT)
|
* The MIT License (MIT)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2015 Johan Kanflo (github.com/kanflo)
|
* Copyright (c) 2015 Johan Kanflo (github.com/kanflo)
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
|
@ -37,35 +37,32 @@
|
||||||
|
|
||||||
#define CLK_STRETCH (10)
|
#define CLK_STRETCH (10)
|
||||||
|
|
||||||
static bool started;
|
static uint8_t freq ; // Store CPU frequency for optimisation speed in delay function ( Warning: Don't change CPU frequency during a transaction)
|
||||||
static bool flag;
|
static i2c_bus_description_t i2c_bus[MAX_I2C_BUS];
|
||||||
static bool force;
|
|
||||||
static uint8_t freq ;
|
|
||||||
static uint8_t g_scl_pin;
|
|
||||||
static uint8_t g_sda_pin;
|
|
||||||
|
|
||||||
inline bool i2c_status(void)
|
inline bool i2c_status(uint8_t bus)
|
||||||
{
|
{
|
||||||
return started;
|
return i2c_bus[bus].started;
|
||||||
}
|
}
|
||||||
|
|
||||||
void i2c_init(uint8_t scl_pin, uint8_t sda_pin)
|
void i2c_init(uint8_t bus, uint8_t scl_pin, uint8_t sda_pin, uint8_t freq)
|
||||||
{
|
{
|
||||||
started = false;
|
i2c_bus[bus].started = false;
|
||||||
flag = false ;
|
i2c_bus[bus].flag = false ;
|
||||||
g_scl_pin = scl_pin;
|
i2c_bus[bus].g_scl_pin = scl_pin;
|
||||||
g_sda_pin = sda_pin;
|
i2c_bus[bus].g_sda_pin = sda_pin;
|
||||||
|
i2c_bus[bus].frequency = freq ;
|
||||||
|
|
||||||
// Just to prevent these pins floating too much if not connected.
|
// Just to prevent these pins floating too much if not connected.
|
||||||
gpio_set_pullup(g_scl_pin, 1, 1);
|
gpio_set_pullup(i2c_bus[bus].g_scl_pin, 1, 1);
|
||||||
gpio_set_pullup(g_sda_pin, 1, 1);
|
gpio_set_pullup(i2c_bus[bus].g_sda_pin, 1, 1);
|
||||||
|
|
||||||
gpio_enable(g_scl_pin, GPIO_OUT_OPEN_DRAIN);
|
gpio_enable(i2c_bus[bus].g_scl_pin, GPIO_OUT_OPEN_DRAIN);
|
||||||
gpio_enable(g_sda_pin, GPIO_OUT_OPEN_DRAIN);
|
gpio_enable(i2c_bus[bus].g_sda_pin, GPIO_OUT_OPEN_DRAIN);
|
||||||
|
|
||||||
// I2C bus idle state.
|
// I2C bus idle state.
|
||||||
gpio_write(g_scl_pin, 1);
|
gpio_write(i2c_bus[bus].g_scl_pin, 1);
|
||||||
gpio_write(g_sda_pin, 1);
|
gpio_write(i2c_bus[bus].g_sda_pin, 1);
|
||||||
|
|
||||||
// Prevent user, if frequency is high
|
// Prevent user, if frequency is high
|
||||||
if (sdk_system_get_cpu_freq() == SYS_CPU_80MHZ)
|
if (sdk_system_get_cpu_freq() == SYS_CPU_80MHZ)
|
||||||
|
|
@ -74,180 +71,185 @@ void i2c_init(uint8_t scl_pin, uint8_t sda_pin)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void i2c_delay(void)
|
void i2c_frequency(uint8_t bus, uint8_t freq)
|
||||||
|
{
|
||||||
|
i2c_bus[bus].frequency = freq ;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void i2c_delay(uint8_t bus)
|
||||||
{
|
{
|
||||||
uint32_t delay;
|
uint32_t delay;
|
||||||
if (freq == SYS_CPU_160MHZ)
|
if (freq == SYS_CPU_160MHZ)
|
||||||
{
|
{
|
||||||
__asm volatile (
|
delay = i2c_freq_array[i2c_bus[bus].frequency][0];
|
||||||
"movi %0, %1" "\n"
|
__asm volatile (
|
||||||
"1: addi %0, %0, -1" "\n"
|
"1: addi %0, %0, -1" "\n"
|
||||||
"bnez %0, 1b" "\n"
|
"bnez %0, 1b" "\n"
|
||||||
: "=a" (delay) : "i" (I2C_CUSTOM_DELAY_160MHZ));
|
:: "a" (delay));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
__asm volatile (
|
delay = i2c_freq_array[i2c_bus[bus].frequency][1];
|
||||||
"movi %0, %1" "\n"
|
__asm volatile (
|
||||||
"1: addi %0, %0, -1" "\n"
|
"1: addi %0, %0, -1" "\n"
|
||||||
"bnez %0, 1b" "\n"
|
"bnez %0, 1b" "\n"
|
||||||
: "=a" (delay) : "i" (I2C_CUSTOM_DELAY_80MHZ));
|
:: "a" (delay));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set SCL as input, allowing it to float high, and return current
|
// Set SCL as input, allowing it to float high, and return current
|
||||||
// level of line, 0 or 1
|
// level of line, 0 or 1
|
||||||
static inline bool read_scl(void)
|
static inline bool read_scl(uint8_t bus)
|
||||||
{
|
{
|
||||||
gpio_write(g_scl_pin, 1);
|
gpio_write(i2c_bus[bus].g_scl_pin, 1);
|
||||||
return gpio_read(g_scl_pin); // Clock high, valid ACK
|
return gpio_read(i2c_bus[bus].g_scl_pin); // Clock high, valid ACK
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set SDA as input, allowing it to float high, and return current
|
// Set SDA as input, allowing it to float high, and return current
|
||||||
// level of line, 0 or 1
|
// level of line, 0 or 1
|
||||||
static inline bool read_sda(void)
|
static inline bool read_sda(uint8_t bus)
|
||||||
{
|
{
|
||||||
gpio_write(g_sda_pin, 1);
|
gpio_write(i2c_bus[bus].g_sda_pin, 1);
|
||||||
// TODO: Without this delay we get arbitration lost in i2c_stop
|
// TODO: Without this delay we get arbitration lost in i2c_stop
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
return gpio_read(g_sda_pin); // Clock high, valid ACK
|
return gpio_read(i2c_bus[bus].g_sda_pin); // Clock high, valid ACK
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actively drive SCL signal low
|
// Actively drive SCL signal low
|
||||||
static inline void clear_scl(void)
|
static inline void clear_scl(uint8_t bus)
|
||||||
{
|
{
|
||||||
gpio_write(g_scl_pin, 0);
|
gpio_write(i2c_bus[bus].g_scl_pin, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actively drive SDA signal low
|
// Actively drive SDA signal low
|
||||||
static inline void clear_sda(void)
|
static inline void clear_sda(uint8_t bus)
|
||||||
{
|
{
|
||||||
gpio_write(g_sda_pin, 0);
|
gpio_write(i2c_bus[bus].g_sda_pin, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output start condition
|
// Output start condition
|
||||||
void i2c_start(void)
|
void i2c_start(uint8_t bus)
|
||||||
{
|
{
|
||||||
uint32_t clk_stretch = CLK_STRETCH;
|
uint32_t clk_stretch = CLK_STRETCH;
|
||||||
freq = sdk_system_get_cpu_freq();
|
freq = sdk_system_get_cpu_freq();
|
||||||
if (started) { // if started, do a restart cond
|
if (i2c_bus[bus].started) { // if started, do a restart cond
|
||||||
// Set SDA to 1
|
// Set SDA to 1
|
||||||
(void) read_sda();
|
(void) read_sda(bus);
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
while (read_scl() == 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();
|
i2c_delay(bus);
|
||||||
}
|
}
|
||||||
started = true;
|
i2c_bus[bus].started = true;
|
||||||
if (read_sda() == 0) {
|
if (read_sda(bus) == 0) {
|
||||||
debug("arbitration lost in i2c_start");
|
debug("arbitration lost in i2c_start from bus %u",bus);
|
||||||
}
|
}
|
||||||
// SCL is high, set SDA from 1 to 0.
|
// SCL is high, set SDA from 1 to 0.
|
||||||
clear_sda();
|
clear_sda(bus);
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
clear_scl();
|
clear_scl(bus);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output stop condition
|
// Output stop condition
|
||||||
bool i2c_stop(void)
|
bool i2c_stop(uint8_t bus)
|
||||||
{
|
{
|
||||||
uint32_t clk_stretch = CLK_STRETCH;
|
uint32_t clk_stretch = CLK_STRETCH;
|
||||||
// Set SDA to 0
|
// Set SDA to 0
|
||||||
clear_sda();
|
clear_sda(bus);
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
// Clock stretching
|
// Clock stretching
|
||||||
while (read_scl() == 0 && clk_stretch--) ;
|
while (read_scl(bus) == 0 && clk_stretch--) ;
|
||||||
// Stop bit setup time, minimum 4us
|
// Stop bit setup time, minimum 4us
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
// SCL is high, set SDA from 0 to 1
|
// SCL is high, set SDA from 0 to 1
|
||||||
if (read_sda() == 0) {
|
if (read_sda(bus) == 0) {
|
||||||
debug("arbitration lost in i2c_stop");
|
debug("arbitration lost in i2c_stop from bus %u",bus);
|
||||||
}
|
}
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
if (!started) {
|
if (!i2c_bus[bus].started) {
|
||||||
debug("link was break!");
|
debug("bus %u link was break!",bus);
|
||||||
return false ; //If bus was stop in other way, the current transmission Failed
|
return false ; //If bus was stop in other way, the current transmission Failed
|
||||||
}
|
}
|
||||||
started = false;
|
i2c_bus[bus].started = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write a bit to I2C bus
|
// Write a bit to I2C bus
|
||||||
static void i2c_write_bit(bool bit)
|
static void i2c_write_bit(uint8_t bus, bool bit)
|
||||||
{
|
{
|
||||||
uint32_t clk_stretch = CLK_STRETCH;
|
uint32_t clk_stretch = CLK_STRETCH;
|
||||||
if (bit) {
|
if (bit) {
|
||||||
(void) read_sda();
|
(void) read_sda(bus);
|
||||||
} else {
|
} else {
|
||||||
clear_sda();
|
clear_sda(bus);
|
||||||
}
|
}
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
// Clock stretching
|
// Clock stretching
|
||||||
while (read_scl() == 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() == 0) {
|
if (bit && read_sda(bus) == 0) {
|
||||||
debug("arbitration lost in i2c_write_bit");
|
debug("arbitration lost in i2c_write_bit from bus %u",bus);
|
||||||
}
|
}
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
clear_scl();
|
clear_scl(bus);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a bit from I2C bus
|
// Read a bit from I2C bus
|
||||||
static bool i2c_read_bit(void)
|
static bool i2c_read_bit(uint8_t bus)
|
||||||
{
|
{
|
||||||
uint32_t clk_stretch = CLK_STRETCH;
|
uint32_t clk_stretch = CLK_STRETCH;
|
||||||
bool bit;
|
bool bit;
|
||||||
// Let the slave drive data
|
// Let the slave drive data
|
||||||
(void) read_sda();
|
(void) read_sda(bus);
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
// Clock stretching
|
// Clock stretching
|
||||||
while (read_scl() == 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();
|
bit = read_sda(bus);
|
||||||
i2c_delay();
|
i2c_delay(bus);
|
||||||
clear_scl();
|
clear_scl(bus);
|
||||||
return bit;
|
return bit;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool i2c_write(uint8_t byte)
|
bool i2c_write(uint8_t bus, uint8_t byte)
|
||||||
{
|
{
|
||||||
bool nack;
|
bool nack;
|
||||||
uint8_t bit;
|
uint8_t bit;
|
||||||
for (bit = 0; bit < 8; bit++) {
|
for (bit = 0; bit < 8; bit++) {
|
||||||
i2c_write_bit((byte & 0x80) != 0);
|
i2c_write_bit(bus,(byte & 0x80) != 0);
|
||||||
byte <<= 1;
|
byte <<= 1;
|
||||||
}
|
}
|
||||||
nack = i2c_read_bit();
|
nack = i2c_read_bit(bus);
|
||||||
return !nack;
|
return !nack;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t i2c_read(bool ack)
|
uint8_t i2c_read(uint8_t bus, bool ack)
|
||||||
{
|
{
|
||||||
uint8_t byte = 0;
|
uint8_t byte = 0;
|
||||||
uint8_t bit;
|
uint8_t bit;
|
||||||
for (bit = 0; bit < 8; bit++) {
|
for (bit = 0; bit < 8; bit++) {
|
||||||
byte = (byte << 1) | i2c_read_bit();
|
byte = (byte << 1) | i2c_read_bit(bus);
|
||||||
}
|
}
|
||||||
i2c_write_bit(ack);
|
i2c_write_bit(bus,ack);
|
||||||
return byte;
|
return byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
void i2c_force_bus(bool state)
|
void i2c_force_bus(uint8_t bus, bool state)
|
||||||
{
|
{
|
||||||
force = state ;
|
i2c_bus[bus].force = state ;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int i2c_bus_test()
|
static int i2c_bus_test(uint8_t bus)
|
||||||
{
|
{
|
||||||
taskENTER_CRITICAL(); // To prevent task swaping after checking flag and before set it!
|
taskENTER_CRITICAL(); // To prevent task swaping after checking flag and before set it!
|
||||||
bool status = flag ; // get current status
|
bool status = i2c_bus[bus].flag ; // get current status
|
||||||
if(force)
|
if(i2c_bus[bus].force)
|
||||||
{
|
{
|
||||||
flag = true ; // force bus on
|
i2c_bus[bus].flag = true ; // force bus on
|
||||||
taskEXIT_CRITICAL();
|
taskEXIT_CRITICAL();
|
||||||
if(status)
|
if(status)
|
||||||
i2c_stop(); //Bus was busy, stop it.
|
i2c_stop(bus); //Bus was busy, stop it.
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -260,68 +262,68 @@ static int i2c_bus_test()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
flag = true ; // Set Bus busy
|
i2c_bus[bus].flag = true ; // Set Bus busy
|
||||||
taskEXIT_CRITICAL();
|
taskEXIT_CRITICAL();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0 ;
|
return 0 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i2c_slave_write(uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len)
|
int i2c_slave_write(uint8_t bus, uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len)
|
||||||
{
|
{
|
||||||
if(i2c_bus_test())
|
if(i2c_bus_test(bus))
|
||||||
return -EBUSY ;
|
return -EBUSY ;
|
||||||
i2c_start();
|
i2c_start(bus);
|
||||||
if (!i2c_write(slave_addr << 1))
|
if (!i2c_write(bus, slave_addr << 1))
|
||||||
goto error;
|
goto error;
|
||||||
if(data != NULL)
|
if(data != NULL)
|
||||||
if (!i2c_write(*data))
|
if (!i2c_write(bus,*data))
|
||||||
goto error;
|
goto error;
|
||||||
while (len--) {
|
while (len--) {
|
||||||
if (!i2c_write(*buf++))
|
if (!i2c_write(bus,*buf++))
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (!i2c_stop())
|
if (!i2c_stop(bus))
|
||||||
goto error;
|
goto error;
|
||||||
flag = false ; // Bus free
|
i2c_bus[bus].flag = false ; // Bus free
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
debug("Write Error");
|
debug("Bus %u Write Error",bus);
|
||||||
i2c_stop();
|
i2c_stop(bus);
|
||||||
flag = false ; // Bus free
|
i2c_bus[bus].flag = false ; // Bus free
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i2c_slave_read(uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len)
|
int i2c_slave_read(uint8_t bus, uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len)
|
||||||
{
|
{
|
||||||
if(i2c_bus_test())
|
if(i2c_bus_test(bus))
|
||||||
return -EBUSY ;
|
return -EBUSY ;
|
||||||
if(data != NULL) {
|
if(data != NULL) {
|
||||||
i2c_start();
|
i2c_start(bus);
|
||||||
if (!i2c_write(slave_addr << 1))
|
if (!i2c_write(bus,slave_addr << 1))
|
||||||
goto error;
|
goto error;
|
||||||
if (!i2c_write(*data))
|
if (!i2c_write(bus,*data))
|
||||||
goto error;
|
goto error;
|
||||||
if (!i2c_stop())
|
if (!i2c_stop(bus))
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
i2c_start();
|
i2c_start(bus);
|
||||||
if (!i2c_write(slave_addr << 1 | 1)) // Slave address + read
|
if (!i2c_write(bus,slave_addr << 1 | 1)) // Slave address + read
|
||||||
goto error;
|
goto error;
|
||||||
while(len) {
|
while(len) {
|
||||||
*buf = i2c_read(len == 1);
|
*buf = i2c_read(bus,len == 1);
|
||||||
buf++;
|
buf++;
|
||||||
len--;
|
len--;
|
||||||
}
|
}
|
||||||
if (!i2c_stop())
|
if (!i2c_stop(bus))
|
||||||
goto error;
|
goto error;
|
||||||
flag = false ; // Bus free
|
i2c_bus[bus].flag = false ; // Bus free
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
debug("Read Error");
|
debug("Read Error");
|
||||||
i2c_stop();
|
i2c_stop(bus);
|
||||||
flag = false ; // Bus free
|
i2c_bus[bus].flag = false ; // Bus free
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
/*
|
/*
|
||||||
* The MIT License (MIT)
|
* The MIT License (MIT)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2015 Johan Kanflo (github.com/kanflo)
|
* Copyright (c) 2015 Johan Kanflo (github.com/kanflo)
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
|
@ -35,26 +35,33 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
#define I2C_FREQUENCY_400K true // for test WIP
|
* Define i2c bus max number
|
||||||
|
*/
|
||||||
|
#define MAX_I2C_BUS 2
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Some bit can be transmit slower.
|
* following array contain value for different frequency
|
||||||
* Selected frequency fix the speed of a bit transmission
|
* Warning : 1 is minimal, that mean at 80MHz clock, frequency max is 320kHz
|
||||||
* I2C lib take the maximum frequency defined
|
* Array format is { {160MHz, 80MHz} , {160MHz, 80MHz} , ... }
|
||||||
* Don't change frequency when I2C transaction had begin
|
|
||||||
*/
|
*/
|
||||||
|
#define I2C_FREQ_100K 0
|
||||||
|
#define I2C_FREQ_400K 1
|
||||||
|
#define I2C_FREQ_500K 2
|
||||||
|
|
||||||
|
const static uint8_t i2c_freq_array[3][2] = { {100,20}, {10,1}, {6,1} } ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct i2c_bus_description {
|
||||||
|
uint8_t g_scl_pin; // Scl pin
|
||||||
|
uint8_t g_sda_pin; // Sda pin
|
||||||
|
uint8_t frequency; // frequency selection
|
||||||
|
bool started;
|
||||||
|
bool flag;
|
||||||
|
bool force;
|
||||||
|
} i2c_bus_description_t ;
|
||||||
|
|
||||||
#ifdef I2C_FREQUENCY_500K
|
|
||||||
#define I2C_CUSTOM_DELAY_160MHZ 6
|
|
||||||
#define I2C_CUSTOM_DELAY_80MHZ 1 //Sry, maximum is 320kHz at 80MHz
|
|
||||||
#elif defined(I2C_FREQUENCY_400K)
|
|
||||||
#define I2C_CUSTOM_DELAY_160MHZ 10
|
|
||||||
#define I2C_CUSTOM_DELAY_80MHZ 1 //Sry, maximum is 320kHz at 80MHz
|
|
||||||
#else
|
|
||||||
#define I2C_CUSTOM_DELAY_160MHZ 100
|
|
||||||
#define I2C_CUSTOM_DELAY_80MHZ 20
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// I2C driver for ESP8266 written for use with esp-open-rtos
|
// I2C driver for ESP8266 written for use with esp-open-rtos
|
||||||
// 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
|
||||||
|
|
@ -64,71 +71,88 @@ extern "C" {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init bitbanging I2C driver on given pins
|
* Init bitbanging I2C driver on given pins
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @param scl_pin SCL pin for I2C
|
* @param scl_pin SCL pin for I2C
|
||||||
* @param sda_pin SDA pin for I2C
|
* @param sda_pin SDA pin for I2C
|
||||||
|
* @param freq frequency of bus (ex : I2C_FREQ_400K)
|
||||||
*/
|
*/
|
||||||
void i2c_init(uint8_t scl_pin, uint8_t sda_pin);
|
void i2c_init(uint8_t bus, uint8_t scl_pin, uint8_t sda_pin, uint8_t freq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change bus frequency
|
||||||
|
* @param bus Bus i2c selection
|
||||||
|
* @param freq frequency of bus (ex : I2C_FREQ_400K)
|
||||||
|
*/
|
||||||
|
void i2c_frequency(uint8_t bus, uint8_t freq);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a byte to I2C bus.
|
* Write a byte to I2C bus.
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @param byte Pointer to device descriptor
|
* @param byte Pointer to device descriptor
|
||||||
* @return true if slave acked
|
* @return true if slave acked
|
||||||
*/
|
*/
|
||||||
bool i2c_write(uint8_t byte);
|
bool i2c_write(uint8_t bus, uint8_t byte);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a byte from I2C bus.
|
* Read a byte from I2C bus.
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @param ack Set Ack for slave (false: Ack // true: NoAck)
|
* @param ack Set Ack for slave (false: Ack // true: NoAck)
|
||||||
* @return byte read from slave.
|
* @return byte read from slave.
|
||||||
*/
|
*/
|
||||||
uint8_t i2c_read(bool ack);
|
uint8_t i2c_read(uint8_t bus, bool ack);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send start or restart condition
|
* Send start or restart condition
|
||||||
|
* @param bus Bus i2c selection
|
||||||
*/
|
*/
|
||||||
void i2c_start(void);
|
void i2c_start(uint8_t bus);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send stop condition
|
* Send stop condition
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @return false if link was broken
|
* @return false if link was broken
|
||||||
*/
|
*/
|
||||||
bool i2c_stop(void);
|
bool i2c_stop(uint8_t bus);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get status from I2C bus.
|
* get status from I2C bus.
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @return true if busy.
|
* @return true if busy.
|
||||||
*/
|
*/
|
||||||
bool i2c_status(void);
|
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.
|
||||||
* Warning: Use with precaution. Don't use it if you can avoid it. Usefull for priority transmission.
|
* Warning: Use with precaution. Don't use it if you can avoid it. Usefull for priority transmission.
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @param state Force the next I2C transmission if true (Use with precaution)
|
* @param state Force the next I2C transmission if true (Use with precaution)
|
||||||
*/
|
*/
|
||||||
void i2c_force_bus(bool state);
|
void i2c_force_bus(uint8_t bus, bool state);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write 'len' bytes from 'buf' to slave at 'data' register adress .
|
* Write 'len' bytes from 'buf' to slave at 'data' register adress .
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @param slave_addr slave device address
|
* @param slave_addr slave device address
|
||||||
* @param data Pointer to register address to send if non-null
|
* @param data Pointer to register address to send if non-null
|
||||||
* @param buf Pointer to data buffer
|
* @param buf Pointer to data buffer
|
||||||
* @param len Number of byte to send
|
* @param len Number of byte to send
|
||||||
* @return Non-Zero if error occured
|
* @return Non-Zero if error occured
|
||||||
*/
|
*/
|
||||||
int i2c_slave_write(uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len);
|
int i2c_slave_write(uint8_t bus, uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Issue a send operation of 'data' register adress, followed by reading 'len' bytes
|
* Issue a send operation of 'data' register adress, followed by reading 'len' bytes
|
||||||
* from slave into 'buf'.
|
* from slave into 'buf'.
|
||||||
|
* @param bus Bus i2c selection
|
||||||
* @param slave_addr slave device address
|
* @param slave_addr slave device address
|
||||||
* @param data Pointer to register address to send if non-null
|
* @param data Pointer to register address to send if non-null
|
||||||
* @param buf Pointer to data buffer
|
* @param buf Pointer to data buffer
|
||||||
* @param len Number of byte to read
|
* @param len Number of byte to read
|
||||||
* @return Non-Zero if error occured
|
* @return Non-Zero if error occured
|
||||||
*/
|
*/
|
||||||
int i2c_slave_read(uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len);
|
int i2c_slave_read(uint8_t bus, uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue