keil CMSIS-RTOS API介绍
2015-06-15 15:58
274 查看
1.Kernel information and Control:
osKernelInitialize (void)
Initialize the RTOS Kernel for creating objects.
osKernelStart (void)
Start the RTOS Kernel.
osKernelRunning (void)
Check if the RTOS kernel is already started.
osKernelSysTick (void)
Get the RTOS kernel system timer counter.
Code Example
#include "cmsis_os.h"
int main (void) {
if (osKernelInitialize
() !=
osOK) { // check osStatus for other possible valid values
// exit with an error message
}
if (!osKernelRunning
()) { // is the kernel running ?
if (osKernelStart
() !=
osOK) { // start the kernel
// kernel could not be started
}
}
}
2.Thread Management
keil
RTOS thread 有四种状态,RUNNING,READY,WAITING,INACTIVE。
osThreadDef(
name,priority,instances,stacksz ) Define the attributes of a thread functions that can be created by the function osThreadCreate using osThread.
osThreadCreate (const
osThreadDef_t * thread_def, void * argument )
Parameters
return
thread ID.
需要注意的是,当使用osThreadCreate来创建thread时,会将线程置于READY or RUNNING 状态
osThreadId osThreadGetId(void )
得到当前线程的ID
还有线程优先级的设置osStatus
osThreadSetPriority (osThreadId
thread_id,osPriority priority )
CodeExample
#include "cmsis_os.h"
void Thread_1 (voidconst *arg) {// Thread function
osThreadId
id; // id for the currently running thread
osPriority pr;//
thread priority
osStatus status;//
status of the executed function
:
id =
osThreadGetId (); // Obtain ID of current running thread
if (id != NULL) {
status =
osThreadSetPriority (id,osPriorityBelowNormal);
if (status ==
osOK) {
// Thread priority changed to BelowNormal
}
else {
// Failed to set the priority
}
}
else {
// Failed to get the id
}
:
}
3.Generic Wait Functions
4.Timer Management
osTimerDef
osTimerCreate osTimerDelete
[b]osTimerStart [b]osTimerStop[/b][/b]
Code Example
#include "cmsis_os.h"
void Timer_Callback (voidconst *arg);
// prototype for timer callback function
osTimerDef
(Timer, Timer_Callback); // define timer
void TimerStop_example (void) {
osTimerId
id; // timer id
osStatus
status;// function return status
/ Create periodic timer
exec = 1;
id =
osTimerCreate (osTimer(Timer2),osTimerPeriodic,
NULL);
osTimerStart
(id, 1000);
// start timer
:
status =
osTimerStop (id);// stop timer
if (status !=
osOK) {
// Timer could not be stopped
}
:
osTimerStart
(id, 1000);
// start timer again
:
}
os_timer_type:
osTimerOnce
one-shot timer
osTimerPeriodic
repeating timer
osSignalSet osSignalClear osSignalWait
Example
voidThread_2 (void const *arg);
osThreadDef(Thread_2, osPriorityHigh, 1, 0);
staticvoidEX_Signal_1 (void) {
osThreadIdthread_id;
osEventevt;
thread_id = osThreadCreate (osThread(Thread_2), NULL);
if(thread_id == NULL) {
// Failed to create a thread.
}
else{
:
// wait for a signal
evt = osSignalWait (0x01, 100);
if(evt.status == osEventSignal) {
// handle event status
}
}
}
PS: osSignalWait(0,million)意思是收到任何信号都可以
当然,这边有个疑问就是在第一个线程中发了一个信号,程序会是执行完这个线程后再去到另一个接收信号的线程执行呢,还是发完信号直接去另一个线程去处理,执行完了再返回第一个线程继续执行,有人说是跟买票一样,一人走一步。等待在板子上跑跑看
6.Mutex Management
Mutext用于各个线程之间对资源的独享,互斥锁不能被中断服务程序调用。
osMutexDef osMutexCreate osMutexDelete osMutexRelease osMutexWait
个人感觉在RTX操作系统里osMutexWait应该是UC/OSII里的OsMutexPend,
例子:
osMutexDef( Mutex );
osMutexId mutex;
void Thread0( void * arg);
void Thread1( void * arg);
osThreadDef( Thread0, Thread0, osPriorityNormal, 512 );
osThreadDef( Thread1, Thread1, osPriorityAboveNormal, 512 );
void Thread0( void * arg)
{
while(1)
{
osMutexWait( mutex, osWaitForever );
osDelay( 10 );
osMutexRelease( mutex );
osDelay( 10 );
}
}
void Thread1( void * arg)
{
while(1)
{
osMutexWait( mutex, osWaitForever );
osDelay( 10 );
osMutexRelease( mutex );
osDelay( 10 );
}
}
int main( void )
{
osKernelInitialize();
osThreadCreate( osThread(Thread0), (void *)100 );
osThreadCreate( osThread(Thread1), (void *)200 );
mutex = osMutexCreate( osMutex(Mutex) );
osKernelStart();
return 0;
}
7.Semaphore Management
信号量和mutex类似,mutex一次只允许一个线程访问共享资源,信号量允许固定数量的线程访问共享资源池,
8.Message Queue Management
osMessageGet(queue_id,millisec) 会挂起正在执行的线程,直到一个消息到来。当此函数在ISR调用时,millisec必须是0
osMessagePut (queue_id,info,millisec)向队列发送消息,info整数(uint32_t)或指针类型
9.Mail Queue Management
使用邮箱首先要申请一块内存,使用osMailAlloc(osMailQId queue_id, uint32_t millisec)或者是osMailCAlloc(osMailQId queue_id, uint32_t millisec),两者唯一区别就是后者直接里面全赋0.发送给邮箱用osMailPut(osMailQId queue_id, void *mail),从邮箱里读信息用osMailGet(osMailQId queue_id, uint32_t millisec),osMailGet()和osMessageq_get,一样,也是挂起当前的任务直到邮箱里来消息才运行,与MessageQ好一点的是,可以传递的是结构体。
例子:
#include <stdio.h>
#include "cmsis_os.h"
/* Thread IDs */
osThreadId tid_thread1; /* assigned ID for thread 1 */
osThreadId tid_thread2; /* assigned ID for thread 2 */
typedef struct { /* Mail object structure */
float voltage; /* AD result of measured voltage */
float current; /* AD result of measured current */
uint32_t counter; /* A counter value */
} T_MEAS;
osMailQDef(mail, 16, T_MEAS); /* Define mail queue */
osMailQId mail;
/* Forward reference */
void send_thread (void const *argument);
void recv_thread (void const *argument);
/* Thread definitions */
osThreadDef(send_thread, osPriorityNormal, 1, 0);
osThreadDef(recv_thread, osPriorityNormal, 1, 2000);
/*----------------------------------------------------------------------------
* Thread 1: Send thread
*---------------------------------------------------------------------------*/
void send_thread (void const *argument) {
T_MEAS *mptr;
mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */
mptr->voltage = 223.72; /* Set the mail content */
mptr->current = 17.54;
mptr->counter = 120786;
osMailPut(mail, mptr); /* Send Mail */
osDelay(100);
mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */
mptr->voltage = 227.23; /* Prepare 2nd mail */
mptr->current = 12.41;
mptr->counter = 170823;
osMailPut(mail, mptr); /* Send Mail */
osThreadYield(); /* Cooperative multitasking */
osDelay(100);
mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */
mptr->voltage = 229.44; /* Prepare 3rd mail */
mptr->current = 11.89;
mptr->counter = 237178;
osMailPut(mail, mptr); /* Send Mail */
osDelay(100);
/* We are done here, exit this thread */
}
/*----------------------------------------------------------------------------
* Thread 2: Receive thread
*---------------------------------------------------------------------------*/
void recv_thread (void const *argument) {
T_MEAS *rptr;
osEvent evt;
for (;;) {
evt = osMailGet(mail, osWaitForever); /* wait for mail */
if (evt.status == osEventMail) {
rptr = evt.value.p;
printf ("\nVoltage: %.2f V\n",rptr->voltage);
printf ("Current: %.2f A\n",rptr->current);
printf ("Number of cycles: %d\n",(int)rptr->counter);
#ifdef __USE_FFLUSH
fflush (stdout);
#endif
osMailFree(mail, rptr); /* free memory allocated for mail */
}
}
}
/*----------------------------------------------------------------------------
* Main:
*---------------------------------------------------------------------------*/
int main (void) { /* program execution starts here */
mail = osMailCreate(osMailQ(mail), NULL); /* create mail queue */
tid_thread1 = osThreadCreate(osThread(send_thread), NULL);
tid_thread2 = osThreadCreate(osThread(recv_thread), NULL);
osDelay(osWaitForever);
for (;;);
}
osKernelInitialize (void)
Initialize the RTOS Kernel for creating objects.
osKernelStart (void)
Start the RTOS Kernel.
osKernelRunning (void)
Check if the RTOS kernel is already started.
osKernelSysTick (void)
Get the RTOS kernel system timer counter.
Code Example
#include "cmsis_os.h"
int main (void) {
if (osKernelInitialize
() !=
osOK) { // check osStatus for other possible valid values
// exit with an error message
}
if (!osKernelRunning
()) { // is the kernel running ?
if (osKernelStart
() !=
osOK) { // start the kernel
// kernel could not be started
}
}
}
2.Thread Management
keil
RTOS thread 有四种状态,RUNNING,READY,WAITING,INACTIVE。
osThreadDef(
name,priority,instances,stacksz ) Define the attributes of a thread functions that can be created by the function osThreadCreate using osThread.
osThreadCreate (const
osThreadDef_t * thread_def, void * argument )
Parameters
[in] | thread_def | thread definition referenced with osThread. |
[in] | argument | pointer that is passed to the thread function as start argument |
thread ID.
需要注意的是,当使用osThreadCreate来创建thread时,会将线程置于READY or RUNNING 状态
osThreadId osThreadGetId(void )
得到当前线程的ID
还有线程优先级的设置osStatus
osThreadSetPriority (osThreadId
thread_id,osPriority priority )
CodeExample
#include "cmsis_os.h"
void Thread_1 (voidconst *arg) {// Thread function
osThreadId
id; // id for the currently running thread
osPriority pr;//
thread priority
osStatus status;//
status of the executed function
:
id =
osThreadGetId (); // Obtain ID of current running thread
if (id != NULL) {
status =
osThreadSetPriority (id,osPriorityBelowNormal);
if (status ==
osOK) {
// Thread priority changed to BelowNormal
}
else {
// Failed to set the priority
}
}
else {
// Failed to get the id
}
:
}
3.Generic Wait Functions
osStatus | osDelay (uint32_t millisec) |
Wait for Timeout (Time Delay). | |
osEvent | osWait (uint32_t millisec) |
Wait for Signal, Message, Mail, or Timeout. |
osTimerDef
osTimerCreate osTimerDelete
[b]osTimerStart [b]osTimerStop[/b][/b]
Code Example
#include "cmsis_os.h"
void Timer_Callback (voidconst *arg);
// prototype for timer callback function
osTimerDef
(Timer, Timer_Callback); // define timer
void TimerStop_example (void) {
osTimerId
id; // timer id
osStatus
status;// function return status
/ Create periodic timer
exec = 1;
id =
osTimerCreate (osTimer(Timer2),osTimerPeriodic,
NULL);
osTimerStart
(id, 1000);
// start timer
:
status =
osTimerStop (id);// stop timer
if (status !=
osOK) {
// Timer could not be stopped
}
:
osTimerStart
(id, 1000);
// start timer again
:
}
os_timer_type:
osTimerOnce
one-shot timer
osTimerPeriodic
repeating timer
5.Signal Management
三个函数osSignalSet osSignalClear osSignalWait
Example
voidThread_2 (void const *arg);
osThreadDef(Thread_2, osPriorityHigh, 1, 0);
staticvoidEX_Signal_1 (void) {
osThreadIdthread_id;
osEventevt;
thread_id = osThreadCreate (osThread(Thread_2), NULL);
if(thread_id == NULL) {
// Failed to create a thread.
}
else{
:
// wait for a signal
evt = osSignalWait (0x01, 100);
if(evt.status == osEventSignal) {
// handle event status
}
}
}
PS: osSignalWait(0,million)意思是收到任何信号都可以
当然,这边有个疑问就是在第一个线程中发了一个信号,程序会是执行完这个线程后再去到另一个接收信号的线程执行呢,还是发完信号直接去另一个线程去处理,执行完了再返回第一个线程继续执行,有人说是跟买票一样,一人走一步。等待在板子上跑跑看
6.Mutex Management
Mutext用于各个线程之间对资源的独享,互斥锁不能被中断服务程序调用。
osMutexDef osMutexCreate osMutexDelete osMutexRelease osMutexWait
个人感觉在RTX操作系统里osMutexWait应该是UC/OSII里的OsMutexPend,
例子:
osMutexDef( Mutex );
osMutexId mutex;
void Thread0( void * arg);
void Thread1( void * arg);
osThreadDef( Thread0, Thread0, osPriorityNormal, 512 );
osThreadDef( Thread1, Thread1, osPriorityAboveNormal, 512 );
void Thread0( void * arg)
{
while(1)
{
osMutexWait( mutex, osWaitForever );
osDelay( 10 );
osMutexRelease( mutex );
osDelay( 10 );
}
}
void Thread1( void * arg)
{
while(1)
{
osMutexWait( mutex, osWaitForever );
osDelay( 10 );
osMutexRelease( mutex );
osDelay( 10 );
}
}
int main( void )
{
osKernelInitialize();
osThreadCreate( osThread(Thread0), (void *)100 );
osThreadCreate( osThread(Thread1), (void *)200 );
mutex = osMutexCreate( osMutex(Mutex) );
osKernelStart();
return 0;
}
7.Semaphore Management
信号量和mutex类似,mutex一次只允许一个线程访问共享资源,信号量允许固定数量的线程访问共享资源池,
8.Message Queue Management
osMessageGet(queue_id,millisec) 会挂起正在执行的线程,直到一个消息到来。当此函数在ISR调用时,millisec必须是0
osMessagePut (queue_id,info,millisec)向队列发送消息,info整数(uint32_t)或指针类型
9.Mail Queue Management
使用邮箱首先要申请一块内存,使用osMailAlloc(osMailQId queue_id, uint32_t millisec)或者是osMailCAlloc(osMailQId queue_id, uint32_t millisec),两者唯一区别就是后者直接里面全赋0.发送给邮箱用osMailPut(osMailQId queue_id, void *mail),从邮箱里读信息用osMailGet(osMailQId queue_id, uint32_t millisec),osMailGet()和osMessageq_get,一样,也是挂起当前的任务直到邮箱里来消息才运行,与MessageQ好一点的是,可以传递的是结构体。
例子:
#include <stdio.h>
#include "cmsis_os.h"
/* Thread IDs */
osThreadId tid_thread1; /* assigned ID for thread 1 */
osThreadId tid_thread2; /* assigned ID for thread 2 */
typedef struct { /* Mail object structure */
float voltage; /* AD result of measured voltage */
float current; /* AD result of measured current */
uint32_t counter; /* A counter value */
} T_MEAS;
osMailQDef(mail, 16, T_MEAS); /* Define mail queue */
osMailQId mail;
/* Forward reference */
void send_thread (void const *argument);
void recv_thread (void const *argument);
/* Thread definitions */
osThreadDef(send_thread, osPriorityNormal, 1, 0);
osThreadDef(recv_thread, osPriorityNormal, 1, 2000);
/*----------------------------------------------------------------------------
* Thread 1: Send thread
*---------------------------------------------------------------------------*/
void send_thread (void const *argument) {
T_MEAS *mptr;
mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */
mptr->voltage = 223.72; /* Set the mail content */
mptr->current = 17.54;
mptr->counter = 120786;
osMailPut(mail, mptr); /* Send Mail */
osDelay(100);
mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */
mptr->voltage = 227.23; /* Prepare 2nd mail */
mptr->current = 12.41;
mptr->counter = 170823;
osMailPut(mail, mptr); /* Send Mail */
osThreadYield(); /* Cooperative multitasking */
osDelay(100);
mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */
mptr->voltage = 229.44; /* Prepare 3rd mail */
mptr->current = 11.89;
mptr->counter = 237178;
osMailPut(mail, mptr); /* Send Mail */
osDelay(100);
/* We are done here, exit this thread */
}
/*----------------------------------------------------------------------------
* Thread 2: Receive thread
*---------------------------------------------------------------------------*/
void recv_thread (void const *argument) {
T_MEAS *rptr;
osEvent evt;
for (;;) {
evt = osMailGet(mail, osWaitForever); /* wait for mail */
if (evt.status == osEventMail) {
rptr = evt.value.p;
printf ("\nVoltage: %.2f V\n",rptr->voltage);
printf ("Current: %.2f A\n",rptr->current);
printf ("Number of cycles: %d\n",(int)rptr->counter);
#ifdef __USE_FFLUSH
fflush (stdout);
#endif
osMailFree(mail, rptr); /* free memory allocated for mail */
}
}
}
/*----------------------------------------------------------------------------
* Main:
*---------------------------------------------------------------------------*/
int main (void) { /* program execution starts here */
mail = osMailCreate(osMailQ(mail), NULL); /* create mail queue */
tid_thread1 = osThreadCreate(osThread(send_thread), NULL);
tid_thread2 = osThreadCreate(osThread(recv_thread), NULL);
osDelay(osWaitForever);
for (;;);
}
相关文章推荐
- CentOS 系统下更新 GCC版本 4.9.1
- 回顾系列---关于卡通农场
- MacOS下的IntelliJ IDEA & Android Studio 通用配置
- 在代码中调用 mvc 4 api
- Count and Say
- Elasticsearch 集成IK分词器
- iPhone6以及iphone6Plus 的放大模式
- Ajax实例
- Android中判断网络是否处于连接状态
- SqlServer Bug:复制架构更改参数(replicate_ddl)无效
- spring mvc 配置 velocity 模板
- IP分类
- 从标准输入读命令并执行
- iOS本地闹钟提醒实现
- Objective-C 关于锁的种类
- Eclipse中,字体修改及背景色修改
- UML学习笔记(9)——组件图
- 解决Myeclipse部署错误:Deploy Location:INVALID
- 解决Android Activity切换时出现白屏问题
- IOS用正则验证手机号