- #include "stm32f4xx_hal.h"
- I2C_HandleTypeDef hi2c1;
- DMA_HandleTypeDef hdma_i2c1_tx;
- /* 定义数据缓存区 */
- uint8_t data_buf[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
- int main(void)
- {
- /* STM32初始化 */
- HAL_Init();
- /* 配置I2C1 */
- hi2c1.Instance = I2C1;
- hi2c1.Init.ClockSpeed = 100000;
- hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- Error_Handler();
- }
- /* 配置I2C1的DMA传输 */
- hdma_i2c1_tx.Instance = DMA1_Stream7;
- hdma_i2c1_tx.Init.Channel = DMA_CHANNEL_1;
- hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_i2c1_tx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_i2c1_tx.Init.Mode = DMA_NORMAL;
- hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_HIGH;
- hdma_i2c1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
- if (HAL_DMA_Init(&hdma_i2c1_tx) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(&hi2c1, hdmatx, hdma_i2c1_tx);
- /* 启动I2C1的DMA传输 */
- if (HAL_I2C_Mem_Write_DMA(&hi2c1, 0xA0, 0x00, I2C_MEMADD_SIZE_8BIT, data_buf, 10) != HAL_OK)
- {
- Error_Handler();
- }
- /* 死循环 */
- while (1)
- {
- }
- }
- /* 错误处理函数 */
- void Error_Handler(void)
- {
- while (1)
- {
- }
- }
- /* DMA中断处理函数 */
- void DMA1_Stream7_IRQHandler(void)
- {
- HAL_DMA_IRQHandler(&hdma_i2c1_tx);
- }
|