cancel
Showing results for 
Search instead for 
Did you mean: 

I2C interrupt not working

SGUIG.1
Associate II

Hello i am trying to use I2C interrupt communication between my STM32L4 and an NFC module, however when i try to visualize the signal i have nthg. I feel like i have forgotten sth, but i can't figure out what is it. Can you please help?

This is what i currently have:

I2C init:

static void MX_I2C1_Init(void)
{
 
  /* USER CODE BEGIN I2C1_Init 0 */
 
  /* USER CODE END I2C1_Init 0 */
 
  /* USER CODE BEGIN I2C1_Init 1 */
 
  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x10909CEC;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  
   // Interrupts Activation
   HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
   HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
    
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */
 
  /* USER CODE END I2C1_Init 2 */
 
}

Master transmit IT function:

/**
* @brief  Set data in Tx Buffer function
* @param  None
* @retval None
*/
HAL_StatusTypeDef Set_I2CTxBuffer( void )
{
    HAL_StatusTypeDef ret;
    uint8_t comI2C_NFC_TxBuffer[ I2C_NFC_TX_BUFF_SIZE ];
    uint32_t Data = 4967295; //[4] = { 1, 1, 2, 3};  
    uint16_t MemoAddress = 0;
 
    comI2C_NFC_TxBuffer[0] = (uint8_t) ((MemoAddress & 0xFF00) >> 8);
    comI2C_NFC_TxBuffer[1] = (uint8_t) (MemoAddress & 0x00FF);
    comI2C_NFC_TxBuffer[2] = (uint8_t) (Data & 0x000000FF );
    comI2C_NFC_TxBuffer[3] = (uint8_t) ((Data & 0x0000FF00) >> 8 );
    comI2C_NFC_TxBuffer[4] = (uint8_t) ((Data & 0x00FF0000) >> 16);
    comI2C_NFC_TxBuffer[5] = (uint8_t) ((Data & 0xFF000000) >> 24 );
    
    ret = HAL_I2C_Master_Transmit_IT(&hi2c1, I2C_NFC_ADRESS, comI2C_NFC_TxBuffer, I2C_NFC_TX_BUFF_SIZE);
    
    return ret;       
}

the signal visualization:

0693W00000HqcTcQAJ.png

4 REPLIES 4
TDK
Guru

Nothing obviously incorrect that would cause this issue in the code you presented. Are you actually calling these functions in the code? Are pins initialized? What is the return status from HAL_I2C_Master_Transmit_IT?

Verify you're monitoring the right pins and the hardware connection exists by initializing them as GPIO outputs and toggling.

Edit: Your buffer in comI2C_NFC_TxBuffer must remain in scope. It can't be stack-based variable, it needs to be global or otherwise remain valid until completion. However, this would not cause the issue.

If you feel a post has answered your question, please click "Accept as Solution".

Hello, thank you for replying. Yes, i already initialized the pins. return status from HAL_I2C_Master_Transmit_IT is HAL_OK. However when i use HAL_I2C_Master_Receive_IT the return value is HAL_BUSY. I already verified if the slave received the data i sent but it didn't receive anything. Do you have anything in mind that can cause this?

Could it be the master Address or the It Priority? if so do you have any idea on what to change and why?

TDK
Guru

Well if you're getting HAL_BUSY, it's still working on the previous transaction. You don't show the code in which you use this which definitely has a bug. I can only help so much trying to debug through a keyhole. You don't mention HAL_I2C_Master_Receive_IT anywhere in your original post.

There are I2C IT examples you can follow in the CubeMX. Get those working first if you don't know what is working.

> Could it be the master Address or the It Priority? if so do you have any idea on what to change and why?

Change the part of your code with the bug in it. I don't know what part that is because you haven't shown it here.

If you feel a post has answered your question, please click "Accept as Solution".