# Category Archives: Embedded Systems

## 实时操作系统杂谈

- uCosII / uCosIII | Micrium
- FreeRTOS
- Nucleus RTOS | Mentor Graphics
- RTLinux (需要MMU支持)
- QNX (需要MMU支持)
- VxWorks | WindRiver
- eCos

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

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

1、用了实时系统后，系统响应速度一定更快。

2、用实时操作系统就可以保证实时性。

## 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:

## 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 :

Figure 1. Raspberry PI Model B

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:

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

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

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

(To be continue)

## 串行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进行校验与坏块标记。

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

1、确定了外壳方案

2、确定了硬件选型

1）主控芯片使用STM32F103C8；

3）蓝牙模块使用BC-04；

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

3、确定了上位机平台