This application note explains how to use the STM32H7 I2C Master mode in the FreeRTOS demo application.
1. Understanding I2C Master Mode Interfaces
1.1. FreeRTOS I2C Master Mode Implementation
The FreeRTOS BSP provides a device driver for the I2C Master mode operation. The driver is configured (enabled / disabled) at the BSP build time, using the HAL_I2C_MODULE_ENABLED
configuration option, defined in the stm32h7xx_hal_conf.h
file.
1.2. FreeRTOS I2C Master Mode C-Binding API
The I2C Master Mode driver implements the following C-binding API:
Function | Description | Comments |
| ||
Initialize a specified I2C bus |
| |
| ||
Read a specified I2C device on a specified I2C interface |
| |
| ||
Write a specified I2C device on a specified I2C interface |
| |
| ||
Switch a specified I2C bus to 400KHz operation | ||
| ||
Switch a specified I2C bus to `100KHz operation |
1.3. I2C Master Mode CLI Command
The FreeRTOS application implements the following I2C Master mode related CLI commands:
Command | Description | Comments |
| ||
Read a specified I2C device on a specified I2C interface |
| |
| ||
Write a specified I2C device on a specified I2C interface |
| |
| ||
Scan a I2C interface |
|
2. Validating I2C Master Operation
2.1. Scanning I2C Buses
Use the following step-wise procedure to scan the I2C buses:
Scan I2C bus 1:
CLI> i2c_scan 1 Scanning........ I2C1: 0x50 0x58 0x5d
Scan I2C bus 4:
CLI> i2c_scan 4 Scanning........ I2C4:
2.2. Reading I2C EEPRIOM
Use the following step-wise procedure to read the I2C EEPROM:
From the FreeRTOS CLI, read 4 bytes from the EEPROM:
CLI> i2c_read 1 50 14 4 14: ff ff ff ff
Update the 4 bytes of the EEPROM (the same offset that was just read in the previous command):
CLI> i2c_write 1 50 14 0x1a2b3c4d Done
Read the 4 bytes of the EEPROM back, confirm that the read data matches the written data:
CLI> i2c_read 1 50 14 4 14: 1a 2b 3c 4d
2.3. Reading Data from I2C Touch Controller
If your starter kit includes the LCD, use the following step-wise procedure to read the configured resolution from the I2C touch controller:
Scan I2C bus 1:
CLI> i2c_scan 1 Scanning........ I2C1: 0x14
From the FreeRTOS CLI, read the configured X axis resolution from the I2C touch controller configuration register:
CLI> i2c_read 1 14 8048 2 8048: e0 01 // 480
From the FreeRTOS CLI, read the configured Y axis resolution from the I2C touch controller configuration register:
CLI> i2c_read 1 14 804a 2 804a: 10 01 // 272
Verify that the configured values are equal to the LCD resolution.
The configuration values are written by the I2C touch controller driver on start-up and it is recommended not to modify them at runtime.