cancel
Showing results for 
Search instead for 
Did you mean: 

I2C by IT [solved]

MMath.10
Associate II

Hi,

I use I2C like this

uint16_t I2cAddrTemp = 0x90;
uint8_t I2cWritedata = 0x00;
uint8_t I2cReaddata = 0xFF;
 
uint8_t read_i2c_temp(void)
{
	hi2c1_status = HAL_I2C_Master_Transmit(&hi2c1, I2cAddrTemp, &I2cWritedata, 1, 1000);
	//PRINTF("I2c = %02X",hi2c1_status);
	hi2c1_status = HAL_I2C_Master_Receive(&hi2c1, I2cAddrTemp, &I2cReaddata, 1, 1000);
	if (hi2c1_status == HAL_OK)
	{
		PRINTF("I2C temp = %d\n\r",(int8_t)I2cReaddata);
	}
	else
	{
		PRINTF("I2C ERROR\n\r",hi2c1_status);
	}
	return 0;
}

And i want to use IT instead

So i change for that :

uint16_t I2cAddrTemp = 0x90;
uint8_t I2cWritedata = 0x00;
uint8_t I2cReaddata = 0xFF;
 
void read_i2c_temp(void)
{
	HAL_I2C_Master_Transmit_IT(&hi2c1, I2cAddrTemp, &I2cWritedata, 1);
}
 
 
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	PRINTF("I2C temp TX OK\n\r");
	HAL_I2C_Master_Receive_IT(&hi2c1, I2cAddrTemp, &I2cReaddata, 1);
}
 
 
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	PRINTF("I2C temp = %d\n\r",(int8_t)I2cReaddata);
}

I had this lign in the HAL_I2C_MspInit function

HAL_NVIC_EnableIRQ(I2C1_IRQn);

And this one in HAL_I2C_MspDeInit

HAL_NVIC_DisableIRQ(I2C1_IRQn);

But I2C freeze in this configuration. Someone found a mistake?

EDIT 1 : not only I2C everything freeze.

I have 2 USART on my project.

One use DMA, the other is in direct mode.

EDIT 2 :

I found the solution

Had this code somewhere (me in mlm32l0xx_it.c)

in the .c
extern I2C_HandleTypeDef hi2c1;
 
/**
  * @brief This function handles I2C1 event global interrupt / I2C1 wake-up interrupt through EXTI line 23.
  */
void I2C1_IRQHandler(void)
{
  /* USER CODE BEGIN I2C1_IRQn 0 */
 
  /* USER CODE END I2C1_IRQn 0 */
  if (hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) {
    HAL_I2C_ER_IRQHandler(&hi2c1);
  } else {
    HAL_I2C_EV_IRQHandler(&hi2c1);
  }
  /* USER CODE BEGIN I2C1_IRQn 1 */
 
  /* USER CODE END I2C1_IRQn 1 */
}
 
 
 don't forget the .h
void I2C1_IRQHandler(void);

And i change the priority in HAL_I2C_MspInit

 
 HAL_NVIC_SetPriority(I2C1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(I2C1_IRQn);

0 REPLIES 0