Common interface functions of micro-embedded real-time operating system SmallRTOS
Micro-embedded real-time operating system SmallRTOS is a micro-kernel real-time operating system with open source code, which is easy to transplant and oriented to deep embedded applications. Its main application fields include industrial control and intelligent sensor development, smart terminals, Iot, etc. Anyone who complies with the SmallRTOS license agreement can use the embedded real-time operating system for free. Release site for the latest source code and sample projects: http://www.smallrtos.org
Download the source code package to SmallRTOS and decompress it. The directory structure of SmallRTOS is as follows:
Kernel: stores the Kernel File OS of SmallRTOS and CPU-related porting files;
Demo: stores sample files provided by SmallRTOS;
Doc: stores SmallRTOS instructions/tutorials;
License: used to store SmallRTOS;
During the design of the micro-embedded real-time operating system SmallRTOS, the file name, function name, and variable name are differentiated by the dedicated Prefix: OS prefix, which is represented as the SmallRTOS kernel, these are platform-independent kernel parts, which do not need to be changed during cross-platform transplantation; the prefix is Fit, which indicates the parts related to hardware (chip type, etc.). During transplantation, this part of files, functions, and variables should be adjusted according to the hardware platform (chip type, etc;
The micro-embedded real-time operating system SmallRTOS is a multi-task preemptible operating system. The high-priority tasks can be executed first, reflecting the real-time performance of the operating system. In the SmallRTOS system, priority 0 is the lowest priority, which is the reserved priority of the SmallRTOS system. As the dedicated priority of the idle task OSIdleTask, tasks created by the user cannot be used. In addition, the priority can be assigned based on the importance of the task. Tasks with higher priority will be executed first.
In order to highlight the flexibility of embedded operating system configuration, a comprehensive consideration was made at the beginning of the design of the SmallRTOS system. Some parameters were configured using macro-defined methods. Each task has its own name and priority. The maximum Task Name Is OSNAME_MAX_LEN. This variable is a macro definition. Names that exceed the maximum length are automatically discarded. The default value is 10 characters. The task priority is defined as OSTASK_MAX_PRIORITY, which is a very important parameter. The default value is 10.
Ticks is often referred to as the clock tick, which is the smallest time unit in the SmallRTOS system. This parameter can be set based on the performance of the hardware platform. In the SmallRTOS system, this parameter is configured using the macro-defined configTICK_RATE_HZ. In most of the sample projects provided by SmallRTOS, 1000Hz is configured, that is, the interval between every clock tick is 1 millisecond. This parameter affects the control precision of the SmallRTOS system on the task. After the system scheduler is started, it will be executed according to the priority of the task until the task is given the execution right or is snatched by a more advanced task. If no qualified task needs to be executed, the OSIdleTask reserved in the system is run ).
In the SmallRTOS system, in addition to macro-defined parameters, function modules also use macro-defined configuration. The macro definition OS _SEMAPHORE_ON indicates whether to enable the Semaphore (Semaphore, also known as the flag) function module. If it is defined as 1, it indicates that the Semaphore (also called the flag) function module is enabled, if it is defined as 0, this function module is not enabled; macro OS _MSGQ_ON indicates whether to enable Message Queue; usage: OS _SEMAPHORE_ON; macro definition: OS _MUTEX_ON indicates whether to enable mutex semaphores; usage: OS _SEMAPHORE_ON;
The following are frequently used interface functions in the micro-embedded real-time operating system SmallRTOS for your reference;
I. Task-related API functions in the system
OSTaskHandle_t OSTaskCreate (OSTaskFunction_t pxTaskFunction,
Void * pvParameter,
Const uOS16_t usStackDepth,
UOSBase_t uxPriority,
SOS8_t * pcTaskName );
OSTaskCreate creates a function for the SmallRTOS task of the micro-embedded real-time operating system. The parameter OSTaskFunction_t pxTaskFunction is a task function, which is defined as void TaskFunction (void * pParameters ); the void * pParameters parameter in the task function is the second parameter of OSTaskCreate; the third parameter is the stack space usStackDepth of the task, and the stack space needs to be adjusted according to the space occupied by the task; the fourth parameter is the priority of the task. It can be used except that the lowest priority 0 is reserved by the system. The fifth parameter is the task name, which is the tag of the task. It is mainly used to facilitate the debugging of tasks in different time zones.
The Return Value of the function OSTaskCreate is a task handle of the OSTaskHandle_t type. This handle can be called by other system functions to set or control the task status;
Void OSTaskSleep (uOS32_t uxWatiTicks );
OSTaskSleep is a function used to set the Execution Delay of tasks in the micro-embedded real-time operating system SmallRTOS. This function can sleep the current task for several milliseconds. The uOS32_t uxWatiTicks parameter indicates the sleep duration, measured in Ticks. You can use OSTICKS_PER_MS to convert milliseconds to Ticks counts;
Void OSTaskYield ()
The OSTaskYield function is a task control function in SmallRTOS. It is called in a task to give the execution right of the current task and switch to the next task in the eTaskStateReady state. The OSTaskYield function does not change the status of the task, only the tasks currently being executed are arranged at the end of the task queue in eTaskStateReady state. If only the current task is in eTaskStateReady state, the current task is still executed;
UOS16_t OSStart (void)
OSStart is the macro definition of the OSStartScheduler () function. OSStartScheduler () is the task scheduling start function in SmallRTOS. In this function, the system sets the idle task OSIdleTask and the clock interruption; OSIdleTask is a system idle task. if the system does not have a task to be executed, the system calls this idle task. The idle task can be used to count the utilization rate of the current system, and release the resources of the tasks in the pending deletion State. If the clock is interrupted, the ticks configuration of the system is used. The running of the entire system depends on the ticks driver;
Ii. API functions related to task synchronization semaphores
OSSemHandle_t OSSemCreate ()
The OSSemCreate function creates a function for semaphores (Semaphore) to create semaphores for synchronization between tasks. After a semaphore is created, the default valid semaphore Count value is 0, indicating that the semaphore does not have a valid signal. The corresponding OSSemPend function is in the blocking status and waits for the valid signal. The return value is a handle of the OSSemHandle_t type, this facilitates the control of the semaphore;
SOSBase_t OSSemPend (OSSemHandle_t SemHandle, uOSTick_t xTicksToWait)
The OSSemPend function is a semaphores waiting function called in the task execution function. It is used to wait for the semaphores of related synchronization. The OSSemHandle_t SemHandle parameter is the semaphores handle, and the uOSTick_t xTicksToWait parameter indicates, the Unit is the number of Tick. If it is set to OSPEND_FORVER_VALUE, it will be blocked forever until the specified semaphore SemHandle gets a valid signal;
SOSBase_t OSSemPost (OSSemHandle_t SemHandle)
The OSSemPost function is used to send a valid signal to a specified semaphore, so that the task waiting for the semaphore obtains the synchronous signal for resuming execution. Note: This function cannot be called in the interrupt service function.
SOSBase_t OSSemPostFromISR (OSSemHandle_t SemHandle)
The OSSemPostFromISR function is used to send a valid signal to a specified semaphore, so that the task waiting for the semaphore obtains the synchronous signal for resuming execution. Note: This function can only be called in the interrupt service function.
Iii. API functions related to task synchronization messages
OSMsgQHandle_t OSMsgQCreate (const uOSBase_t uxQueueLength, const uOSBase_t uxItemSize)
The OSMsgQCreate function is used to create a message queue for synchronization between tasks. The uxQueueLength parameter is the capacity of the number of messages in the Message Queue (the number of messages exceeds this capacity, the sending task is suspended until the message queue is idle. The second parameter uxItemSize is the length of a single message. The return value is a message queue handle of the OSMsgQHandle_t type to facilitate message queue control;
SOSBase_t OSMsgQReceive (OSMsgQHandle_t MsgQHandle, void * const pvBuffer, uOSTick_t xTicksToWait)
The OSMsgQReceive function is used to receive messages from a specified message queue in a task and called in the task execution function. This function is a task blocking function. The MsgQHandle parameter is the message queue handle, and the pvBuffer parameter is the message pointer. The xTicksToWait parameter is the waiting time of the message queue, measured in Tick. If it is set to OSPEND_FORVER_VALUE, it will always wait, until MsgQHandle of the specified message queue gets a valid message. The Return Value of the function indicates the message receiving status. If it is FALSE, no valid message is received. If it is TRUE, a valid message is received;
SOSBase_t OSMsgQReceiveFromISR (OSMsgQHandle_t MsgQHandle, void * const pvBuffer)
The OSMsgQReceiveFromISR function is used to receive messages from a specified message queue in the interrupt function. The MsgQHandle parameter is the message queue handle, and the pvBuffer parameter indicates the message pointer. This function is not blocked. The Return Value of the function indicates the message receiving status. If it is FALSE, no valid message is received. If it is TRUE, a valid message is received. Note: This function can only be called in the interrupt service function.
SOSBase_t OSMsgQSend (OSMsgQHandle_t MsgQHandle, const void * const pvItemToQueue, uOSTick_t xTicksToWait)
The OSMsgQSend function is used to send messages to a specified message queue, so that the task waiting for the message gets the synchronous message and resumes execution. The MsgQHandle parameter indicates the message queue, the pvItemToQueue parameter indicates the Message Address (pointer). The xTicksToWait parameter indicates the waiting time for message sending and receiving. The unit is Tick. If it is set to OSPEND_FORVER_VALUE, it will always wait, until the specified message queue MsgQHandle is idle. The Return Value of the function indicates the message sending status. If it is FALSE, the message fails to be sent. If it is TRUE, the message is successfully sent. Note: This function cannot be called in the interrupt service function.
SOSBase_t OSMsgQSendFromISR (OSMsgQHandle_t MsgQHandle, const void * const pvItemToQueue)
The OSMsgQSendFromISR function is used to send messages to a specified message queue, so that the task waiting for the message gets the synchronous message and resumes execution. The MsgQHandle parameter indicates the message queue, the pvItemToQueue parameter indicates the Message Address (pointer). If the message queue is full, the function will not block and the message sending failure information will be returned directly. Note: This function can only be called in the interrupt service function.
Iv. Timer-related API functions
OSTimerHandle_t OSTimerCreate (OSTimerFunction_t Function, void * pvParameter, uOSBase_t uxPeriodicTimeMS, sOS8_t * pcName)
The interface function OSTimerCreate is used to create a timer. The Parameter Function is a timer service Function used to respond to the timer. The OSTimerFunction_t Function is defined as void TimerFunction (void * pParameters ). PvParameter is the parameter of the timer service function. It can be set to NULL when not in use. The uxPeriodicTimeMS parameter is the timer cycle, in milliseconds. The pcName parameter is the name of the timer to distinguish between different timers. Note: The timer service function prohibits the addition of semaphores waiting, message queue waiting, and other functions for blocking, in order not to affect the performance of the entire system, the less time consumed by the timer service function, the better.
UOSBase_t OSTimerStart (OSTimerHandle_t const TimerHandle)
The timer will not be automatically started after it is created. You need to call the startup function OSTimerStart () shown by the user before the timer takes effect. The TimerHandle parameter is the timer handle and the return value of the OSTimerCreate () function for the timer;
UOSBase_t OSTimerStop (OSTimerHandle_t const TimerHandle)
After scheduled start, you can use the interface function OSTimerStop () to stop the scheduled task. The TimerHandle parameter is the timer handle and the return value of the OSTimerCreate () function for the timer;
Simple example Program
The following is a demo of multi-task processing using the real-time embedded operating system SmallRTOS, which mainly includes task creation, semaphore creation, task latency, and other functions. The Code is as follows:
OSTaskHandle_t HWTaskHandle = NULL;
OSTaskHandle_t GBTaskHandle = NULL;
OSSemHandle_t GBSemaphoreHandle = NULL;
Void TaskHelloWorld (void * pvParameters );
Void TaskGoodBye (void * pvParameters );
Int main (void)
{
GBSemaphoreHandle = OSSemCreate ();
// Configure and start tasks
HWTaskHandle = OSTaskCreate (TaskHelloWorld, NULL, OSMINIMAL_STACK_SIZE, OSLOWEAST_PRIORITY + 1, "HW ");
GBTaskHandle = OSTaskCreate (TaskGoodBye, NULL, OSMINIMAL_STACK_SIZE, OSLOWEAST_PRIORITY + 2, "GB ");
/* Start the tasks and timer running .*/
OSStart ();
For (;;);
Return 0;
}
Void TaskHelloWorld (void * pvParameters)
{
/* Remove compiler warning about unused parameter .*/
(Void) pvParameters;
For (;;)
{
OSSemPost (GBSemaphoreHandle );
OSTaskSleep (200 x OSTICKS_PER_MS );
}
}
Void TaskGoodBye (void * pvParameters)
{
/* Remove compiler warning about unused parameter .*/
(Void) pvParameters;
For (;;)
{
OSSemPend (GBSemaphoreHandle, OSPEND_FOREVER_VALUE );
}
}
This article permanently updates the link address: