2020-06-06 14:35:55 +02:00
|
|
|
/** library to communicate using I²C as master
|
2019-12-05 13:26:49 +01:00
|
|
|
* @file
|
2017-04-03 13:07:03 +02:00
|
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
2020-06-06 14:35:55 +02:00
|
|
|
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
|
2020-02-11 12:20:13 +01:00
|
|
|
* @date 2017-2020
|
2020-12-12 14:08:05 +01:00
|
|
|
* @note peripherals used: I²C @ref i2c_master_i2c
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-12-05 13:26:49 +01:00
|
|
|
/** I²C return codes */
|
2018-04-03 16:59:02 +02:00
|
|
|
enum i2c_master_rc {
|
|
|
|
I2C_MASTER_RC_NONE = 0, /**< no error */
|
|
|
|
I2C_MASTER_RC_START_STOP_IN_PROGESS, /**< a start or stop condition is already in progress */
|
|
|
|
I2C_MASTER_RC_NOT_MASTER, /**< not in master mode */
|
|
|
|
I2C_MASTER_RC_NOT_TRANSMIT, /**< not in transmit mode */
|
|
|
|
I2C_MASTER_RC_NOT_RECEIVE, /**< not in receive mode */
|
2019-12-05 13:26:49 +01:00
|
|
|
I2C_MASTER_RC_NOT_READY, /**< slave is not read (previous operations has been NACKed) */
|
2018-04-03 16:59:02 +02:00
|
|
|
I2C_MASTER_RC_NAK, /**< not acknowledge received */
|
2019-12-05 13:26:49 +01:00
|
|
|
I2C_MASTER_RC_BUS_ERROR, /**< an error on the I²C bus occurred */
|
2020-02-27 13:05:35 +01:00
|
|
|
I2C_MASTER_RC_TIMEOUT, /**< a timeout has occurred because an operation has not completed in the expected time */
|
|
|
|
I2C_MASTER_RC_OTHER, /** any other error (does not have to be I²C related) */
|
2018-04-03 16:59:02 +02:00
|
|
|
};
|
|
|
|
|
2019-12-05 13:26:49 +01:00
|
|
|
/** setup I²C peripheral
|
2018-04-03 16:59:02 +02:00
|
|
|
* @param[in] frequency frequency to use in kHz (1-400)
|
|
|
|
* @note Standard mode (Sm) is used for frequencies up to 100 kHz, and Fast mode (Fm) is used for frequencies up to 400 kHz
|
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
void i2c_master_setup(uint16_t frequency);
|
2019-12-05 13:26:49 +01:00
|
|
|
/** release I²C peripheral
|
2018-04-03 16:59:02 +02:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
void i2c_master_release(void);
|
2018-04-03 16:59:02 +02:00
|
|
|
/** reset I2C peripheral, fixing any locked state
|
|
|
|
* @warning the I2C peripheral needs to be re-setup
|
|
|
|
* @note to be used after failed start or stop, and bus error
|
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
void i2c_master_reset(void);
|
|
|
|
/** check if SDA and SCL signals are pulled up externally
|
|
|
|
* @return SDA and SCL signals are pulled up externally
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
bool i2c_master_check_signals(void);
|
2018-02-18 15:21:18 +01:00
|
|
|
/** send start condition
|
2018-04-03 16:59:02 +02:00
|
|
|
* @return I2C return code
|
2018-02-18 15:21:18 +01:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_start(void);
|
2019-12-05 13:26:49 +01:00
|
|
|
/** select I²C slave device
|
2018-02-18 15:21:18 +01:00
|
|
|
* @warning a start condition should be sent before this operation
|
2019-12-05 13:26:49 +01:00
|
|
|
* @param[in] slave I²C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I²C slave address is 10 bits wide
|
2018-02-18 15:21:18 +01:00
|
|
|
* @param[in] write this transaction will be followed by a read (false) or write (true) operation
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2018-02-18 15:21:18 +01:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_select_slave(uint16_t slave, bool address_10bit, bool write);
|
2019-12-05 13:26:49 +01:00
|
|
|
/** read data over I²C
|
2018-02-18 15:21:18 +01:00
|
|
|
* @param[out] data array to store bytes read
|
|
|
|
* @param[in] data_size number of bytes to read
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2020-02-11 12:21:35 +01:00
|
|
|
* @warning the slave device must be selected before this operation
|
|
|
|
* @note a stop condition will be sent at the end (I²C does not permit multiple reads, and this is necessary for 1-byte transfer)
|
2018-02-18 15:21:18 +01:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_read(uint8_t* data, size_t data_size);
|
2019-12-05 13:26:49 +01:00
|
|
|
/** write data over I²C
|
2018-02-18 15:21:18 +01:00
|
|
|
* @param[in] data array of byte to write to slave
|
|
|
|
* @param[in] data_size number of bytes to write
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2020-02-11 12:21:35 +01:00
|
|
|
* @warning the slave device must be selected before this operation
|
|
|
|
* @note no stop condition is sent at the end, allowing multiple writes
|
2018-02-18 15:21:18 +01:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_write(const uint8_t* data, size_t data_size);
|
2018-04-03 16:59:02 +02:00
|
|
|
/** sent stop condition
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2018-04-03 16:59:02 +02:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_stop(void);
|
2018-04-03 16:59:02 +02:00
|
|
|
/** read data from slave device
|
2019-12-05 13:26:49 +01:00
|
|
|
* @param[in] slave I²C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I²C slave address is 10 bits wide
|
2018-02-18 15:21:18 +01:00
|
|
|
* @param[out] data array to store bytes read
|
|
|
|
* @param[in] data_size number of bytes to read
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2020-02-11 12:21:35 +01:00
|
|
|
* @note start and stop conditions are included
|
2018-02-18 15:21:18 +01:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_slave_read(uint16_t slave, bool address_10bit, uint8_t* data, size_t data_size);
|
2018-04-03 16:59:02 +02:00
|
|
|
/** write data to slave device
|
2019-12-05 13:26:49 +01:00
|
|
|
* @param[in] slave I²C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I²C slave address is 10 bits wide
|
2018-02-18 15:21:18 +01:00
|
|
|
* @param[in] data array of byte to write to slave
|
|
|
|
* @param[in] data_size number of bytes to write
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2020-02-11 12:21:35 +01:00
|
|
|
* @note start and stop conditions are included
|
2018-02-18 15:21:18 +01:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_slave_write(uint16_t slave, bool address_10bit, const uint8_t* data, size_t data_size);
|
2019-12-05 13:26:49 +01:00
|
|
|
/** read data at specific address from an I²C memory slave
|
|
|
|
* @param[in] slave I²C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I²C slave address is 10 bits wide
|
2017-04-03 13:07:03 +02:00
|
|
|
* @param[in] address memory address of slave to read from
|
|
|
|
* @param[in] address_size address size in bytes
|
|
|
|
* @param[out] data array to store bytes read
|
|
|
|
* @param[in] data_size number of bytes to read
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2020-02-11 12:21:35 +01:00
|
|
|
* @note start and stop conditions are included
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_address_read(uint16_t slave, bool address_10bit, const uint8_t* address, size_t address_size, uint8_t* data, size_t data_size);
|
2019-12-05 13:26:49 +01:00
|
|
|
/** write data at specific address on an I²C memory slave
|
|
|
|
* @param[in] slave I²C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I²C slave address is 10 bits wide
|
2017-04-03 13:07:03 +02:00
|
|
|
* @param[in] address memory address of slave to write to
|
|
|
|
* @param[in] address_size address size in bytes
|
|
|
|
* @param[in] data array of byte to write to slave
|
|
|
|
* @param[in] data_size number of bytes to write
|
2019-12-05 13:26:49 +01:00
|
|
|
* @return I²C return code
|
2020-02-11 12:21:35 +01:00
|
|
|
* @note start and stop conditions are included
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
2020-12-12 14:08:05 +01:00
|
|
|
enum i2c_master_rc i2c_master_address_write(uint16_t slave, bool address_10bit, const uint8_t* address, size_t address_size, const uint8_t* data, size_t data_size);
|