The Kinetis SDK provides the Bare Metal Abstraction Layer for synchronization, mutual exclusion, message queue, etc.
More...
Overview
When RTOSes are not used, bare metal abstraction layer provides semaphore, mutex, event, message queue and so on. Because bare metal does not have a task scheduler, it is necessary to exercise caution while using bare metal abstraction layer.
Bare Metal's Task Management
By contrast to RTOSes, bare metal abstraction layer uses a poll mechanism to simulate a task. All task functions are linked into a list and called one by one. Therefore, bare metal task function should not contains an infinite loop. It must return at a proper time to let the other tasks run.Bare metal task does not support priority, all tasks use the same priority. The macro TASK_MAX_NUM defines how many tasks applications could create. If it is set to 0, then applications could not use task APIs.
Bare Metal's Wait Functions
Bare metal wait functions, such as OSA_SemaWait and OSA_EventWait, return the kStatus_OSA_Idle if wait condition is not met and timeout has not occurred. Applications should catch this value and take proper actions. If the wait condition is set by the ISR, applications could wait in a loop:
void post_ISR(void)
{
}
{
do
{
}
In this example, if my_sem is not posted by post_ISR, but posted by a task post_task, then OSA_SemaWait in loop could never get my_sem within timeout, because post_task does not have chance to post my_sem. In this situation, applications could be implemented like this:
{
}
{
switch (status)
{
return;
break;
break;
break;
}
}
Wait the semaphore at the start of the task, if kStatus_OSA_Idle is got, return and let other task run, then post_task has chance to post my_sem.
The other method is using function
OSA_PollAllOtherTasks(). This function calls all other tasks one time, then post_task could post my_sem.
The limitation of this method is that only one task can use the
OSA_PollAllOtherTasks() function. If both task_A and task_B call this function, then the stack overflow may occur, because the call stack is like this: task_A -> OSA_PollAllOtherTasks -> task_B -> OSA_PollAllOtherTasks -> task_A -> ...
Bare Metal Mutex
Bare matal OSA implements mutex as a binary semaphore, this is different from RTOSes mutex. Applications could choose to use it or not for bare metal.
Bare Metal Time management
Bare metal OSA implements two configurations for time management. The first one is using lowpower timer. The second one is empty, in other words, this configuration disables time management in bare metal OSA. To use different configurations, please set the macro FSL_OSA_BM_TIMER_CONFIG in file fsl_os_abstraction_bm.h.
Time management with LPTMR
To use lowpower timer in bare metal OSA, please define FSL_OSA_BM_TIMER_CONFIG as FSL_OSA_BM_TIMER_LPTMER.
Bare metal OSA maintains a system time by the lowpower timer module. Applications can get system time in milliseconds using the function
OSA_TimeGetMsec(). At the same time, all wait functions such as
OSA_SemaWait(),
OSA_EventWait() depend on this system time. Lowpower timer module is set up in the function
OSA_Init(). To use this time function, ensure that the
OSA_Init() function is called. Please note that lowpower timer provides only 16-bit time count, it wraps every 65536ms. So take care to use these three kinds of functions:
- OSA_TimeDelay() cannot delay longer than 65536ms.
- Wait functions, such as OSA_SemaWait(), cannot set timeout longer than 65536ms, however, OSA_WAIT_FOREVER is allowed.
- OSA_TimeGetMsec() wraps every 65536ms, if it does not meet the requirement, please implement use other timer module in application.
Disable time management in BM OSA
To disable time management bare metal OSA, please define FSL_OSA_BM_TIMER_CONFIG as FSL_OSA_BM_TIMER_NONE.
With this configuration, LPTMR is not used by BM OSA, then the footprint is smaller. Time services such OSA_TimeGetMsec, OSA_TimeDelay could not be used any more. At the same time, the wait functions such as
OSA_SemaWait(),
OSA_EventWait() could only use 0 or OSA_WAIT_FOREVER as the parameter timeout.
User defined time management
Sometimes the LPTMR should be used for the other purpose, and also BM OSA should provide time services. In this situation, BM OSA must use other timers for the time services, please re-write these functions defined in fsl_os_abstraction_bm.c:
void OSA_TimeInit(void);
uint32_t OSA_TimeDiff(uint32_t time_start, uint32_t time_end);
There are two methods to re-write these functions:
- Modify the functions in fsl_os_abstraction_bm.c directly.
- Define the functions in application, then the functions in fsl_os_abstraction_bm.c will be overridden.