ChibiOS/HAL 9.0.0
|
Generic I2C Driver. More...
Generic I2C Driver.
This module implements a generic I2C (Inter-Integrated Circuit) driver.
HAL_USE_I2C
option must be enabled in halconf.h
.The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).
The driver is not thread safe for performance reasons, if you need to access the I2C bus from multiple threads then use the i2cAcquireBus()
and i2cReleaseBus()
APIs in order to gain exclusive access.
I2C bus error conditions | |
#define | I2C_NO_ERROR 0x00 |
No error. | |
#define | I2C_BUS_ERROR 0x01 |
Bus Error. | |
#define | I2C_ARBITRATION_LOST 0x02 |
Arbitration Lost. | |
#define | I2C_ACK_FAILURE 0x04 |
Acknowledge Failure. | |
#define | I2C_OVERRUN 0x08 |
Overrun/Underrun. | |
#define | I2C_PEC_ERROR 0x10 |
PEC Error in reception. | |
#define | I2C_TIMEOUT 0x20 |
Hardware timeout. | |
#define | I2C_SMB_ALERT 0x40 |
SMBus Alert. |
Macro Functions | |
#define | _i2c_wakeup_isr(i2cp) |
Wakes up the waiting thread notifying no errors. | |
#define | _i2c_wakeup_error_isr(i2cp) |
Wakes up the waiting thread notifying errors. | |
#define | i2cMasterTransmit(i2cp, addr, txbuf, txbytes, rxbuf, rxbytes) |
Wrap i2cMasterTransmitTimeout function with TIME_INFINITE timeout. | |
#define | i2cMasterReceive(i2cp, addr, rxbuf, rxbytes) |
Wrap i2cMasterReceiveTimeout function with TIME_INFINITE timeout. | |
#define | i2cSlaveIsAnswerRequired(i2cp) |
Answer required. |
PLATFORM configuration options | |
#define | PLATFORM_I2C_USE_I2C1 FALSE |
I2C1 driver enable switch. |
Data Structures | |
struct | hal_i2c_config |
I2C driver configuration structure. More... | |
struct | hal_i2c_driver |
Structure representing an I2C driver. More... |
Macros | |
#define | I2C_USE_MUTUAL_EXCLUSION TRUE |
Enables the mutual exclusion APIs on the I2C bus. | |
#define | I2C_ENABLE_SLAVE_MODE FALSE |
Slave mode API enable switch. | |
#define | I2C_SUPPORTS_SLAVE_MODE FALSE |
#define | i2c_lld_get_errors(i2cp) |
Get errors from I2C driver. |
Typedefs | |
typedef uint16_t | i2caddr_t |
Type representing an I2C address. | |
typedef uint32_t | i2cflags_t |
Type of I2C Driver condition flags. | |
typedef struct hal_i2c_config | I2CConfig |
Type of a structure representing an I2C configuration. | |
typedef struct hal_i2c_driver | I2CDriver |
Type of a structure representing an I2C driver. |
Enumerations | |
enum | i2cstate_t { I2C_UNINIT = 0 , I2C_STOP = 1 , I2C_READY = 2 , I2C_ACTIVE_TX = 3 , I2C_ACTIVE_RX = 4 , I2C_LOCKED = 5 } |
Driver state machine possible states. More... |
Functions | |
void | i2cInit (void) |
I2C Driver initialization. | |
void | i2cObjectInit (I2CDriver *i2cp) |
Initializes the standard part of a I2CDriver structure. | |
msg_t | i2cStart (I2CDriver *i2cp, const I2CConfig *config) |
Configures and activates the I2C peripheral. | |
void | i2cStop (I2CDriver *i2cp) |
Deactivates the I2C peripheral. | |
i2cflags_t | i2cGetErrors (I2CDriver *i2cp) |
Returns the errors mask associated to the previous operation. | |
msg_t | i2cMasterTransmitTimeout (I2CDriver *i2cp, i2caddr_t addr, const uint8_t *txbuf, size_t txbytes, uint8_t *rxbuf, size_t rxbytes, sysinterval_t timeout) |
Sends data via the I2C bus. | |
msg_t | i2cMasterReceiveTimeout (I2CDriver *i2cp, i2caddr_t addr, uint8_t *rxbuf, size_t rxbytes, sysinterval_t timeout) |
Receives data from the I2C bus. | |
void | i2cAcquireBus (I2CDriver *i2cp) |
Gains exclusive access to the I2C bus. | |
void | i2cReleaseBus (I2CDriver *i2cp) |
Releases exclusive access to the I2C bus. | |
msg_t | i2cSlaveMatchAddress (I2CDriver *i2cp, i2caddr_t addr) |
Listen I2C bus for address match. | |
msg_t | i2cSlaveReceiveTimeout (I2CDriver *i2cp, uint8_t *rxbuf, size_t rxbytes, sysinterval_t timeout) |
Receive data via the I2C bus as slave and call handler. | |
msg_t | i2cSlaveTransmitTimeout (I2CDriver *i2cp, const uint8_t *txbuf, size_t txbytes, sysinterval_t timeout) |
Transmits data via the I2C bus as slave. | |
void | i2c_lld_init (void) |
Low level I2C driver initialization. | |
void | i2c_lld_start (I2CDriver *i2cp) |
Configures and activates the I2C peripheral. | |
void | i2c_lld_stop (I2CDriver *i2cp) |
Deactivates the I2C peripheral. | |
msg_t | i2c_lld_master_receive_timeout (I2CDriver *i2cp, i2caddr_t addr, uint8_t *rxbuf, size_t rxbytes, sysinterval_t timeout) |
Receives data via the I2C bus as master. | |
msg_t | i2c_lld_master_transmit_timeout (I2CDriver *i2cp, i2caddr_t addr, const uint8_t *txbuf, size_t txbytes, uint8_t *rxbuf, size_t rxbytes, sysinterval_t timeout) |
Transmits data via the I2C bus as master. |
Variables | |
I2CDriver | I2CD1 |
I2C1 driver identifier. |
#define I2C_NO_ERROR 0x00 |
No error.
Definition at line 43 of file hal_i2c.h.
Referenced by i2cMasterReceiveTimeout(), i2cMasterTransmitTimeout(), i2cSlaveReceiveTimeout(), and i2cSlaveTransmitTimeout().
#define I2C_USE_MUTUAL_EXCLUSION TRUE |
#define I2C_ENABLE_SLAVE_MODE FALSE |
#define _i2c_wakeup_isr | ( | i2cp | ) |
#define _i2c_wakeup_error_isr | ( | i2cp | ) |
#define i2cMasterTransmit | ( | i2cp, | |
addr, | |||
txbuf, | |||
txbytes, | |||
rxbuf, | |||
rxbytes ) |
Wrap i2cMasterTransmitTimeout function with TIME_INFINITE timeout.
#define i2cMasterReceive | ( | i2cp, | |
addr, | |||
rxbuf, | |||
rxbytes ) |
Wrap i2cMasterReceiveTimeout function with TIME_INFINITE timeout.
#define i2cSlaveIsAnswerRequired | ( | i2cp | ) |
Answer required.
[in] | i2cp | pointer to the I2CDriver object |
false | if the slave must not answer. |
true | if the slave must answer. |
#define PLATFORM_I2C_USE_I2C1 FALSE |
I2C1 driver enable switch.
If set to TRUE
the support for I2C1 is included.
FALSE
. Definition at line 48 of file hal_i2c_lld.h.
#define i2c_lld_get_errors | ( | i2cp | ) |
Get errors from I2C driver.
[in] | i2cp | pointer to the I2CDriver object |
Definition at line 126 of file hal_i2c_lld.h.
Referenced by i2cGetErrors().
typedef uint16_t i2caddr_t |
Type representing an I2C address.
Definition at line 63 of file hal_i2c_lld.h.
typedef uint32_t i2cflags_t |
Type of I2C Driver condition flags.
Definition at line 68 of file hal_i2c_lld.h.
typedef struct hal_i2c_config I2CConfig |
Type of a structure representing an I2C configuration.
Definition at line 83 of file hal_i2c_lld.h.
typedef struct hal_i2c_driver I2CDriver |
Type of a structure representing an I2C driver.
Definition at line 88 of file hal_i2c_lld.h.
enum i2cstate_t |
void i2cInit | ( | void | ) |
I2C Driver initialization.
halInit()
, there is no need to explicitly initialize the driver.Definition at line 59 of file hal_i2c.c.
References i2c_lld_init().
Referenced by halInit().
void i2cObjectInit | ( | I2CDriver * | i2cp | ) |
Initializes the standard part of a I2CDriver
structure.
[out] | i2cp | pointer to the I2CDriver object |
Definition at line 71 of file hal_i2c.c.
References hal_i2c_driver::config, I2C_STOP, hal_i2c_driver::mutex, osalMutexObjectInit(), and hal_i2c_driver::state.
Referenced by i2c_lld_init().
Configures and activates the I2C peripheral.
Definition at line 94 of file hal_i2c.c.
References hal_i2c_driver::config, HAL_RET_SUCCESS, i2c_lld_start(), I2C_LOCKED, I2C_READY, I2C_STOP, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and hal_i2c_driver::state.
void i2cStop | ( | I2CDriver * | i2cp | ) |
Deactivates the I2C peripheral.
[in] | i2cp | pointer to the I2CDriver object |
Definition at line 131 of file hal_i2c.c.
References hal_i2c_driver::config, i2c_lld_stop(), I2C_LOCKED, I2C_READY, I2C_STOP, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and hal_i2c_driver::state.
i2cflags_t i2cGetErrors | ( | I2CDriver * | i2cp | ) |
Returns the errors mask associated to the previous operation.
[in] | i2cp | pointer to the I2CDriver object |
Definition at line 155 of file hal_i2c.c.
References i2c_lld_get_errors, and osalDbgCheck.
msg_t i2cMasterTransmitTimeout | ( | I2CDriver * | i2cp, |
i2caddr_t | addr, | ||
const uint8_t * | txbuf, | ||
size_t | txbytes, | ||
uint8_t * | rxbuf, | ||
size_t | rxbytes, | ||
sysinterval_t | timeout ) |
Sends data via the I2C bus.
Function designed to realize "read-through-write" transfer paradigm. If you want transmit data without any further read, than set rxbytes field to 0.
[in] | i2cp | pointer to the I2CDriver object |
[in] | addr | slave device address (7 bits) without R/W bit |
[in] | txbuf | pointer to transmit buffer |
[in] | txbytes | number of bytes to be transmitted |
[out] | rxbuf | pointer to receive buffer |
[in] | rxbytes | number of bytes to be received, set it to 0 if you want transmit only |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the function succeeded. |
MSG_RESET | if one or more I2C errors occurred, the errors can be retrieved using i2cGetErrors() . |
MSG_TIMEOUT | if a timeout occurred before operation end. |
Definition at line 187 of file hal_i2c.c.
References hal_i2c_driver::errors, I2C_ACTIVE_TX, i2c_lld_master_transmit_timeout(), I2C_LOCKED, I2C_NO_ERROR, I2C_READY, MSG_TIMEOUT, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), hal_i2c_driver::state, and TIME_IMMEDIATE.
msg_t i2cMasterReceiveTimeout | ( | I2CDriver * | i2cp, |
i2caddr_t | addr, | ||
uint8_t * | rxbuf, | ||
size_t | rxbytes, | ||
sysinterval_t | timeout ) |
Receives data from the I2C bus.
[in] | i2cp | pointer to the I2CDriver object |
[in] | addr | slave device address (7 bits) without R/W bit |
[out] | rxbuf | pointer to receive buffer |
[in] | rxbytes | number of bytes to be received |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the function succeeded. |
MSG_RESET | if one or more I2C errors occurred, the errors can be retrieved using i2cGetErrors() . |
MSG_TIMEOUT | if a timeout occurred before operation end. |
Definition at line 237 of file hal_i2c.c.
References hal_i2c_driver::errors, I2C_ACTIVE_RX, i2c_lld_master_receive_timeout(), I2C_LOCKED, I2C_NO_ERROR, I2C_READY, MSG_TIMEOUT, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), hal_i2c_driver::state, and TIME_IMMEDIATE.
void i2cAcquireBus | ( | I2CDriver * | i2cp | ) |
Gains exclusive access to the I2C bus.
This function tries to gain ownership to the I2C bus, if the bus is already being used then the invoking thread is queued.
I2C_USE_MUTUAL_EXCLUSION
must be enabled.[in] | i2cp | pointer to the I2CDriver object |
Definition at line 277 of file hal_i2c.c.
References hal_i2c_driver::mutex, osalDbgCheck, and osalMutexLock().
void i2cReleaseBus | ( | I2CDriver * | i2cp | ) |
Releases exclusive access to the I2C bus.
I2C_USE_MUTUAL_EXCLUSION
must be enabled.[in] | i2cp | pointer to the I2CDriver object |
Definition at line 293 of file hal_i2c.c.
References hal_i2c_driver::mutex, osalDbgCheck, and osalMutexUnlock().
Listen I2C bus for address match.
Use 7 bit address (10 bit,dual and general call address dosn't implement yet) .
[in] | i2cp | pointer to the I2CDriver object |
[in] | addr | slave device address |
MSG_OK | if the function succeeded. |
MSG_RESET | if one or more I2C errors occurred, the errors can be retrieved using i2cGetErrors() . |
Definition at line 315 of file hal_i2c.c.
References osalDbgCheck.
msg_t i2cSlaveReceiveTimeout | ( | I2CDriver * | i2cp, |
uint8_t * | rxbuf, | ||
size_t | rxbytes, | ||
sysinterval_t | timeout ) |
Receive data via the I2C bus as slave and call handler.
[in] | i2cp | pointer to the I2CDriver object |
[out] | rxbuf | pointer to the receive buffer |
[in] | rxbytes | size of receive buffer |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the function succeeded. |
MSG_RESET | if one or more I2C errors occurred, the errors can be retrieved using i2cGetErrors() . |
MSG_TIMEOUT | if a timeout occurred before operation end. After a timeout the driver must be stopped and restarted because the bus is in an uncertain state. |
Definition at line 347 of file hal_i2c.c.
References hal_i2c_driver::errors, I2C_ACTIVE_RX, I2C_LOCKED, I2C_NO_ERROR, I2C_READY, MSG_TIMEOUT, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), hal_i2c_driver::state, and TIME_IMMEDIATE.
msg_t i2cSlaveTransmitTimeout | ( | I2CDriver * | i2cp, |
const uint8_t * | txbuf, | ||
size_t | txbytes, | ||
sysinterval_t | timeout ) |
Transmits data via the I2C bus as slave.
Call this function when Master request data (in request handler)
[in] | i2cp | pointer to the I2CDriver object |
[in] | txbuf | pointer to the transmit buffer |
[in] | txbytes | number of bytes to be transmitted |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the function succeeded. |
MSG_RESET | if one or more I2C errors occurred, the errors can be retrieved using i2cGetErrors() . |
MSG_TIMEOUT | if a timeout occurred before operation end. After a timeout the driver must be stopped and restarted because the bus is in an uncertain state. |
Definition at line 398 of file hal_i2c.c.
References hal_i2c_driver::errors, I2C_ACTIVE_TX, I2C_LOCKED, I2C_NO_ERROR, I2C_READY, MSG_TIMEOUT, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), hal_i2c_driver::state, and TIME_IMMEDIATE.
void i2c_lld_init | ( | void | ) |
Low level I2C driver initialization.
Definition at line 65 of file hal_i2c_lld.c.
References I2CD1, and i2cObjectInit().
Referenced by i2cInit().
void i2c_lld_start | ( | I2CDriver * | i2cp | ) |
Configures and activates the I2C peripheral.
[in] | i2cp | pointer to the I2CDriver object |
Definition at line 79 of file hal_i2c_lld.c.
References I2C_STOP, I2CD1, and hal_i2c_driver::state.
Referenced by i2cStart().
void i2c_lld_stop | ( | I2CDriver * | i2cp | ) |
Deactivates the I2C peripheral.
[in] | i2cp | pointer to the I2CDriver object |
Definition at line 99 of file hal_i2c_lld.c.
References I2C_STOP, I2CD1, and hal_i2c_driver::state.
Referenced by i2cStop().
msg_t i2c_lld_master_receive_timeout | ( | I2CDriver * | i2cp, |
i2caddr_t | addr, | ||
uint8_t * | rxbuf, | ||
size_t | rxbytes, | ||
sysinterval_t | timeout ) |
Receives data via the I2C bus as master.
[in] | i2cp | pointer to the I2CDriver object |
[in] | addr | slave device address |
[out] | rxbuf | pointer to the receive buffer |
[in] | rxbytes | number of bytes to be received |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the function succeeded. |
MSG_RESET | if one or more I2C errors occurred, the errors can be retrieved using i2cGetErrors() . |
MSG_TIMEOUT | if a timeout occurred before operation end. After a timeout the driver must be stopped and restarted because the bus is in an uncertain state. |
Definition at line 132 of file hal_i2c_lld.c.
References MSG_OK.
Referenced by i2cMasterReceiveTimeout().
msg_t i2c_lld_master_transmit_timeout | ( | I2CDriver * | i2cp, |
i2caddr_t | addr, | ||
const uint8_t * | txbuf, | ||
size_t | txbytes, | ||
uint8_t * | rxbuf, | ||
size_t | rxbytes, | ||
sysinterval_t | timeout ) |
Transmits data via the I2C bus as master.
[in] | i2cp | pointer to the I2CDriver object |
[in] | addr | slave device address |
[in] | txbuf | pointer to the transmit buffer |
[in] | txbytes | number of bytes to be transmitted |
[out] | rxbuf | pointer to the receive buffer |
[in] | rxbytes | number of bytes to be received |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the function succeeded. |
MSG_RESET | if one or more I2C errors occurred, the errors can be retrieved using i2cGetErrors() . |
MSG_TIMEOUT | if a timeout occurred before operation end. After a timeout the driver must be stopped and restarted because the bus is in an uncertain state. |
Definition at line 167 of file hal_i2c_lld.c.
References MSG_OK.
Referenced by i2cMasterTransmitTimeout().
I2CDriver I2CD1 |
I2C1 driver identifier.
Definition at line 41 of file hal_i2c_lld.c.
Referenced by i2c_lld_init(), i2c_lld_start(), and i2c_lld_stop().