I2C bus upgrade (#432)
This commit is contained in:
parent
d100f42b1f
commit
b83c2629b9
56 changed files with 909 additions and 804 deletions
252
extras/i2c/i2c.c
252
extras/i2c/i2c.c
|
@ -1,18 +1,18 @@
|
|||
/*
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2015 Johan Kanflo (github.com/kanflo)
|
||||
*
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
*
|
||||
* 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
|
||||
|
@ -37,217 +37,219 @@
|
|||
|
||||
#define CLK_STRETCH (10)
|
||||
|
||||
static bool started;
|
||||
static bool flag;
|
||||
static bool force;
|
||||
static uint8_t freq ;
|
||||
static uint8_t g_scl_pin;
|
||||
static uint8_t g_sda_pin;
|
||||
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];
|
||||
|
||||
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, i2c_freq_t freq)
|
||||
{
|
||||
started = false;
|
||||
flag = false ;
|
||||
g_scl_pin = scl_pin;
|
||||
g_sda_pin = sda_pin;
|
||||
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 ;
|
||||
|
||||
// Just to prevent these pins floating too much if not connected.
|
||||
gpio_set_pullup(g_scl_pin, 1, 1);
|
||||
gpio_set_pullup(g_sda_pin, 1, 1);
|
||||
gpio_set_pullup(i2c_bus[bus].g_scl_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(g_sda_pin, GPIO_OUT_OPEN_DRAIN);
|
||||
gpio_enable(i2c_bus[bus].g_scl_pin, GPIO_OUT_OPEN_DRAIN);
|
||||
gpio_enable(i2c_bus[bus].g_sda_pin, GPIO_OUT_OPEN_DRAIN);
|
||||
|
||||
// I2C bus idle state.
|
||||
gpio_write(g_scl_pin, 1);
|
||||
gpio_write(g_sda_pin, 1);
|
||||
gpio_write(i2c_bus[bus].g_scl_pin, 1);
|
||||
gpio_write(i2c_bus[bus].g_sda_pin, 1);
|
||||
|
||||
// Prevent user, if frequency is high
|
||||
if (sdk_system_get_cpu_freq() == SYS_CPU_80MHZ)
|
||||
if (I2C_CUSTOM_DELAY_80MHZ == 1)
|
||||
if (i2c_freq_array[i2c_bus[bus].frequency][1] == 1)
|
||||
debug("Max frequency is 320Khz at 80MHz");
|
||||
|
||||
}
|
||||
|
||||
static inline void i2c_delay(void)
|
||||
void i2c_frequency(uint8_t bus, i2c_freq_t freq)
|
||||
{
|
||||
i2c_bus[bus].frequency = freq ;
|
||||
}
|
||||
|
||||
static inline void i2c_delay(uint8_t bus)
|
||||
{
|
||||
uint32_t delay;
|
||||
if (freq == SYS_CPU_160MHZ)
|
||||
{
|
||||
__asm volatile (
|
||||
"movi %0, %1" "\n"
|
||||
"1: addi %0, %0, -1" "\n"
|
||||
"bnez %0, 1b" "\n"
|
||||
: "=a" (delay) : "i" (I2C_CUSTOM_DELAY_160MHZ));
|
||||
delay = i2c_freq_array[i2c_bus[bus].frequency][0];
|
||||
__asm volatile (
|
||||
"1: addi %0, %0, -1" "\n"
|
||||
"bnez %0, 1b" "\n"
|
||||
:: "a" (delay));
|
||||
}
|
||||
else
|
||||
{
|
||||
__asm volatile (
|
||||
"movi %0, %1" "\n"
|
||||
"1: addi %0, %0, -1" "\n"
|
||||
"bnez %0, 1b" "\n"
|
||||
: "=a" (delay) : "i" (I2C_CUSTOM_DELAY_80MHZ));
|
||||
delay = i2c_freq_array[i2c_bus[bus].frequency][1];
|
||||
__asm volatile (
|
||||
"1: addi %0, %0, -1" "\n"
|
||||
"bnez %0, 1b" "\n"
|
||||
:: "a" (delay));
|
||||
}
|
||||
}
|
||||
|
||||
// Set SCL as input, allowing it to float high, and return current
|
||||
// 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);
|
||||
return gpio_read(g_scl_pin); // Clock high, valid ACK
|
||||
gpio_write(i2c_bus[bus].g_scl_pin, 1);
|
||||
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
|
||||
// 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
|
||||
i2c_delay();
|
||||
return gpio_read(g_sda_pin); // Clock high, valid ACK
|
||||
i2c_delay(bus);
|
||||
return gpio_read(i2c_bus[bus].g_sda_pin); // Clock high, valid ACK
|
||||
}
|
||||
|
||||
// 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
|
||||
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
|
||||
void i2c_start(void)
|
||||
void i2c_start(uint8_t bus)
|
||||
{
|
||||
uint32_t clk_stretch = CLK_STRETCH;
|
||||
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
|
||||
(void) read_sda();
|
||||
i2c_delay();
|
||||
while (read_scl() == 0 && clk_stretch--) ;
|
||||
(void) read_sda(bus);
|
||||
i2c_delay(bus);
|
||||
uint32_t clk_stretch = CLK_STRETCH;
|
||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
||||
// Repeated start setup time, minimum 4.7us
|
||||
i2c_delay();
|
||||
i2c_delay(bus);
|
||||
}
|
||||
started = true;
|
||||
if (read_sda() == 0) {
|
||||
debug("arbitration lost in i2c_start");
|
||||
i2c_bus[bus].started = true;
|
||||
if (read_sda(bus) == 0) {
|
||||
debug("arbitration lost in i2c_start from bus %u",bus);
|
||||
}
|
||||
// SCL is high, set SDA from 1 to 0.
|
||||
clear_sda();
|
||||
i2c_delay();
|
||||
clear_scl();
|
||||
clear_sda(bus);
|
||||
i2c_delay(bus);
|
||||
clear_scl(bus);
|
||||
}
|
||||
|
||||
// Output stop condition
|
||||
bool i2c_stop(void)
|
||||
bool i2c_stop(uint8_t bus)
|
||||
{
|
||||
uint32_t clk_stretch = CLK_STRETCH;
|
||||
// Set SDA to 0
|
||||
clear_sda();
|
||||
i2c_delay();
|
||||
clear_sda(bus);
|
||||
i2c_delay(bus);
|
||||
// Clock stretching
|
||||
while (read_scl() == 0 && clk_stretch--) ;
|
||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
||||
// Stop bit setup time, minimum 4us
|
||||
i2c_delay();
|
||||
i2c_delay(bus);
|
||||
// SCL is high, set SDA from 0 to 1
|
||||
if (read_sda() == 0) {
|
||||
debug("arbitration lost in i2c_stop");
|
||||
if (read_sda(bus) == 0) {
|
||||
debug("arbitration lost in i2c_stop from bus %u",bus);
|
||||
}
|
||||
i2c_delay();
|
||||
if (!started) {
|
||||
debug("link was break!");
|
||||
i2c_delay(bus);
|
||||
if (!i2c_bus[bus].started) {
|
||||
debug("bus %u link was break!",bus);
|
||||
return false ; //If bus was stop in other way, the current transmission Failed
|
||||
}
|
||||
started = false;
|
||||
i2c_bus[bus].started = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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;
|
||||
if (bit) {
|
||||
(void) read_sda();
|
||||
(void) read_sda(bus);
|
||||
} else {
|
||||
clear_sda();
|
||||
clear_sda(bus);
|
||||
}
|
||||
i2c_delay();
|
||||
i2c_delay(bus);
|
||||
// Clock stretching
|
||||
while (read_scl() == 0 && clk_stretch--) ;
|
||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
||||
// SCL is high, now data is valid
|
||||
// If SDA is high, check that nobody else is driving SDA
|
||||
if (bit && read_sda() == 0) {
|
||||
debug("arbitration lost in i2c_write_bit");
|
||||
if (bit && read_sda(bus) == 0) {
|
||||
debug("arbitration lost in i2c_write_bit from bus %u",bus);
|
||||
}
|
||||
i2c_delay();
|
||||
clear_scl();
|
||||
i2c_delay(bus);
|
||||
clear_scl(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;
|
||||
bool bit;
|
||||
// Let the slave drive data
|
||||
(void) read_sda();
|
||||
i2c_delay();
|
||||
(void) read_sda(bus);
|
||||
i2c_delay(bus);
|
||||
// Clock stretching
|
||||
while (read_scl() == 0 && clk_stretch--) ;
|
||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
||||
// SCL is high, now data is valid
|
||||
bit = read_sda();
|
||||
i2c_delay();
|
||||
clear_scl();
|
||||
bit = read_sda(bus);
|
||||
i2c_delay(bus);
|
||||
clear_scl(bus);
|
||||
return bit;
|
||||
}
|
||||
|
||||
bool i2c_write(uint8_t byte)
|
||||
bool i2c_write(uint8_t bus, uint8_t byte)
|
||||
{
|
||||
bool nack;
|
||||
uint8_t bit;
|
||||
for (bit = 0; bit < 8; bit++) {
|
||||
i2c_write_bit((byte & 0x80) != 0);
|
||||
i2c_write_bit(bus,(byte & 0x80) != 0);
|
||||
byte <<= 1;
|
||||
}
|
||||
nack = i2c_read_bit();
|
||||
nack = i2c_read_bit(bus);
|
||||
return !nack;
|
||||
}
|
||||
|
||||
uint8_t i2c_read(bool ack)
|
||||
uint8_t i2c_read(uint8_t bus, bool ack)
|
||||
{
|
||||
uint8_t byte = 0;
|
||||
uint8_t 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;
|
||||
}
|
||||
|
||||
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!
|
||||
bool status = flag ; // get current status
|
||||
if(force)
|
||||
bool status = i2c_bus[bus].flag ; // get current status
|
||||
if(i2c_bus[bus].force)
|
||||
{
|
||||
flag = true ; // force bus on
|
||||
i2c_bus[bus].flag = true ; // force bus on
|
||||
taskEXIT_CRITICAL();
|
||||
if(status)
|
||||
i2c_stop(); //Bus was busy, stop it.
|
||||
i2c_stop(bus); //Bus was busy, stop it.
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -260,68 +262,68 @@ static int i2c_bus_test()
|
|||
}
|
||||
else
|
||||
{
|
||||
flag = true ; // Set Bus busy
|
||||
i2c_bus[bus].flag = true ; // Set Bus busy
|
||||
taskEXIT_CRITICAL();
|
||||
}
|
||||
}
|
||||
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 ;
|
||||
i2c_start();
|
||||
if (!i2c_write(slave_addr << 1))
|
||||
i2c_start(bus);
|
||||
if (!i2c_write(bus, slave_addr << 1))
|
||||
goto error;
|
||||
if(data != NULL)
|
||||
if (!i2c_write(*data))
|
||||
if (!i2c_write(bus,*data))
|
||||
goto error;
|
||||
while (len--) {
|
||||
if (!i2c_write(*buf++))
|
||||
if (!i2c_write(bus,*buf++))
|
||||
goto error;
|
||||
}
|
||||
if (!i2c_stop())
|
||||
if (!i2c_stop(bus))
|
||||
goto error;
|
||||
flag = false ; // Bus free
|
||||
i2c_bus[bus].flag = false ; // Bus free
|
||||
return 0;
|
||||
|
||||
error:
|
||||
debug("Write Error");
|
||||
i2c_stop();
|
||||
flag = false ; // Bus free
|
||||
debug("Bus %u Write Error",bus);
|
||||
i2c_stop(bus);
|
||||
i2c_bus[bus].flag = false ; // Bus free
|
||||
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 ;
|
||||
if(data != NULL) {
|
||||
i2c_start();
|
||||
if (!i2c_write(slave_addr << 1))
|
||||
i2c_start(bus);
|
||||
if (!i2c_write(bus,slave_addr << 1))
|
||||
goto error;
|
||||
if (!i2c_write(*data))
|
||||
if (!i2c_write(bus,*data))
|
||||
goto error;
|
||||
if (!i2c_stop())
|
||||
if (!i2c_stop(bus))
|
||||
goto error;
|
||||
}
|
||||
i2c_start();
|
||||
if (!i2c_write(slave_addr << 1 | 1)) // Slave address + read
|
||||
i2c_start(bus);
|
||||
if (!i2c_write(bus,slave_addr << 1 | 1)) // Slave address + read
|
||||
goto error;
|
||||
while(len) {
|
||||
*buf = i2c_read(len == 1);
|
||||
*buf = i2c_read(bus,len == 1);
|
||||
buf++;
|
||||
len--;
|
||||
}
|
||||
if (!i2c_stop())
|
||||
if (!i2c_stop(bus))
|
||||
goto error;
|
||||
flag = false ; // Bus free
|
||||
i2c_bus[bus].flag = false ; // Bus free
|
||||
return 0;
|
||||
|
||||
error:
|
||||
debug("Read Error");
|
||||
i2c_stop();
|
||||
flag = false ; // Bus free
|
||||
i2c_stop(bus);
|
||||
i2c_bus[bus].flag = false ; // Bus free
|
||||
return -EIO;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue