嵌入式设计模式(Design Patterns for Embedded Systems in C)的学习记录

版权声明:进步始于交流,收获源于分享!转载请保留原文出处,谢谢! https://blog.csdn.net/ZCShouCSDN/article/details/80217199

  工作三年有余,一直在搞嵌入式。准确的说从大学开始就一直在搞,已经到了病入膏肓的地步。此间总是不时出现一些奇怪的想法:人家搞Java、搞C#的动不动就是什么架构/框架的,搞了这么久的的嵌入式,我到底搞了什么?架构/框架?统统木听说过。。。似乎那些高大上的东西都是针对上层编程的!难道嵌入式就这么Low么?
  最近终于静下心来,有功夫读了读Bruce Powel Douglass的《Design Patterns for Embedded Systems in C》。读完全书用了好久。。在书中发现了不少更加系统化的东西,一些在实际工作中用到的但是不知道其准确定义的东西!但是,也同时发现,好多东西完全看不懂。。。以下内容主要是边读边记录的一些自己感觉有用的知识点,除此之外没有任何意义!

(1)最开始看了看这本书的中文翻译版,看看了里面有些翻译看不懂(没办法,水平太低,可能还没有理解那些高深的东西)!
(2)貌似在这本书中,作者都是以面向对象的思想来进行举例说明的。如果你是一个纯嵌入式底层人员,对于面向对象可能有些陌生。
(3)以下内容不是翻译!不是翻译!不是翻译!仅仅是在看书的时候觉得有用的东西,做一下记录而已。其中有些部分确实不懂,均已??处理。

看的第一部分

  主要就是本书的前三个章节。陆陆续续看完,这部分看完比较早,先记录一下。下面这些英文标题、章节名称,与原书中的标题是一致的。

CHAPTER 1 What Is Embedded Programming?

1.1 What’s Special About Embedded Systems?

  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.

1.1.1 Embedded Design Constraints

  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(可靠性、健壮性、安全性是嵌入式系统需要遵循的另一些约束条件).

1.1.3 OS, RTOS, or Bareback?

  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).

1.1.4 Embedded Middleware

  中间件在实际工作中,还是经常用到的!

1.1.6 Debugging and Testing

  The state of the art in developing defect-free software is an agile practice known as ***test-driven development (TDD)***(开发没有缺陷的软件的最先进的方法被称为测试驱动开发). In TDD, the unit tests for a piece of software are written simultaneously with, or even slightly before, the software it will verify(单元测试程序要与主程序同时编写,甚至提前编写).
  There are many different kinds of unit tests that can be applied to software in general and embedded software in particular. These include(软件常用的单元测试分类如下):

  • Functional(功能) – tests the behavior or functionality of a system or system element(测试功能是否完善)
  • Quality of Service(服务质量) – tests the “performance” of a system or system element, often to measure the performance of the system or element against its performance requirements (测试性能是否达标)
  • Precondition tests(先决条件) – tests that the behavior of the system or system element is correct in the case that the preconditional invariants are met and in the case that the preconditional invariants are violated (测试预定的条件满足或者不满足时,系统的表现)
  • Range(区间) – tests values within a data range
  • Statistical(统计) – tests values within a range by selecting them stochastically from a probability density function (PDF)
  • Boundary(边界) – tests values just at the edges of, just inside, and just outside a data range (测试区间边界内、边界上及边界外的值)
  • Coverage(覆盖率) – tests that all execution paths are executed during a test suite
  • Stress(压力) – tests data that exceeds the expected bandwidth of a system or system element
  • Volume(容积) – also known as “load testing(负载测试)” – tests the system with large amounts of data that meet or exceed its design load
  • Fault Seeding(故障播种?) – tests in which a fault is intentionally introduced to the system to ensure the system handles it properly (这个应该是指故意引入一些错误,看看系统反应)
  • Regression tests(回归测试) – normally a subset of previously passed tests to ensure that modification to a system did not introduce errors into previously correctly functioning systems

  In a desktop system, unit testing software with test cases that explore all of these concerns is rare (and difficult) enough. When you embedded that software on a less-accessible target platform it becomes even more difficult. A number of different strategies can be employed to execute test cases, such as(这里主要是说,写测试用例是,要覆盖全部的问题是非常困难的,尤其是在嵌入式平台上,以下是一些嵌入式平台常用的测试手段)

  • “printf” testing – tests the system by writing to a file or to stdout (使用printf打印可能的错误,这个非常常用!)
  • “Test buddies” – writing test fixtures that embed the test cases in their own functionality
  • Testing on host – performing most of the tests on the host platforms using a host native complier and a critical subset on the target platform using a cross compiler (这个在嵌入式Linux中非常常用,其他也可以通过硬件调试器来实现)
  • Simulating on host – simulating the target platform on the host with cross-compiled software and retesting a critical subset on the target with the same object code (许多开发工具中自带软件模拟功能,基本可以模拟硬件运行,可以试着用用,但是通常会使用硬件调试器来实现硬件调试)
  • Commercial software testing tools – using software testing tools, such as TestRT™, LDRA™, or VectorCAST™ (主要是使用一些软件追踪器)
  • Commercial hardware-software integrated tools – this includes tools such as logic analyzers, in-circuit emulators, JTAG-compliant testing tools, and ROM emulators (使用硬件调试器)

1.2 OO or Structured – It’s Your Choice

Structured programming:

  • On one hand, functions or procedures form the foundation of behavioral programming(函数和过程形成基本的编程基础)
  • The other side of structured programming is the notion of data structuring(数据的结构化).

OO:Object-oriented programming is based on an orthogonal paradigm(面向对象编程基于正交范式).

(1)这本书采用了基于面向对象的编程思想,因此后续章节讲了如何用C语言来模拟面向对象的思想。
(2)基本的模拟方法就是用结构体来封装数据和方法(函数指针)
(3)如果想要详细了解C实现面向对象的思想,光看本书这一点章节中的东西是远远不够的!

CHAPTER 2 Embedded Programming with The H a r m o n y T M Harmony^{TM} for Embedded RealTime Process

  整个第二章主要就是以作者自己搞得Harmony™ for Embedded RealTime为例,来讲解如何进行嵌入式系统设计。?至于这个东西是个啥我还没搞清楚?!

2.1.2 What Is a Design Pattern?

  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.

CHAPTER 3 Design Patterns for Accessing Hardware

  整个第三章主要讲了各种访问硬件的设计模式。
  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 – infra­structure, 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(定时器中断模式扩展了中断定时器,为嵌入式系统提供准确的定时).

Hardware Proxy Pattern(硬件代理模式)

  The Hardware Proxy Pattern creates a software element responsible for access to a piece of hardware and encapsulation of hardware compression and coding implementation(硬件代理模式创建一个软件元素,负责访问硬件并封装硬件压缩和编码实现).

Hardware Adapter Pattern(硬件适配模式)

  The Hardware Adapter Pattern extends the Hardware Proxy Pattern to provide the ability to support different hardware interfaces.
  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.

Mediator Pattern(中介者模式)

  The Mediator Pattern supports coordination of multiple hardware devices to achieve a system level behavior.
  The Mediator Pattern provides a means of coordinating a complex interaction among a set of elements.

Observer Pattern(观察者模式)

  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.

Debouncing Pattern(去抖模式)

  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(嵌入式的许多输入设备,例如按键、开关等受制于物理限制,其响应速度与嵌入式设备处理速度不是一个数量级)
按键
去抖模式

Interrupt Pattern(中断模式)

  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.
中断模式

Polling Pattern(轮询模式)

  Another common pattern for getting sensor data or signals from hardware is to check periodi­cally, 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 (Opportu­nisticPoller or PeriodicPoller) serving as the data server and the PollDataClients serving as the data clients.

看的第二部分

  主要就是本书的第四、五、六三个章节。相比于前面的几个章节,该部分在实际中接触使用的更加多。

CHAPTER 4 Design Patterns for Embedding Concurrency and Resource Management

先跳过,看看后面的状态机

CHAPTER 5 Design Patterns for State Machines

  状态机在嵌入式设计中是非常常见的,在裸机程序中是一种相当有效稳定的模式。本书的第五章就是对状态机这种设计模式的详细介绍。

5.1 Oh Behave!

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.

5.2 Basic State Machine Concepts

  A Finite State Machine (FSM,有限状态机) is a directed graph composed of three primary elements(三种主要元素组成的有向图): states(状态), transitions(转换), and actions(动作). A state is a condition of a system or element (usually a class in an object-oriented system)(状态是指系统或者元素的状态). Looking back to , the element is the microwave oven (or at least the collection of elements involved in cooking) and the states are the conditions of that system – Off, Ready, Microwaving, DoorOpen, and Paused. Usual names for states are verbs, adverbs, and verb phrases.
State
  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(实际的动作通过代表动作的元素来执行).

5.3 Single Event Receptor Pattern

  Single event receptor state machines (henceforth known as SERSMs,单事件接收器状态机) can be used for both synchronous and asynchronous events(同时用于同步事件和异步事件).

5.4 Multiple Event Receptor Pattern

  Multiple event receptor finite state machines (MERSMs,多事件接收器状态机) are generally only used for synchro­nous 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.

未完待续。。。。

猜你喜欢

转载自blog.csdn.net/ZCShouCSDN/article/details/80217199