keil CMSIS-RTOS API介绍

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
[in] thread_defthread definition referenced with osThread.
[in] argumentpointer that is passed to the thread function as start argument 

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 ( void const *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) {
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. 

4.Timer Management

 osTimerDef  osTimerCreate   osTimerDelete  osTimerStart  osTimerStop

Code Example

#include "cmsis_os.h"
void Timer_Callback ( void const *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 (;;);

 }

 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值