STM32 HAL API

From ElectroDragon Wiki
Revision as of 00:30, 9 March 2021 by Chao (talk | contribs) (→‎Example 2)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

GPIO

  • HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2);


  • HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
  • HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3);


Delay

  • HAL_Delay(100);

I2C

  • HAL_I2C_Mem_Write
  • HAL_I2C_Mem_Read


HAL_I2C_Mem_Write(&hbus_i2c1, DevAddr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length, BUS_I2C1_POLL_TIMEOUT)

  • I2C_MEMADD_SIZE_8BIT 固定方式
  • BUS_I2C1_POLL_TIMEOUT = 0x1000U
  • uint8_t * const pICRef
  • pICRef Pointeron a uint8_t used to return the ST25DV ID.


  • HAL_I2C_Mem_Read(&I2C2, 0x0017, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length, 0x1000U)

Example 2

#define ADDR_24LCxx_Read 0xA1
#define BufferSize 0x100
uint8_t ReadBuffer[BufferSize];


HAL_I2C_Mem_Read(&hi2c1, ADDR_24LCxx_Read, 0, I2C_MEMADD_SIZE_8BIT,ReadBuffer,BufferSize, 0x10);

  • 第一个参数为I2C操作句柄。
  • 第二个参数为E2PROM的写操作设备地址。
  • 第三个参数为内存地址,
  • 第二个参数为内存地址长度,E2PROM内存长度为8bit
  • 第四个参数为数据缓存的起始地址,
  • 第五个参数为传输数据的大小,
  • 第六个参数为操作超时时间。


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. Parameters:

  • hi2c Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
  • DevAddress Target device address: The device 7 bits address value in datasheet must be shifted to the left before calling the interface
  • MemAddress Internal memory address
  • MemAddSize Size of internal memory address
  • pData Pointer to data buffer
  • Size Amount of data to be sent
  • Timeout Timeout duration

AT24C02

  • HAL_I2C_Mem_Write(&hi2c1, 0XA0,1,I2C_MEMADD_SIZE_8BIT,sbuf,1,0xff);
  • HAL_Delay(5);
  • HAL_I2C_Mem_Read(&hi2c1, 0xA0, 1, I2C_MEMADD_SIZE_8BIT, rbuf, 1,0xfff);
  • HAL_Delay(1000);

HAL_I2C_Mem_Read(

  • I2C_HandleTypeDef *hi2c, // 使用的 I2C 模块的 Handle 的指针
  • uint16_t DevAddress, // I2C 器件的地址,这里是 24C02 的地址 0xA0
  • uint16_t MemAddress, // 存储器内部地址
  • uint16_t MemAddSize, // 存储器内部地址位数 8BIT or 16BIT ?
  • uint8_t *pData, // 接收数据缓冲区指针
  • uint16_t Size, // 接收数据长度=
  • uint32_t Timeout // 超时设置

);