完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
void MX_I2C2_Init(void)
{ /* USER CODE BEGIN I2C2_Init 0 */ /* USER CODE END I2C2_Init 0 */ /* USER CODE BEGIN I2C2_Init 1 */ /* USER CODE END I2C2_Init 1 */ hi2c2.Instance = I2C2; hi2c2.Init.timing = 0x10C0ECFF; hi2c2.Init.OwnAddress1 = 0; hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c2.Init.OwnAddress2 = 0; hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK; hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init( hi2c2) != HAL_OK) { Error_Handler(); } /** Configure Analogue filter */ if (HAL_I2CEx_ConfigAnalogFilter( hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK) { Error_Handler(); } /** Configure Digital filter */ if (HAL_I2CEx_ConfigDigitalFilter( hi2c2, 0) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN I2C2_Init 2 */ /* USER CODE END I2C2_Init 2 */ } void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; if(i2cHandle->Instance==I2C2) { /* USER CODE BEGIN I2C2_MspInit 0 */ /* USER CODE END I2C2_MspInit 0 */ /** Initializes the peripherals clock */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2; PeriphClkInitStruct.I2c123ClockSelection = RCC_I2C123CLKSOURCE_D2PCLK1; if (HAL_RCCEx_PeriphCLKConfig( PeriphClkInitStruct) != HAL_OK) { Error_Handler(); } __HAL_RCC_GPIOH_CLK_ENABLE(); /**I2C2 GPIO Configuration PH4 ------> I2C2_SCL PH5 ------> I2C2_SDA */ GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C2; HAL_GPIO_Init(GPIOH, GPIO_InitStruct); /* I2C2 clock enable */ __HAL_RCC_I2C2_CLK_ENABLE(); /* I2C2 DMA Init */ /* I2C2_RX Init */ hdma_i2c2_rx.Instance = DMA1_Stream2; hdma_i2c2_rx.Init.Request = DMA_REQUEST_I2C2_RX; hdma_i2c2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_i2c2_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_i2c2_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_i2c2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_i2c2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_i2c2_rx.Init.Mode = DMA_CIRCULAR; hdma_i2c2_rx.Init.Priority = DMA_PRIORITY_LOW; hdma_i2c2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; if (HAL_DMA_Init( hdma_i2c2_rx) != HAL_OK) { Error_Handler(); } __HAL_LINKDMA(i2cHandle,hdmarx,hdma_i2c2_rx); /* I2C2_TX Init */ hdma_i2c2_tx.Instance = DMA1_Stream3; hdma_i2c2_tx.Init.Request = DMA_REQUEST_I2C2_TX; hdma_i2c2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_i2c2_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_i2c2_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_i2c2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_i2c2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_i2c2_tx.Init.Mode = DMA_CIRCULAR; hdma_i2c2_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_i2c2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; if (HAL_DMA_Init( hdma_i2c2_tx) != HAL_OK) { Error_Handler(); } __HAL_LINKDMA(i2cHandle,hdmatx,hdma_i2c2_tx); /* USER CODE BEGIN I2C2_MspInit 1 */ /* USER CODE END I2C2_MspInit 1 */ } } 以上是初始化代码。然后使用以下代码发送数据: HAL_I2C_Master_Transmit_DMA( hi2c2, PCF8574_ADDR, temp, 1); 现象是只能发一次,之后hi2c2.state一直是busy。在中断处理函数中手动改为ready后,dma的状态一直处于busy. |
|
相关推荐
1个回答
|
|
我用示波器看了,第一帧数据发出去了,也收到了ACK。但第二帧数据就不发送了。 如果STOP 信号也发出去了,说明一个buffer 数据发送完成。 试试将DMA 的模式 由 DMA_CIRCULAR 改为 DMA_NORMAL 模式看看。 |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1441 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1373 浏览 3 评论
3018 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1519 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1949 浏览 1 评论
浏览过的版块 |
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-4 03:22 , Processed in 0.831738 second(s), Total 76, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号