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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __I2C_H__
|
|
|
|
#define __I2C_H__
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2017-03-21 06:41:47 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2016-10-24 13:09:17 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
|
|
|
|
/*
|
2017-09-01 09:29:32 +00:00
|
|
|
* Define i2c bus max number
|
2017-03-21 06:41:47 +00:00
|
|
|
*/
|
2017-09-01 09:29:32 +00:00
|
|
|
#define MAX_I2C_BUS 2
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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_100K,
|
|
|
|
I2C_FREQ_400K,
|
|
|
|
I2C_FREQ_500K,
|
|
|
|
} i2c_freq_t;
|
|
|
|
|
|
|
|
const static uint8_t i2c_freq_array[NB_FREQ_AVAILABLE][2] = { {255,35}, {100,20}, {10,1}, {6,1} } ;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Device descriptor
|
|
|
|
*/
|
|
|
|
typedef struct i2c_dev {
|
|
|
|
uint8_t bus ;
|
|
|
|
uint8_t addr ;
|
|
|
|
} i2c_dev_t ;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bus settings
|
|
|
|
*/
|
|
|
|
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 ;
|
2017-03-21 06:41:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// With calling overhead, we end up at ~320kbit/s
|
|
|
|
|
|
|
|
//Level 0 API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init bitbanging I2C driver on given pins
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @param scl_pin SCL pin for I2C
|
|
|
|
* @param sda_pin SDA pin for I2C
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param freq frequency of bus (ex : I2C_FREQ_400K)
|
|
|
|
*/
|
|
|
|
void i2c_init(uint8_t bus, uint8_t scl_pin, uint8_t sda_pin, i2c_freq_t freq);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change bus frequency
|
|
|
|
* @param bus Bus i2c selection
|
|
|
|
* @param freq frequency of bus (ex : I2C_FREQ_400K)
|
2017-03-21 06:41:47 +00:00
|
|
|
*/
|
2017-09-01 09:29:32 +00:00
|
|
|
void i2c_frequency(uint8_t bus, i2c_freq_t freq);
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
/**
|
|
|
|
* Write a byte to I2C bus.
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @param byte Pointer to device descriptor
|
|
|
|
* @return true if slave acked
|
|
|
|
*/
|
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
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
/**
|
|
|
|
* Read a byte from I2C bus.
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @param ack Set Ack for slave (false: Ack // true: NoAck)
|
|
|
|
* @return byte read from slave.
|
|
|
|
*/
|
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
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
/**
|
|
|
|
* Send start or restart condition
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Send stop condition
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @return false if link was broken
|
|
|
|
*/
|
2017-09-01 09:29:32 +00:00
|
|
|
bool i2c_stop(uint8_t bus);
|
2015-08-25 12:45:48 +00:00
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
/**
|
|
|
|
* get status from I2C bus.
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @return true if busy.
|
|
|
|
*/
|
2017-09-01 09:29:32 +00:00
|
|
|
bool i2c_status(uint8_t bus);
|
2017-03-21 06:41:47 +00:00
|
|
|
|
|
|
|
//Level 1 API (Don't need functions above)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @param state Force the next I2C transmission if true (Use with precaution)
|
|
|
|
*/
|
2017-09-01 09:29:32 +00:00
|
|
|
void i2c_force_bus(uint8_t bus, bool state);
|
2017-03-21 06:41:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write 'len' bytes from 'buf' to slave at 'data' register adress .
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @param slave_addr slave device address
|
|
|
|
* @param data Pointer to register address to send if non-null
|
|
|
|
* @param buf Pointer to data buffer
|
|
|
|
* @param len Number of byte to send
|
|
|
|
* @return Non-Zero if error occured
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Issue a send operation of 'data' register adress, followed by reading 'len' bytes
|
|
|
|
* from slave into 'buf'.
|
2017-09-01 09:29:32 +00:00
|
|
|
* @param bus Bus i2c selection
|
2017-03-21 06:41:47 +00:00
|
|
|
* @param slave_addr slave device address
|
|
|
|
* @param data Pointer to register address to send if non-null
|
|
|
|
* @param buf Pointer to data buffer
|
|
|
|
* @param len Number of byte to read
|
|
|
|
* @return Non-Zero if error occured
|
|
|
|
*/
|
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);
|
2016-10-24 13:09:17 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __I2C_H__ */
|