Skip to main content
Associate II
December 27, 2024
Solved

osMessageQueuePut blocks indefinitely when used within an ISR

STM32CubeIDE 1.17.0, using FreeRTOS (CMSISRTOS-2) on a NUCLEO-F446RE board.  I am trying to call `osMessageQueuePut(...)` from an ISR (CAN RX0 Interrupt) and find my program hangs indefinitely at that call.

 

// Globals
const osMessageQueueAttr_t canMsgQueueAttr = {
 .name = "CanMsgQueue"
};
osMessageQueueId_t canMsgQueue = nullptr;

// Create the queue
canMsgQueue = osMessageQueueNew(10, 256, &canMsgQueueAttr);

// CAN RX0 callback
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
 char buf[128];
 snprintf((char*)buf, sizeof(buf), "Got message");
 osMessageQueuePut(canMsgQueue, buf, 0U, 0U); // Stuck forever
}

 

I'm following this documentation:

https://www.keil.com/pack/doc/CMSIS_Dev/RTOS2/html/group__CMSIS__RTOS__Message.html#gad90d4959466a7a65105061da8256ab9e

 

There is a note:
> May be called from Interrupt Service Routines if the parameter timeout is set to 0.

And that is what I have done.  Otherwise it returns `-4`, which is consistent with the documentation:
> osErrorParameter: parameter mq_id is NULL or invalid, non-zero timeout specified in an ISR.

 

Feels like I'm missing something very basic - With a value of `0` I would expect it to return instantly even if it failed.

This topic has been closed for replies.
drtarrBest answer
Associate II
December 27, 2024

My interrupt priority (4) was less than the max sys call (5).  Fix:

HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 5, 0);

2 replies

drtarrAuthor
Associate II
December 27, 2024

Screenshot from 2024-12-27 13-37-54.png

This is where its sticking.

drtarrAuthorBest answer
Associate II
December 27, 2024

My interrupt priority (4) was less than the max sys call (5).  Fix:

HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 5, 0);