NavHAL 0.1.0
NAVRobotec's architecture-agnostic HAL for embedded systems.
Loading...
Searching...
No Matches
i2c.h File Reference

Cortex-M4 I²C HAL interface. More...

#include "utils/gpio_types.h"
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for i2c.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  hal_i2c_config_t
 I²C configuration structure. More...

Macros

#define I2C_MASTER   0
#define GPIO_FUNC_I2C   GPIO_AF04

Enumerations

enum  hal_i2c_status_t {
  HAL_I2C_OK = 0 , HAL_I2C_ERR_TIMEOUT , HAL_I2C_ERR_BUS , HAL_I2C_ERR_NACK ,
  HAL_I2C_ERR_REINIT
}
 I²C operation status codes. More...
enum  hal_i2c_bus_t { I2C1 = 0 , I2C2 = 1 , I2C3 = 2 }
 Supported I²C bus instances. More...
enum  hal_i2c_speed_t { STANDARD_MODE = 0 , FAST_MODE = 1 }
 I²C speed modes. More...

Functions

hal_i2c_status_t hal_i2c_init (hal_i2c_bus_t bus, hal_i2c_config_t *config)
 Initialize the I²C peripheral.
hal_i2c_status_t hal_i2c_write (uint8_t bus, uint8_t dev_addr, const uint8_t *data, uint16_t len)
 Write data to an I²C device.
hal_i2c_status_t hal_i2c_read (uint8_t bus, uint8_t dev_addr, uint8_t *data, uint16_t len)
 Read data from an I²C 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)
 Write to a device register and read back data.
uint8_t hal_i2c_get_init_status (void)
 Get the initialization status of the I²C peripheral.

Detailed Description

Cortex-M4 I²C HAL interface.

This header defines types, structures, and function prototypes for I²C communication on Cortex-M4 microcontrollers. It provides functions for initializing the I²C peripheral, reading/writing data, and performing combined write-read transactions.

Supported features:

  • Standard and Fast mode I²C communication.
  • Master mode operations.
  • Error handling with status codes.

Macro Definition Documentation

◆ GPIO_FUNC_I2C

#define GPIO_FUNC_I2C   GPIO_AF04

GPIO alternate function for I²C pins

◆ I2C_MASTER

#define I2C_MASTER   0

Master mode identifier

Enumeration Type Documentation

◆ hal_i2c_bus_t

Supported I²C bus instances.

Enumerator
I2C1 

I²C bus 1

I2C2 

I²C bus 2

I2C3 

I²C bus 3

◆ hal_i2c_speed_t

I²C speed modes.

Enumerator
STANDARD_MODE 

100 kHz standard mode

FAST_MODE 

400 kHz fast mode

◆ hal_i2c_status_t

I²C operation status codes.

Enumerator
HAL_I2C_OK 

Operation successful

HAL_I2C_ERR_TIMEOUT 

Timeout occurred

HAL_I2C_ERR_BUS 

Bus error

HAL_I2C_ERR_NACK 

NACK received

HAL_I2C_ERR_REINIT 

Reinitialization required

Function Documentation

◆ hal_i2c_get_init_status()

uint8_t hal_i2c_get_init_status ( void )

Get the initialization status of the I²C peripheral.

Returns
0 if not initialized, non-zero if initialized.

Get the initialization status of the I²C peripheral.

Returns
Bitmask indicating which I2C buses are initialized
Return values
Bit0: I2C1 status
Bit1: I2C2 status
Bit2: I2C3 status

◆ hal_i2c_init()

hal_i2c_status_t hal_i2c_init ( hal_i2c_bus_t bus,
hal_i2c_config_t * config )

Initialize the I²C peripheral.

Parameters
busThe I²C bus instance to initialize.
configPointer to the configuration structure.
Returns
Status code of the initialization operation.

Initialize the I²C peripheral.

Parameters
[in]busI2C bus identifier (I2C1, I2C2, I2C3)
[in]configPointer to configuration structure
Returns
Status of initialization
Return values
HAL_I2C_OKInitialization successful
HAL_I2C_ERR_REINITBus already initialized
HAL_I2C_ERR_BUSUnsupported mode (slave not implemented)

Configures the specified I2C bus with:

  • Clock speed (standard/fast mode)
  • APB1 clock prescaler
  • Rise time calculation
  • Peripheral enable
Note
Automatically enables the required RCC clock for the I2C peripheral

< I²C base address

◆ hal_i2c_read()

hal_i2c_status_t hal_i2c_read ( uint8_t bus,
uint8_t dev_addr,
uint8_t * data,
uint16_t len )

Read data from an I²C device.

Parameters
busThe I²C bus instance.
dev_addrThe 7-bit address of the target device.
dataPointer to buffer for received data.
lenNumber of bytes to read.
Returns
Status code of the read operation.

Read data from an I²C device.

Parameters
[in]busI2C bus identifier
[in]dev_addr7-bit device address
[out]dataPointer to receive buffer
[in]lenNumber of bytes to receive
Returns
Status of operation
Return values
HAL_I2C_OKReception successful
HAL_I2C_ERR_TIMEOUTBus timeout occurred
HAL_I2C_ERR_BUSInvalid parameters

Performs complete I2C read transaction:

  1. START condition
  2. Address + Read bit
  3. Data bytes with proper ACK/NACK
  4. STOP condition

< I²C base address

◆ hal_i2c_write()

hal_i2c_status_t hal_i2c_write ( uint8_t bus,
uint8_t dev_addr,
const uint8_t * data,
uint16_t len )

Write data to an I²C device.

Parameters
busThe I²C bus instance.
dev_addrThe 7-bit address of the target device.
dataPointer to data buffer to transmit.
lenNumber of bytes to transmit.
Returns
Status code of the write operation.

Write data to an I²C device.

Parameters
[in]busI2C bus identifier
[in]dev_addr7-bit device address
[in]dataPointer to transmit buffer
[in]lenNumber of bytes to transmit
Returns
Status of operation
Return values
HAL_I2C_OKTransmission successful
HAL_I2C_ERR_TIMEOUTBus timeout occurred

Performs complete I2C write transaction:

  1. START condition
  2. Address + Write bit
  3. Data bytes
  4. STOP condition

◆ hal_i2c_write_read()

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 )

Write to a device register and read back data.

Parameters
busThe I²C bus instance.
dev_addrThe 7-bit address of the target device.
tx_dataPointer to data to write.
tx_lenNumber of bytes to write.
rx_dataPointer to buffer for received data.
rx_lenNumber of bytes to read.
Returns
Status code of the write-read operation.

Write to a device register and read back data.

Parameters
[in]busI2C bus identifier
[in]dev_addr7-bit device address
[in]tx_dataPointer to transmit buffer
[in]tx_lenNumber of bytes to transmit
[out]rx_dataPointer to receive buffer
[in]rx_lenNumber of bytes to receive
Returns
Status of operation
Return values
HAL_I2C_OKOperation successful
HAL_I2C_ERR_TIMEOUTBus timeout occurred
HAL_I2C_ERR_BUSInvalid parameters

Performs combined I2C transaction:

  1. Write phase (registers/commands)
  2. Repeated START
  3. Read phase (data) Handles all ACK/NACK and STOP conditions automatically

< I²C base address