Bare Metal C teaches you to program embedded systems with the C programming language. You’ll learn how embedded programs interact with bare hardware directly, go behind the scenes with the compiler and linker, and learn C features that are important for programming regular computers.
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:
The basic program creation processSimple GPIO programming (blink an LED)Writing serial device driversThe C linker and preprocessorDecision and control statementsNumbers, arrays, pointers, strings, and complex data typesLocal variables and proceduresDynamic memoryFile and raw I/OFloating-point numbersModular programming
Author(s): Stephen Oualline
Edition: 1
Publisher: No Starch Press
Year: 2022
Commentary: NOTE 1: PDF generated from EPUB document. // NOTE 2: EPUB to PDF convertions can be performed by the user in several ways. Uploading this kind of files is both a disapointment for the user looking for this material and a waste of resources and time. // NOTE 3: EPUB version located at E443C7F4FF849087D7C8A9E8D6F820B3
Pages: 445
Tags: Embedded System Programming; C Programming Language; GPIO Programming; Device Driver Programming; Embedded Programming Optimization Techniques
Title Page
Copyright
About the Author
Introduction
Part I: Embedded Programming
Chapter 1: Hello World
Installing GCC
Downloading System Workbench for STM32
Our First Program
Compiling the Program
Making Mistakes
Understanding the Program
Adding Comments
Improving the Program and Build Process
The make Program
Compiler Flags
How the Compiler Works Behind the Scenes
The Preprocessor
The Compiler
The Assembler
The Linker
Adding to Your Makefile
Summary
Questions
Chapter 2: Introduction to the Integrated Development Environment
Using System Workbench for STM32
Starting the IDE
Creating Hello World
Debugging the Program
What the IDE Did for Us
Importing the Book’s Programming Examples
Summary
Programming Problems
Questions
Chapter 3: Programming the Microcontroller
The NUCLEO-F030R8 Development Board
Programming and Debugging the Board
Setting Up the Board
Setting Up an Embedded Project
Your First Embedded Program
Initializing the Hardware
Programming a GPIO Pin
Toggling the LED
Building the Completed Program
Exploring the Build Process
Exploring the Project Files
Debugging the Application
Stepping Through the Program
Summary
Programming Problems
Questions
Chapter 4: Numbers and Variables
Working with Integers
Declaring Variables to Hold Integers
Assigning Values to Variables
Initializing Variables
Integer Sizes and Representations
Number Representations
Standard Integers
Unsigned Integer Types
Overflow
Two’s Complement Representation in Signed Integer Types
Shorthand Operators
Controlling Memory-Mapped I/O Registers Using Bit Operations
OR
AND
NOT
Exclusive OR
Shifting
Defining the Meaning of Bits
Setting the Values of Two Bits at Once
Turning Off a Bit
Checking the Values of Bits
Summary
Programming Problems
Chapter 5: Decision and Control Statements
The if Statement
The if/else Statement
Looping Statements
The while Loop
The for Loop
Using the Button
Initialization
Choosing a Pulldown Circuit
Getting the State of the Button
Running the Program
Loop Control
The break Statement
The continue Statement
Anti-patterns
The Empty while Loop
Assignment in while
Summary
Programming Problems
Chapter 6: Arrays, Pointers, and Strings
Arrays
Under the Hood: Pointers
Array and Pointer Arithmetic
Array Overflow
Characters and Strings
Summary
Programming Problems
Chapter 7: Local Variables and Procedures
Local Variables
Hidden Variables
Procedures
Stack Frames
Recursion
Programming Style
Summary
Programming Problems
Chapter 8: Complex Data Types
Enums
Preprocessor Tricks and Enums
Structures
Structures in Memory
Accessing Unaligned Data
Structure Initialization
Structure Assignment
Structure Pointers
Structure Naming
Unions
Creating a Custom Type
Structures and Embedded Programming
typedef
Function Pointers and typedef
typedef and struct
Summary
Programming Problems
Chapter 9: Serial Output on the STM
Writing a String One Character at a Time
Defining Our Own putchar
Serial Output
A Brief History of Serial Communications
Serial Hello World!
UART Initialization
Transmitting a Character
Communicating with the Device
Windows
Linux and macOS
Summary
Programming Problems
Chapter 10: Interrupts
Polling vs. Interrupts
Interrupts for Serial I/O
Interrupt Routines
Writing a String with Interrupts
Program Details
Interrupt Hell
Using a Buffer to Increase Speed
Sending Function
Interrupt Routine
Full Program
The Problem
Summary
Programming Problems
Chapter 11: The Linker
The Linker’s Job
Compilation and Linking Memory Models
The Ideal C Model
Nonstandard Sections
The Linking Process
Symbols Defined by the Linker
Relocation and Linking Object Files
The Linker Map
Advanced Linker Usage
Flash Memory for “Permanent” Storage
Multiple Configuration Items
Field Customization Example
Firmware Upgrade
Summary
Programming Problems
Chapter 12: The Preprocessor
Simple Macros
Parameterized Macros
Code Macros
Conditional Compilation
Where Symbols Get Defined
Command Line Symbols
Predefined Symbols
Include Files
Other Preprocessor Directives
Preprocessor Tricks
Summary
Programming Problems
Part II: C for Big Machines
Chapter 13: Dynamic Memory
Basic Heap Allocation and Deallocation
Linked Lists
Adding a Node
Printing the Linked List
Deleting a Node
Putting It All Together
Dynamic Memory Problems
Valgrind and the GCC Address Sanitizer
Summary
Programming Problems
Chapter 14: Buffered File I/O
The printf Function
Writing the ASCII Table
Writing to Predefined Files
Reading Data
The Evil gets Function
Opening Files
Binary I/O
Copying a File
Buffering and Flushing
Closing Files
Summary
Programming Problems
Chapter 15: Command Line Arguments and Raw I/O
Command Line Arguments
Raw I/O
Using Raw I/O
Using Binary Mode
ioctl
Summary
Programming Problems
Chapter 16: Floating-Point Numbers
What Is a Floating-Point Number?
Floating-Point Types
Automatic Conversions
Problems with Floating-Point Numbers
Rounding Errors
Digits of Precision
Infinity, NaN, and Subnormal Numbers
Implementation
Alternatives
Summary
Programming Problems
Chapter 17: Modular Programming
Simple Modules
Problems with the Simple Module
Making the Module
What Makes Good Modules
Namespaces
Libraries
ranlib and Library Linking
Deterministic vs. Nondeterministic Libraries
Weak Symbols
Summary
Programming Problems
Afterword
Learn How to Write
Learn How to Read
Collaboration and Creative Theft
Useful Open Source Tools
Cppcheck
Doxygen
Valgrind
SQLite
Never Stop Learning
Appendix: Project Creation Checklist
Native C Project
STM32 Workbench Embedded Project
Index