2022-09-22 10:09

Chapter 5. Getting to Know the Hardware

hardware n. The part of a computer system that can be kicked.

As an embedded software engineer, youll have the opportunity to work with many different pieces of hardware in your career. In this chapter, I will teach you a simple procedure that I use to familiarize myself with any new board. In the process, Ill guide you through the creation of a header file that describes the boards most important features and a piece of software that initializes the hardware to a known state.

5.1 Understand the Big Picture

Before writing software for an embedded system, you must first be familiar with the hardware on which it will run. At first, you just need to understand the general operation of the system. You do not need to understand every little detail of the hardware; that kind of knowledge will not be needed right away and will come with time.

Whenever you receive a new board, you should take some time to read whatever documents have been provided with it. If the board is an off-the-shelf product, it might arrive with a 'Users Guide' or 'Programmers Manual' that has been written with the software developer in mind. However, if the board was custom designed for your project, the documentation might be more cryptic or written mainly for the reference of the hardware designers. Either way, this is the single best place for you to start.

While you are reading the documentation, set the board itself aside. This will help you to focus on the big picture. There will be plenty of time to examine the actual board more closely when you have finished reading. Before picking up the board, you should be able to answer two basic questions about it:

  • What is the overall purpose of the board?
  • How does data flow through it?

For example, imagine that you are a member of a modem design team. You are a software developer who has just received an early prototype board from the hardware designers. Because you are already familiar with modems, the overall purpose of the board and the data-flow through it should be fairly obvious to you. The purpose of the board is to send and receive digital data over an analog telephone line. The hardware reads digital data from one set of electrical connections and writes an analog version of the data to an attached telephone line. Data also flows in the opposite direction, when analog data is read from the telephone line jack and output digitally.

Though the purpose of most systems is fairly obvious, the flow of the data might not be. I often find that a data-flow diagram is helpful in achieving rapid comprehension. If you are lucky, the documentation provided with your hardware will contain a superset of the block diagram you need. However, you might still find it useful to create your own data-flow diagram. That way, you can leave out those hardware components that are unrelated to the basic flow of data through the system.

In the case of the Arcom board, the hardware was not designed with a particular application in mind. So for the remainder of this chapter, well have to imagine that it does have a purpose. We shall assume the board was designed for use as a printer-sharing device. A printer-sharing device allows two computers to share a single printer. The user of the device connects one computer to each serial port and a printer to the parallel port. Both computers can then send documents to the printer, though only one of them can do so at a given time.

In order to illustrate the flow of data through the printer-sharing device, Ive drawn the diagram in Figure 5-1. (Only those hardware devices that are involved in this application of the Arcom board are shown.) By looking at the block diagram, you should be able to quickly visualize the flow of the data through the system. Data to be printed is accepted from either serial port, held in RAM until the printer is ready for more data, and delivered to the printer via the parallel port. The software that makes all of this happen is stored in ROM.

Figure 5-1. Data-flow diagram for the printer-sharing device

Once youve created a block diagram, dont just crumple it up and throw it away. You should instead put it where you can refer to it throughout the project. I recommend creating a project notebook or binder, with this data-flow diagram on the first page. As you continue working with this piece of hardware, write down everything you learn about it in your notebook. You might also want to keep notes about the software design and implementation. A project notebook is valuable not only while you are developing the software, but also once the project is complete. You will appreciate the extra effort you put into keeping a notebook when you need to make changes to your software, or work with similar hardware, months or years later.

If you still have any big-picture questions after reading the hardware documents, ask a hardware engineer for some help. If you dont already know the hardwares designer, take a few minutes to introduce yourself. If you have some time, take him out to lunch or buy him a beer after work. (You dont even have to talk about the project the whole time!) I have found that many software engineers have difficulty communicating with hardware engineers, and vice versa. In embedded systems development, it is especially important that the hardware and software teams be able to communicate with one another.

5.2 Examine the Landscape

It is often useful to put yourself in the processors shoes for a while. After all, the processor is only going to do what you ultimately instruct it to do with your software. Imagine what it is like to be the














为了说明通过打印机共享设备的数据流动,我画了图5-1的示图。 (仅示出了Arcom板里所涉及的那些硬件装置。)通过查看框图,你应该能够直观了解通过该系统的数据流动。要打印的数据从任一串行端口接受,在RAM中保存直到打印机准备好接收更多的数据,并通过并行端口传送到打印机。使得这一切发生的软件存储在ROM中。

图5-1 打印机共享设备的数据流程图





首先要注意的事情是有两种基本类型:存储器和外设。显然,存储器是用于数据及代码存储和检索的。但是,你可能想知道的外设是什么。这些要么是与外界坐标(I / O)相互作用的专门硬件设备,要么是执行特定功能的硬件。例如,在嵌入式系统中最常见的两个外设是串行端口和定时器。前者是一个I / O设备,而后者基本上是一个计数器。

英特尔80x86和其他一些家族处理器的成员有两个不同的地址空间,通过这些存储器和外设它们可以进行通信。第一地址空间被称为存储器空间,主要用于存储器装置;第二个是专门预留给外设的,被称为I / O空间。然而,外围设备也可以位于存储器空间内,这由硬件设计者决定。当这种情况发生时,我们说这些外设是内存映射。

但从处理器的角度来看,内存映射外设的外观和行为非常像存储设备。然而,外围设备的功能很明显跟一个存储器的完全不同。外围可能被解释为命令或数据以某种方式被处理,而不是简单地存储提供给它的数据。如果外设位于存储空间内,我们说系统有内存映射I / O。

嵌入式硬件的设计人员往往喜欢只使用内存映射I / O,因为它对硬件和软件开发商都有优势。这对硬件开发者有吸引力,因为他也许能消除I / O空间,以及一些与其相关的电线。这可能不能显著降低电路板的生产成本,但它可能会降低硬件设计的复杂性。内存映射的外设也更有利于程序员,使他们能够更有效且更容易地使用指针,数据结构以及与外围设备进行交互联合。[1]




如果你回头看看图5-1 Arcom板的框图,你会看到有三种设备附加到地址和数据总线上。这些器件分别是RAM、ROM以及一个标有“Zilog85230串行控制器”的神秘装置。由Arcom提供的文档说RAM位于存储器的底部,向上延伸到第一个128 KB的内存空间。该ROM位于内存的顶部向下延伸256 KB。但是,这种存储区实际上包含两个ROM中,一个EPROM和一个闪存器件,每个大小为128 KB。第三个装置中,Zilog85230串行通信控制器,是一个寄存器地址为70000h和72000h之间的存储器映射外设。


图5-2 Arcom板的存储器映射图

对于每一个新板子,你应创建一个描述其最重要特点的头文件。此文件为硬件提供一个抽象接口。实际上,它允许你通过名字来引用在电路板上的各种设备,而不是地址。这对于使你的应用软件更便携很有好处。如果存储器映射发生了改变,例如,如果RAM的128 KB发生移动,你只需要更改受影响的特定板子的头文件然后重新编译你的应用程序。




* Memory Map


* Base Address Size Description

* -------------- ----- -----------------------------------

* 0000:0000h 128K SRAM

* 2000:0000h Unused

* 7000:0000h Zilog SCC Registers

* 7000:1000h Zilog SCC Interrupt Acknowledge

* 7000:2000h Unused

* C000:0000h 128K Flash

* E000:0000h 128K EPROM



#define SRAM_BASE (void *) 0x00000000

#define SCC_BASE (void *) 0x70000000

#define SCC_INTACK (void *) 0x70001000

#define FLASH_BASE (void *) 0xC0000000

#define EPROM_BASE (void *) 0xE0000000


在C和C 中,指针的值是一个地址。所以,当我们说我们有一个数据指针式,我们实际意味着我们有该数据的存储地址。但是程序员通常不直接设置或检查这些地址。例外的是那些需要在他们的代码中明确设置一个指针值的操作系统、设备驱动程序和嵌入式软件的开发人员。


大多数80times;86处理器的C/ C 编译器使用32位指针。然而,较旧的处理器没有简单的线性32位地址空间。例如,Intel的80188EB处理器仅具有20位的地址空间。并且,除此之外,它的内部寄存器不能容纳超过16位字节。所以在这个处理器上,2个16位寄存器,段寄存器和一个偏移寄存器被组合起来以创建一个20位的物理地址。 (物理地址计算涉及左移四位段寄存器的内容,并加上所述偏移寄存器的内容到结果。在任何溢出道21位的地址会被忽略。)


int * pRegister = (int *) 0x10002345;



5.2.2 I / O地图

如果一个单独的I / O空间存在,重复存储器映射锻炼以创建板上的I / O映射将是必要的。过程是完全相同的。简单地创建外围的名称和地址范围的表格,以这样的方式则最低地址在底部。典型的是,I / O空间的很大比例将是未使用的,因为大多数位于外设上的空间将只有屈指可数的寄存器。

Arcom板的I / O映射如图5-3所示。它包括三个器件:外围控制块(PCB),并行端口,和调试端口。该PCB是一组位于80188EB内用于控制片上外设寄存器的。控制并行端口和调试端口的芯片驻留在处理器的外部。这些端口用于与打印机和基于主机的调试器分别进行通信。

图5-3 Arcom板的I / O图

创建您的主板头文件时,I / O映射也很有用。每一个I / O空间中的区域直接映射到一个常数,称为基址。上面的I / O映射为一组常量的转换可以在下面的清单中找到:



* I/O Maplt;



原文和译文剩余内容已隐藏,您需要先支付 20元 才能查看原文和译文全部内容!立即支付