Bug in hal/osal/os-less/ARMCMx/osal.c Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
Post Reply
wolf
Posts: 42
Joined: Mon Dec 31, 2012 1:55 pm
Been thanked: 9 times

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

Post 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
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

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

Post by Giovanni »

Hi,

Could you try to make it static volatile?

Giovanni
wolf
Posts: 42
Joined: Mon Dec 31, 2012 1:55 pm
Been thanked: 9 times

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

Post 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
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

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

Post 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
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

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

Post by Giovanni »

Hi,

Fixed in trunk and 21.11.x.

Giovanni
Post Reply