Page 1 of 1

Bug in hal/osal/os-less/ARMCMx/osal.c

Posted: Thu Feb 12, 2026 1:43 pm
by wolf
STM32F4-DIscovery MB997C
chibios-code-r17689-branches-stable_21.11.x

Hi all!

I tried to run the demo HAL-STM32F407-DISCOVERY and found that it gets stuck upon the second call to osalThreadSleepMilliseconds(500) in main. The very first call works OK.

Code: Select all

  while (true) {
    chnWriteTimeout(&SD2, (uint8_t *)"Hello World!\r\n", 14, TIME_INFINITE);

    palSetPad(GPIOD, GPIOD_LED3);       /* Orange.  */
    osalThreadSleepMilliseconds(500);
    palClearPad(GPIOD, GPIOD_LED3);     /* Orange.  */
    osalThreadSleepMilliseconds(500);
  }


Debugging into, i foud that the program circles in void vtSetI(virtual_timer_t *vtp,....

A quick check of the same demo using branch ChibiOS_20.3.4 shows it working as expected.

A diff of osal.c of both branches shows one single difference in function msg_t osalThreadSuspendS(thread_reference_t *trp)

In chibios_stable-21.11.x (not working):

Code: Select all

msg_t osalThreadSuspendS(thread_reference_t *trp) {
  static thread_t self = {MSG_WAIT};

  osalDbgCheck(trp != NULL);

  *trp = &self;
  while (self.message == MSG_WAIT) {
    osalSysUnlock();
    /* A state-changing interrupt could occur here and cause the loop to
       terminate, an hook macro is executed while waiting.*/
    OSAL_IDLE_HOOK();
    osalSysLock();
  }

  return self.message;
}


In ChibiOS_20.3.4 (working OK):

Code: Select all

msg_t osalThreadSuspendS(thread_reference_t *trp) {
  thread_t self = {MSG_WAIT};

  osalDbgCheck(trp != NULL);

  *trp = &self;
  while (self.message == MSG_WAIT) {
    osalSysUnlock();
    /* A state-changing interrupt could occur here and cause the loop to
       terminate, an hook macro is executed while waiting.*/
    OSAL_IDLE_HOOK();
    osalSysLock();
  }

  return self.message;
}


So declaring thread_t self = {MSG_WAIT} static seems to be the problem.
Removing static from the declaration makes the demo run OK in trunk.

Best regards,

Wolf

Re: Bug in hal/osal/os-less/ARMCMx/osal.c

Posted: Thu Feb 12, 2026 3:00 pm
by Giovanni
Hi,

Could you try to make it static volatile?

Giovanni

Re: Bug in hal/osal/os-less/ARMCMx/osal.c

Posted: Thu Feb 12, 2026 4:23 pm
by wolf
Hi,

I get a compiler warning when doing so:

Code: Select all

Compiling osal.c
../../chibios_trunk/os/hal/osal/os-less/ARMCMx/osal.c: In function 'osalThreadSuspendS':
../../chibios_trunk/os/hal/osal/os-less/ARMCMx/osal.c:208:8: warning: assignment discards 'volatile' qualifier from pointer target type [-Wdiscarded-qualifiers]
  208 |   *trp = &self;
      |        ^
Compiling osal_vt.c


And it doesn't work as expected, just gets stuck as decribed before.

Regards Wolf

Re: Bug in hal/osal/os-less/ARMCMx/osal.c

Posted: Thu Feb 12, 2026 5:27 pm
by Giovanni
Ok, it is because the static variable is initialized once not each time.

Will be fixed in next release, removing "static" is the correct thing to do.

Giovanni

Re: Bug in hal/osal/os-less/ARMCMx/osal.c

Posted: Sun Feb 15, 2026 11:27 am
by Giovanni
Hi,

Fixed in trunk and 21.11.x.

Giovanni