Skip to main content
Visitor II
December 21, 2023
Question

Problem using Slave to transmit I2C Interrupt

  • December 21, 2023
  • 4 replies
  • 1484 views

Hi,

In my project, I communicate with STM slave I2C. I can receive data via I2C but I cannot send data. As you can see in the photo below, the packetData is filling up, but when you send it, it only sends 0x00. what is the problem? what can I do?i2c-interrupt.PNGi2c-interrupt-2.PNG

    This topic has been closed for replies.

    4 replies

    Super User
    December 21, 2023

    Probably a code bug. Perhaps your data goes out of scope immediately. Perhaps you aren't using HAL_I2C_Slave_Seq_Transmit_IT correctly.

    Does HAL_I2C_Slave_Transmit work as expected?

    res_queenAuthor
    Visitor II
    January 2, 2024

    HAL_I2C_Slave_Transmit don't work as expected. @TDK 

    res_queenAuthor
    Visitor II
    January 2, 2024

    I am receiving data with I2C Slave Interrupt and I am having problem while sending data and I am using HAL_I2C_Slave_Transmit while sending data. Below are my interrupt codes. Could the interrupt setup have caused an error?

    /* USER CODE BEGIN 1 */
    void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
    {
    first = SET;
    if(i2c_interrupt_enable)
    {
    HAL_I2C_EnableListen_IT(hi2c); // slave is ready again
    
    }
    }
    
    void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
    {
    if( TransferDirection==I2C_DIRECTION_TRANSMIT ) {
    if( first ) {
    HAL_I2C_Slave_Seq_Receive_IT(hi2c, &offset, 1, I2C_NEXT_FRAME);
    } else {
    HAL_I2C_Slave_Seq_Receive_IT(hi2c, &ram[offset], 1, I2C_NEXT_FRAME);
    }
    } else {
    HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &ram[offset], 1, I2C_NEXT_FRAME);
    }
    }
    
    void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
    {
    
    
    if(first) {
    first = RESET;
    } else {
    offset++;
    }
    I2C_NFCReaderCallbackPacketCapture(&hi2c2,&ram[offset],esp32PacketBuffer);
    callback_counter++;
    
    }
    
    void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
    {
    offset++;
    HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &ram[offset], 1, I2C_NEXT_FRAME);
    }
    
    void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
    {
    if( HAL_I2C_GetError(hi2c)==HAL_I2C_ERROR_AF ) {
    offset--; // transaction terminated by master
    } else {
    }
    }
    /* USER CODE END 1 */
    Super User
    January 2, 2024

    Your code there looks quite different than your code in the original post.

    You can see an example of how these functions are used here:

    https://github.com/STMicroelectronics/STM32CubeF3/blob/bb8f80a09a0154897c6e01ceb9e9f4b94aa88375/Projects/STM32F302R8-Nucleo/Examples/I2C/I2C_TwoBoards_RestartAdvComIT/Src/main.c#L490

    Not sure you've included enough code to diagnose. The "offset" variable is never initialized, for instance.