Welcome to STM32-BoardCar repository
17. DMA:I2C
DMA:I2C
DMA:I2C1、software-hardware2、Brief principle2.1、Hardware schematic diagram2.2、Physical connection diagram2.3、Principle of control3、Engineering configuration3.1、Notes3.2、Pin configuration4、Main function4.1、User function4.2、HAL library function parsing5、Experimental phenomenon
This tutorial demonstrates I2C (I2C1) communication via DMA
1、software-hardware
STM32F103CubeIDE
STM32 robot expansion board
I2C: chip internal peripheral
OLED: External
Type-C cable or ST-Link
Download or simulate the program of the development board
2、Brief principle
2.1、Hardware schematic diagram
The schematic shows only the I2C (I2C1) interface used in the tutorial

2.2、Physical connection diagram

| OLED | STM32 board |
|---|---|
| VCC | VCC |
| SCL | SCL |
| SDA | SDA |
| GND | GND |
2.3、Principle of control
- I2C
The use of I2C1 peripheral interface and 0.91 inch OLED display for communication, I2C related knowledge will not be introduced, you can see before [Chapter 3:3.12 I2C communication]
- DMA(Direct Memory Access)
STM32F103ZET6 has a total of two DMA controllers, DMA1 has 7 channels, DMA2 has 5 channels;
It is used for high-speed data transfer between peripheral equipment and memory and between memory and memory.
DMA features
The initialization and start of DMA are completed by the CPU, and the transfer process is executed by the DMA controller without the participation of the CPU, so that the CPU resources are saved to do other operations.
DMA1 requests for each channel

DMA2 requests for each channel

DMA1 channel 6 is used in this tutorial
3、Engineering configuration
Project Configuration: Prompts for configuration options in the STM32CubeIDE project configuration process
3.1、Notes
Omitted project configuration: New project, chip selection, project configuration, SYS for pin configuration, RCC configuration, clock configuration, and project configuration content
The project configuration part, which is not omitted, is the key point to configure in this tutorial.
Please refer to [2. Development environment construction and use: STM32CubeIDE installation and use] to understand how to configure the omitted parts of the project.
3.2、Pin configuration
- Configure the specified pin function
You can directly select the corresponding pin number in the pin view, and the corresponding option will appear when the mouse is left clicked

- I2C




- Advanced Settings

- 生成代码

4、Main function
This section mainly introduces the functional code written by users. Detailed code can be opened by yourself in the project file we provide, and enter the Bsp folder to view the source code. .
4.1、User function
Functions that have been covered in this tutorial will not be covered anymore.Feel free to refer to the previous tutorials
function:OLED_Draw_Line
| Function prototypes | void OLED_Draw_Line(char *data, uint8_t line, bool clear, bool refresh) |
|---|---|
| Functional Description | Write a single character |
| Input parameters1 | data:Displaying data |
| Input parameters2 | line:Number of rows |
| Input parameters3 | clear:Clear content or ot |
| Input parameters4 | refresh:refreash or not |
| Return value | None |
4.2、HAL library function parsing
The HAL library functions that were covered in the previous tutorial will not be covered
xxxxxxxxxxIf you want to find the HAL library and LL library function analysis involved in the entire tutorial, you can view the documents in the folder [8. STM32 Manual: STM32F1_HAL Library and LL Library_User Manual]
function:HAL_I2C_Init
| Function prototypes | HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c) |
|---|---|
| Functional Description | Initialize the I2C peripheral parameters |
| Input parameters | hi2c:I2C handle address |
| Return value | HAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT |
function:HAL_I2C_MspInit
| Function prototypes | void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) |
|---|---|
| Functional Description | Initialize the clock, GPIO and NVIC of the I2C peripheral |
| Input parameters | i2cHandle:I2C handle address |
| Return value | None |
function:HAL_I2C_MspDeInit
| Function prototypes | void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) |
|---|---|
| Functional Description | Uninitialize the clock, GPIO, and NVIC of the I2C peripheral |
| Input parameters | i2cHandle:I2C handle address |
| Return value | None |
function:HAL_I2C_Mem_Write
| Function prototypes | 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) |
|---|---|
| Functional Description | Write data to a specific memory address of the I2C device |
| Input parameters1 | hi2c:I2C handle address |
| Input parameters2 | DevAddress:Target device address |
| Input parameters3 | MemAddress:Device memory address |
| Input parameters4 | MemAddSize:The size of the device memory address |
| Input parameters5 | pData:Data head address |
| Input parameters6 | Size:Number of bytes of data |
| Input parameters7 | Timeout:Timeout time |
| Return value | HAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT |
function:HAL_DMA_Init
| Function prototypes | HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma) |
|---|---|
| Functional Description | Initialize the DMA controller |
| Input parameters | hdma:DMA handle address |
| Return value | HAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT |
function:HAL_I2C_Mem_Write_DMA
| Function prototypes | HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size) |
|---|---|
| Functional Description | Data is written to the internal registers of the I2C device by DMA |
| Input parameters1 | hi2c:I2C handle address |
| Input parameters2 | DevAddress:Target device address |
| Input parameters3 | MemAddress:Device memory address |
| Input parameters4 | MemAddSize:The size of the device memory address |
| Input parameters5 | pData:Data head address |
| Input parameters6 | Size:Number of bytes of data |
| Return value | HAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT |
5、Experimental phenomenon
After downloading the program successfully, press the RESET button of the development board to observe the OLED display
xxxxxxxxxxFor program download, please refer to [2. Development environment construction and use: program download and simulation]
phenomenon:
OLED:The first line shows OLED init success! , the second line shows yahboom! , the third line shows This is a DMA+I2c!
