STM32 Keil5 的RTOS --- RTX

  1. 相关资料、手册
    从keil4到keil5,keil的功能越来越强大。特别是keil5之后,出现了RTE配置环境,我感觉特别方便,省去了那么多移植步骤,对于像我们这种“懒癌”真是福音啊!RTX这款实时操作系统还是我在逛论坛是发现的,不看不知道,一看忘不了啊!从此就觉得得好好研习一番,不过实时操作系统原理基本都一样。但是RTX有它独特之处,就是它中断延时对M3/M4内核可以实现零等待,带有免版税,确定性的RTOS。话不多说,咱们开始创建RTX工程。前提是你得安装了keil5
    准备: 在http://www.keil.com/dd2/Pack/。这个操作系统由Hitex公司开发,CMSIS-RTOS是为CORTEX-M定制的RTOS,截至目前,它的版本是1.1.0,且暂时只支持STM32F1系列。
    这里写图片描述
    安装好这个pack,打开keil5,可以看到很多例程,参照例程,结合CMSIS-RTOS的手册,就可以开始学习了,手册在你的keil文件夹下:
    C:\Program Files (x86)\KEIL5\ARM\Pack\Hitex\CMSIS_RTOS_Tutorial\1.1.0\Documentation
    这里写图片描述
    对RTX的应用及学习参照上述例程。
    注意:用RTOS Debug选项设置:
    Dialog DLL默认是DARMSTM.DLL
    Parameter默认是-pSTM32F103VC 更换成所用芯片-pSTM32F103R8
    工程所在目录C:\Users\Administrator\Documents\Examples
    例子:如下:
    这里写图片描述 注意: DLL 、Parameter处需要修改。
    这里写图片描述

常用API 函数:

线程相关:

  #include <cmsis_os.h>
osThreadId  led_ID1 ;   
 osThreadDef(led_thread1, osPriorityNormal, 1, 0);
// 设置栈的大小 osThreadDef(led_thread1, osPriorityNormal, 1, 1024);
注意,如果此处更改了栈的大小,下图的
 Number of threads with user-provided stack size  要修改成1,保险起见勾选上stack usage watermark 选项,观察最大栈的使用情况。

这里写图片描述

l

ed_ID1 = osThreadCreate(osThread(led_thread1), NULL);
osKernelInitialize ();      osKernelStart ();  
延时函数:   
osDelay(); 单位为ms      osDelay(500); 为延时500ms
虚拟定时器:
osTimerDef(timer0_handle, callback);// callback 为定时器的回调函数
osTimerId timer0 = osTimerCreate(osTimer(timer0_handle), osTimerPeriodic, (void *)0);
osTimerStart(timer0, 500);      //500ms后启动定时器
 虚拟定时器的启动在osKernelStart (); 前?????
信号:
osSignalSet (T_led_ID1,0x01); //往 线程id为T_led_ID1 发送01 信号
osSignalWait (0x01,osWaitForever); // 等待01信号,为阻塞状态。
中断:
   修改svc.c 
SVC_Table
; Insert user SVC functions here
                 DCD    __SVC_0 
                DCD     __SVC_1                 ; SVC 1 Function Entry
                DCD     __SVC_2                 ; SVC 2 Function Entry
                DCD     __SVC_3                 ; SVC 2 Function Entry
   void __svc(1) init_ADC (void); // main 中
   void __SVC_1 (void){初始化ADC的外设}
    osSignalSet(T_adc_ID,0x01); // 中断函数发送信号
osSignalWait(0x01,osWaitForever);   
信号量:
osSemaphoreId sem1; 
osSemaphoreDef(sem1);
sem1 = osSemaphoreCreate(osSemaphore(sem1), 0); 
osSemaphoreWait(sem1, osWaitForever);
osSemaphoreRelease(sem1);
互斥量:(禁止操作系统调度)
osMutexId uart_mutex;      osMutexDef(uart_mutex); // 定义互斥
uart_mutex = osMutexCreate(osMutex(uart_mutex));    // 创建互斥
osMutexWait(uart_mutex, osWaitForever); // 启用互斥
 Function();  //互斥执行的函数
osMutexRelease(uart_mutex); // 释放互斥

官方RTX提供常用API:

Kernel Information and Control
· osKernelInitialize: Initialize the RTOS kernel.
· osKernelStart: Start the RTOS kernel.
· osKernelRunning: Query if the RTOS kernel is running.
· osKernelSysTick$ : Get RTOS kernel system timer counter.
· osKernelSysTickFrequency$ : RTOS kernel system timer frequency in Hz.
· osKernelSysTickMicroSec$ : Convert microseconds value to RTOS kernel system timer value.
Thread Management
· osThreadCreate: Start execution of a thread function.
· osThreadTerminate: Stop execution of a thread function.
· osThreadYield: Pass execution to next ready thread function.
· osThreadGetId: Get the thread identifier to reference this thread.
· osThreadSetPriority: Change the execution priority of a thread function.
· osThreadGetPriority: Obtain the current execution priority of a thread function.
Generic Wait Functions
· osDelay: Wait for a specified time.
· osWait$ : Wait for any event of the type Signal, Message, or Mail.
· Timer Management$ 
· osTimerCreate: Define attributes of the timer callback function.
· osTimerStart: Start or restart the timer with a time value.
· osTimerStop: Stop the timer.
· osTimerDelete: Delete a timer.
Signal Management
· osSignalSet: Set signal flags of a thread.
· osSignalClear: Reset signal flags of a thread.
· osSignalWait: Suspend execution until specific signal flags are set.
· Mutex Management$ 
· osMutexCreate: Define and initialize a mutex.
· osMutexWait: Obtain a mutex or Wait until it becomes available.
· osMutexRelease: Release a mutex.
· osMutexDelete: Delete a mutex.
· Semaphore Management$ 
· osSemaphoreCreate: Define and initialize a semaphore.
· osSemaphoreWait: Obtain a semaphore token or Wait until it becomes available.
· osSemaphoreRelease: Release a semaphore token.
· osSemaphoreDelete: Delete a semaphore.
· Memory Pool Management$ 
· osPoolCreate: Define and initialize a fix-size memory pool.
· osPoolAlloc: Allocate a memory block.
· osPoolCAlloc: Allocate a memory block and zero-set this block.
· osPoolFree: Return a memory block to the memory pool.
· Message Queue Management$ 
· osMessageCreate: Define and initialize a message queue.
· osMessagePut: Put a message into a message queue.
· osMessageGet: Get a message or suspend thread execution until message arrives.
· Mail Queue Management$ 
· osMailCreate: Define and initialize a mail queue with fix-size memory blocks.
· osMailAlloc: Allocate a memory block.
· osMailCAlloc: Allocate a memory block and zero-set this block.
· osMailPut: Put a memory block into a mail queue.
· osMailGet: Get a mail or suspend thread execution until mail arrives.
· osMailFree: Return a memory block to the mail queue.
The following CMSIS-RTOS functions can be called from threads and interrupt service routines (ISR):
· osSignalSet
· osSemaphoreRelease
· osPoolAlloc, osPoolCAlloc, osPoolFree
· osMessagePut, osMessageGet
· osMailAlloc, osMailCAlloc, osMailGet, osMailPut, osMailFree

RTOS 官方学习文档:
官方文件目录:
Keil_v5\ARM\CMSIS\Documentation\RTOS\html, 进入index.html

http://www.keil.com/pack/doc/CMSIS/RTOS/html/index.html 官方链接
http://www.keil.com/pack/doc/MW/FileSystem/html/index.html 里边有对stm32的网络 、usb 、文件系统的详细说明

猜你喜欢

转载自blog.csdn.net/tiger15605353603/article/details/81352029