2017-09-01 09:29:32 +00:00
|
|
|
/*
|
2015-08-25 12:45:48 +00:00
|
|
|
* The MIT License (MIT)
|
2017-09-01 09:29:32 +00:00
|
|
|
*
|
2015-08-25 12:45:48 +00:00
|
|
|
* Copyright (c) 2015 Johan Kanflo (github.com/kanflo)
|
2017-09-01 09:29:32 +00:00
|
|
|
*
|
2015-08-25 12:45:48 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-09-01 09:29:32 +00:00
|
|
|
*
|
2015-08-25 12:45:48 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-09-01 09:29:32 +00:00
|
|
|
*
|
2015-08-25 12:45:48 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <esp8266.h>
|
|
|
|
#include <espressif/esp_misc.h> // sdk_os_delay_us
|
2017-03-21 06:41:47 +00:00
|
|
|
#include <espressif/esp_system.h>
|
2015-08-25 12:45:48 +00:00
|
|
|
#include "i2c.h"
|
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
//#define I2C_DEBUG true
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
#ifdef I2C_DEBUG
|
|
|
|
#define debug(fmt, ...) printf("%s: " fmt "\n", "I2C", ## __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define debug(fmt, ...)
|
|
|
|
#endif
|
2015-08-25 12:45:48 +00:00
|
|
|
|
|
|
|
#define CLK_STRETCH (10)
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
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];
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
inline bool i2c_status(uint8_t bus)
|
2017-03-21 06:41:47 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
return i2c_bus[bus].started;
|
2017-03-21 06:41:47 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
void i2c_init(uint8_t bus, uint8_t scl_pin, uint8_t sda_pin, i2c_freq_t freq)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].started = false;
|
|
|
|
i2c_bus[bus].flag = false ;
|
|
|
|
i2c_bus[bus].g_scl_pin = scl_pin;
|
|
|
|
i2c_bus[bus].g_sda_pin = sda_pin;
|
|
|
|
i2c_bus[bus].frequency = freq ;
|
2016-07-16 12:18:55 +00:00
|
|
|
|
|
|
|
// Just to prevent these pins floating too much if not connected.
|
2017-09-01 09:29:32 +00:00
|
|
|
gpio_set_pullup(i2c_bus[bus].g_scl_pin, 1, 1);
|
|
|
|
gpio_set_pullup(i2c_bus[bus].g_sda_pin, 1, 1);
|
2016-07-16 12:18:55 +00:00
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
gpio_enable(i2c_bus[bus].g_scl_pin, GPIO_OUT_OPEN_DRAIN);
|
|
|
|
gpio_enable(i2c_bus[bus].g_sda_pin, GPIO_OUT_OPEN_DRAIN);
|
2016-07-16 12:18:55 +00:00
|
|
|
|
2016-09-01 11:18:09 +00:00
|
|
|
// I2C bus idle state.
|
2017-09-01 09:29:32 +00:00
|
|
|
gpio_write(i2c_bus[bus].g_scl_pin, 1);
|
|
|
|
gpio_write(i2c_bus[bus].g_sda_pin, 1);
|
2017-03-21 06:41:47 +00:00
|
|
|
|
|
|
|
// Prevent user, if frequency is high
|
|
|
|
if (sdk_system_get_cpu_freq() == SYS_CPU_80MHZ)
|
2017-09-01 09:29:32 +00:00
|
|
|
if (i2c_freq_array[i2c_bus[bus].frequency][1] == 1)
|
2017-03-21 06:41:47 +00:00
|
|
|
debug("Max frequency is 320Khz at 80MHz");
|
|
|
|
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
void i2c_frequency(uint8_t bus, i2c_freq_t freq)
|
|
|
|
{
|
|
|
|
i2c_bus[bus].frequency = freq ;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void i2c_delay(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-03-21 06:41:47 +00:00
|
|
|
uint32_t delay;
|
|
|
|
if (freq == SYS_CPU_160MHZ)
|
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
delay = i2c_freq_array[i2c_bus[bus].frequency][0];
|
|
|
|
__asm volatile (
|
|
|
|
"1: addi %0, %0, -1" "\n"
|
|
|
|
"bnez %0, 1b" "\n"
|
|
|
|
:: "a" (delay));
|
2017-03-21 06:41:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
delay = i2c_freq_array[i2c_bus[bus].frequency][1];
|
|
|
|
__asm volatile (
|
|
|
|
"1: addi %0, %0, -1" "\n"
|
|
|
|
"bnez %0, 1b" "\n"
|
|
|
|
:: "a" (delay));
|
2017-03-21 06:41:47 +00:00
|
|
|
}
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 12:18:55 +00:00
|
|
|
// Set SCL as input, allowing it to float high, and return current
|
|
|
|
// level of line, 0 or 1
|
2017-09-01 09:29:32 +00:00
|
|
|
static inline bool read_scl(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
gpio_write(i2c_bus[bus].g_scl_pin, 1);
|
|
|
|
return gpio_read(i2c_bus[bus].g_scl_pin); // Clock high, valid ACK
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 11:18:09 +00:00
|
|
|
// Set SDA as input, allowing it to float high, and return current
|
|
|
|
// level of line, 0 or 1
|
2017-09-01 09:29:32 +00:00
|
|
|
static inline bool read_sda(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
gpio_write(i2c_bus[bus].g_sda_pin, 1);
|
2015-08-25 12:45:48 +00:00
|
|
|
// TODO: Without this delay we get arbitration lost in i2c_stop
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_delay(bus);
|
|
|
|
return gpio_read(i2c_bus[bus].g_sda_pin); // Clock high, valid ACK
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actively drive SCL signal low
|
2017-09-01 09:29:32 +00:00
|
|
|
static inline void clear_scl(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
gpio_write(i2c_bus[bus].g_scl_pin, 0);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actively drive SDA signal low
|
2017-09-01 09:29:32 +00:00
|
|
|
static inline void clear_sda(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
gpio_write(i2c_bus[bus].g_sda_pin, 0);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output start condition
|
2017-09-01 09:29:32 +00:00
|
|
|
void i2c_start(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-03-21 06:41:47 +00:00
|
|
|
freq = sdk_system_get_cpu_freq();
|
2017-09-01 09:29:32 +00:00
|
|
|
if (i2c_bus[bus].started) { // if started, do a restart cond
|
2015-08-25 12:45:48 +00:00
|
|
|
// Set SDA to 1
|
2017-09-01 09:29:32 +00:00
|
|
|
(void) read_sda(bus);
|
|
|
|
i2c_delay(bus);
|
|
|
|
uint32_t clk_stretch = CLK_STRETCH;
|
|
|
|
while (read_scl(bus) == 0 && clk_stretch--) ;
|
2015-08-25 12:45:48 +00:00
|
|
|
// Repeated start setup time, minimum 4.7us
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_delay(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].started = true;
|
|
|
|
if (read_sda(bus) == 0) {
|
|
|
|
debug("arbitration lost in i2c_start from bus %u",bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
// SCL is high, set SDA from 1 to 0.
|
2017-09-01 09:29:32 +00:00
|
|
|
clear_sda(bus);
|
|
|
|
i2c_delay(bus);
|
|
|
|
clear_scl(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output stop condition
|
2017-09-01 09:29:32 +00:00
|
|
|
bool i2c_stop(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
|
|
|
uint32_t clk_stretch = CLK_STRETCH;
|
|
|
|
// Set SDA to 0
|
2017-09-01 09:29:32 +00:00
|
|
|
clear_sda(bus);
|
|
|
|
i2c_delay(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
// Clock stretching
|
2017-09-01 09:29:32 +00:00
|
|
|
while (read_scl(bus) == 0 && clk_stretch--) ;
|
2015-08-25 12:45:48 +00:00
|
|
|
// Stop bit setup time, minimum 4us
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_delay(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
// SCL is high, set SDA from 0 to 1
|
2017-09-01 09:29:32 +00:00
|
|
|
if (read_sda(bus) == 0) {
|
|
|
|
debug("arbitration lost in i2c_stop from bus %u",bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_delay(bus);
|
|
|
|
if (!i2c_bus[bus].started) {
|
|
|
|
debug("bus %u link was break!",bus);
|
2017-03-21 06:41:47 +00:00
|
|
|
return false ; //If bus was stop in other way, the current transmission Failed
|
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].started = false;
|
2017-03-21 06:41:47 +00:00
|
|
|
return true;
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write a bit to I2C bus
|
2017-09-01 09:29:32 +00:00
|
|
|
static void i2c_write_bit(uint8_t bus, bool bit)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
|
|
|
uint32_t clk_stretch = CLK_STRETCH;
|
|
|
|
if (bit) {
|
2017-09-01 09:29:32 +00:00
|
|
|
(void) read_sda(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
} else {
|
2017-09-01 09:29:32 +00:00
|
|
|
clear_sda(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_delay(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
// Clock stretching
|
2017-09-01 09:29:32 +00:00
|
|
|
while (read_scl(bus) == 0 && clk_stretch--) ;
|
2015-08-25 12:45:48 +00:00
|
|
|
// SCL is high, now data is valid
|
|
|
|
// If SDA is high, check that nobody else is driving SDA
|
2017-09-01 09:29:32 +00:00
|
|
|
if (bit && read_sda(bus) == 0) {
|
|
|
|
debug("arbitration lost in i2c_write_bit from bus %u",bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_delay(bus);
|
|
|
|
clear_scl(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read a bit from I2C bus
|
2017-09-01 09:29:32 +00:00
|
|
|
static bool i2c_read_bit(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
|
|
|
uint32_t clk_stretch = CLK_STRETCH;
|
|
|
|
bool bit;
|
|
|
|
// Let the slave drive data
|
2017-09-01 09:29:32 +00:00
|
|
|
(void) read_sda(bus);
|
|
|
|
i2c_delay(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
// Clock stretching
|
2017-09-01 09:29:32 +00:00
|
|
|
while (read_scl(bus) == 0 && clk_stretch--) ;
|
2015-08-25 12:45:48 +00:00
|
|
|
// SCL is high, now data is valid
|
2017-09-01 09:29:32 +00:00
|
|
|
bit = read_sda(bus);
|
|
|
|
i2c_delay(bus);
|
|
|
|
clear_scl(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
bool i2c_write(uint8_t bus, uint8_t byte)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
|
|
|
bool nack;
|
|
|
|
uint8_t bit;
|
|
|
|
for (bit = 0; bit < 8; bit++) {
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_write_bit(bus,(byte & 0x80) != 0);
|
2015-08-25 12:45:48 +00:00
|
|
|
byte <<= 1;
|
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
nack = i2c_read_bit(bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
return !nack;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
uint8_t i2c_read(uint8_t bus, bool ack)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
|
|
|
uint8_t byte = 0;
|
|
|
|
uint8_t bit;
|
|
|
|
for (bit = 0; bit < 8; bit++) {
|
2017-09-01 09:29:32 +00:00
|
|
|
byte = ((byte << 1)) | (i2c_read_bit(bus));
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_write_bit(bus,ack);
|
2015-08-25 12:45:48 +00:00
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
void i2c_force_bus(uint8_t bus, bool state)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].force = state ;
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
static int i2c_bus_test(uint8_t bus)
|
2015-08-25 12:45:48 +00:00
|
|
|
{
|
2017-03-21 06:41:47 +00:00
|
|
|
taskENTER_CRITICAL(); // To prevent task swaping after checking flag and before set it!
|
2017-09-01 09:29:32 +00:00
|
|
|
bool status = i2c_bus[bus].flag ; // get current status
|
|
|
|
if(i2c_bus[bus].force)
|
2017-03-21 06:41:47 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].flag = true ; // force bus on
|
2017-03-21 06:41:47 +00:00
|
|
|
taskEXIT_CRITICAL();
|
|
|
|
if(status)
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_stop(bus); //Bus was busy, stop it.
|
2017-03-21 06:41:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
taskEXIT_CRITICAL();
|
|
|
|
debug("busy");
|
|
|
|
taskYIELD(); // If bus busy, change task to try finish last com.
|
|
|
|
return -EBUSY ; // If bus busy, inform user
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-03-21 06:41:47 +00:00
|
|
|
else
|
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].flag = true ; // Set Bus busy
|
2017-03-21 06:41:47 +00:00
|
|
|
taskEXIT_CRITICAL();
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-03-21 06:41:47 +00:00
|
|
|
}
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
int i2c_slave_write(uint8_t bus, uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len)
|
2017-03-21 06:41:47 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
if(i2c_bus_test(bus))
|
2017-03-21 06:41:47 +00:00
|
|
|
return -EBUSY ;
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_start(bus);
|
|
|
|
if (!i2c_write(bus, slave_addr << 1))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
|
|
|
if(data != NULL)
|
2017-09-01 09:29:32 +00:00
|
|
|
if (!i2c_write(bus,*data))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
|
|
|
while (len--) {
|
2017-09-01 09:29:32 +00:00
|
|
|
if (!i2c_write(bus,*buf++))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
if (!i2c_stop(bus))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].flag = false ; // Bus free
|
2017-03-21 06:41:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2017-09-01 09:29:32 +00:00
|
|
|
debug("Bus %u Write Error",bus);
|
|
|
|
i2c_stop(bus);
|
|
|
|
i2c_bus[bus].flag = false ; // Bus free
|
2017-03-21 06:41:47 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
int i2c_slave_read(uint8_t bus, uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len)
|
2017-03-21 06:41:47 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
if(i2c_bus_test(bus))
|
2017-03-21 06:41:47 +00:00
|
|
|
return -EBUSY ;
|
|
|
|
if(data != NULL) {
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_start(bus);
|
|
|
|
if (!i2c_write(bus,slave_addr << 1))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
2017-09-01 09:29:32 +00:00
|
|
|
if (!i2c_write(bus,*data))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
2017-09-01 09:29:32 +00:00
|
|
|
if (!i2c_stop(bus))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_start(bus);
|
|
|
|
if (!i2c_write(bus,slave_addr << 1 | 1)) // Slave address + read
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
|
|
|
while(len) {
|
2017-09-01 09:29:32 +00:00
|
|
|
*buf = i2c_read(bus,len == 1);
|
2017-03-21 06:41:47 +00:00
|
|
|
buf++;
|
|
|
|
len--;
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|
2017-09-01 09:29:32 +00:00
|
|
|
if (!i2c_stop(bus))
|
2017-03-21 06:41:47 +00:00
|
|
|
goto error;
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_bus[bus].flag = false ; // Bus free
|
2017-03-21 06:41:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
debug("Read Error");
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_stop(bus);
|
|
|
|
i2c_bus[bus].flag = false ; // Bus free
|
2017-03-21 06:41:47 +00:00
|
|
|
return -EIO;
|
2015-08-25 12:45:48 +00:00
|
|
|
}
|