My I2C communication in DMA mode is not working properly
Hello,
I have developed two codes to test the I2C communication between a master device (STM32F411RE) and a slave device (also STM32F411RE) in DMA mode. The expected result is as follows: whenever there is a rising or falling edge interruption on GPIO_PIN_11 of the master device, it should send a TX buffer to the slave device. After receiving the buffer by the slave device, master device should enter the reception mode for I2C and wait for a response from the slave.
On the slave device, I have initially set it to the reception mode for I2C. Once the first buffer sent by the master is received by the slave, it waits for 1 seconds before switching to the transmission mode to send a response back to the master. I am using a button connected to GPIO pin 11 to generate the interruptions.
The result that I get is when I trigger the first interruption on the master device, it successfully sends the buffer to the slave, and the slave accurately receives the buffer. However, I am not receiving any response from the slave device afterward.
My master code
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
/*for(int i = 1; i<20; i++){
ret = HAL_I2C_IsDeviceReady(&hi2c1, i, 3, 100);
if(ret == HAL_OK){
uint8_t address = i;
}
while(HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY){
}
}*/
txbuffer[0] = 0x01;
txbuffer[1] = 0xFF;
txbuffer[2] = 0x05;
txbuffer[3] = 0xFF;
txbuffer[4] = 0xFF;
txbuffer[5] = 0xFF;
txbuffer[6] = 0xFF;
txbuffer[7] = 0xFF;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(RXflag == true){
if(HAL_I2C_Master_Receive_DMA(&hi2c1, 16, rxbuffer, sizeof(rxbuffer)) != HAL_OK)
{
/* Transfer error in reception process */
Error_Handler();
}
RXflag = false;
}
if(TXflag == true){
if(HAL_I2C_Master_Transmit_DMA(&hi2c1, 16, txbuffer, sizeof(txbuffer)) != HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
TXflag = false;
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef*hi2c)
{
RXflag = true;
}
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef*hi2c)
{
TXflag = true;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin){
case GPIO_PIN_11:
HAL_I2C_Master_Transmit_DMA(&hi2c1, 16, (uint8_t *)txbuffer, sizeof(txbuffer));
break;
case GPIO_PIN_12:
break;
default:
break;
}
}My Slave code
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
HAL_I2C_MspInit(&hi2c1);
/* USER CODE BEGIN 2 */
if(HAL_I2C_Slave_Receive_DMA(&hi2c1, rxbuffer, sizeof(rxbuffer)) != HAL_OK)
{
/* Transfer error in reception process */
Error_Handler();
}
txbuffer[0] = 11;
txbuffer[1] = 12;
txbuffer[2] = 13;
txbuffer[3] = 14;
txbuffer[4] = 15;
txbuffer[5] = 16;
txbuffer[6] = 17;
txbuffer[7] = 18;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(RXflag == true){
if(HAL_I2C_Slave_Receive_DMA(&hi2c1, rxbuffer, sizeof(rxbuffer)) != HAL_OK)
{
/* Transfer error in reception process */
Error_Handler();
}
RXflag = false;
}
if(TXflag == true){
HAL_Delay(1000);
if(HAL_I2C_Slave_Transmit_DMA(&hi2c1, txbuffer, sizeof(txbuffer)) != HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
TXflag = false;
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *I2cHandle)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
RXflag = true;
}
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
TXflag = true;
if(rxbuffer[0] == 0x01)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
}
}