2022-01-05 02:24 AM
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:
2022-01-05 07:20 AM
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.
2022-01-06 01:39 AM
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?
2022-01-06 01:42 AM
Could it be the master Address or the It Priority? if so do you have any idea on what to change and why?
2022-01-06 07:09 AM
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.