cancel
Showing results for 
Search instead for 
Did you mean: 

[Bug] CMSIS-RTOS V2 has incorrect implementation of osFlagsWaitAll option for osEventFlagsWait()

fronders
Senior

Outlined the bug on github page for L4 firmware pack: https://github.com/STMicroelectronics/STM32CubeL4/issues/7

Thought someone might find this useful until its fixed. Details below.

The set-up

 * Custom board using STM32L452CCUx MCU

 * STM32CubeIDE v1.3.1 under Windows 10 x64 using STM32CubeL4 pack v1.15.1

 * FreeRTOS v10.2.1 with CMSIS-RTOS v2.00

Description of the bug

Incorrect implementation of `osFlagsWaitAll` option for `osEventFlagsWait()` is causing incorrect code functioning. When using `osEventFlagsWait()` function with `osFlagsWaitAll` I expect that the thread calling gets unblocked when *all* of the *specified* flags are set. However if any other flags (additional to specified) are also set - `osEventFlagsWait()` returns with `osErrorTimeout`. This happens even with `osWaitForever` timeout, which is nonsense.

Problem lies in the ST's wrapper around FreeRTOS function `xEventGroupWaitBits()`. If more flags then specified are set - the function will report error, which shouldn't happen.

Specifically this check on cmsis_os2.c line 1179 is causing the bug. Here is the piece of code around those lines:

  rflags = xEventGroupWaitBits (hEventGroup, (EventBits_t)flags, exit_clr, wait_all, (TickType_t)timeout);
  if (options & osFlagsWaitAll) {
   if (flags != rflags) {  // <--------- THIS IS A BUG
    if (timeout > 0U) {
     rflags = (uint32_t)osErrorTimeout;
    } else {
     rflags = (uint32_t)osErrorResource;
    }
   }
  }
  else {
   if ((flags & rflags) == 0U) {
    if (timeout > 0U) {
     rflags = (uint32_t)osErrorTimeout;
    } else {
     rflags = (uint32_t)osErrorResource;
    }
   }
  }

Proposed solution

Replace the buggy condition check:

if (flags != rflags) {  // <--------- THIS IS A BUG
  // ...
}

with a correct one:

if ((flags & rflags) != flags) {  // <--------- THIS IS CORRECT IMPLEMENTATION
  // ...
}

--

Same problem is observed with STM32CubeF4 v1.25.0 pack and many others since they contain CMSIS-RTOS v2.00 Middleware implementation as well.

2 REPLIES 2
Imen.D
ST Employee

Thank you @fronders​  for sharing the issue with proposed solution. This may be helpful for other Community users.

As mentioned in the Github, this will be fixed in the next release

Best Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
GMG
Associate III

I'm using STM32H7 with

STM32CubeH7 Firmware Package V1.9.0 / 12-February-2021

using osEventFlagsWait with osFlagsWaitAny and timout 0 returns always error -3

flags = osEventFlagsWait(AdcReadyEventHandle, 0x000003U, osFlagsWaitAny, 0);

using

flags = osEventFlagsWait(AdcReadyEventHandle, 0x000003U, osFlagsWaitAny, 1);

all is working correctly

with timeout 0 and osFlagsWaitAny if any flag bit is set always function returns with error -3