ebook img

Making Embedded Systems: Design Patterns for Great Software PDF

314 Pages·2011·13.065 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Making Embedded Systems: Design Patterns for Great Software

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

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.