Professional Documents
Culture Documents
Firmware
A Practical Approach to APIs,
HALs and Drivers
Jacob Beningo
Contents
Acknowledgements ............................................................................... xiii
Foreword ..................................................................................................... xv
Preface....................................................................................................... xvii
Introduction ................................................................................................. 1
Chapter 1 Concepts for Developing Portable Firmware ............. 3
Why Code Reuse Matters ................................................................................. 3
Portable Firmware ............................................................................................ 5
Modularity ......................................................................................................... 11
Module Coupling and Cohesion.................................................................. 12
Following A Standard .................................................................................... 14
Portability Issues in C – Data Types ......................................................... 15
Portability Issues in C – Structures and Unions................................... 17
Portability Issues in C – Bit Fields............................................................. 18
Portability Issues in C – Preprocessor Directives ............................... 19
Hardware Abstraction Layers (HAL) ....................................................... 21
Application Programming Interfaces (APIs) ........................................ 28
Project Organization ...................................................................................... 28
Getting Started Writing Portable Firmware ......................................... 29
Going Further ................................................................................................... 32
Chapter 2 API and HAL Fundamentals .............................................37
The Wonderful World of HALs ................................................................... 37
APIs versus HALs ............................................................................................. 38
The API and HAL Landscape ....................................................................... 40
The Good, Bad and Ugly ................................................................................ 42
Potential Issues and the Boogeyman ....................................................... 42
Characteristics Every HAL should Exhibit ............................................. 46
To Build or Not to Build ................................................................................ 56
A First Look at a HAL...................................................................................... 58
The API Scope ................................................................................................... 59
API Characteristics to look for ................................................................... 60
Designing your own APIs ............................................................................. 65
A First Look at an API .................................................................................... 65
Going Further ................................................................................................... 67
Chapter 3 Device Driver Fundamentals in C.................................. 73
Understanding the Memory Map ............................................................... 73
Identifying Driver Interfaces....................................................................... 77
Design by Contract .......................................................................................... 80
Assertion Fundamentals ............................................................................... 83
Device Driver Models ..................................................................................... 85
Polled versus Interrupt Driven Drivers .................................................. 87
Deciphering the volatile, const and static keywords ......................... 93
Driver Module Definition.............................................................................. 93
Naming Convention Recommendations .................................................. 95
Object Oriented Programming in C ........................................................... 96
Encapsulation and Data Hiding ............................................................... 105
Abstract Data Types ..................................................................................... 105
Callback Functions ....................................................................................... 105
Error Handling ............................................................................................... 110
Expected Results and Recommendations ............................................ 113
Leverage Design Patterns .......................................................................... 114
Going Further ................................................................................................. 114
Chapter 4 Writing Reusable Drivers ............................................. 119
It’s All about the Interface ......................................................................... 119
How much Abstraction is too much? ..................................................... 120
Wrapping the Interface .............................................................................. 120
Memory Mapping Methodologies ........................................................... 128
Mapping Memory Directly ......................................................................... 129
Mapping Memory with Pointers.............................................................. 130
Mapping Memory with Structures .......................................................... 133
Using Pointer Arrays in Driver Design ................................................. 136
Creating a Timer Driver Overview ......................................................... 138
Step #1 – Define the Timers Configuration Table ............................ 138
Step #2 – Define the Timers Peripheral Channels............................ 140
Step #3 – Populate the Timers Configuration Table........................ 141
Step #4 – Create the Timer Pointer Arrays ......................................... 143
Step #5 – Create the Initialization Function ....................................... 145
Step #6 – Fill in the Timer Driver Interface ........................................ 150
Step #7 – Maintain and Port the Design Pattern ............................... 150
Selecting the Right Driver Implementation ........................................ 151
Going Further ................................................................................................. 152
Chapter 5 Documenting Firmware with Doxygen ................... 157
The Importance of Good Documentation ............................................ 157
Easing the Documentation Load ............................................................. 159
An Introduction to Doxygen ..................................................................... 162