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 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 the post_ISR, but posted by a task post_task, then OSA_SemaWait in loop could never get my_sem within the timeout because the post_task does not have a chance to post my_sem. In this situation, applications could be implemented as follows:
{
}
{
switch (status)
{
return;
break;
break;
break;
}
}
Wait the semaphore at the start of the task, if kStatus_OSA_Idle is received. Return and let other tasks run. Then, post_task has a chance to post my_sem.
The other method is using the function
OSA_PollAllOtherTasks(). This function calls all other tasks one at a time and then post_task can 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 flows like this: task_A -> OSA_PollAllOtherTasks -> task_B -> OSA_PollAllOtherTasks -> task_A -> ...
Bare Metal Mutex
Bare metal OSA implements mutex as a binary semaphore, which is different than the RTOS mutex. Applications could choose whether to use if for the bare metal.
Bare Metal Time management
Bare metal OSA implements two configurations for the time management. The first one uses the low power timer. The second one is empty. In other words, this configuration disables time management in bare metal OSA. To use a different configuration, set the macro FSL_OSA_BM_TIMER_CONFIG in the file fsl_os_abstraction_bm.h.
Time management with LPTMR
To use the low power timer in bare metal OSA, define the FSL_OSA_BM_TIMER_CONFIG as the FSL_OSA_BM_TIMER_LPTMER.
Bare metal OSA maintains a system time with the low power timer module. Applications can get the system time in milliseconds using the function
OSA_TimeGetMsec(). At the same time, all wait functions, such as
OSA_SemaWait() and
OSA_EventWait(), depend on the system time. Low power timer module is set up in the function
OSA_Init(). To use this timer function, ensure that the
OSA_Init() function is called. Note that the low power timer provides only 16-bit time count and wraps every 65536 ms. Use these three functions:
- OSA_TimeDelay() cannot delay longer than 65536 ms.
- Wait functions, such as OSA_SemaWait(), cannot set timeout longer than 65536 ms, however, OSA_WAIT_FOREVER is allowed.
- OSA_TimeGetMsec() wraps every 65536 ms; if it does not meet the requirement, use a different timer module in application.
Disable time management in bare metal OSA
To disable time management bare metal OSA, define the FSL_OSA_BM_TIMER_CONFIG as the FSL_OSA_BM_TIMER_NONE.
With this configuration, the LPTMR is not used by bare metal OSA and the footprint is smaller. Time services such OSA_TimeGetMsec and OSA_TimeDelay can't be used any more. At the same time, the wait functions such as
OSA_SemaWait() and
OSA_EventWait() can only use 0 or OSA_WAIT_FOREVER as the parameter timeout.
User-defined time management
When the LPTMR is occupied by other tasks and bare metal OSA is used to provide time services, bare metal OSA must use other timers for the time services. In this case, customizeS these functions defined in fsl_os_abstraction_bm.c:
There are two methods to customize these functions:
- Modify the functions in fsl_os_abstraction_bm.c directly.
- Define the functions in the application, which means that the functions in fsl_os_abstraction_bm.c are overridden.