2017-04-03 13:07:03 +02:00
|
|
|
/* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/** library to communicate using I2C as master (API)
|
|
|
|
* @file i2c_master.h
|
|
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
2018-02-06 11:53:50 +01:00
|
|
|
* @date 2017-2018
|
2018-03-21 10:10:19 +01:00
|
|
|
* @note peripherals used: I2C
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2018-03-21 14:13:55 +01:00
|
|
|
/** I2C return codes */
|
|
|
|
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 */
|
|
|
|
I2C_MASTER_RC_NOT_READY, /**< slave is not read (previous operations has been nacked) */
|
|
|
|
I2C_MASTER_RC_NAK, /**< not acknowledge received */
|
2018-04-03 15:41:31 +02:00
|
|
|
I2C_MASTER_RC_BUS_ERROR, /**< an error on the I2C bus occurred */
|
2018-03-21 14:13:55 +01:00
|
|
|
};
|
|
|
|
|
2017-04-03 13:07:03 +02:00
|
|
|
/** setup I2C peripheral
|
2018-03-20 09:37:31 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-03-20 14:25:51 +01: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
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
2018-03-20 14:25:51 +01:00
|
|
|
void i2c_master_setup(uint32_t i2c, uint16_t frequency);
|
2018-03-20 09:37:31 +01:00
|
|
|
/** release I2C peripheral
|
|
|
|
* @param[in] i2c I2C base address
|
|
|
|
*/
|
|
|
|
void i2c_master_release(uint32_t i2c);
|
2018-03-21 10:10:19 +01:00
|
|
|
/** reset I2C peripheral, fixing any locked state
|
2018-04-03 15:41:31 +02:00
|
|
|
* @warning the I2C peripheral needs to be re-setup
|
|
|
|
* @note to be used after failed start or stop, and bus error
|
2018-03-21 10:10:19 +01:00
|
|
|
* @param[in] i2c I2C base address
|
|
|
|
*/
|
|
|
|
void i2c_master_reset(uint32_t i2c);
|
2018-03-20 09:37:31 +01:00
|
|
|
/** check if SDA and SCL signals are high
|
|
|
|
* @param[in] i2c I2C base address
|
|
|
|
* @return SDA and SCL signals are high
|
|
|
|
*/
|
|
|
|
bool i2c_master_check_signals(uint32_t i2c);
|
2018-02-06 11:53:50 +01:00
|
|
|
/** send start condition
|
2018-03-20 09:37:31 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2018-02-06 11:53:50 +01:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_start(uint32_t i2c);
|
2018-03-21 23:20:05 +01:00
|
|
|
/** select I2C slave device
|
2018-02-06 11:53:50 +01:00
|
|
|
* @warning a start condition should be sent before this operation
|
2018-03-20 09:37:31 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-03-21 10:10:19 +01:00
|
|
|
* @param[in] slave I2C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I2C slave address is 10 bits wide
|
2018-02-06 11:53:50 +01:00
|
|
|
* @param[in] write this transaction will be followed by a read (false) or write (true) operation
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2018-02-06 11:53:50 +01:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_select_slave(uint32_t i2c, uint16_t slave, bool address_10bit, bool write);
|
2018-03-21 23:20:05 +01:00
|
|
|
/** read data over I2C
|
2018-02-06 11:53:50 +01:00
|
|
|
* @warning the slave device must be selected before this operation
|
2018-03-21 23:20:05 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-02-06 11:53:50 +01:00
|
|
|
* @param[out] data array to store bytes read
|
|
|
|
* @param[in] data_size number of bytes to read
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2018-02-06 11:53:50 +01:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_read(uint32_t i2c, uint8_t* data, size_t data_size);
|
2018-03-21 23:20:05 +01:00
|
|
|
/** write data over I2C
|
2018-02-06 11:53:50 +01:00
|
|
|
* @warning the slave device must be selected before this operation
|
2018-03-21 23:20:05 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-02-06 11:53:50 +01:00
|
|
|
* @param[in] data array of byte to write to slave
|
|
|
|
* @param[in] data_size number of bytes to write
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2018-02-06 11:53:50 +01:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_write(uint32_t i2c, const uint8_t* data, size_t data_size);
|
2018-03-20 09:37:31 +01:00
|
|
|
/** sent stop condition
|
|
|
|
* @param[in] i2c I2C base address
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2018-03-20 09:37:31 +01:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_stop(uint32_t i2c);
|
2018-03-21 10:10:19 +01:00
|
|
|
/** read data from slave device
|
|
|
|
* @warning the slave device must be selected before this operation
|
2018-03-20 09:37:31 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-03-21 10:10:19 +01:00
|
|
|
* @param[in] slave I2C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I2C slave address is 10 bits wide
|
2018-02-06 11:53:50 +01:00
|
|
|
* @param[out] data array to store bytes read
|
|
|
|
* @param[in] data_size number of bytes to read
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2018-02-06 11:53:50 +01:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_slave_read(uint32_t i2c, uint16_t slave, bool address_10bit, uint8_t* data, size_t data_size);
|
2018-03-21 10:10:19 +01:00
|
|
|
/** write data to slave device
|
|
|
|
* @warning the slave device must be selected before this operation
|
2018-03-20 09:37:31 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-03-21 10:10:19 +01:00
|
|
|
* @param[in] slave I2C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I2C slave address is 10 bits wide
|
2018-02-06 11:53:50 +01:00
|
|
|
* @param[in] data array of byte to write to slave
|
|
|
|
* @param[in] data_size number of bytes to write
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2018-02-06 11:53:50 +01:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_slave_write(uint32_t i2c, uint16_t slave, bool address_10bit, const uint8_t* data, size_t data_size);
|
2018-02-06 11:53:50 +01:00
|
|
|
/** read data at specific address from an I2C memory slave
|
2018-03-20 09:37:31 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-03-21 10:10:19 +01:00
|
|
|
* @param[in] slave I2C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I2C 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
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_address_read(uint32_t i2c, uint16_t slave, bool address_10bit, const uint8_t* address, size_t address_size, uint8_t* data, size_t data_size);
|
2018-02-06 11:53:50 +01:00
|
|
|
/** write data at specific address on an I2C memory slave
|
2018-03-20 09:37:31 +01:00
|
|
|
* @param[in] i2c I2C base address
|
2018-03-21 10:10:19 +01:00
|
|
|
* @param[in] slave I2C address of slave device to select
|
|
|
|
* @param[in] address_10bit if the I2C 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
|
2018-03-21 14:13:55 +01:00
|
|
|
* @return I2C return code
|
2017-04-03 13:07:03 +02:00
|
|
|
*/
|
2018-03-21 14:13:55 +01:00
|
|
|
enum i2c_master_rc i2c_master_address_write(uint32_t i2c, uint16_t slave, bool address_10bit, const uint8_t* address, size_t address_size, const uint8_t* data, size_t data_size);
|