Category Archives: Embedded Systems

实时操作系统杂谈

实时操作系统 (RTOS) 在工业控制和电力系统中有大量应用。我自己接触实时操作系统已有几年时间,虽然并没有实际的项目使用经验,然而耳濡目染总归积累了一些经验和知识。在这里进行整理和分享。

实时操作系统(Real-time Operating System, RTOS)是针对有实时性要求的应用而设计的操作系统。这些应用通常包括汽车引擎控制、轨道交通、工业机器人、飞行器控制系统等。实时操作系统一般提供抢占式调度机制,重要的高优先级任务可以剥夺低优先级任务对CPU的使用权;同时,任务在等待使用资源时,RTOS可以将其CPU的使用权释放给其他就绪的任务,从而使得系统的总体响应速度更快。

目前市场上常见的商用实时操作系统一般有:
- uCosII / uCosIII | Micrium
- FreeRTOS
- Nucleus RTOS | Mentor Graphics
- RTLinux (需要MMU支持)
- QNX (需要MMU支持)
- VxWorks | WindRiver
- eCos

其中除了FreeRTOS和RTLinux外,其余RTOS都是需要商业授权的。uCos II和FreeRTOS是平时接触比较多的RTOS,相关资料比较多。而VxWorks是安全性公认最佳的,用于航空航天、轨道交通和卫星的应用。如果系统中需要使用复杂的文件、数据库、网络等功能,那么RTLinux是比较好的选择;但是如果系统对实时性和性能的要求极高,那么可以使用较为简单的RTOS(如uCosII),再根据需要开发协议或者软件包。总体上来说,操作系统的复杂性是与应用软件的复杂性一致的。同时,功能上更复杂的RTOS对硬件系统的要求也会更高。

一般的RTOS会提供以下全部或部分功能:
- 基于静态优先级(fixed-priority)的抢占式(preemptive)任务调度;
- 进程间通信(基于消息,消息邮箱,管道);
- 基于信号量(semaphore)的进程间同步;
- 任务的创建、暂停、删除;
- 资源访问控制;
- 临界区(critical section)控制;
- 驱动程序接口;
- MMU内存管理、内存动态申请与分配;
- 其他:如GUI用户界面和TCP/IP相关功能。

一般来说,实时操作系统的主要参数指标有:
- 支持的优先级数量,如64、128或256;
- 使用的任务调度算法;时间片轮转调度,加权轮转调度(weighted round-robin),先入先出(FIFO),优先级调度;
- 中断响应速度,即从中断产生到进入中断服务程序的时间;
- 上下文切换时间,即任务切换时间;
- 操作系统大小以及资源使用(ROM及RAM的占用);
- 授权费用与授权方式,是按产品型号计费、产品数量计费还是一次性授权。

其他的选择指标主要就是文档的完整程度,是否有GUI支持,团队对该OS的了解程度,所支持的CPU型号以及需求功能的规模。从更专业的角度上来说,还有是否支持防死锁(deadlock)和优先级反转(priority inversion)等提高系统可靠性的功能,操作系统自身服务程序占用的时间大小。对于时间关键性应用,操作系统需要具备相对确定的执行时间(deterministic execution time)。从调试的角度来说,操作系统是否具有调试功能(尤其是多线程、多核)以及支持的调试工具也是重要的指标。

关于实时操作系统的两个误区:
1、用了实时系统后,系统响应速度一定更快。
不一定。因为实时操作系统本身引入了执行开销,所以对于小型应用来说,有RTOS的性能也许不如无操作系统的情况。实时操作系统的优势最能体现在中大型系统中,当任务间存在复杂的耦合和依赖关系,并且应用程序经常要长时间等待外部资源时。

2、用实时操作系统就可以保证实时性。
不一定。相对来说,使用实时系统可以改善系统的实时性。但是实时操作系统只是作为工具存在的,如果需要提供实时性保障,还需要使用实时系统理论对任务的可调度性和响应时间进行分析,才可以得到科学、系统的响应性保障。

实时系统研究意义的思考

要想弄清研究实时系统的意义,首先必须明确实时系统在整个人类生活中的角色。

实时系统的主要研究对象是工业控制,交通和航空航天,电力及能源,网络设备及网络服务。这些系统的稳定性很大程度上决定了生命财产与经济的安全性:如电力系统的短时间崩溃会导致工业和交通停滞,并可能带来生命财产事故;航天飞行器的软件故障,会导致与地面控制中心的失联甚至是坠毁,带来巨大的财产损失。社会的正常发展和秩序很大程度上依赖于这些实时系统的稳定性和可靠性。纵使我们不想如此,人类生活的方方面面还是很大程度上依存于并不稳定的软件系统。设想如果有这样一个软件漏洞,使得多个重要系统在同一时间失控,那么其带来的社会影响和经济损失将不可估量。 实时系统的主要研究目标:保障实时系统在时间和行为上的可预测性,设立可靠的软件设计方法;就是在预防和避免以上的不可靠情况的出现。

至于在当前计算机速度已经如此之快的情况下,实时性研究是否还有其必要性。

答案我想也是肯定的。在新的计算机结构出现之前,CPU速度的提升不等同于系统实时性的提升。CPU速度的提升显然会减少程序的执行时间 (execution time),但是对于从请求产生到得出结果的响应时间 (response time) 及IO的输出间隔的稳定性 (IO jitter) 依然没有明确的保证。与此同时,更加复杂的操作系统和计算机硬件也提高了这种不确定性,例如Cache缓存和内存分页带来的运行时间浮动。综上所述,CPU速度的提升无法带来实时性上的保证,依然需要依靠实时性分析来保障系统在时间上的可预测性。

Hard v.s. Soft real-time systems

When you study your first lecture in Embedded Systems, you will definitely hear the term 'hard' real-time and 'soft' real-time. Systems such as avionic systems, automobile engine control systems and cardiac pacemakers could be included in the category of hard real-time systems, while web servers, human-machine interfaces and multimedia systems are soft real-time systems. By intuition, we can infer that these hard real-time systems are more critical and important than the soft ones. But what is the really distinction between them?

To answer this question, we should first introduce the notion of 'timing requirements'. Timing requirements, which is also known as timing constraints, are metrics that specify the temporal behaviors of tasks during run-time. Two most important parameters when defining timing requirements are the response time (R) and the deadline (D). The deadline of a task is the time instant by which the task should be completed and the response time is the execution time between the release and the completion of the task.

Now we can give a formal definition of hard real-time: a hard real-time system is a system where all tasks in that system should be completed before their own pre-defined deadlines, i.e., R_i <= D_i . Any failure to meet the deadline could possibly lead to disastrous consequence, e.g., chemical leakage, airplane crash or life risk.

When it comes to soft real-time systems, the deadline could be missed occasionally with the only consequence that the usefulness of the result could be degraded. Take the MP3 player system as an example. The failure of delivering the audio steam in time will only cause some annoying lags, but will not do any harm to the person who is listening to it.

Since hard real-time systems require a high level of predictability, the specification is often given in a deterministic way. On the other hand, the timing specification of a soft real-time system is often defined in a probabilistic form, e.g., the probabilistic of deadline misses in every 5 minutes is less than 1%.

The high requirement of temporal behavior of hard real-time systems poses a great challenge in designing such systems. Many restrictions should be imposed on the design of the software, as well as the hardware architecture. Formal methods should also be used to prove the system could work properly under any possible conditions without any deadline miss.

In reality, not all systems can be identified as either soft or hard real-time systems. In these circumstances, the system can be separated into the hard part and the soft part sub-systems and be designed and verified separately, in order to meet their own specifications.

Reference
[1] Jane W. S. Liu, Real-time Systems, Prentice Hall, 2000, page 26-33

Real-time Scheduling in a big picture

Real-time Scheduling Theory has been developed over last 40 years, since the first published work of Liu and Layland in 1973. Before that, real-times systems were designed with cyclic executives, which is in a ad-hoc manner and very difficult to maintain. In general, the theory is consist of task modelling, scheduling policy and schedulability test. Some other issues, e.g., resource management, response time analysis, worst-case execution time analysis, are also involved in the design of real-time systems. Here is a big picture of the real-time theory, based on my knowledge so far:

Research_Realtime Scheduling

What is the BEST Hardware Platform meaning to me

Last week I tried to make my new Intel Galileo running a basic 14 x 7 LED Matrix program which was working perfectly on my old Arduino UNO. This Intel powered opensource platform has a CPU frequency of 400MHz while the Arduino only got 16MHz. The interesting thing I found is, after I spent 3 hours to port the program to Galileo and executed it with full of expectation, the program turned out to work crappy: the LED matrix looked unstable and each pixel had a different luminance.

I expected the Galileo, which has 20 times more computational power, should have an brilliant performance and blows my Arduino two streets away. But the thing is, is it really appropriate to run a real time program on a Linux based platform? And is it fair enough to compare two platforms in one certain application?

The answer is NO, and this is exactly the motivation for me to write this article. During the last year, I had collected and played enormous platforms, either for using in my project or simply explore the features of a new platform. During this process, I realized that tools are just tools and there is no BEST platform exists. To make it clear, I will compare some of the platforms I got in hand and analysis their own advantages and disadvantages, and finally make a conclusion.

1. Linux Platforms

Well, apart from the aforementioned Intel Galileo, I got the popular Raspberry PI and less known pcDuino :

DSC05191_out
Figure 1. Raspberry PI Model B

DSC05195_out
Figure 2. pcDuino version 2

Generally speaking, these two platforms have a similar performance and functions: both are working at a high frequency, both running Linux operating system and all of them have some kind of GPIO extensions and USB connections. The Raspberry PI has a large community and no matter what problem you encountered, from running a program in desktop environment to compiling the Linux Kernel, you can find your answer. On the other hand, the pcDuino is not well supported and also not even well documented. So why I bought another Linux single board computer when I already got one? The answer is the additional Arduino connectivity of the pcDuino.

The pcDuino can seemless 'run' Arduino sketch locally without the need to adhere another Arduino board. Is this convenience really worth 60 pounds? Well, I think so. This design eliminates the overhead of communicating with additional hardware through serial and makes my life easier to develop more complex project where the need of hardware functions like GPIO / ADC / DAC is an essential. Apart form this aspect, I couldn't find other difference between these two platforms, and it is also for this reason that I decided to buy one more Linux platform.

2. Visual Sensors

I am pretty new to computer vision and I only started to investigate different camera since the beginning of this year. What I got so far are two normal web cameras, two PS Eye cameras for Playstation 3 and a Kinect from Microsoft:

DSC05200_out2
Figure 3. My Collection of Cameras and Visual Sensors

Among them, the Kinect is a star which is being used by hundreds of institutions and researchers in recent years. It is actually a stereo camera which can give you depth image of the scenario. What I have done so far is simply to configure the SDK and run some examples on Processing, a JAVA programming software for interactive user interface. It is a perfect sensor for robotics and can be used in applications such as V-SLAM, 3D reconstruction and manipulator as what has been done with the Boris, the newest robot invented by Birmingham University. The only thing that prohibits me from using it further, is the complex toolchain that it comes with. As first released only for XBOX 360, it is not well support by PCs and you have to use opensource packages like OpenNI and the driver from PrimeSense to make it work.

Another idea to play with stereo vision is to use a pair of monocular cameras and I found the Sony PS3 Eye is an ideal platform for me. It is cheap (8 pounds each), fast (60 fps @ 640 x 480) and fairly easy to use. You can simply buy a SDK from the company Code Laboratories and you can simply use OpenCV like what you have done with other web cameras. The adjustment and configuration of two separate cameras are tricky but it is a good idea for researchers who want to have a feeling of basic principles about stereo vision. What really interested me about this camera is it is really easy to use and can also achieve a competitive frame rate compared to these professional industry cameras. It is indeed a good platform for fast object tracking, but the thing is, it is not general and you should buy a SDK before use. So if you want to make it a product for customs, it would be problematic for the software license. So sometimes we may prefer to use off-the-shelf web cameras.

If you tried different cameras, you may find there is even difference between web cameras. I bought this HP HD2300 after I got my previous Logitech C270. Why? Simply because it has a higher and crisp resolution and it has a better white balance performance which is important for vision systems for detecting color features. But why I still keep & use the Logitech C270? Well, for CPU with limited computational power (such as the Raspberry PI), higher resolution doesn't mean higher performance but resulted in lags and failures.

3. Microcontrollers

DSC05202_out
Figure 4. Microcontrollers I got in my personal lab (AVRs)

DSC05208_out
Figure 5. Microcontrollers I got in my personal lab (Cortex-M3 / M4)

(To be continue)

串行Flash芯片w25x16使用心得

这一段时间因项目需要使用了Winbond公司的w25x16/w25x32串行Flash存储芯片,对Flash芯片的使用方法略有心得,现整理成文。

在Flash中存储数据与在EEPROM中存储数据不同,不仅需要考虑数据的存储结构,还需要针对Flash独有的特性进行特殊处理。其中最显著的不同点在于Flash在编程前,必须保证待写入区域全部为空(0xFF),否则无法正常执行写入操作;并且Flash在操作时一般需要按数据区域(Section)进行操作,而并非字节。

串行Flash的主要特性有(以w25x16为例):
1、写入前必须保证待写区域为空(全部为0xFF);
2、一次批量编程最多只能写入Page(256B)的大小,如果中途发生换页,需要更改地址后重新发送写入命令;
3、一次擦除至少擦除一个Section(4KB);
4、16个页(Page)构成一个数据区域(Section),16个数据区域(Section)构成一个数据块(Block);
5、擦除时间较长,约为300ms/Sector;
6、编程时间为150+12*n us (折合3ms / page);
7、大容量Flash存在坏区的可能。

对以上问题进行分析,我们可以得到以下一些结论:
1、必须对当前可用的数据块进行检查与标记,从而在编程前保证数据区域的写有效性;
2、单次最多只能写入256字节,当写入的数据发生了换页,需要进行页面切换;如果写入的数据大于256字节,必须进行拆分;
3、若想修改单个数据,必须将整个数据进行读-修改-擦除-写入操作,必须保留与Section同样大小(4K)的临时存放空间。又因为擦除和编程都是比较费时的,可以看到,这个操作代价很高,所以应该尽量避免频繁的修改;
4、擦除时间长还会提高在擦除后、写入前发生断电导致数据全部丢失的风险,需要有一定的安全机制;
5、批量写入的效率更高,可以有一定的写缓冲机制以提高性能;
6、需要对Flash进行校验与坏块标记。

可以看到,Flash的这些特性很像硬盘。在操作系统下,可以使用近乎相同的驱动与文件系统,比如现在流行的SSD硬盘,正是发挥了Flash电原理而非机械原理的速度优势,并且很容易对现有硬盘系统进行直接替换。但换一个角度,在嵌入式应用中,这些特性确实大大增加了编程与设计的难度。

Wattmate家电功耗监测仪 | 工作最新进展

自Wattmate立项以来已有数月时间,目前主要的进展有:

1、确定了外壳方案

这是第一版的外壳方案,将插线座的前端断开,串联主PCB,并放在一个小盒子中。

这是第二版方案,比第一版更加简洁。新版本在常见的插线座上进行改造,将插线座的前两排去除、修整后,用于放置PCB板。

2、确定了硬件选型

经过很长一段时间的比较选型之后,最后确定了以下硬件方案:

1)主控芯片使用STM32F103C8;

2)电量采集芯片使用ADI的ADE7753;

3)蓝牙模块使用BC-04;

4)采样电阻使用锰铜合金:2mΩ±5%-1.5~2W,锰铜/温度系数50ppm/℃,ф2.5mm*8mm/跨度10mm。

3、确定了上位机平台

上位机支持通过Android智能手机查看实时数据和历史数据,Android版本V4.0。

嵌入式操作系统uCosII的资费

Micrium的μc/OS-II及系列产品全部提供源代码,销售模式为一次性买断授权 (license),没有时间及数量的限制,并根据客户产品的类型分为:

  1. Single Product(一个产品),此种授权需要定义客户使用的主CPU及详细的产品名称及型号。一次性买断授权的价格为¥59950(含17%税)。
  2. Product Line(产品线),此种授权和CPU无关,需要定义客户的产品线/产品系列名称。价格为single 的6倍。
  3. Per-CPU type(按照CPU型号授权),此种授权和产品名称/类型无关,需要定义客户使用的CPU型号。价格为single的8倍。