Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 314 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
314
Dung lượng
13,06 MB
Nội dung
MakingEmbedded Systems
www.it-ebooks.info
www.it-ebooks.info
?? EDITION
Making Embedded Systems
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
www.it-ebooks.info
Making Embedded Systems
by
Printing History:
ISBN: 978-1-449-30214-6
1310483910
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Compilers, Languages, and Object-Oriented Programming 1
Embedded System Development 2
Debugging 2
More Challenges 4
Principles to Confront those Challenges 5
Further Reading 7
2. Creating a System Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Creating System Diagrams 10
The Block Diagram 10
Hierarchy of Control 13
Layered View 15
From Diagram to Architecture 16
Encapsulate Modules 16
Delegation of Tasks 17
Driver Interface: Open, Close, Read, Write, IOCTL 18
Adapter Pattern 19
Getting Started With Other Interfaces 22
Example: A Logging Interface 22
A Sandbox to Play In 28
Further Reading 33
3. Getting the Code Working . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Hardware/Software Integration 35
Ideal Project Flow 36
Board Bring Up 37
Reading a Datasheet 38
Datasheet Sections You Need When Things Go Wrong 40
v
www.it-ebooks.info
Important Text for Software Developers 42
Evaluating Components Using the Datasheet 46
Your Processor Is a Language 49
Reading a Schematic 51
Having a Debugging Toolbox (and a Fire Extinguisher) 54
Keep your Board Safe 54
Toolbox 54
Digital Multimeter 55
Oscilloscopes and Logic Analyzers 56
Testing the Hardware (and Software) 59
Building Tests 60
Flash Test Example 61
Command and Response 64
Command Pattern 67
Handling Errors Gracefully 70
Consistent Methodology 70
Error Handling Library 71
Further Reading 71
4. Outputs, Inputs, and Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Toggling an Output 75
Starting with Registers 76
Set the Pin to be an Output 77
Turn On the LED 79
Blinking the LED 80
Troubleshooting 81
Separating the Hardware from the Action 82
Board-Specific Header File 82
I/O Handling Code 83
Main Loop 86
Facade Pattern 86
The Input In I/O 87
A Simple Interface to a Button 89
Momentary Button Press 91
Interrupt on a Button Press 92
Configuring the Interrupt 92
Debouncing Switches 93
Runtime Uncertainty 96
Dependency Injection 96
Using a Timer 98
Timer Pieces 98
Doing the Math 100
A Long Wait between Timer Ticks 105
vi | Table of Contents
www.it-ebooks.info
Using the Timer 106
Using Pulse Width Modulation (PWM) 106
Shipping the Product 108
Further Reading 110
5. Task Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Scheduling and Operating System Basics 111
Tasks 111
Communication between Tasks 112
Avoiding Race Conditions 114
Priority Inversion 115
State Machines 116
State Machine Example: Stoplight Controller 117
State-centric State Machine 117
State-Centric State Machine with Hidden Transitions 118
Event-centric 119
State Pattern 120
Table Driven State Machine 121
Choosing a State Machine Implementation 123
Interrupts 123
An IRQ Happens 124
Save the Context 129
Get the ISR from the Vector Table 131
Calling the ISR 133
Restore the Context 136
When To Use Interrupts 137
How Not to Use Interrupts 138
Polling 138
System Tick 138
Time Based Events 141
A Very Small Scheduler 141
Watchdog 143
Further Reading 145
6. Communicating with Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
The Wide Reach of Peripherals 149
External Memory 149
Buttons and Key Matrices 150
Sensors 152
Actuators 155
Displays 160
Hosts and Other Processors 165
So Many Ways of Communicating 166
Table of Contents | vii
www.it-ebooks.info
OSI Model 166
Ethernet and WiFi 168
Serial 168
Serial Peripheral Bus Profiles 170
Parallel 176
Putting Peripherals and Communication Together 178
Data Handling 178
Adding Robustness to the Communication 188
Changing Data 191
Changing Algorithms 194
Further Reading 195
7.
Updating Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
On-Board Bootloader 200
Build Your Own Bootloader 201
Modifying the Resident Bootloader 203
Brick Loader 204
Linker Scripts 205
Copy Loader to RAM 208
Run the Loader 209
Copy New Code to Scratch 210
Dangerous Time: Erase and Program 210
Reset to New Code 211
Security 212
Summary 212
8. Doing More with Less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Code Space 216
Reading a Map File (Part 1) 216
Process of Elimination 219
Libraries 220
Functions and Macros 221
Constants and Strings 222
RAM 223
Free malloc 223
Reading a Map File (Part 2) 228
Registers and Local Variables 229
Function Chains 231
Pros and Cons of Globals 232
Memory Overlays 233
Speed 234
Profiling 235
Optimizing 239
viii | Table of Contents
www.it-ebooks.info
Summary 249
Further Reading 250
9. Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Identifying Fast and Slow Operations 254
Taking an Average 255
Use an Existing Algorithm 258
Designing and Modifying Algorithms 260
Factor Polynomials 261
Taylor Series 261
Dividing by a Constant 264
Scaling the Input 265
Lookup Tables 266
Fake Floating Point Numbers 274
Precision 275
Addition (and Subtraction) 276
Multiplication (and Division) 277
Determining the Error 278
Further Reading 282
10. Reducing Power Consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Understanding Power Consumption 286
Turn Off the Light When You Leave the Room 288
Turn Off Peripherals 288
Turn Off Unused I/O devices 289
Turn Off Processor Subsystems 289
Slowing Down to Conserve Energy 290
Putting the Processor to Sleep 290
Interrupt-based code flow model 291
A Closer Look at the Main Loop 294
Processor Watchdog 295
Avoid Frequent Wake-ups 296
Chained Processors 296
Further Reading 296
Table of Contents | ix
www.it-ebooks.info
www.it-ebooks.info
[...]... www.it-ebooks.info CHAPTER 1 Introduction Embeddedsystems are different things to different people To someone who has been working on servers, an application developed for a phone is an embedded system To someone who has written code for tiny 8-bit microprocessors, anything with an operating system doesn't seem very embedded I tend to tell non-technical people that embeddedsystems are things like microwaves... design principles transcend language Embedded System Development Embeddedsystems are special, offering special challenges to developers Most embedded software engineers develop a toolkit for dealing with the constraints Before we can start building yours, let's look at the difficulties associated with developing an embedded system Once you become familiar with how your embedded system might be limited,... for embedded system development I've also filled in a number of chapters with other useful information not found in most books About This Book After seeing embeddedsystems in medical devices, race cars, airplanes, children's toys, and gunshot location systems, I've found a lot of commonalities There are a lot of things I wish I knew then on how to go about designing and implementing software for an embedded. .. an embedded system? How is development different from traditional software? Chapter 2 Creating a System Architecture How to create (and document) a system architecture Chapter 3 Getting the Code Working Hardware/software integration during board bring up can be scary, but there are some ways to make it smoother Chapter 4 Outputs, Inputs and Timers The embeddedsystems version of “Hello World” is making. .. not require permission Incorporating a significant amount of example code from this book into your product's documentation does require permission We appreciate, but do not require, attribution An attribution usually includes the title, author, publisher, and ISBN For example: MakingEmbeddedSystems by Elecia White (O'Reilly) Copyright 2011 Some Copyright Holder, 9781449302146.” If you feel your use... Patterns: Elements of Reusable Object-Oriented Software xi www.it-ebooks.info One note, though: my embedded systems don't have operating systems The software runs on the bare metal When the software says “turn that light on,” it says it to the processor without an intermediary This isn't a book about an embedded operating system (OS) But the concepts translate to processors running OSs, so if you stick...Preface I love embedded systems The first time a motor turned because I told it to, I was hooked I quickly moved away from pure software and into a field where I can touch the world Just as I was leaving software, the seminal work was done on design patterns.*My team went through the book, discussing the patterns and where we'd consider using them As I got more into embedded systems, I found compilers... lifespan Our goal is to be flexible enough to Embedded System Development | 5 www.it-ebooks.info meet the product goals while dealing with the resource constraints and other challenges inherent to embedded systems There are some excellent principles we can take from software design to make the system more flexible Using modularity we separate the functionality into subsystems and hide the data each subsystem... some systems, the software must act deterministically (exactly the same each time) or real-time (always reacting to an event fast enough) Some systems require that the software be faulttolerant with graceful degradation in the face of errors For example, consider a system where servicing faulty software or broken hardware may be infeasible (i.e a satellite or a tracking tag on a whale) Other systems. .. monitor should not fail quietly) Compilers, Languages, and Object-Oriented Programming Another way to identify embedded systems is that they use cross compilers While a cross compiler runs on your desktop or laptop computer, it creates code that does not The cross compiled image runs on your target embedded system Since the code needs to run on your processor, the vendor for the target system usually sells . Making Embedded Systems
www.it-ebooks.info
www.it-ebooks.info
?? EDITION
Making Embedded Systems
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
www.it-ebooks.info
Making. Systems
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
www.it-ebooks.info
Making Embedded Systems
by
Printing History:
ISBN: 978-1-449-30214-6
1310483910
www.it-ebooks.info
Table