赞
踩
时至今日,已经不知道在嵌入式的道路上到底挣扎了多少个岁月,总感觉要“病入膏肓”了。此间总是不时出现一些疑惑:人家搞 Java、搞 C# 的动不动就是什么架构 / 框架的,搞了这么久的的嵌入式,我到底搞了什么?架构 / 框架统统木听说过。。。似乎那些高大上的东西都是针对上层编程的!难道嵌入式就这么 Low 么?
最近终于静下心来,有功夫拜读 Bruce Powel Douglass 的《Design Patterns for Embedded Systems in C》。在书中发现了不少更加系统化的东西,一些在实际工作中用到的但是不知道其准确定义的东西!但是,也同时发现,好多东西完全看不懂。。。以下内容主要是边读边记录的一些自己感觉有用的知识点,除此之外没有任何意义!此外,在这本书中,作者都是以面向对象的思想来进行举例说明的。如果你是一个纯嵌入式 C 编程人员,对于面向对象可能有些陌生。
最开始看的是这本书的中文翻译版,但是里面有些翻译看不懂。可能是个人水平太低,还没有理解那些高深的东西吧!而后直接去看了英文原版。因此,该文的学习记录都是基于英文原版的。具体为:英文原版内容都是 英文 +(翻译) 的形式给出,其他内容都是我自己的一些理解。其中有些部分确实不懂,均已 ??处理。
我们常说嵌入式系统,那什么是嵌入式系统?作者给出的定义:“a computerized system dedicated to performing a specific set of real-world functions, rather than to providing a generalized computing environment.(一个旨在处理现实世界中的具体功能的计算机系统,而不是提供通用的计算机环境的系统)”。我们生活中常见到的非 PC 参与的计算机系统基本都属于嵌入式系统。
An important subset of embedded systems are real-time systems(实时系统是嵌入式系统的一个重要子集). Many people have the mistaken impression that “real time” means “real fast” but that is not true(很多人错误的认为“实时”就是“真正的快”,这是不正确的). A real-time system is one in which timeliness constraints must be satisfied for system correctness(实时系统是指为了保证系统的正确性而必须满足时效性约束的系统).
A common, if simplistic, categorization of real-time systems is into two groups(实时系统可简单的分为两大类). “Hard” real-time systems(“硬”实时系统) are ones in which timeliness constraints are modeled as deadlines, points in time by which the execution of specific actions are required to be complete(完全以时间作为实时性约束条件,时间到之前必须完成指定动作). “Soft” real-time systems(“软”实时系统) are those that are not “hard”; that is, some other(usually stochastic) measure than deadlines is used to determine timeliness(引入除时间限制外的其他实时性约束条件). This may include average throughput(平均吞吐量), average execution time(平均执行时间), maximum burst length(最大脉冲长度), or some other measure. All systems may be modeled as hard real-time systems, but this often results in “over-designing” the system to be faster or have more available resources than is necessary, raising the recurring cost(approximately “manufacturing cost”) of the system.
From the inside, one of the most striking characteristics of embedded systems is severity of their constraints(从内部来看,嵌入式系统的一个显著地特点就是严格的约束条件).
Reliability, robustness, and safety are other kinds of constraints levied on embedded systems(可靠性、健壮性、安全性是嵌入式系统需要遵循的另一些约束条件).
在嵌入式系统中,我们可以选择使用操作系统来实现,也可以直接以裸机的形式来实现。嵌入式系统中,经常使用的系统都是实时操作系统(real-time operating system,RTOS)。实时操作系统(RTOS)是一种用于实时和嵌入式应用的多任务操作系统。
RTOSs run applications and tasks using one of three basic design schemas(实时操作系统使用三种基本设计模式之一运行应用程序和任务). Event-driven systems handle events as they arise and schedule tasks to handle the processing(事件驱动的系统通过任务调度器来处理出现的多个任务). Most such systems use task priority as a quantitative means by which to determine which task will run if multiple tasks are ready to run(通过任务优先级来决定哪个就绪的任务来运行). Task priorities are most often static(任务优先级通常为静态的)(i.e., specified at design time as such with rate-monotonic scheduling) but some are dynamic(也有动态的), varying the task priorities to account for current operating conditions(such as earliest deadline first scheduling*). The other two approaches to task scheduling implement a “fairness doctrine” either by giving all tasks a periodic time slice in which to run(time-base schemas, such as round robin scheduling(轮询调度)) or by running the task set cyclically(sequence-based schemas, such as cyclic executive scheduling).
中间件在实际工作中,还是经常用到的!在嵌入式中,RTOS 通常仅仅实现与 RTOS 相关的功能,其他的组件,例如网络协议栈、文件系统等则通常是有其他实现方式的。这行统统都可以称为中间件。
嵌入式系统的开发一个显著的特点就是需要与硬件进行协同开发。任何硬件的变动都可能导致之前设计的软件架构的变动。
嵌入式系统开发的一个难点就是在指定硬件上进行调试。The state of the art in developing defect-free software is an agile practice known as test-driven development(TDD)(开发没有缺陷的软件的最先进的方法被称为测试驱动开发(TDD)). In TDD, the unit tests for a piece of software are written simultaneously with, or even slightly before, the software it will verify(在 TDD 中,单元测试程序要与主程序同时编写,甚至提前编写). All too commonly, unit testing is skipped entirely or performed far too late to have any beneficial effect on the software(通常情况下,单元测试被完全跳过,或者执行得太晚,对软件没有任何有益的影响。).
There are many different kinds of unit tests that can be applied to software in general and embedded software in particular. These include(有许多不同类型的单元测试可以应用于一般的软件,特别是嵌入式软件。具体如下):
即使实在桌面系统(如 Windows)中,测试用例要涵盖以上所有的这些点也是非常困难。在嵌入式平台上就更加困难了。在嵌入式平台我们可以通过以下方式来进行测试:
Of the various kinds of tests, performance tests are usually the most difficult to adequately perform(在各种测试中,性能测试通常是最难以充分执行的).
Structured programming(结构化编程):Structured programming is a disciplined form of software development that emphasizes two separate and distinct aspects(结构化编程是一种规范的软件开发形式,强调两个独立且不同的方面。).
OO(面向对象):Object-oriented programming is based on an orthogonal paradigm(面向对象编程是基于正交范式的). Rather than have two separate taxonomies, object-oriented programming has a single one based on the notion of a class(面向对象编程没有两个单独的分类法,而是基于类的概念有一个单独的分类法。).
(1)这本书采用了基于面向对象的编程思想,因此后续章节讲了如何用 C 语言来模拟面向对象的思想。
(2)基本的模拟方法就是用结构体来封装数据和方法(函数指针)
(3)如果想要详细了解C实现面向对象的思想,光看本书这一点章节中的东西是远远不够的!
整个第二章主要就是以作者自己搞得 Harmony™ for Embedded RealTime 为例,来讲解如何进行嵌入式系统设计。
A design pattern is “a generalized solution to a commonly occurring problem.”(设计模式是对经常出现的问题的通解)To be a pattern, the problem must recur often enough to be usefully generalizable and the solution must be general enough to be applied in a wide set of application domains. If it only applies to a single application domain, then it is probably an analysis pattern5. Analysis patterns define ways for organizing problem-specific models and code for a particular application domain.
According to Gamma, et. al.6, a pattern has four important aspects:
整个第三章主要讲了各种访问硬件的设计模式。Probably the most distinguishing property of embedded systems is that they must access hardware directly(嵌入式系统最显著的特性可能是它们必须直接访问硬件). Broadly, software-accessible hardware can be categorized into four kinds – infrastructure, communications, sensors, and actuators(大体上,嵌入式软件可访问的硬件可以分为四类:基础设施、通信、传感器和致动器).
Let’s turn our attention now to a number of design patterns that have proven themselves useful for the manipulation of hardware(开始介绍设计模式). The Hardware Proxy Pattern, discussed next, is an archetypal pattern for the abstraction of hardware for the purpose of encapsulating details that are likely to change from the usage of the information provided to or by the hardware(接下来讨论的硬件代理模式是以封装详细信息为目的的硬件抽象模式的一个比较典型的模式,这里没明白!). The Hardware Adapter Pattern extends the Hardware Proxy Pattern to provide the ability to support different hardware interfaces(硬件适配模式主要是扩展硬件代理抹模式,以提供支持不同硬件接口的能力). The Mediator Pattern supports coordination of multiple hardware devices to achieve a system level behavior(中介模式支持多个硬件设备的协调以实现系统级行为). The Observer Pattern is a way of distributing sensed data to the software elements that need it(观察者模式是将感测数据分发给需要它的软件元素的一种方式). The Debouncing and Interrupt Patterns are simple reusable approaches to interface with hardware devices(去抖动和中断模式是与硬件设备接口的简单可重用方法). The Timer Interrupt Pattern extends the Interrupt timer to provide accurate timing for embedded systems(定时器中断模式扩展了中断定时器,为嵌入式系统提供准确的定时).
The Hardware Proxy Pattern creates a software element responsible for access to a piece of hardware and encapsulation of hardware compression and coding implementation(硬件代理模式创建一个软件元素,负责访问硬件并封装硬件压缩和编码实现).
The Hardware Adapter Pattern provides a way of adapting an existing hardware interface into the expectations of the application. This pattern is a straightforward derivative of the Adapter Pattern(硬件适配器模式提供了一种使现有硬件接口适应应用程序期望的方法。 此模式是适配器模式的直接派生).
The Hardware Adapter Pattern extends the Hardware Proxy Pattern to provide the ability to support different hardware interfaces(硬件适配器模式扩展了硬件代理模式,以提供支持不同硬件接口的能力). The implementation of the Hardware Proxy and the Hardware Adapter can be merged, but that undermines the reusability of the Hardware Proxy(硬件代理和硬件适配器的实现可以合并,但是这会破坏硬件代理的可重用性).
The Mediator Pattern provides a means of coordinating a complex interaction among a set of elements(中介模式提供了一种协调一组元素之间复杂交互的方法).
The Mediator Pattern supports coordination of multiple hardware devices to achieve a system level behavior(中介模式支持多个硬件设备之间的协调,以实现系统级行为).
The Mediator Pattern uses a mediator class to coordinate the actions of a set of collaborating devices to achieve the desired overall effect(中介模式使用中介类来协调一组协作设备的操作,以达到预期的总体效果。). The Mediator class coordinates the control of the set of multiple Specific Collaborators (their number is indicated by the ‘*’ multiplicity on the association between the Mediator and the Specific Collaborator in Figure 3-5). Each Specific Collaborator must be able to contact the Mediator when an event of interest occurs.
The Observer Pattern is a way of distributing sensed data to the software elements that need it.
The Observer Pattern is one of the most common patterns around. When present, it provides a means for objects to “listen in” on others while requiring no modifications whatsoever to the data servers. In the embedded domain, this means that sensor data can be easily shared to elements that may not even exist when the sensor proxies are written.
This simple pattern is used to reject multiple false events arising from intermittent contact of metal surfaces.
Push buttons, toggle switches, and electromechanical relays are input devices for digital systems that share a common problem – as metal connections make contact, the metal deforms or “bounces”, producing intermittent connections during switch open or closure. Since this happens very slowly(order of milliseconds) compared to the response speed of embedded systems(order of microseconds or faster), this results in multiple electronic signals to the control system. This pattern addresses this concern by reducing the multiple signals into a single one by waiting a period of time after the initial signal and then checking the state(嵌入式的许多输入设备,例如按键、开关等受制于物理限制,其响应速度与嵌入式设备处理速度不是一个数量级)
The physical world is fundamentally both concurrent and asynchronous; it’s nonlinear too, but that’s a different story. Things happen when they happen and if your embedded system isn’t paying attention, those occurrences may be lost. Interrupt handlers(a.k.a. Interrupt Service Routines, or ISRs) are a useful way to be notified when an event of interest occurs even if your embedded system is off doing other processing.
Another common pattern for getting sensor data or signals from hardware is to check periodically, a process known as polling(通过周期性访问来获取数据,即为轮询). Polling is useful when the data or signals are not so urgent that they cannot wait until the next polling period to be received or when the hardware isn’t capable of generating interrupts when data or signals become available(非紧急性数据或没有中断时,该模式非常有用!).
This pattern comes in two flavors(这个模式有两种版本). Figure 3-17 shows the pattern structure for opportunistic polling(机会轮询) while Figure 3-18 shows the pattern for periodic polling(周期轮询). The difference is that in the former pattern the applicationFunction will embed calls to poll() when convenient, and in the latter, a timer is created to start polling(区别是:前者是在何时的实际去轮询,而后者则是在定时器中轮询).
Periodic polling is a special case of the Interrupt Pattern(周期轮序时中断模式的一个特殊情况). In addition, the hardware checks may be done by invoking data acquisition services of Hardware Proxies or Hardware Adapters. In addition, the Observer Pattern can be merged in as well, with the polling element(OpportunisticPoller or PeriodicPoller) serving as the data server and the PollDataClients serving as the data clients.
先跳过,看看后面的状态机
Behavior can be defined as a change in condition or value over time(行为可以定义为条件或值随时间的变化). It is often a response to an external event or request, but autonomous systems decide for themselves when and how to behave(它通常是对外部事件或请求的响应,但自治系统自行决定何时以及如何表现。). 本书中将行为分为四类:
cos(π / 4)
。 A Finite State Machine(FSM) is a directed graph composed of three primary elements: states, transitions, and actions(有限状态机(FSM)是由三个主要元素组成的有向图:状态,转换和动作). A state is a condition of a system or element(usually a class in an object-oriented system)(状态是系统或元素的状态(通常是面向对象系统中的类)).
A transition is a path from one state to another, usually initiated by an event of interest(转换是从一个状态到另一个状态的路径,通常由感兴趣的事件启动); it connects a predecessor state with a subsequent state when the element is in the predecessor state and receives the triggering event.
The actual behaviors executed by the element are represented in actions(实际的动作通过代表动作的元素来执行).
Single event receptor state machines(henceforth known as SERSMs,单事件接收器状态机) can be used for both synchronous and asynchronous events(同时用于同步事件和异步事件).
Multiple event receptor finite state machines(MERSMs,多事件接收器状态机) are generally only used for synchronous state machines(通常用于同步事件状态机) because the client is often aware of the set of events that it might want to send to the server state machine. In this pattern, there is a single event receptor for each event sent from the client.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。