ChibiOS/HAL
6.1.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.
Macros | |
#define | I2C_USE_MUTUAL_EXCLUSION TRUE |
Enables the mutual exclusion APIs on the I2C bus. More... | |
#define | _i2c_wakeup_isr(i2cp) |
Wakes up the waiting thread notifying no errors. More... | |
#define | _i2c_wakeup_error_isr(i2cp) |
Wakes up the waiting thread notifying errors. More... | |
#define | i2cMasterTransmit(i2cp, addr, txbuf, txbytes, rxbuf, rxbytes) |
Wrap i2cMasterTransmitTimeout function with TIME_INFINITE timeout. More... | |
#define | i2cMasterReceive(i2cp, addr, rxbuf, rxbytes) (i2cMasterReceiveTimeout(i2cp, addr, rxbuf, rxbytes, TIME_INFINITE)) |
Wrap i2cMasterReceiveTimeout function with TIME_INFINITE timeout. More... | |
#define | i2c_lld_get_errors(i2cp) ((i2cp)->errors) |
Get errors from I2C driver. More... | |
I2C bus error conditions | |
#define | I2C_NO_ERROR 0x00 |
No error. More... | |
#define | I2C_BUS_ERROR 0x01 |
Bus Error. More... | |
#define | I2C_ARBITRATION_LOST 0x02 |
Arbitration Lost. More... | |
#define | I2C_ACK_FAILURE 0x04 |
Acknowledge Failure. More... | |
#define | I2C_OVERRUN 0x08 |
Overrun/Underrun. More... | |
#define | I2C_PEC_ERROR 0x10 |
PEC Error in reception. More... | |
#define | I2C_TIMEOUT 0x20 |
Hardware timeout. More... | |
#define | I2C_SMB_ALERT 0x40 |
SMBus Alert. More... | |
PLATFORM configuration options | |
#define | PLATFORM_I2C_USE_I2C1 FALSE |
I2C1 driver enable switch. More... | |
Typedefs | |
typedef uint16_t | i2caddr_t |
Type representing an I2C address. More... | |
typedef uint32_t | i2cflags_t |
Type of I2C Driver condition flags. More... | |
typedef struct I2CDriver | I2CDriver |
Type of a structure representing an I2C driver. More... | |
Data Structures | |
struct | I2CConfig |
Type of I2C driver configuration structure. More... | |
struct | I2CDriver |
Structure representing an I2C driver. More... | |
Functions | |
void | i2cInit (void) |
I2C Driver initialization. More... | |
void | i2cObjectInit (I2CDriver *i2cp) |
Initializes the standard part of a I2CDriver structure. More... | |
void | i2cStart (I2CDriver *i2cp, const I2CConfig *config) |
Configures and activates the I2C peripheral. More... | |
void | i2cStop (I2CDriver *i2cp) |
Deactivates the I2C peripheral. More... | |
i2cflags_t | i2cGetErrors (I2CDriver *i2cp) |
Returns the errors mask associated to the previous operation. More... | |
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. More... | |
msg_t | i2cMasterReceiveTimeout (I2CDriver *i2cp, i2caddr_t addr, uint8_t *rxbuf, size_t rxbytes, sysinterval_t timeout) |
Receives data from the I2C bus. More... | |
void | i2cAcquireBus (I2CDriver *i2cp) |
Gains exclusive access to the I2C bus. More... | |
void | i2cReleaseBus (I2CDriver *i2cp) |
Releases exclusive access to the I2C bus. More... | |
void | i2c_lld_init (void) |
Low level I2C driver initialization. More... | |
void | i2c_lld_start (I2CDriver *i2cp) |
Configures and activates the I2C peripheral. More... | |
void | i2c_lld_stop (I2CDriver *i2cp) |
Deactivates the I2C peripheral. More... | |
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. More... | |
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. More... | |
Enumerations |
Variables | |
I2CDriver | I2CD1 |
I2C1 driver identifier. More... | |
#define I2C_NO_ERROR 0x00 |
No error.
Definition at line 43 of file hal_i2c.h.
Referenced by i2cMasterReceiveTimeout(), and i2cMasterTransmitTimeout().
#define I2C_USE_MUTUAL_EXCLUSION TRUE |
#define _i2c_wakeup_isr | ( | i2cp | ) |
Wakes up the waiting thread notifying no errors.
[in] | i2cp | pointer to the I2CDriver object |
#define _i2c_wakeup_error_isr | ( | i2cp | ) |
Wakes up the waiting thread notifying errors.
[in] | i2cp | pointer to the I2CDriver object |
#define i2cMasterTransmit | ( | i2cp, | |
addr, | |||
txbuf, | |||
txbytes, | |||
rxbuf, | |||
rxbytes | |||
) |
Wrap i2cMasterTransmitTimeout function with TIME_INFINITE timeout.
#define i2cMasterReceive | ( | i2cp, | |
addr, | |||
rxbuf, | |||
rxbytes | |||
) | (i2cMasterReceiveTimeout(i2cp, addr, rxbuf, rxbytes, TIME_INFINITE)) |
#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 | ) | ((i2cp)->errors) |
Get errors from I2C driver.
[in] | i2cp | pointer to the I2CDriver object |
Definition at line 121 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.
Type of a structure representing an I2C driver.
Definition at line 83 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 I2CDriver::config, I2C_STOP, osalMutexObjectInit(), and I2CDriver::state.
Referenced by i2c_lld_init().
Configures and activates the I2C peripheral.
Definition at line 93 of file hal_i2c.c.
References I2CDriver::config, i2c_lld_start(), I2C_READY, I2C_STOP, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and I2CDriver::state.
void i2cStop | ( | I2CDriver * | i2cp | ) |
Deactivates the I2C peripheral.
[in] | i2cp | pointer to the I2CDriver object |
Definition at line 113 of file hal_i2c.c.
References I2CDriver::config, i2c_lld_stop(), I2C_READY, I2C_STOP, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and I2CDriver::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 137 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 170 of file hal_i2c.c.
References I2CDriver::errors, I2C_ACTIVE_TX, i2c_lld_master_transmit_timeout(), I2C_NO_ERROR, I2C_READY, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and I2CDriver::state.
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 221 of file hal_i2c.c.
References I2CDriver::errors, I2C_ACTIVE_RX, i2c_lld_master_receive_timeout(), I2C_NO_ERROR, I2C_READY, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and I2CDriver::state.
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 261 of file hal_i2c.c.
References 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 277 of file hal_i2c.c.
References osalDbgCheck, and osalMutexUnlock().
void i2c_lld_init | ( | void | ) |
Low level I2C driver initialization.
Definition at line 65 of file hal_i2c_lld.c.
References 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, and I2CDriver::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, and I2CDriver::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 133 of file hal_i2c_lld.c.
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 169 of file hal_i2c_lld.c.
Referenced by i2cMasterTransmitTimeout().
I2CDriver I2CD1 |
I2C1 driver identifier.
Definition at line 41 of file hal_i2c_lld.c.