Keil Logo

GENERAL: CMSIS: Hard fault on HAL_GetTick() when using CMSIS RTOSv2


Information in this knowledgebase article applies to:

  • ARM.CMSIS.5.0.1.pack or earlier
  • CMSIS-RTOS API Version 2, v2.1.0 or earlier
  • STM32Cube

SYMPTOM

Migrating from earlier CMSIS RTOS RTX to RTXv5 and new CMSIS RTOSv2 API, my application uses an STM32 device with the CubeMX HAL layer. In CubeMX guides, a function called "HAL_GetTick()" is defined with the following code:

uint32_t HAL_GetTick(void) {
  return os_time;
}

In new RTOS, the variable "os_time" is no longer available. I see a new function called "osKernelGetTickCount()". When I try the following code, I receive a hard fault escalated from a usage fault, INVSTATE, with the RTOS library:

uint32_t HAL_GetTick (void) {
    return ((uint32_t)osKernelGetTickCount ());
}

Is there a better way to pass the tick value into the HAL layer?

CAUSE

With the above code, osKernelGetTickCount() can be called from an interrupt service routine(ISR), which was not allowed.

RESOLUTION

There are two resolutions.

Upgrade to CMSIS-RTOS API Version 2, v2.1.1 or later. This API can be found in ARM.CMSIS.5.1.0.pack or later. Newer versions of the RTOS allow osKernelGetTickCount() to be called from an ISR.

Otherwise, use the following implementation of HAL_GetTick():

#include "rtx_os.h"

extern osRtxInfo_t osRtxInfo;

uint32_t HAL_GetTick (void) {
  return ((uint32_t)osRtxInfo.kernel.tick);
}

This will avoid the fault. The code casts an internal uint64_t value as type uint32_t, and the value safely passes into the HAL layer.

The #include above allows the compiler to recognize the struct, osRtxInfo_t, in any source file, which has not yet included "cmsis_os.h".

MORE INFORMATION

Last Reviewed: Monday, October 2, 2017


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.