HAL I2C driver implementation for STM32F4 series. More...
#include "core/cortex-m4/i2c.h"#include "core/cortex-m4/clock.h"#include "core/cortex-m4/i2c_reg.h"#include "core/cortex-m4/rcc_reg.h"#include <stdbool.h>#include <stdint.h>
Macros | |
| #define | TIMEOUT 1000000 |
| Maximum wait cycles for flag checks. | |
| #define | I2C1_EN 1 |
| Init status mask for I2C1. | |
| #define | I2C2_EN 2 |
| Init status mask for I2C2. | |
| #define | I2C3_EN 4 |
| Init status mask for I2C3. | |
Functions | |
| uint8_t | hal_i2c_get_init_status (void) |
| Get initialization status of I2C buses. | |
| hal_i2c_status_t | hal_i2c_init (hal_i2c_bus_t bus, hal_i2c_config_t *config) |
| Initialize I2C peripheral in master mode. | |
| hal_i2c_status_t | hal_i2c_write (uint8_t bus, uint8_t dev_addr, const uint8_t *data, uint16_t len) |
| Write data to I2C device. | |
| hal_i2c_status_t | hal_i2c_read (uint8_t bus, uint8_t dev_addr, uint8_t *data, uint16_t len) |
| Read data from I2C device. | |
| hal_i2c_status_t | hal_i2c_write_read (uint8_t bus, uint8_t dev_addr, const uint8_t *tx_data, uint16_t tx_len, uint8_t *rx_data, uint16_t rx_len) |
| Combined write-read operation. | |
HAL I2C driver implementation for STM32F4 series.
This file provides a complete hardware abstraction layer for I2C communication on STM32F4 microcontrollers. It supports:
The implementation handles all low-level register operations and provides high-level functions for common I2C transactions.
| #define I2C1_EN 1 |
Init status mask for I2C1.
| #define I2C2_EN 2 |
Init status mask for I2C2.
| #define I2C3_EN 4 |
Init status mask for I2C3.
| #define TIMEOUT 1000000 |
Maximum wait cycles for flag checks.
| uint8_t hal_i2c_get_init_status | ( | void | ) |
Get initialization status of I2C buses.
Get the initialization status of the I²C peripheral.
| Bit | 0: I2C1 status |
| Bit | 1: I2C2 status |
| Bit | 2: I2C3 status |
| hal_i2c_status_t hal_i2c_init | ( | hal_i2c_bus_t | bus, |
| hal_i2c_config_t * | config ) |
Initialize I2C peripheral in master mode.
Initialize the I²C peripheral.
| [in] | bus | I2C bus identifier (I2C1, I2C2, I2C3) |
| [in] | config | Pointer to configuration structure |
| HAL_I2C_OK | Initialization successful |
| HAL_I2C_ERR_REINIT | Bus already initialized |
| HAL_I2C_ERR_BUS | Unsupported mode (slave not implemented) |
Configures the specified I2C bus with:
< I²C base address
| hal_i2c_status_t hal_i2c_read | ( | uint8_t | bus, |
| uint8_t | dev_addr, | ||
| uint8_t * | data, | ||
| uint16_t | len ) |
Read data from I2C device.
Read data from an I²C device.
| [in] | bus | I2C bus identifier |
| [in] | dev_addr | 7-bit device address |
| [out] | data | Pointer to receive buffer |
| [in] | len | Number of bytes to receive |
| HAL_I2C_OK | Reception successful |
| HAL_I2C_ERR_TIMEOUT | Bus timeout occurred |
| HAL_I2C_ERR_BUS | Invalid parameters |
Performs complete I2C read transaction:
< I²C base address
| hal_i2c_status_t hal_i2c_write | ( | uint8_t | bus, |
| uint8_t | dev_addr, | ||
| const uint8_t * | data, | ||
| uint16_t | len ) |
Write data to I2C device.
Write data to an I²C device.
| [in] | bus | I2C bus identifier |
| [in] | dev_addr | 7-bit device address |
| [in] | data | Pointer to transmit buffer |
| [in] | len | Number of bytes to transmit |
| HAL_I2C_OK | Transmission successful |
| HAL_I2C_ERR_TIMEOUT | Bus timeout occurred |
Performs complete I2C write transaction:
| hal_i2c_status_t hal_i2c_write_read | ( | uint8_t | bus, |
| uint8_t | dev_addr, | ||
| const uint8_t * | tx_data, | ||
| uint16_t | tx_len, | ||
| uint8_t * | rx_data, | ||
| uint16_t | rx_len ) |
Combined write-read operation.
Write to a device register and read back data.
| [in] | bus | I2C bus identifier |
| [in] | dev_addr | 7-bit device address |
| [in] | tx_data | Pointer to transmit buffer |
| [in] | tx_len | Number of bytes to transmit |
| [out] | rx_data | Pointer to receive buffer |
| [in] | rx_len | Number of bytes to receive |
| HAL_I2C_OK | Operation successful |
| HAL_I2C_ERR_TIMEOUT | Bus timeout occurred |
| HAL_I2C_ERR_BUS | Invalid parameters |
Performs combined I2C transaction:
< I²C base address