思澈科技软件开发工具包  2.20
I2C

I2C HAL 提供用于访问 I2C 外设寄存器的基本 API。 主要功能包括:

  • 仅限主模式 (I2C Master)。
  • 最多支持 6 个实例,并且3个在HCPU上,3个在 LCPU 上使用。
  • 10 位和 7 位地址支持。
  • DMA/中断模式支持
  • 内存模式访问

使用 I2C HAL 驱动程序

I2C 可以支持使用 DMA 和中断模式,它们需要在 I2C 启动之前进行配置。

将 I2C HAL 与内存模式和轮询一起使用的示例:

I2C_HandleTypeDef i2c_Handle = {0};
uint16_t DevAddress, MemAddress, MemAddSize, Size;
uint32_t Timeout;
uint8_t *pData;
/* initial I2C controller */
i2c_Handle.Instance = hwp_i2c1;
i2c_Handle.core = CORE_ID_LCPU;
i2c_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c_Handle.Init.ClockSpeed = 400000;
i2c_Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
ret = HAL_I2C_Init(&i2c_Handle);
if (ret != HAL_OK)
{
return ;
}
DevAddress = 0x76;// slave device 7 bit or 10 bit address
MemAddress = 0x10; // address on slave device
MemAddSize = I2C_MEMADD_SIZE_8BIT; // I2C_MEMADD_SIZE_8BIT or I2C_MEMADD_SIZE_16BIT
pData = (uint8_t *)malloc(256);
Size = 256;
Timeout = 100; // 100ms
/* I2C Write */
HAL_I2C_Mem_Write(&i2c_Handle, DevAddress, MemAddress, MemAddSize, pData, Size, Timeout);
/* I2C read */
HAL_I2C_Mem_Read(&i2c_Handle, DevAddress, MemAddress, MemAddSize, pData,Size, Timeout);
...

Sample for using I2C HAL with register mode and DMA:

uint16_t DevAddress, Size;
uint8_t *pData;
I2C_HandleTypeDef handle = {0};
static struct dma_config i2c_dmarx; // allocated buffer for dma handle in I2C instance
static struct dma_config i2c_dmatx;
struct dma_config dma_set;
/* initial I2C DMA info */
dma_set.dma_rcc = I2C1_DMA_RCC;
dma_set.Instance = I2C1_DMA_INSTANCE;
dma_set.dma_irq = I2C1_DMA_IRQ;
dma_set.request = I2C1_DMA_REQUEST;
/* bind dma handle */
//__HAL_LINKDMA(handle, hdmarx, i2c_dmarx);
//__HAL_LINKDMA(handle, hdmatx, i2c_dmatx);
handle.hdmarx = &i2c_dmarx;
handle.hdmatx = &i2c_dmatx;
HAL_I2C_DMA_Init(&handle, &dma_set, &dma_set);
/* Enable RX dma interrupt by default */
HAL_NVIC_SetPriority(dma_set.dma_irq, 0, 0);
HAL_NVIC_EnableIRQ(dma_set.dma_irq);
handle.Instance = hwp_i2c1;
handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
handle.Init.ClockSpeed = 400000;
handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
handle.core = CORE_ID_LCPU;
HAL_I2C_Init(&handle);
/* I2C Write */
HAL_DMA_Init(handle.dma_tx);
DevAddress = 0x76; // set slave i2c device id
pData = malloc(256);
Size = 256;
HAL_I2C_Master_Transmit_DMA(&handle, DevAddress,pData, Size);
/* I2C read */
HAL_DMA_Init(handle.dma_rx);
HAL_I2C_Master_Receive_DMA(&handle, DevAddress,pData, Size);
...
__I2C_HandleTypeDef::Instance
I2C_TypeDef * Instance
Definition: bf0_hal_i2c.h:213
__I2C_HandleTypeDef::core
uint8_t core
Definition: bf0_hal_i2c.h:236
dma_config
Definition: bf0_hal_dma.h:73
HAL_I2C_MODE_MASTER
@ HAL_I2C_MODE_MASTER
Definition: bf0_hal_i2c.h:169
HAL_OK
@ HAL_OK
Definition: bf0_hal_def.h:75
HAL_I2C_DMA_Init
HAL_StatusTypeDef HAL_I2C_DMA_Init(I2C_HandleTypeDef *hi2c, struct dma_config *dma_rx, struct dma_config *dma_tx)
Initializes the I2C according to the specified parameters with dma mode in the I2C_InitTypeDef and in...
HAL_I2C_Master_Transmit_DMA
HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
Transmit in master mode an amount of data in non-blocking mode with DMA.
__I2C_HandleTypeDef::hdmatx
DMA_HandleTypeDef * hdmatx
Definition: bf0_hal_i2c.h:230
I2C_InitTypeDef::AddressingMode
uint32_t AddressingMode
Definition: bf0_hal_i2c.h:87
__I2C_HandleTypeDef::Mode
__IO HAL_I2C_ModeTypeDef Mode
Definition: bf0_hal_i2c.h:242
CORE_ID_LCPU
#define CORE_ID_LCPU
Definition: bf0_hal_rcc.h:75
HAL_I2C_Mem_Write
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
Write an amount of data in blocking mode to a specific memory address.
I2C_InitTypeDef::GeneralCallMode
uint32_t GeneralCallMode
Definition: bf0_hal_i2c.h:90
__I2C_HandleTypeDef::hdmarx
DMA_HandleTypeDef * hdmarx
Definition: bf0_hal_i2c.h:232
HAL_NVIC_SetPriority
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
Set the priority of an interrupt.
HAL_I2C_Mem_Read
HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
Read an amount of data in blocking mode from a specific memory address.
HAL_NVIC_EnableIRQ
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
Enable a device specific interrupt in the NVIC interrupt controller.
dma_config::dma_rcc
uint32_t dma_rcc
Definition: bf0_hal_dma.h:75
I2C_InitTypeDef::ClockSpeed
uint32_t ClockSpeed
Definition: bf0_hal_i2c.h:77
HAL_I2C_Init
HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
Initializes the I2C according to the specified parameters in the I2C_InitTypeDef and initialize the a...
__I2C_HandleTypeDef::Init
I2C_InitTypeDef Init
Definition: bf0_hal_i2c.h:215
HAL_I2C_Master_Receive_DMA
HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
Receive in master mode an amount of data in non-blocking mode with DMA.
__I2C_HandleTypeDef
Definition: bf0_hal_i2c.h:212
HAL_DMA_Init
HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
Initialize the DMA according to the specified parameters in the DMA_InitTypeDef and initialize the as...