HPM SDK
HPMicro Software Development Kit
hpm_i2c.c File Reference
#include "hpm_i2c.h"
#include "hpm_clock_drv.h"
#include <stdlib.h>

Data Structures

struct  hpm_i2c_cfg_t
 

Functions

static hpm_i2c_cfg_thpm_i2c_get_cfg_obj (I2C_Type *ptr)
 
static void hpm_i2c_master_phase_config (I2C_Type *ptr, uint16_t device_addr, uint8_t flags, uint32_t transfer_size, bool enable_dma)
 
static void hpm_i2c_release_bus (I2C_Type *ptr)
 
static void hpm_i2c_get_default_init_config (hpm_i2c_initialize_config_t *config)
 
void hpm_i2c_get_default_init_context (hpm_i2c_context_t *context)
 
hpm_stat_t hpm_i2c_initialize (hpm_i2c_context_t *context)
 Initialize the I2C interface. More...
 
hpm_stat_t hpm_i2c_master_probe_slave_address (hpm_i2c_context_t *context, uint16_t device_address)
 Probes an I2C slave address. More...
 
hpm_stat_t hpm_i2c_master_addr_write_blocking (hpm_i2c_context_t *context, const uint16_t device_address, uint32_t addr, uint8_t addr_size, uint8_t *buf, uint32_t buf_size, uint32_t timeout)
 I2C master write data to a device's address over I2C in blocking mode. More...
 
hpm_stat_t hpm_i2c_master_addr_read_blocking (hpm_i2c_context_t *context, const uint16_t device_address, uint32_t addr, uint8_t addr_size, uint8_t *buf, uint32_t buf_size, uint32_t timeout)
 I2C master read data from a specified address of an I2C slave device in blocking mode. More...
 
hpm_stat_t hpm_i2c_master_write_blocking (hpm_i2c_context_t *context, uint16_t device_address, uint8_t *buf, uint32_t size, uint32_t timeout)
 I2C master write data to an I2C slave device in blocking mode. More...
 
hpm_stat_t hpm_i2c_master_read_blocking (hpm_i2c_context_t *context, const uint16_t device_address, uint8_t *buf, const uint32_t size, uint32_t timeout)
 I2C master read data from an I2C device in blocking mode. More...
 
hpm_stat_t hpm_i2c_slave_write_blocking (hpm_i2c_context_t *context, uint8_t *buf, uint32_t size, uint32_t timeout)
 I2C slave write operation in blocking mode. More...
 
hpm_stat_t hpm_i2c_slave_read_blocking (hpm_i2c_context_t *context, uint8_t *buf, uint32_t size, uint32_t timeout)
 I2C slave read operation in blocking mode. More...
 

Variables

static hpm_i2c_cfg_t i2c_cfg_table []
 

Function Documentation

◆ hpm_i2c_get_cfg_obj()

static hpm_i2c_cfg_t* hpm_i2c_get_cfg_obj ( I2C_Type ptr)
static

◆ hpm_i2c_get_default_init_config()

static void hpm_i2c_get_default_init_config ( hpm_i2c_initialize_config_t config)
static

◆ hpm_i2c_get_default_init_context()

void hpm_i2c_get_default_init_context ( hpm_i2c_context_t context)

Get the default initialization context for I2C

This function initializes the I2C context structure, setting the default address endianness to little-endian.

Parameters
contextPointer to the I2C context structure

◆ hpm_i2c_initialize()

hpm_stat_t hpm_i2c_initialize ( hpm_i2c_context_t context)

Initialize the I2C interface.

This function initializes the I2C module based on the provided configuration, setting it up for communication. It configures the I2C mode, speed, addressing method, and initializes the I2C device as either a master or slave.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t" which contains initialization configurations and base address information.
Return values
hpm_stat_tReturns the initialization status, indicating success or error codes

◆ hpm_i2c_master_addr_read_blocking()

hpm_stat_t hpm_i2c_master_addr_read_blocking ( hpm_i2c_context_t context,
const uint16_t  device_address,
uint32_t  addr,
uint8_t  addr_size,
uint8_t *  buf,
uint32_t  buf_size,
uint32_t  timeout 
)

I2C master read data from a specified address of an I2C slave device in blocking mode.

This function reads data from a specified address of an I2C slave device using the I2C bus in a blocking manner. It first sends the slave device address, then the memory address to read from, and finally reads the data.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t"
[in]device_addressAddress of the I2C slave device (7-bit or 10-bit address, depending on the hardware).
[in]addrInternal address within the slave device to read from (can be a register or memory address).
[in]addr_sizeSize of the address in bytes, indicating the number of bits in the address.
[in]bufPointer to the buffer where the read data will be stored.
[in]buf_sizeNumber of bytes to read.
[in]timeoutTimeout value in milliseconds. If the read operation does not complete within this time, the function returns a timeout error.
Return values
hpm_stat_tOperation status, indicating success or error codes

◆ hpm_i2c_master_addr_write_blocking()

hpm_stat_t hpm_i2c_master_addr_write_blocking ( hpm_i2c_context_t context,
const uint16_t  device_address,
uint32_t  addr,
uint8_t  addr_size,
uint8_t *  buf,
uint32_t  buf_size,
uint32_t  timeout 
)

I2C master write data to a device's address over I2C in blocking mode.

This function writes data to a specific device over the I2C bus in a blocking manner. It requires the base address of the I2C peripheral, the device's I2C address, the address within the device, the address size, the data buffer, and the buffer size. The function ensures that the data transfer is completed within the specified timeout period.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t"
[in]device_addressAddress of the I2C slave device (7-bit or 10-bit address, depending on the hardware)
[in]addrThe address within the device where data writing should start
[in]addr_sizeThe size of the address, indicating the width of the device's internal address (in bytes)
[in]bufPointer to the buffer containing the data to be written
[in]buf_sizeThe size of the buffer, indicating the length of the data to be written (in bytes)
[in]timeoutThe timeout period, within which the function must complete the data transfer
Return values
hpm_stat_tOperation status, indicating success or error codes

◆ hpm_i2c_master_phase_config()

static void hpm_i2c_master_phase_config ( I2C_Type ptr,
uint16_t  device_addr,
uint8_t  flags,
uint32_t  transfer_size,
bool  enable_dma 
)
static

◆ hpm_i2c_master_probe_slave_address()

hpm_stat_t hpm_i2c_master_probe_slave_address ( hpm_i2c_context_t context,
uint16_t  device_address 
)

Probes an I2C slave address.

This function sends a probe signal over the I2C bus to check if a slave device at the specified address is present. It is primarily used to verify that a slave device is correctly connected to the I2C bus without performing any data transfer.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t"
[in]device_addressaddress of the slave device to probe
Return values
hpm_stat_tOperation status, indicating success or error codes

◆ hpm_i2c_master_read_blocking()

hpm_stat_t hpm_i2c_master_read_blocking ( hpm_i2c_context_t context,
const uint16_t  device_address,
uint8_t *  buf,
const uint32_t  size,
uint32_t  timeout 
)

I2C master read data from an I2C device in blocking mode.

This function reads a specified number of bytes from the I2C device with the given address and stores them in the provided buffer. The read operation is blocking, meaning it will wait until the read is complete or an error occurs.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t"
[in]device_addressAddress of the I2C slave device (7-bit or 10-bit address, depending on the hardware)
[in]bufPointer to the buffer where the read data will be stored
[in]sizeNumber of bytes to read
[in]timeoutTimeout for the operation in milliseconds
Return values
hpm_stat_tOperation status, indicating success or error codes

◆ hpm_i2c_master_write_blocking()

hpm_stat_t hpm_i2c_master_write_blocking ( hpm_i2c_context_t context,
uint16_t  device_address,
uint8_t *  buf,
uint32_t  size,
uint32_t  timeout 
)

I2C master write data to an I2C slave device in blocking mode.

This function performs a blocking write operation for an I2C master device, writing a specified number of bytes to the I2C slave device. Blocking mode means the function will return only after the data transfer is complete, and it will occupy CPU resources during the data transfer.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t"
[in]device_addressAddress of the I2C slave device (7-bit or 10-bit address, depending on the hardware)
[in]bufPointer to the buffer containing the data to be written, data type is uint8_t
[in]sizeNumber of bytes to write, data type is uint32_t
[in]timeoutTimeout for the operation, in milliseconds. If the operation does not complete within this time, the function will return an error
Return values
hpm_stat_tOperation status, indicating success or error codes

◆ hpm_i2c_release_bus()

static void hpm_i2c_release_bus ( I2C_Type ptr)
static

◆ hpm_i2c_slave_read_blocking()

hpm_stat_t hpm_i2c_slave_read_blocking ( hpm_i2c_context_t context,
uint8_t *  buf,
uint32_t  size,
uint32_t  timeout 
)

I2C slave read operation in blocking mode.

This function reads data from the I2C slave device using the specified I2C interface in a blocking mode.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t"
[in]bufPointer to the buffer where the read data will be stored.
[in]sizeNumber of bytes to read from the I2C slave device.
[in]timeoutTimeout value in milliseconds for the read operation. If the read operation does not complete within this time, it will be terminated.
Return values
hpm_stat_tOperation status, indicating success or error codes

◆ hpm_i2c_slave_write_blocking()

hpm_stat_t hpm_i2c_slave_write_blocking ( hpm_i2c_context_t context,
uint8_t *  buf,
uint32_t  size,
uint32_t  timeout 
)

I2C slave write operation in blocking mode.

This function handles the write operation for an I2C slave in a blocking manner. It waits until all data is successfully written or an error occurs, or the specified timeout is exceeded.

Parameters
[in]contextA pointer to the struct of "hpm_i2c_context_t"
[in]bufPointer to the buffer where received data will be stored
[in]sizeNumber of bytes to receive
[in]timeoutTimeout value for the operation; if the operation does not complete within this time, the function returns a timeout error
Return values
hpm_stat_tOperation status, indicating success or error codes

Variable Documentation

◆ i2c_cfg_table

hpm_i2c_cfg_t i2c_cfg_table[]
static
Initial value:
= {
}