cancel
Showing results for 
Search instead for 
Did you mean: 

I2C communication in ISR

federico.massimi
Associate III

Hello everybody,

    I would need to use the HAL_I2C_Master_Transmit function in an ISR, in particular I would like to use it in the HAL_ADC_ConvCpltCallback function.

I have seen that the HAL_I2C_Master_Transmit function calls HAL_GetTick() so I set the interrupt priority of Tima Base: Syste tick timer to a higher priority than all the others interrupts, but the HAL_ADC_ConvCpltCallback function stops when HAL_I2C_Master_Transmit is called.

Can anyone help me?

This is my NVIC configuration:

0693W00000Ho96fQAB.png

5 REPLIES 5
KnarfB
Principal III

Calling a blocking HAL function from an ISR is in most cases not a good idea. Whats usually is done is delayed sending from the main loop where the ISR sets only some flag and saves the data, the main loop reads that data, sends it and clears that flag. In more complicated cases, a RTOS task maybe handy for that. You could also try sending the data with a non-blocking HAL function (_IT or _DMA).

PS: If you insist in the blocking function, explain what "function stops" means and investigate the tick values with a debugger etc..

hth

KnarfB

federico.massimi
Associate III

I tried to do the same test with a simple HAL_Delay () in the ISR routine. And it stops when the function HAL_GetTick() is called.

I've seen dozens of posts asking for the same issue, but they all say that just apply a higher priority to the SysTick interrupt to solve, I don't understand why it doesn't work in my case.

KnarfB
Principal III

How is the ADC triggered, could it be an ADC error? A little more code might help.

federico.massimi
Associate III

yes, ADC is triggered by TIM2.

Attach my entire main.c file, but the only add to the file generate from CubeMX is:

 HAL_ADC_Start_DMA(&hadc1, (uint32_t*) ADC_Buffer, ADC_CHANNEL_NUMBER);
 HAL_TIM_Base_Start(&htim2); // Start ADC timer

Before the main loop, and this ADC ISR:

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
	uint8_t buffer[4] = {0x00};
	HAL_I2C_Master_Receive(&hi2c1, 0xF4, buffer, 4, 0xFFF);
}

@KnarfB​ If useful i can upload the entire project.

> I've seen dozens of posts asking for the same issue, but they all say that just apply a higher priority to the SysTick interrupt to solve

As you see, at least half of advises is "just do not do blocking calls in ISRs". Delay to the main loop or a task.