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. |
如果STOP 信号也发出去了,说明一个buffer 数据发送完成。
试试将DMA 的模式 由 DMA_CIRCULAR 改为 DMA_NORMAL 模式看看。
如果使用CubeMx配置的话,在I2C NVIC setting那个地方,除了使能默认的DMA中断外,I2C通用中断都勾选起来。
对于从机来说,有没有给出相应的ACK。
这样才好判断方向在哪里。
从你调用的函数来看,H743的I2C做MASTER,基于DMA方式做传输。我前不久还测试验证过该功能,没有问题的。
如果使用CubeMx配置的话,在I2C NVIC setting那个地方,除了使能默认的DMA中断外,I2C通用中断都勾选起来。
我用示波器看了,第一帧数据发出去了,也收到了ACK。但第二帧数据就不发送了。
确定DMA模式是circular不是normal?
I2C只能发送一次后一直busy问题解决了吗? 我也是相同问题