UCOSIII (a)

First, the front and back office systems and RTOS

1, front and back office systems

Early embedded development there is no concept of embedded operating system, bare metal operating direct write programs on the bare metal, such as 51 MCU basically no concept of the operating system. The program is usually divided into two parts: the front desk systems and back-office systems.

Simple little system is usually front and back office systems, such programs include an infinite loop and several interrupt service routine :

Application is an infinite loop that calls the API function to complete the desired action, this is called a cycle back-end systems .

Interrupt service routine for asynchronous event handling system, which is the reception system . Reception is interrupt level, the background is the task level .

    

 

 

2,RTOS

RTOS full name: Real Time OS , is the real-time operating system, emphasized that: real-time . Real-time operating system is divided into hard and soft real-time real-time .

Hard real-time requirements must be completed within a set amount of time, hard real-time system does not allow overtime, in which the soft real-time processing of overtime consequences not so strict.

In the real-time operating system, we can to achieve the functionality into multiple tasks, each responsible for implementing some of them, each task is a very simple procedure, usually an endless loop.

RTOS operating system: UCOS, FreeRTOS, RTX, RT-Thread, DJYOS and so on.

Core RTOS operating system are: real-time kernel.

 

3, preemptive kernel

RTOS kernel is responsible for managing all the tasks, the kernel determines which task to run, when to stop the current task switch to another task, this is a multi-task management capabilities kernel.

多任务管理给人的感觉就好像芯片有多个CPU,多任务管理实现了CPU资源的最大化利用,多任务管理有助于实现程序的模块化开发,能够实现复杂的实时应用。

可剥夺内核顾名思义就是可以剥夺其他任务的CPU使用权,它总是运行就绪任务中的优先级最高的那个任务

        

 

 

UCOS系统简介

UCOS是Micrium公司出品的RTOS类实时操作系统, UCOS目前有两个版本:

UCOSII和UCOSIII。

UCOSIII是一个可裁剪、可剥夺型的多任务内核,而且没有任务数限制。

UCOSIII提供了实时操作系统所需的所有功能,包括资源管理、同步、任务通信等。

UCOSIII是用C和汇编来写的,其中绝大部分都是用C语言编写的,只有极少数的与处理器密切相关的部分代码才是用汇编写的, UCOSIII结构简洁,可读性很强!最主

要的是非常适合初次接触嵌入式实时操作系统学生、嵌入式系统开发人员和爱好者学习。

 

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "includes.h"
#include "dht.h"
#include "infra_red.h"


//任务1控制块
OS_TCB Task1_TCB;

void task1(void *parg);

CPU_STK task1_stk[128];            //任务1的任务堆栈,大小为128字,也就是512字节



//任务2控制块
OS_TCB Task2_TCB;

void task2(void *parg);

CPU_STK task2_stk[128];            //任务2的任务堆栈,大小为128字,也就是512字节


//主函数
int main(void)
{
    OS_ERR err;

    delay_init(168);                                                      //时钟初始化
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                        //中断分组配置
    uart_init(9600);                                                       //串口初始化
    LED_Init();                                                         //LED初始化    
    
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);                //打开温湿度模块控制GPIOG的时钟


    //OS初始化,它是第一个运行的函数,初始化各种的全局变量,例如中断嵌套计数器、优先级、存储器
    OSInit(&err);


    //创建任务1
    OSTaskCreate(    (OS_TCB *)&Task1_TCB,                                    //任务控制块
                    (CPU_CHAR *)"Task1",                                    //任务的名字
                    (OS_TASK_PTR)task1,                                        //任务函数
                    (void *)0,                                                //传递参数
                    (OS_PRIO)3,                                                 //任务的优先级        
                    (CPU_STK *)task1_stk,                                    //任务堆栈基地址
                    (CPU_STK_SIZE)128/10,                                    //任务堆栈深度限位,用到这个位置,任务不能再继续使用
                    (CPU_STK_SIZE)128,                                        //任务堆栈大小            
                    (OS_MSG_QTY)0,                                            //禁止任务消息队列
                    (OS_TICK)0,                                                //默认时间片长度                                                                
                    (void  *)0,                                                //不需要补充用户存储区
                    (OS_OPT)OS_OPT_TASK_NONE,                                //没有任何选项
                    &err                                                    //返回的错误码
                );


    //创建任务2
    OSTaskCreate(    (OS_TCB *)&Task2_TCB,                                    //任务控制块
                    (CPU_CHAR *)"Task2",                                    //任务的名字
                    (OS_TASK_PTR)task2,                                        //任务函数
                    (void *)0,                                                //传递参数
                    (OS_PRIO)4,                                                 //任务的优先级        
                    (CPU_STK *)task2_stk,                                    //任务堆栈基地址
                    (CPU_STK_SIZE)128/10,                                    //任务堆栈深度限位,用到这个位置,任务不能再继续使用
                    (CPU_STK_SIZE)128,                                        //任务堆栈大小            
                    (OS_MSG_QTY)0,                                            //禁止任务消息队列
                    (OS_TICK)0,                                                //默认时间片长度                                                                
                    (void  *)0,                                                //不需要补充用户存储区
                    (OS_OPT)OS_OPT_TASK_NONE,                                //没有任何选项
                    &err                                                    //返回的错误码
                );


    //启动OS,进行任务调度
    OSStart(&err);
    
}


void task1(void *parg)
{
    OS_ERR err;
    uint8_t dht_data[5]={0};
    printf("task1 is create ok\r\n");
    //红外初始化
    ir_init();
    while(1)
    {

        if(dht11_read_data(dht_data)==0)
        {
            printf("temp=%d.%d\r\n",dht_data[2],dht_data[3]);
            printf("humi=%d.%d\r\n",dht_data[0],dht_data[1]);    
            printf("\r\n");
        }
        delay_ms(500);
        delay_ms(500);
        
        OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err); //延时1s
    }


}

void task2(void *parg)
{

    OS_ERR err;
    printf("task2 is create ok\r\n");

    while(1)
    {

        //添加LED2闪烁


        printf("task2 is running ...\r\n");

        OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err); //延时1s
    }



}

 

                                      

 

 

 

(1)假如任务1和任务2两个任务的优先级都是一样的,而且任务1比任务2创建更早,会出现什么结果?例如任务1和任务2的运行代码是一样的。如下:

void task1(void *parg)
{
    OS_ERR err;
    printf("task1 is create ok\r\n");
    while(1)
    {
        printf("task1 is running ...\r\n");    
}
}

void task2(void *parg)
{
    OS_ERR err;
    printf("task2 is create ok\r\n");
    while(1)
    {
        printf("task2 is running ...\r\n");
    }
}

 

结果

task1 is running ...
task1 is running ...
task1 is running ...
task1 is running ...
task1 is running ...
......

当两个任务的优先级都是一样且最高,这个两个任务都没有任何的让出CPU的函数,在执行的时候,只执行创建最早的任务,任务1是比任务2创建更早,只能执行任务1.

 

(2)任务1的优先级是比任务2高,代码如下:

void task1(void *parg)
{
    OS_ERR err;
    printf("task1 is create ok\r\n");
    while(1)
    {
        printf("task1 is running ...\r\n");
        OSTimeDlyHMSM(0,0,0,10,OS_OPT_TIME_HMSM_STRICT,&err); //延时10ms
    }
}

void task2(void *parg)
{
    OS_ERR err;
    printf("task2 is create ok\r\n");

    while(1)
    {
        printf("task2 is running ...\r\n");

        OSTimeDlyHMSM(0,0,0,10,OS_OPT_TIME_HMSM_STRICT,&err); //延时10ms
    }
}

结果:

task2task1 is running ...
 is task1 is running ...
runntask1 is running ...
ing task1 is running ...
...
task1 is running ...
task1 is running ...
task2task1 is running ...

 

在任务2在打印的过程当中,打印数据时间超过10ms,所以在打印的中途ucos发现任务1已经就绪了,任务1就会抢夺CPU的使用权,任务2就停止执行,等任务1执行完之后让出CPU,任务2才继续执行。

 

 (三)临界区代码用于资源保护,以下有两种的编写方法,哪一种才是正确的?

 1,

//进入临界区,保护以下的代码,关闭总中断,停止了ucos的任务调度,其他任务已经停止执行
        OS_CRITICAL_ENTER();

        printf("task1 is running ...\r\n");

        //退出临界区,开启总中断,允许ucos的任务调度
        OS_CRITICAL_EXIT();        

OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err); //延时1000ms

2,

//进入临界区,保护以下的代码,关闭总中断,停止了ucos的任务调度,其他任务已经停止执行
        OS_CRITICAL_ENTER();

        printf("task1 is running ...\r\n");

OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err); //延时1000ms

        //退出临界区,开启总中断,允许ucos的任务调度
        OS_CRITICAL_EXIT();        

只有例子1才是正确的,能够进行准确的延时,唯独只有例子2延时是失效的。

 

 

在进入临界区代码,因为它是关闭了总中断,所以跟阻塞相关、时间管理函数都会失效。

 

在ucos3,提供的最小毫米级别的延时是多少,依据是什么?

在os_cfg_app.h当中有关于设置ucos3时钟频率,时钟频率如下:

#define  OS_CFG_TICK_RATE_HZ         200u       /* Tick rate in Hertz (10 to 1000 Hz)    */

 

因此,延时最小只能是5ms。

如果要设置最小的延时为1ms,可以调整当前该宏定义OS_CFG_TICK_RATE_HZ为1000。

OS_CFG_TICK_RATE_HZ的值越小,ucos3能够实现更低的功耗,但是高优先级任务的执行不会太及时。反过来说,该值越大,功耗就越高,同时高优先级的任务被执行的延迟时间会更及时!

应用场景:如果是电池供电且任务数特别少,可以将OS_CFG_TICK_RATE_HZ调小。如果是普通电源供电且任务数特别多,将OS_CFG_TICK_RATE_HZ调大!

延时函数:delay_us与delay_ms能够正常地使用!

 

中断服务函数,如果在ucos3操作系统使用下,还得添加这两个函数,示例如下:

 

void USART1_IRQHandler(void)                    //串口1中断服务程序
{
//进入中断,告诉UCOS3停止任务调度,并进行中断嵌套计数
OSIntEnter();

//添加代码


//退出中断,告诉UCOS3已经完成中断处理,并更新中断嵌套计数,可以进行任务调度
OSIntExit();
}

 

Guess you like

Origin www.cnblogs.com/xiangtingshen/p/10948689.html