# FreeRTOS and interrupts

**URL:** <https://forums.freertos.org/t/freertos-and-interrupts/18013>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [August 18, 2023, 3:39pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013 "2023-08-18T15:39:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tarmogr](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tarmogr](https://forums.freertos.org/u/tarmogr)\
**Post date:** [August 18, 2023, 3:39pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/1 "2023-08-18T15:39:42Z")

</div>

I have a function that reads data from CANBus that works ok without RTOS but as soon as it is activated the interrupt routine is never entered again. I have a general timer interrupt that works ok. Any ideas?  
STM32F412/STMCubeIDE  
I have RX0 interrupt enabeled. Here is the callback for CAN Rx:

```auto
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan1)
 {
	//GPIOC->ODR ^= (1<<13);
 	HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO0, &RxHeader, RxMailbox); //Receive CAN bus message to RxMailbox buffer
 	CANFlag = 1;
 }

```

Here is the CAN wait loop that will never be exited, it is called from a task:

```auto
  
.....

  uint32_t tickstart = HAL_GetTick(); // Setup timeout for waiting reply
  uint32_t wait = 100;
  uint8_t flag = 0;

  while((HAL_GetTick() - tickstart) < wait){

	  osDelay(1);

	 __disable_irq(); // Read receive flag
	  flag = CANFlag;

	  if (flag ==1 && RxMailbox[0] == Tx[0] && RxMailbox[1] == Tx[1] ){ // We got reply, check if the reply is for the same command

              // DATA HANDLING///
		__enable_irq();

		HAL_Delay(PACKETMARGIN);
		return flag;
	  } else {
		 __enable_irq();
		  flag = 0;
		  {}	//nop
	  }
  }
  return flag;	// Timeout, no reply

```

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [August 18, 2023, 5:42pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/2 "2023-08-18T17:42:59Z")

</div>

How are the variables tickStart and wait used? Could it be that the while loop never rubs after first invocation?

---

<div class="post-metadata">

**Author:** ![tarmogr](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tarmogr](https://forums.freertos.org/u/tarmogr)\
**Post date:** [August 18, 2023, 6:25pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/3 "2023-08-18T18:25:48Z")

</div>

I updated the code to include the definitions. Indeed it also seems this loop never exits like it is supposed to when 100ms is up.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [August 18, 2023, 6:55pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/4 "2023-08-18T18:55:44Z")

</div>

well, now you have the problem, it should be easy to nail down… may be an optimization issue. Or a problem with the HAL timer not incrementing, eg due to disabled interrupts. Do not use the HAL, it does nothing but cause headaches.

Why do you use both osdelay and HAL\_Delay in the same function, btw?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [August 18, 2023, 7:50pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/5 "2023-08-18T19:50:18Z")

</div>

CANFlag is `volatile`, right ? Otherwise the code is broken and may or may not work.  
Why not starting with a working FreeRTOS based demo and migrate your code into it ?  
Could be a more successful approach getting more familiar with FreeRTOS applications.

---

<div class="post-metadata">

**Author:** ![skptak](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/skptak/32/3981_2.png) [@skptak](https://forums.freertos.org/u/skptak)\
**Post date:** [August 19, 2023, 2:52am UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/6 "2023-08-19T02:52:54Z")

</div>

Hey @tarmogr, sorry to hear that you’re having some issues with not exiting your interrupt callback.  
I’ve got some questions about the code that you’ve shared  
Is the `HAL_CAN_RxFifio0MsgPendingCallback()` being correctly called from the IRQ itself? Or do you have an IRQ handler that is calling it? And have you verified if you’re correctly returning back to your task after entering that callback?

I know that the [STM32F412 Discovery kit](https://circuitpython.org/board/stm32f412zg_discovery/) integrates a [Cortex-M4](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/V10.6.1/portable/GCC/ARM_CM4F), where is your project using the FreeRTOS port for that chip? If it is a first thought looking at this would be that the `HAL_GetTick()` might not be compatible with the [FreeRTOS Tick Handler](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.6.1/portable/GCC/ARM_CM4F/port.c#L517)? If you are you probably should be using [vTaskDelay()](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L2119) instead of `osDelay()`. And should also be using either [taskENTER/EXIT\_Critical()](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/include/task.h#L226) to enable and disable IRQs instead of the raw calls.

As @hs2 pointed out you may need to set `CANFlag`, and `flag` to volatile to ensure that they get updated correctly on use.

Additionally, how large is the `PACKETMARGIN` delay value? Could you potentially be delaying for too long in there? And what is the difference between `HAL_Delay()` and `osDelay()`? Lastly, is `osDelay()` supposed to only be delaying for a single system tick? Or is it a longer operation?

---

<div class="post-metadata">

**Author:** ![tarmogr](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tarmogr](https://forums.freertos.org/u/tarmogr)\
**Post date:** [August 19, 2023, 10:13am UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/7 "2023-08-19T10:13:22Z")

</div>

> [@skptak](#):
>
> Is the `HAL_CAN_RxFifio0MsgPendingCallback()` being correctly called from the IRQ itself? Or do you have an IRQ handler that is calling it? And have you verified if you’re correctly returning back to your task after entering that callback?

Thr HAL\_CAN\_RxFifio0MsgPendingCallback() is supposed to be called by the code that is generated by STMCubeMX. And indeed it is called without using OS. I have verified that this function is actually never called when using OS.

> [@skptak](#):
>
> As @hs2 pointed out you may need to set `CANFlag`, and `flag` to volatile to ensure that they get updated correctly on use.

I will do that but it seems the code never gets to set the flag in the first place.

> [@skptak](#):
>
> Additionally, how large is the `PACKETMARGIN` delay value? Could you potentially be delaying for too long in there? And what is the difference between `HAL_Delay()` and `osDelay()`? Lastly, is `osDelay()` supposed to only be delaying for a single system tick? Or is it a longer operation?

Packetmargin is about 20ms. HAL\_Delay() seems to be working fine inside task. Bear in mind this code is migrated from non OS project so regarding HAL\_Delay() yes I need to replace them all eventually. The reason I used osDelay() is that to my understanding this function notifies OS so that it can exit the task if needed to do other things, including servicing the interrupt(?). osDelay(1) is 1 millisecond delay.

One sidenote. Initially the HAL\_GetTick did not increment at all, whenever HAL\_Dealy was called the code got stuck. I increased the system tick priority from 15 to 0 and it started incrementing. But the problems stated above still exist.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [August 19, 2023, 12:01pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/8 "2023-08-19T12:01:05Z")

</div>

There is a fundamental tension between FreeRTOS’s tick and the HAL tick. FreeRTOS wants its tic to be low priority (high value), Ike 15, while HAL wants a high priority tic, as some of their ISR routine wait for it to happen. If you must use both, you need to move one of them off of the systick, and make them use different interrupts.

Prioritu 0 is a BAD priority to use, as it has special meaning, use priority 1 at most.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 21, 2023, 7:08am UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/9 "2023-08-21T07:08:07Z")

</div>

> [@tarmogr](#):
>
> One sidenote. Initially the HAL\_GetTick did not increment at all, whenever HAL\_Dealy was called the code got stuck. I increased the system tick priority from 15 to 0 and it started incrementing. But the problems stated above still exist.

Change the HAL tick to some other timer than SysTick. You can do that in the GUI by going to “System Core → Sys → Timeabase Source”:

 ![timbase](https://d2v6vdsk2p900z.cloudfront.net/original/2X/9/970dc3706920ccd9141ad0bcc75ad7b17936311c.png)

---

<div class="post-metadata">

**Author:** ![skptak](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/skptak/32/3981_2.png) [@skptak](https://forums.freertos.org/u/skptak)\
**Post date:** [September 5, 2023, 6:16pm UTC](https://forums.freertos.org/t/freertos-and-interrupts/18013/10 "2023-09-05T18:16:19Z")

</div>

Hey @tarmogr, wanted to ask if you’re still seeing issues with this or if you’ve found a solution to your problem?
