Search code examples
stm32i2cinterrupt-handlingstm32f4discovery

I2C communication between two stm32 board in interrupt mode by low layer functions of STM32CUBEMX


I am working on a project which needs data communication between two stm32 (stm32f103rb and stm32f429zg).

I use stm32f103rb-nucleo examples_ll (I2C _OneBoard_Communication_IT) but it works for one byte and after that, it stops working. I want it to work repetitively.

the code structure is exactly in the way the example is written in the folder related to stm32cubemx repository.

I don't know how to make it work in a loop and by pressing the button, communication gets done every time I push the master and slave push buttons.

I have put the master completion callback function here :

void Master_Complete_Callback(void)
{
  if(ubNbDataToReceive == 3)
  {
    /* Prepare the generation of a Non ACKnowledge condition after next received bytes */
    LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
    
    /* Read character in Receive Data register.
    RXNE flag is cleared by reading data in RXDR register */
    aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C1);
    ubNbDataToReceive--;

    /* Disable Buffer Interrupts */
    LL_I2C_DisableIT_BUF(I2C1);
  }
  else if(ubNbDataToReceive == 2)
  {
    /* Generate Stop condition */
    LL_I2C_GenerateStopCondition(I2C1);

    /* Read character from Receive Data register.
    RXNE flag is cleared by reading data in RXDR register */
    aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C1);
    ubNbDataToReceive--;

    /* Read character from shift register.
    RXNE flag is cleared by reading data in RXDR register */
    aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C1);
    ubNbDataToReceive--;
  }
  else
  {
    if(ubNbDataToReceive > 0)
    {
      /* Read character from shift register.
      RXNE flag is cleared by reading data in RXDR register */
      aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C2);
      
      /* Update ubNbDataToReceive variable */
      ubNbDataToReceive--;
    }
  }

  if(ubNbDataToReceive == 0)
  {
    /* (1) Disable I2C1 transfer event/error interrupts:
     *  - Disable Events Interrupt
     *  - Disable Error interrupts
     */
    LL_I2C_DisableIT_EVT(I2C1);
    LL_I2C_DisableIT_ERR(I2C1);

        LL_I2C_EnableIT_EVT(I2C1);
    LL_I2C_EnableIT_ERR(I2C1);
    /* Read Received character.
    RXNE flag is cleared by reading of RXDR register */
    if(aReceiveBuffer[ubReceiveIndex-1] == SLAVE_BYTE_TO_SEND)
    {
      /* Turn LED2 On:
       * - Expected byte has been received
       * - Master Rx sequence completed successfully
       */
            
      //LED_On();
            HAL_UART_Transmit_IT(&huart2,(uint8_t*)"#Data transfer is OK!$",strlen("#Data transfer is OK!$"));
            ubNbDataToTransmit     = sizeof(SLAVE_BYTE_TO_SEND);
            ubNbDataToReceive      = sizeof(SLAVE_BYTE_TO_SEND);
    }
    else
    {
      /* Call Error function */
      Error_Callback();
    }
  }
}

it is easily understood that the end of the transmission process is when I add HAL_UART function to send to serial port the transmission completion.

but after this time , it works one more time and after that it stops. I have checked the oscilloscope signal show and after the second time transmission SCL pin gets LOW . Thanks


Solution

  • Did not use LL for a long time, but isnt I2C interrupt disabled after single transfer with LL_I2C_DisableIT_EVT(I2C1);?? Make sure to enable it again before next transmission.