描述
内核信息和控制功能组允许:
- 获取有关系统和底层内核的信息。
- 获取有关CMSIS-RTOS API的版本信息。
- 用于创建对象的RTOS内核的初始化。
- 启动RTOS内核和线程切换。
- 检查RTOS内核的执行状态。
注意:
不能从中断服务例程中调用内核信息和控制功能。
RTX5的内核初始化记录在系统启动中。
代码示例:
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
void app_main (void *argument) {
// ...
for (;;) {}
}
int main (void) {
// System Initialization
SystemCoreClockUpdate();
// ...
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(app_main, NULL, NULL); // Create application main thread
osKernelStart(); // Start thread execution
for (;;) {}
}
/// Version information.
typedef struct {
uint32_t api; ///< API version (major.minor.rev: mmnnnrrrr dec).
uint32_t kernel; ///< Kernel version (major.minor.rev: mmnnnrrrr dec).
} osVersion_t;
标识基础的RTOS内核和API版本号。版本以组合的十进制数字表示,格式为:major.minor.rev:mmnnnrrrr
使用osKernelGetInfo检索版本号。
/// Kernel state.
typedef enum {
osKernelInactive = 0, ///< Inactive.
osKernelReady = 1, ///< Ready.
osKernelRunning = 2, ///< Running.
osKernelLocked = 3, ///< Locked.
osKernelSuspended = 4, ///< Suspended.
osKernelError = -1, ///< Error.
osKernelReserved = 0x7FFFFFFFU ///< Prevents enum down-size compiler optimization.
} osKernelState_t;
osKernelGetState检索的内核状态。万一osKernelGetState失败或从ISR调用,它将返回osKernelError
,否则返回内核状态。
/// Initialize the RTOS Kernel.
/// \return status code that indicates the execution status of the function.
osStatus_t osKernelInitialize (void);
返回值:
返回指示函数执行状态的状态代码。
函数osKernelInitialize初始化RTOS内核。在成功执行之前,只能调用函数osKernelGetInfo和osKernelGetState。
可能的osStatus_t返回值:
- osOK成功。
- osError如果发生不确定的错误。
- osErrorISR(如果从中断服务例程中调用)。
- osErrorNoMemory如果无法为该操作保留任何内存。
注意:
不能从中断服务程序中调用此函数。
代码示例
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
void app_main (void *argument) {
// ...
for (;;) {}
}
int main (void) {
// System Initialization
SystemCoreClockUpdate();
// ...
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(app_main, NULL, NULL); // Create application main thread
osKernelStart(); // Start thread execution
for (;;) {}
}
/// Get RTOS Kernel Information.
/// \param[out] version 指向用于检索版本信息的缓冲区的版本指针.
/// \param[out] id_buf 指向用于检索内核标识字符串的缓冲区的指针.
/// \param[in] id_size 内核标识字符串的缓冲区大小.
/// \ 返回指示函数执行状态的状态代码.
osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size);
函数osKernelGetInfo检索底层RTOS内核的API和内核版本以及该内核的可读标识符字符串。在初始化或启动RTOS之前可以安全地调用它(调用osKernelInitialize或osKernelStart)。
可能的osStatus_t返回值:
- osOK 成功。
- osError 如果发生不确定的错误。
注意:
可以从中断服务程序中调用此函数。
代码示例
void info (void)
{
char infobuf[100];
osVersion_t osv;
osStatus_t status;
status = osKernelGetInfo(&osv, infobuf, sizeof(infobuf));
if(status == osOK)
{
printf("Kernel Information: %s\r\n", infobuf);
printf("Kernel Version : %d\r\n", osv.kernel);
printf("Kernel API Version: %d\r\n", osv.api);
}
}
/// Get the current RTOS Kernel state.
/// \return current RTOS Kernel state.
osKernelState_t osKernelGetState (void);
函数osKernelGetState返回内核的当前状态,可以在初始化或启动RTOS之前安全地调用该函数(调用osKernelInitialize或osKernelStart)。如果失败,它将返回osKernelError
,否则将返回内核状态(有关内核状态列表,请参见osKernelState_t)。
可能的osKernelState_t返回值:
- osKernelError如果发生了不确定的错误。
- 否则为实际内核状态。
注意
可以从中断服务程序中调用此函数。
代码示例
int main (void)
{
// System Initialization
SystemCoreClockUpdate();
// ...
if(osKernelGetState() == osKernelInactive) // Is the kernel initialized?
{
osKernelInitialize(); // Initialize CMSIS-RTOS kernel
}
;
}
/// Start the RTOS Kernel scheduler.
/// \return status code that indicates the execution status of the function.
osStatus_t osKernelStart (void);
函数osKernelStart启动RTOS内核并开始线程切换。如果成功,它将不会返回其调用函数。在成功执行之前,只能调用函数osKernelGetInfo,osKernelGetState和对象创建函数(osXxxNew)。
至少应在osKernelStart之前创建一个初始线程,请参见osThreadNew。
可能的osStatus_t返回值:
- osError如果发生不确定的错误。
- osErrorISR(如果从中断服务例程中调用)。
注意
不能从中断服务程序中调用此函数。
代码示例
int main (void)
{
// System Initialization
SystemCoreClockUpdate();
// ...
if(osKernelGetState() == osKernelInactive)
{
osKernelInitialize();
}
; // ... Start Threads
if (osKernelGetState() == osKernelReady) // If kernel is ready to run...
{
osKernelStart(); // ... start thread execution
}
while(1); // only reached in case of error
}
/// Lock the RTOS Kernel scheduler.
/// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
int32_t osKernelLock (void);
功能osKernelLock允许锁定所有任务开关。它返回锁定状态的前一个值(如果已锁定,则返回1;如果未锁定,则返回0),否则返回表示错误代码的负数(请参阅osStatus_t)。
可能的osStatus_t返回值:
- osError如果发生不确定的错误。
- osErrorISR(如果从中断服务例程中调用)。
注意
不能从中断服务程序中调用此函数。
代码示例
int32_t state = osKernelLock();
// ... critical code
osKernelRestore(state);
/// Unlock the RTOS Kernel scheduler.
/// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
int32_t osKernelUnlock (void);
函数osKernelUnlock从osKernelLock恢复。它返回锁定状态的前一个值(如果已锁定,则返回1;如果未锁定,则返回0),否则返回表示错误代码的负数(请参阅osStatus_t)。
可能的osStatus_t返回值:
- osError如果发生不确定的错误。
- osErrorISR(如果从中断服务例程中调用)。
注意
不能从中断服务程序中调用此函数。
代码示例
int32_t sl = osKernelLock();
// ... critical code
{
int32_t su = osKernelUnlock();
// ... uncritical code
osKernelRestoreLock(su);
}
// ... critical code
osKernelRestoreLock(sl);
/// Restore the RTOS Kernel scheduler lock state.
/// \param[in] lock lock state obtained by \ref osKernelLock or \ref osKernelUnlock.
/// \return new lock state (1 - locked, 0 - not locked, error code if negative).
int32_t osKernelRestoreLock (int32_t lock);
函数osKernelRestoreLock在osKernelLock或osKernelUnlock之后恢复先前的锁定状态。
参数lock指定由osKernelLock或osKernelUnlock获得的锁定状态。
该函数返回锁定状态的新值(如果已锁定,则返回1;如果未锁定,则返回0),否则返回表示错误代码的负数(请参阅osStatus_t)。
可能的osStatus_t返回值:
- osError如果发生不确定的错误。
- osErrorISR(如果从中断服务例程中调用)。
注意
不能从中断服务程序中调用此函数。
代码示例
int32_t sl = osKernelLock();
// ... critical code
{
int32_t su = osKernelUnlock();
// ... uncritical code
osKernelRestoreLock(su);
}
// ... critical code
osKernelRestoreLock(sl);
/// Suspend the RTOS Kernel scheduler.
/// \return time in ticks, for how long the system can sleep or power-down.
uint32_t osKernelSuspend (void);
CMSIS-RTOS扩展了无滴答操作,这对于使用广泛的低功耗模式(同时也禁用了SysTick定时器)的应用程序很有用。为了在这种省电模式下提供时间间隔,使用唤醒定时器来得出定时器间隔。函数osKernelSuspend挂起RTX内核调度程序,从而启用睡眠模式。
返回值可用于确定系统滴答的数量,直到发生下一个基于滴答的内核事件,即,延迟的线程再次变为就绪状态为止。建议设置低功耗定时器,以基于该返回值生成唤醒中断。
注意:
不能从中断服务程序中调用此函数。
代码示例
void osRtxIdleThread (void)
{
/* 当没有其他线程准备运行时,空闲线程正在运行.*/
unsigned int sleep;
for (;;)
{
/* 这里:包括在没有任务运行时要执行的可选用户代码。 */
sleep = osKernelSuspend(); /* 挂起RTX线程调度程序 */
if (sleep) /* 我们能睡多久? */
{
/* “sleep”在RTX定时器信号中,在这个配置中是1毫秒 */
/* 设置唤醒,如看门狗 */
__WFE(); /* Enter Power-down mode */
/* After Wake-up */
sleep = tc; /* 调整睡眠周期 */
}
osKernelResume(sleep); /* 恢复线程调度程序 */
}
}
/// Resume the RTOS Kernel scheduler.
/// \param[in] sleep_ticks time in ticks for how long the system was in sleep or power-down mode.
void osKernelResume (uint32_t sleep_ticks);
CMSIS-RTOS扩展了无滴答操作,这对于使用广泛的低功耗模式(同时也禁用了SysTick定时器)的应用程序很有用。为了在这种省电模式下提供时间间隔,使用唤醒定时器来得出定时器间隔。函数osKernelResume启用RTX内核调度程序,从而将系统从睡眠模式中唤醒。
注意
不能从中断服务程序中调用此函数。
代码示例
参考 osKernelSuspend (void);
/// Get the RTOS kernel tick count.
/// \return RTOS kernel current tick count.
uint32_t osKernelGetTickCount (void);
函数osKernelGetTickCount返回当前的RTOS内核滴答计数。
注意:
可以从中断服务程序中调用此函数。
代码示例