单片机操作系统RTX51原理分析与移植

发布者:EnigmaticSoul最新更新时间:2013-01-07 来源: 51hei关键字:单片机  操作系统  RTX51 手机看文章 扫描二维码
随时随地手机看文章

一.课题要求:

仔细分析单片机操作系统RTX51的原理,将其移植到单片机实验平台上,并要求编写一个简短的程序来验证其运行的正确性(比如,编写两个具有显示不一样内容的任务)。

二.RTX51原理

注释:由于英文原文会比较好,所以我没有翻译过来(不过也写了部分的翻译),直接提取出来一些英文,总结在一起。

1.       进程管理( Task Management )

1)       进程类型( Classes of Tasks )

RTX-51 recognizes two classes of tasks:

1.       Fast tasks

n       Contain especially short responses and interrupt disable times.

n       Contain a separate register bank and a separate stack area (register banks 1, 2 and 3).(Figure 1)

n       Contain the highest task priority (priority 3) and can therefore interrupt standard tasks.

n       All contain the same priority and can therefore not be mutually interrupted.

n       Can be interrupted by c51 interrupt functions.

n       A maximum of three fast tasks can be active in the system.

 

2.       Standard tasks

n       Require somewhat more time for the task switching compared to fast tasks.

n       Share a common register bank and a common stack area (register bank 0).

n       The current contents of registers and stack are stored in the external (XDATA) memory during a task change.

n       Can be interrupted by fast tasks.

n       Can interrupt themselves mutually.

n       Can be interrupt by c51 interrupt functions.

n       A maximum 16 standard tasks can be active in the system.
点击浏览下一页

Figure 3: Task Classes and Memory Allocation

每一个标准进程都包含一个设备上下文在扩展内存(XDATA)中。在标准进程执行进程切换的时候,会把它自己的Register和Stack存储到对应的设备上下文中(在扩展内存中的一个区域)。之后,Register和Statck又从设备上下文中重新载入,继续执行。(交换技术)

相比而言,快速进程则不用这么麻烦,因为它们有各自独立的Register和Stack,所以只要激活对应的Register(修改PSW)和指向Stack的指针(Mov SP,#XX)即可。

2)       进程状态(Task states)

RTX-51 recognizes four task states:

1.       READY     All tasks which can run are READY. One of these tasks is the RUNNING (ACTIVE)task.

2.       RUNNING   (ACTIVE) Task which is currently being executed bythe processor. Only one task (maximum) can be in this state at a time.

3.       BLOCKED   (WAITING) Task waits for an event.

4.       SLEEPING  All tasks which were not started or which have terminated themselves are in this state.
 

点击浏览下一页 

 

3)       进程调度(Task switch)

The RTX-51 system section which the processors assigns to the individual tasks is referred to as the scheduler (also dispatcher).

The RTX_51 scheduler works according to the following rules:

1.       The task with the highest priority of all tasks in the READY state is executed.

2.       If several tasks of the same priority are in the READY state,the task that has been ready the longest will be the next to execute.

3.       Task switchings are only executed if the first rule would have been otherwise violated (exception: round-robin scheduling).

Time-slice task change (round-robin scheduling) are executed if the following conditions are satisfied:

1.       Round-robin scheduling must be enabled (see configuration).

2.       The RUNNING task has the priority of 0 and is currently not executing a floating-point operation (see section "Floating-Point Operations", page 28).

3.       At least one task with the priority zero must be in the READY state.

4.       The last task change must have occurred after the selected system time interval (see system function "os_set_slice"). The system time interval can be changed dynamically during program execution.

4)       进程通信和同步(Task Communication and Synchronisation)

There three two mechanisms:

1.       Signal

Signals represent the simplest and fastest form of task communication. These can always be used when a pure task synchronisation is required without data exchange.

Each active task contains its own signal flag with which the following operations can be executed:

l       Wait for a signal

l       Send signal

l       Clear signal

The task number (see section section "Task Declaration") of the receiver task is used for identifying the signals for the individual operations.

2.       Message(via MailBoxes(FIFO))

n       By means of the mailbox concept, messages can be exchanged free of conflicts between the individual tasks.

n       RTX-51 provides a fixed number of eight mailboxes. Messages can be exchanged in words (2 bytes) via these mailboxes. In this case, a message can represent the actual data to be transferred or the identification of a data buffer(defined by the user). In comparison to the signals, mailboxes are not assigned a fixed task, but can be freely used by all tasks and interrupt functions. These are identified with a mailbox number.

Mailboxes allow the following operations:

l           Send a message

l           Read a message[page]

Each mailbox is internally consists of three wait lists.(Figure 2)
点击浏览下一页

Figure 2

1.       Message list        

List of the messages written in the mailbox. These comprise a maximum of eight messages.

2.       Write wait list     

Wait list for tasks which want to write a message in the message list of the mailbox (maximum 16 tasks).

3.       Read wait list      

Wait list for tasks which want to read a message from the message list of the mailbox (maximum 16 tasks).

 

3.       Semaphore

n       By means of the semaphore concept, resources can be shared free of conflicts between the individual tasks.

n       A semaphore contains a token that your code acquires to continue execution. If the resource is already in use, the requesting task is blocked until the token is returned to the semaphore by its current owner.

n       There are two types of semaphores: binary semaphores and counting semaphores. As its name implies, a binary semaphore can only take two values: zero or one (token is in or out). A counting semaphore, however, allows values between zero and 65535.

RTX-51 provides a fixed number of eight semaphores of the binary type.

Semaphores allow the following operations:

l                 Wait for token

l                 Return (send) token

 

2.       中断管理

RTX-51 performs task synchronisation for external events by means of the interrupt system.

Two types of interrupt processing are basically supported in this case:

 

1.     单片机c语言 Interrupt Functions (Interrupt are processed by c51 interrupt funcions)

l           Very sudden, periodically occurring interrupts without large coupling with the rest of the system (only infrequent communication with RTX-51 tasks, etc.).

l           Very important interrupts which must be served immediately independent of the current system state.

2.     Task Interrupts(Interrupt are processed by fast or standard tasks of RTX-51

l           Fast Task Interrupts

Important or periodic interrupts which must heavily communicate with the rest of the system when they occur.

l           Standard Task Interrupts

Only seldom occurring interrupts which must not be served immediately.

RTX-51 shows considerable different response times for fast and standard tasks.

u   The INTERRUPT ENABLE registers of the 8051 are managed by RTX-51 and must not be directly manipulated by the user!

u   The Interrupt Priority registers of the 8051 (not to be confused with the softwaretask priorities) are not influenced by RTX-51.

3.       动态内存管理

RTX-51 uses a simple and effective algorithm, which functions with memory blocks of a fixed size. All memory blocks of the same size are managed in a socalled memory pool. A maximum of 16 memory pools each a different block size can be defined. A maximum of 255 memory blocks can be managed in each pool.

n         Generate Memory Pool

The application can generate a maximum of 16 memory pools with various block sizes. The application must provide an XDATA area for this purpose. The pool is stored and managed by RTX-51 in this area (see system function "os_create_pool").

n         Request Memory Block from Pool

As soon as a pool has been generated, the application can request memory

blocks. The individual pools are identified by their block size in this case.

If an additional block is still free in the pool, RTX-51 supplies the start address

of this block to the application. If no block is free, a null pointer is returned (see

system function "os_get_block").

n         Return Memory Block to Pool

If the application no longer needs a requested memory block, it can be returned

to the pool for additional use (see system function "os_free_block").

4.       时间管理

RTX-51 maintains an internal time counter, which measures the relative time passed since system start. The physical source of this time base is a hardware timer that generates an interrupt periodically. The time passed between these interrupts is called a system time slice or a system tick.

This time base is used to support time dependent services, such as pause or timeout on a task wait.

Three time-related functions are supported:

n         Set system time slice

The period between the interrupts of the system timer sets the "granularity" of the time base. The length of this period, also called a time slice, can be set by the application in a wide range (see system function "os_set_slice").

n         Delay a task

A task may be delayed for a selectable number of time slices. Upon calling this system function the task will be blocked (sleep) until the specified number of system ticks has passed (see system function "os_wait").

n         Cyclic task activation

For many real-time applications it is a requirement to do something on a regular basis. A periodic task activation can be achieved by the RTX interval wait function (see system function "os_wait"). The amount of time spent between two execution periods of the same task is controlled, using os_wait, and is measured in number of system ticks and may be set by the application.

5.       RTX(FULL)函数纵览(Functions Overview)

Initialize and Start the System:

os_start_system (task_number)

Task Management:

os_create_task (task_number)

os_delete_task (task_number)

os_ running_task_id ()

Interrupt Management:

os_attach_interrupt (interrupt)

os_detach_interrupt (interrupt)

os_enable_isr (interrupt)

os_disable_isr (interrupt)

os_wait (event_selector, timeout, 0)

oi_set_int_masks (ien0, ien1, ien2)

oi_reset_int_masks (ien0, ien1, ien2)

Signal Functions:

os_send_signal (task_number)

os_wait (event_selector, timeout, 0)

os_clear_signal (task_number)

isr_send_signal (task_number)

Message Functions:

os_send_message (mailbox, message, timeout)

os_wait (event_selector, timeout, *message)

isr_send_message (mailbox, message)

isr_recv_message (mailbox, *message)

Semaphore Functions:

os_send_token (semaphore)

os_wait (event_selector, timeout, 0)

Dynamic Memory Management:

os_create_pool (block_size, *memory, mem_size)

os_get_block (block_size)

os_free_block (block_size, *block)

Functions with the System Clock:

os_set_slice (timeslice)

os_wait (event_selector, timeout, 0)

Debug Functions:

os_check_tasks (*table)

os_check_task (task_number, *table)

os_check_mailboxes (*table)

os_check_mailbox (mailbox, *table)

os_check_semaphores (*table)

os_check_semaphore (semaphore, *table)

os_check_pool (block_size, *table)

三.RTX51移植

本试验是用的RTX的Tiny版本。也就是说没有优先级之分,没有邮箱机制,没有动态内存的管理。移植它很简单,就是配置一下它带的配置文件,然后和写好的程序一起编译连接,连接的时候加一个rtxtny参数,意思是说当我连接的时候,我把RTXtiny的库文件连接上,也就等于是程序和操作系统编译在一起了。该配置文件能在安装目的rtxtiny2底下找到。文件名称为Conf_tny.A51,例如,在我的电脑中,路径为:D:\\Keil\\单片机c语言\\RtxTiny2\\SourceCode\\ Conf_tny.A51。如下图所示:

[page]

点击浏览下一页 

由于试验箱里面的芯片是AT89C51,所以要配置Conf_tny.A51的RAMTOP EQU 07F,目的是说配置内部RAM为128字节。

四.源程序代码:

源程序代码如下,说明请看代码里面的注释。

/*

**  RTX-51的移植

**  移植到AT89S52

**  此程序是循环花样显示LED灯

**  有三个显示样式,分别对应下面的三个进程

**  下面的算法中用到了“时间到空间”的转换,使得算法简化不少

**  此程序我已在最小系统板上试验通过。

*/

#include

#include

 

const unsigned char table[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80

                                               ,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0xFF,0x00};

/*时间到空间的转换,如果table是:

const unsigned char table[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

则算法和代码会多出一倍来。table数组虽然增加了一倍,算法也随之减少了一半,

好处当然不止在这里体现,下面的进程2也减少了一半*/

 

//进程0 左->右->左

void LED0 (void) _task_ 0

{

     int i;

     os_create_task(1);//创建进程1

     os_create_task(2);//创建进程2

     while(1)

     {

            for (i = 0; i < 15; i++)

            {

                      P1 = table[i];

                   os_wait(K_TMO,30,0);//等待30*10000微妙 = 0.3秒

            }

            os_send_signal(1);    //发送Signal信号,激活进程1

            os_wait(K_SIG,0,0);   //等待信号

     }

}

 

//进程1 全亮->全灭->全亮

void LED1 (void) _task_ 1

{

     int i;

     while(1)

     {

            os_wait(K_SIG,0,0);

            for (i = 0; i < 3; i++)

            {

                      P1 = table[15];   //全亮

                   os_wait(K_TMO,30,0);

                   P1 = table[16];       //全灭

                   os_wait(K_TMO,30,0);

            }

         os_send_signal(2);

     }

}

 

//进程2 两边->中间中间->两边

void LED2 (void) _task_ 2

{

     int i;

     while(1)

     {

            os_wait(K_SIG,0,0);

            for (i = 0; i < 8; i++)

            {

                   P1 =  table[i] | table[i+7];  //由于table长度多一倍,省去了一个循环,而且算法也简化了。

                   os_wait(K_TMO,30,0);

            }

            os_send_signal(0);

     }

}

五.总结:

本试验用的RTX 的Tiny 版本。许多比较高级的功能没有去实现。目的主要是理解RTX的原理,然后移植它到某个单片机上面,编写个小程序来测试一下。通过阅读RTX附带的英文文档,我对此操作系统有了深刻的认识,感到此操作系统有很多优点,也有很多不足的地方。比如支持的任务较少,不过由于是单片机,“承受”能力也有限,也能理解。总的来说,对于单片机来说是个不错的操作系统。

六.参考文档:

[1]《RTX-51 官方英文文档》(包括FULL版,和Tiny版)

[2]《Keil Software –Cx51 编译器用户手册》中文版

关键字:单片机  操作系统  RTX51 引用地址:单片机操作系统RTX51原理分析与移植

上一篇:HD44780读写C51程序
下一篇:基于GP21+EFM32的超低功耗超声波热量表

推荐阅读最新更新时间:2024-03-16 13:15

更耐用,ST推出新型STM32L4微控制器
意法半导体的 STM32L412和STM32L422 微控制器(MCU)以功能专一和封装紧凑为特色,为注重成本预算的消费类、工业和医疗应用带来超低功耗技术和优异的处理性能。 新微控制器为设计人员提供了更大的发挥空间,突破各种设计资源的制约,不论是内核性能和能耗限制,还是空间尺寸和物料清单成本限制。通过整合采用意法半导体的FlexPowerControl(FPC)技术经济合算的64KB或128KB闪存和80MHzArm®Cortex®-M4处理器内核,新微控制器的能效和性能达到了同级产品中最佳的EEMBC®测试成绩:273CoreMark® 可提升智能传感器或消费类穿戴设备的处理速度,同时167 ULPMark™-PP(P
[嵌入式]
更耐用,ST推出新型STM32L4<font color='red'>微控制器</font>
STM32——MCU结构简述(中英文对照)
首先STM32 MCU是什么? 其实他就是一个微控制单元(MicroController Unit) NVIC 嵌套向量中断控制器(Nested Vectored Interrupt Controller)(优先级) 作用 用于为中断分组,从而分配抢占优先级和响应优先级 SysTick 系统节拍定时器(system TIck ) 作用 具有自动重载和溢出中断功能,所有基于Cortex_M3处理器的微控制器都可以由这个定时器获得一定的时间间隔 Flash 存储器又称闪存,它结合了ROM和RAM的长处,不仅具备电子可擦除可编程(EEPROM)的性能,还不会断电丢失数据同时可以快速读取数据(NVRAM的优
[单片机]
STM32——<font color='red'>MCU</font>结构简述(中英文对照)
基于AVR Flash微控制器的电动车窗防夹系统设计的基本原理
  汽车上可自动关闭的电动车窗或车门设备潜藏着卡死,挤压以及可能伤人的危险。它们必须能够反向移动以防止马达所施加的力超出正常限制。这种特性意味着必须持续监视速度、电流和玻璃的位置。   由于成本和简化的原因,本文所描述的系统使用普通的带有霍尔效应传感器的刷式马达。基于速度和扭矩导数的检测算法已通过健壮性和容错性的验证。该算法可用于所有带有A/D 转换器和通过变化引发中断的I/O 口的AtmelAVR Flash 微控制器。本文描述的是基本原理,Atmel网站上的应用笔记有关于实现的详细描述。 现代汽车中的电动设备   目前,在高端客用汽车中电子组件和系统在成本中已占20%以上。增加电子设备的数目可以更好的控制传感器和致动器
[单片机]
基于AVR Flash<font color='red'>微控制器</font>的电动车窗防夹系统设计的基本原理
51单片机系列(1)-keil4工程创建
我使用的keil uvision4进行51单片机开发。 那么创建一个keil工程有以下几步: 1、keil4软件安装: keil4软件很容易下载,一般在淘宝卖家处购买单片机之后,会附赠相关的软件安装包和破解包,还有相关破解视频,故不赘述。 2、创建第一个工程: 进入软件之后我们定位到上方菜单栏,点击工程(如下图) 再点击新建工程 接下来就会保存工程路径,路径可以自行选择,注意文件名需要自行填写,否则无法保存,保存类型是默认的,不需要更改 接下来会弹出一个选择单片机CPU的窗口 我们使用的是51单片机,所以直接定位Atmel,我的单片机芯片型号是89C52,则定位Atmel下属的AT89C52,最
[单片机]
51<font color='red'>单片机</font>系列(1)-keil4工程创建
对于51单片机和arm9开发板串口通信问题的分析
距离毕设的时间还剩20天左右,这几天一直忙着做毕设,今天终于将51单片机和串口通信的问题解决了,抽出点时间,写一下遇到的问题。不然,过几天又忘了,记录下来也给后续学习的技术宅能提供一点帮助。 我的串口实验是:ARM9 控制板通过串口发送一个指令,51接受到这个指令后,根据这个指令控制小车的运行方式。(为什么不用arm9直接控制小车而采用51控制小车呢? 答:我的想法是电机驱动这一块直接交付给下一级控制器,程序简单,容易实现,况且通过串口,只需解析一个指令就可以实现)。 1、做此类串口通信一定要记得共地,我采用的方法是直接用5v直流输出引出了两个接口,分别为两个控制板供电,这样的话串口通讯的电压没有一点问题。 2、做串口通讯的
[单片机]
单片机EEPROM单字节读写操作时序
EEPROM 写数据流程 第一步,首先是 I2C 的起始信号,接着跟上首字节,也就是我们前边讲的 I2C 的器件地址,并且在读写方向上选择“写”操作。 第二步,发送数据的存储地址。24C02 一共 256 个字节的存储空间,地址从 0x00~0xFF,我们想把数据存储在哪个位置,此刻写的就是哪个地址。 第三步,发送要存储的数据第一个字节、第二个字节„„注意在写数据的过程中,EEPROM 每个字节都会回应一个“应答位 0”,来告诉我们写 EEPROM 数据成功,如果没有回应答位,说明写入不成功。 在写数据的过程中,每成功写入一个字节,EEPROM 存储空间的地址就会自动加 1,当加到 0xFF 后,再写一个字节,地址
[单片机]
义隆单片机PT2262无线解码程序
Start(void); void Stop(void); u8 CurrentRead(void); u8 RandomRead(u8 addr); void ByteWrite(u8 addr,u8 data); void SendByte(u8 data); void I2c_Ack(void); void I2c_NoAck(void); u8 I2c_CheckAck(void); u8 ReadByte(void); void CommandWrite(u8 addr); void PageWrite(u8 *data,u8 cnt,u8 BeginAddr); void SequentialRead(u8 *
[单片机]
单片机工作频率切换的实现
    随着单片机技术的发展,单片机各方面的性能都有了很大的提高,运行速度也越来越快,这使得单片机的应用也越来越广泛。     有时候由于外围设备的速度或者其他个些要求,单片机的运行速度并不能很高,有时还要求比较低,比如当8位单片机用于PC/XT总线接口时,工作频率是最高4.77MHz,那么单片机的工作频率就只能是4.77MHz,这样就会导致运行速度下降,当又有其它硬件要求工作频率较高时,怎样才能调和这两方面的矛盾呢?可不可以让单片机在与PC/XT接口时工作在4.77MHz,而在运行其他程序时工作在比较高的频率呢?     首先我们做了这样的实验,如图1,用外部振荡电路,通过分频得到两个高低不同震荡源(4:
[应用]
小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved