I2C loop backing with DMA not work
I am using Nucleo-h74ziq . I am working with i2c loop backing with DMA.I am using 2 board. One board is master and one board as slave. The master is able to send the data and the slave is receiving the data and after that is not again get ready and I get the zero value. I am uanable to understand. I am sharing the code please help me on this issue.
static void MX_I2C4_Init(void)
{
hi2c4.Instance = I2C4;
hi2c4.Init.Timing = 0x009034B6;
hi2c4.Init.OwnAddress1 = 0x30F;
hi2c4.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT;
hi2c4.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c4.Init.OwnAddress2 = 0xFF;
hi2c4.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c4.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c4.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c4) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c4, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c4, 0) != HAL_OK)
{
Error_Handler();
}
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if(hi2c->Instance==I2C4)
{
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C4;
PeriphClkInitStruct.I2c4ClockSelection = RCC_I2C4CLKSOURCE_D3PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_GPIOF_CLK_ENABLE();
PF14 ------> I2C4_SCL
PF15 ------> I2C4_SDA
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C4;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C4;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C4_CLK_ENABLE();
/* I2C4 DMA Init */
/* I2C4_RX Init */
hdma_i2c4_rx.Instance = BDMA_Channel0;
hdma_i2c4_rx.Init.Request = BDMA_REQUEST_I2C4_RX;
hdma_i2c4_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_i2c4_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2c4_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2c4_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_i2c4_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_i2c4_rx.Init.Mode = DMA_NORMAL;
hdma_i2c4_rx.Init.Priority = DMA_PRIORITY_HIGH;
if (HAL_DMA_Init(&hdma_i2c4_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(hi2c,hdmarx,hdma_i2c4_rx);
/* I2C4_TX Init */
hdma_i2c4_tx.Instance = BDMA_Channel1;
hdma_i2c4_tx.Init.Request = BDMA_REQUEST_I2C4_TX;
hdma_i2c4_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_i2c4_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2c4_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2c4_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_i2c4_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_i2c4_tx.Init.Mode = DMA_NORMAL;
hdma_i2c4_tx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_i2c4_tx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(hi2c,hdmatx,hdma_i2c4_tx);
/* I2C4 interrupt Init */
HAL_NVIC_SetPriority(I2C4_ER_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(I2C4_ER_IRQn);
HAL_NVIC_SetPriority(I2C4_EV_IRQn, 0, 2);
HAL_NVIC_EnableIRQ(I2C4_EV_IRQn);
/* USER CODE BEGIN I2C4_MspInit 1 */
/* USER CODE END I2C4_MspInit 1 */
}}
ALIGN_32BYTES (uint8_t aTxBuffer[]) = "HELLO\n\r ";
ALIGN_32BYTES (uint8_t aRxBuffer[RXBUFFERSIZE]);
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin==GPIO_PIN_13)
flag=1;
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle)
{
if (HAL_I2C_GetError(I2cHandle) == HAL_I2C_ERROR_AF){}
if (HAL_I2C_GetError(I2cHandle) == HAL_I2C_ERROR_OVR){}
if (HAL_I2C_GetError(I2cHandle) == HAL_I2C_ERROR_DMA){}
if (HAL_I2C_GetError(I2cHandle) == HAL_I2C_ERROR_TIMEOUT){}
if (HAL_I2C_GetError(I2cHandle) == HAL_I2C_ERROR_SIZE){}
if (HAL_I2C_GetError(I2cHandle) == HAL_I2C_ERROR_DMA_PARAM{}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* USER CODE BEGIN Boot_Mode_Sequence_0 */
int32_t timeout;
/* USER CODE END Boot_Mode_Sequence_0 */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
while (1){
if(flag==1)
{
flag=0;
if(HAL_I2C_Slave_Receive_DMA(&hi2c4, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
{
Error_Handler();
}
// while (HAL_I2C_GetState(&hi2c4) != HAL_I2C_STATE_READY){}
printf("COMPLETION\n\r");
SCB_InvalidateDCache_by_Addr ((uint32_t *)aRxBuffer, RXBUFFERSIZE);
printf("aRxBuffer=%s\n\r",aRxBuffer);
HAL_Delay(9000);
if(HAL_I2C_Slave_Transmit_DMA(&hi2c4, (uint8_t*)aTxBuffer,RXBUFFERSIZE)!= HAL_OK)
{
Error_Handler();
}
while (HAL_I2C_GetState(&hi2c4) != HAL_I2C_STATE_READY){}
printf("SENDING COMPLETE\n\r");
}
/* if(flag==1)
{
flag=0;
// while (HAL_I2C_IsDeviceReady(&hi2c4, (uint16_t)(I2C_ADDRESS), 3, 100) != HAL_OK) {}
if(HAL_I2C_Master_Transmit_DMA(&hi2c4, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, RXBUFFERSIZE)!= HAL_OK)
{
printf("error1\n\r");
Error_Handler();
}
while (HAL_I2C_GetState(&hi2c4) != HAL_I2C_STATE_READY){}
while(HAL_I2C_GetError(&hi2c4) == HAL_I2C_ERROR_AF);
printf("Completion\n\r");
HAL_Delay(9000);
if(HAL_I2C_Master_Receive_DMA(&hi2c4, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
{
Error_Handler();
}
while (HAL_I2C_GetState(&hi2c4) != HAL_I2C_STATE_READY){}
while(HAL_I2C_GetError(&hi2c4) == HAL_I2C_ERROR_AF);
printf("receving\n\r");
}*/
}
}
