|
px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
|
Driver to communicate with I2C slaves.
File(s):
The driver must be configured by supplying a project specific "px_i2c_cfg.h". "px_i2c_cfg_template.h" can be copied, renamed and modified to supply compile time options.
Data Structures | |
| struct | px_i2c_handle_t |
| Define I2C handle for a slave. More... | |
Macros | |
| #define | PX_I2C_CFG_PER_COUNT |
| Number of enabled peripherals. More... | |
| #define | PX_I2C_CFG_I2C1_EN 1 |
| Enable/disable support for I2C1 peripheral. More... | |
| #define | PX_I2C_CFG_I2C2_EN 0 |
| Enable/disable support for I2C2 peripheral. More... | |
| #define | PX_I2C_CFG_TIMINGR 0x20302e37 |
| I2C peripheral timing register value Calculated using STM32CubeMX graphical tool: More... | |
Enumerations | |
| enum | px_i2c_nr_t |
| Specify I2C peripheral. More... | |
Functions | |
| void | px_i2c_init (void) |
| Initialise I2C driver. More... | |
| bool | px_i2c_open (px_i2c_handle_t *handle, px_i2c_nr_t i2c_nr, uint8_t slave_adr) |
| Open I2C slave handle. More... | |
| bool | px_i2c_close (px_i2c_handle_t *handle) |
| Close specified handle. More... | |
| bool | px_i2c_wr (px_i2c_handle_t *handle, const void *data, size_t nr_of_bytes, uint8_t flags) |
| Perform an I2C write transaction with an I2C slave. More... | |
| bool | px_i2c_rd (px_i2c_handle_t *handle, void *data, size_t nr_of_bytes, uint8_t flags) |
| Perform an I2C read transaction with an I2C slave. More... | |
| void | px_i2c_change_slave_adr (px_i2c_handle_t *handle, uint8_t slave_adr) |
| Change I2C slave address. More... | |
I2C bit flags to demarcate the start and end of a transaction | |
| #define | PX_I2C_FLAG_START (1 << 0) |
| Begin I2C transaction with a START condition. More... | |
| #define | PX_I2C_FLAG_STOP (1 << 1) |
| Finish I2C transaction with a STOP condition. More... | |
| #define | PX_I2C_FLAG_END (1 << 2) |
| Signal END of current I2C transaction for REP_START to follow. More... | |
| #define | PX_I2C_FLAG_REP_START (1 << 3) |
| Begin I2C transaction with a REPEATED START condition. More... | |
| #define | PX_I2C_FLAG_START_AND_STOP (PX_I2C_FLAG_START + PX_I2C_FLAG_STOP) |
| Begin and finish an I2C transaction with a START and STOP condition. More... | |
| #define | PX_I2C_FLAG_START_AND_END (PX_I2C_FLAG_START + PX_I2C_FLAG_END) |
| Begin and finish an I2C transaction with a START and END condition. More... | |
| #define | PX_I2C_FLAG_REP_START_AND_STOP (PX_I2C_FLAG_REP_START + PX_I2C_FLAG_STOP) |
| Begin and finish an I2C transaction with a REP START and STOP condition. More... | |
| #define | PX_I2C_FLAG_REP_START_AND_END (PX_I2C_FLAG_REP_START + PX_I2C_FLAG_END) |
| Begin and finish an I2C transaction with a REP START and END condition. More... | |
| struct px_i2c_handle_t |
| #define PX_I2C_CFG_PER_COUNT |
Number of enabled peripherals.
| #define PX_I2C_FLAG_START (1 << 0) |
| #define PX_I2C_FLAG_STOP (1 << 1) |
| #define PX_I2C_FLAG_END (1 << 2) |
| #define PX_I2C_FLAG_REP_START (1 << 3) |
| #define PX_I2C_FLAG_START_AND_STOP (PX_I2C_FLAG_START + PX_I2C_FLAG_STOP) |
| #define PX_I2C_FLAG_START_AND_END (PX_I2C_FLAG_START + PX_I2C_FLAG_END) |
| #define PX_I2C_FLAG_REP_START_AND_STOP (PX_I2C_FLAG_REP_START + PX_I2C_FLAG_STOP) |
| #define PX_I2C_FLAG_REP_START_AND_END (PX_I2C_FLAG_REP_START + PX_I2C_FLAG_END) |
| #define PX_I2C_CFG_I2C1_EN 1 |
Enable/disable support for I2C1 peripheral.
Definition at line 36 of file px_i2c_cfg_template.h.
| #define PX_I2C_CFG_I2C2_EN 0 |
Enable/disable support for I2C2 peripheral.
Definition at line 39 of file px_i2c_cfg_template.h.
| #define PX_I2C_CFG_TIMINGR 0x20302e37 |
I2C peripheral timing register value Calculated using STM32CubeMX graphical tool:
Definition at line 49 of file px_i2c_cfg_template.h.
| enum px_i2c_nr_t |
| bool px_i2c_open | ( | px_i2c_handle_t * | handle, |
| px_i2c_nr_t | i2c_nr, | ||
| uint8_t | slave_adr | ||
| ) |
| bool px_i2c_close | ( | px_i2c_handle_t * | handle | ) |
| bool px_i2c_wr | ( | px_i2c_handle_t * | handle, |
| const void * | data, | ||
| size_t | nr_of_bytes, | ||
| uint8_t | flags | ||
| ) |
Perform an I2C write transaction with an I2C slave.
With the use of flags this function allows concatenation of one or more writes to an I2C slave to form one big write transaction. It is also possible to generate a REPEATED START (RESTART) condition by not generating an intervening STOP condition.
If the PX_I2C_FLAG_START flag is specified, a START condition is generated and the 7-bit slave address and WRITE bit is sent on the bus (SLA+W). If a NAK is received, a STOP condition is generated to return the I2C bus to idle state.
The specified number of bytes is sent with the slave supposed to ACK each byte. If a NAK is received, a STOP condition is generated to return the I2C bus to idle state.
If the PX_I2C_FLAG_STOP flag is specified, a STOP condition is generated to complete the transaction and return the I2C bus to the idle state.
More than one I2C transaction may be concatenated without releasing the I2C bus by using a REPEATED START condition. This is acheived by signalling the end of the current transaction with the PX_I2C_FLAG_END flag and starting the next transaction with a PX_I2C_FLAG_REP_START flag.
| handle | Pointer to handle data structure |
| data | Buffer containing data to write |
| nr_of_bytes | Number of bytes to write to the slave |
| flags | Bit combination of PX_I2C_FLAG_START, PX_I2C_FLAG_STOP, PX_I2C_FLAG_REP_START, PX_I2C_FLAG_END or nothing (0) |
| true | Slave ACK'd all bytes written to it |
| false | Received a NAK or bus error |
| bool px_i2c_rd | ( | px_i2c_handle_t * | handle, |
| void * | data, | ||
| size_t | nr_of_bytes, | ||
| uint8_t | flags | ||
| ) |
Perform an I2C read transaction with an I2C slave.
With the use of flags this function allows concatenation of one or more reads from an I2C slave to form one big read transaction. It is also possible to generate a REPEATED START (RESTART) condition by not generating an intervening STOP condition.
If the PX_I2C_FLAG_START flag is specified, A START condition is generated and the 7-bit slave address and READ bit is sent on the bus (SLA+R). If a NAK is received, a STOP condition is generated to return the I2C bus to idle state.
The specified number of bytes is read from the slave and each byte is ACKed, except the last one. If the PX_I2C_FLAG_STOP or PX_I2C_FLAG_END flag is specified, the last byte is NAKed to indicate to the I2C slave that this is the end of the read transaction, otherwise it is also ACKed to allow the read transaction to continue.
If the PX_I2C_FLAG_STOP flag is specified, a STOP condition is generated to complete the transaction and return the I2C bus to the idle state.
More than one I2C transaction may be concatenated without releasing the I2C bus by using a REPEATED START condition. This is acheived by signalling the end of the current transaction with the PX_I2C_FLAG_END flag and starting the next transaction with a PX_I2C_FLAG_REP_START flag.
| handle | Pointer to handle data structure |
| data | Pointer to a buffer where the received data must be stored |
| nr_of_bytes | Number of bytes to read from slave |
| flags | Bit combination of PX_I2C_FLAG_START, PX_I2C_FLAG_STOP, PX_I2C_FLAG_REP_START, PX_I2C_FLAG_END or nothing (0) |
| true | Byte(s) succesfully read |
| false | Received NAK or bus error |
| void px_i2c_change_slave_adr | ( | px_i2c_handle_t * | handle, |
| uint8_t | slave_adr | ||
| ) |