![Bare Metal C: Embedded Programming for the Real World](http://img.images-bn.com/static/redesign/srcs/images/grey-box.png?v11.9.4)
![Bare Metal C: Embedded Programming for the Real World](http://img.images-bn.com/static/redesign/srcs/images/grey-box.png?v11.9.4)
Paperback
-
PICK UP IN STORECheck Availability at Nearby Stores
Available within 2 business hours
Related collections and offers
Overview
Bare Metal C will teach you how to program embedded devices with the C programming language. For embedded system programmers who want precise and complete control over the system they are using, this book pulls back the curtain on what the compiler is doing for you so that you can see all the details of what's happening with your program.
The first part of the book teaches C basics with the aid of a low-cost, widely available bare metal system (the Nucleo Arm evaluation system), which gives you all the tools needed to perform basic embedded programming. As you progress through the book you’ll learn how to integrate serial input/output (I/O) and interrupts into your programs. You’ll also learn what the C compiler and linker do behind the scenes, so that you’ll be better able to write more efficient programs that maximize limited memory. Finally, you’ll learn how to use more complex, memory hungry C features like dynamic memory, file I/O, and floating-point numbers.
Topic coverage includes:
Product Details
ISBN-13: | 9781718501621 |
---|---|
Publisher: | No Starch Press |
Publication date: | 08/02/2022 |
Pages: | 304 |
Sales rank: | 1,033,363 |
Product dimensions: | 6.90(w) x 9.10(h) x 0.80(d) |
About the Author
Table of Contents
Introduction xvii
Part I Embedded Programming 1
1 Hello World 3
Installing GCC 4
Downloading System Workbench for STM32 4
Our First Program 5
Compiling the Program 5
Making Mistakes 6
Understanding the Program 7
Adding Comments 8
Improving the Program and Build Process 9
The make Program 9
Compiler Flags 10
How the Compiler Works Behind the Scenes 10
The Preprocessor 12
The Compiler 12
The Assembler 12
The Linker 14
Adding to Your Makefile 15
Summary 16
Questions 17
2 Introduction to the Integrated Development Environment 19
Using System Workbench for STM32 20
Starting the IDE 20
Creating Hello World 21
Debugging the Program 26
What the IDE Did for Us 30
Importing the Book's Programming Examples 31
Summary 31
Programming Problems 32
Questions 32
3 Programming the Microcontroller 33
The NUCLEO-FO3OR8 Development Board 34
Programming and Debugging the Board 34
Setting Up the Board 35
Setting Up an Embedded Project 37
Your First Embedded Program 40
Initializing the Hardware 41
Programming a GPIO Pin 41
Toggling the LED 42
Building the Completed Program 43
Exploring the Build Process 44
Exploring the Project Files 46
Debugging the Application 47
Stepping Through the Program 50
Summary 51
Programming Problems 52
Questions 52
4 Numbers and Variables 53
Working with Integers 54
Declaring Variables to Hold Integers 55
Assigning Values to Variables 56
Initializing Variables 56
Integer Sizes and Representations 57
Number Representations 59
Standard Integers 61
Unsigned integer Types 62
Overflow 63
Two's Complement Representation in Signed Integer Types 64
Shorthand Operators 65
Controlling Memory-Mapped I/O Registers Using Bit Operations 67
OR 67
AND 68
NOT 68
Exclusive OR 69
Shifting 69
Defining the Meaning of Bits 70
Setting the Values of Two Bits at Once 72
Turning Off a Bit 72
Checking the Values of Bits 72
Summary 75
Programming Problems 75
5 Decision and Control Statements 77
The if Statement 77
The if/else Statement 79
Looping Statements 80
The while Loop 80
The for Loop 82
Using the Button 83
Initialization 84
Choosing a Pulldown Circuit 85
Getting the State of the Button 86
Running the Program 87
Loop Control 87
The break Statement 87
The continue Statement 88
Anti-patterns 88
The Empty while Loop 89
Assignment in while 89
Summary 90
Programming Problems 90
6 Arrays, Pointers, and Strings 91
Arrays 92
Under the Hood: Pointers 94
Array and Pointer Arithmetic 97
Array Overflow 98
Characters and Strings 100
Summary 102
Programming Problems 103
7 Local Variables and Procedures 105
Local Variables 106
Hidden Variables 107
Procedures 108
Stack Frames 109
Recursion 112
Programming Style 114
Summary 114
Programming Problems 115
8 Complex Data Types 117
Enums 118
Preprocessor Tricks and Enums 119
Structures 121
Structures in Memory 122
Accessing Unaligned Data 125
Structure Initialization 127
Structure Assignment 128
Structure Pointers 128
Structure Naming 129
Unions 130
Creating a Custom Type 132
Structures and Embedded Programming 133
Typedef 135
Function Pointers and typedef 136
Typedef and struct 137
Summary 137
Programming Problems 138
9 Serial Output on the STM 141
Writing a String One Character at a Time 142
Defining Our Own putchar 142
Serial Output 143
A Brief History of Serial Communications 145
Serial Hello World! 147
UART Initialization 147
Transmitting a Character 150
Communicating with the Device 156
Windows 156
Linux and macOS 158
Summary 158
Programming Problems 159
10 Interrupts 161
Polling vs. Interrupts 161
Interrupts for Serial I/O 162
Interrupt Routines 163
Writing a String with Interrupts 164
Program Details 167
Interrupt Hell 171
Using a Buffer to Increase Speed 172
Sending Function 173
Interrupt Routine 174
Full Program 174
The Problem 177
Summary 182
Programming Problems 182
11 The Linker 183
The Linker's Job 184
Compilation and Linking Memory Models 185
The Ideal C Model 185
Nonstandard Sections 190
The Linking Process 191
Symbols Defined by the Linker 192
Relocation and Linking Object Files 192
The Linker Map 193
Advanced Linker Usage 195
Flash Memory for "Permanent" Storage 195
Multiple Configuration Items 202
Field Customization Example 203
Firmware Upgrade 204
Summary 204
Programming Problems 205
12 The Preprocessor 207
Simple Macros 208
Parameterized Macros 210
Code Macros 211
Conditional Compilation 214
Where Symbols Get Defined 216
Command Line Symbols 216
Predefined Symbols 217
Include Files 217
Other Preprocessor Directives 217
Preprocessor Tricks 218
Summary 219
Programming Problems 220
Part II C for Big Machines 221
13 Dynamic Memory 223
Basic Heap Allocation and Deallocation 224
Linked Lists 226
Adding a Node 227
Printing the Linked List 229
Deleting a Node 230
Purling It All Together 231
Dynamic Memory Problems 233
Valgrind and the GCC Address Sanitizer 234
Summary 236
Programming Problems 236
14 Buffered File I/O 237
The printf Function 238
Writing the ASCII Table 239
Writing to Predefined Files 240
Reading Data 240
The Evil gets Function 241
Opening Files 242
Binary I/O 244
Copying a File 244
Buffering and Flushing 246
Closing Files 247
Summary 247
Programming Problems 248
15 Command Line Arguments and Raw I/O 249
Command Line Arguments 249
Raw I/O 250
Using Raw I/O 251
Using Binary Mode 254
Ioctl 255
Summary 255
Programming Problems 256
16 Floating-Point Numbers 257
What Is a Floating-Point Number? 258
Floating-Point Types 258
Automatic Conversions 258
Problems with Floating-Point Numbers 259
Rounding Errors 259
Digits of Precision 260
Infinity, NaN, and Subnormal Numbers 260
Implementation 262
Alternatives 262
Summary 265
Programming Problems 266
17 Modular Programming 267
Simple Modules 268
Problems with the Simple Module 269
Making the Module 271
What Makes Good Modules 272
Namespaces 272
Libraries 273
Ranlib and Library Linking 276
Deterministic vs. Nondeterministic Libraries 278
Weak Symbols 278
Summary 280
Programming Problems 280
Afterword 281
Learn How to Write 281
Learn How to Read 282
Collaboration and Creative Theft 282
Useful Open Source Tools 282
Cppcheck 283
Doxygen 283
Valgrind 283
SQLite 284
Never Stop Learning 284
Appendix: Project Creation Checklist 285
Native C Project 285
STM32 Workbench Embedded Project 287
Index 289