Bare Metal C

Book description

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.

Table of contents

  1. Title Page
  2. Copyright
  3. About the Author
  4. Introduction
  5. Part I: Embedded Programming
    1. Chapter 1: Hello World
      1. Installing GCC
      2. Downloading System Workbench for STM32
      3. Our First Program
        1. Compiling the Program
        2. Making Mistakes
        3. Understanding the Program
        4. Adding Comments
      4. Improving the Program and Build Process
        1. The make Program
        2. Compiler Flags
      5. How the Compiler Works Behind the Scenes
        1. The Preprocessor
        2. The Compiler
        3. The Assembler
        4. The Linker
      6. Adding to Your Makefile
      7. Summary
      8. Questions
    2. Chapter 2: Introduction to the Integrated Development Environment
      1. Using System Workbench for STM32
        1. Starting the IDE
        2. Creating Hello World
        3. Debugging the Program
      2. What the IDE Did for Us
      3. Importing the Book’s Programming Examples
      4. Summary
      5. Programming Problems
      6. Questions
    3. Chapter 3: Programming the Microcontroller
      1. The NUCLEO-F030R8 Development Board
        1. Programming and Debugging the Board
        2. Setting Up the Board
      2. Setting Up an Embedded Project
      3. Your First Embedded Program
        1. Initializing the Hardware
        2. Programming a GPIO Pin
        3. Toggling the LED
        4. Building the Completed Program
      4. Exploring the Build Process
      5. Exploring the Project Files
      6. Debugging the Application
      7. Stepping Through the Program
      8. Summary
      9. Programming Problems
      10. Questions
    4. Chapter 4: Numbers and Variables
      1. Working with Integers
        1. Declaring Variables to Hold Integers
        2. Assigning Values to Variables
        3. Initializing Variables
      2. Integer Sizes and Representations
        1. Number Representations
        2. Standard Integers
        3. Unsigned Integer Types
        4. Overflow
        5. Two’s Complement Representation in Signed Integer Types
      3. Shorthand Operators
      4. Controlling Memory-Mapped I/O Registers Using Bit Operations
        1. OR
        2. AND
        3. NOT
        4. Exclusive OR
        5. Shifting
      5. Defining the Meaning of Bits
        1. Setting the Values of Two Bits at Once
        2. Turning Off a Bit
        3. Checking the Values of Bits
      6. Summary
      7. Programming Problems
    5. Chapter 5: Decision and Control Statements
      1. The if Statement
      2. The if/else Statement
      3. Looping Statements
        1. The while Loop
        2. The for Loop
      4. Using the Button
        1. Initialization
        2. Choosing a Pulldown Circuit
        3. Getting the State of the Button
        4. Running the Program
      5. Loop Control
        1. The break Statement
        2. The continue Statement
      6. Anti-patterns
        1. The Empty while Loop
        2. Assignment in while
      7. Summary
      8. Programming Problems
    6. Chapter 6: Arrays, Pointers, and Strings
      1. Arrays
        1. Under the Hood: Pointers
        2. Array and Pointer Arithmetic
        3. Array Overflow
      2. Characters and Strings
      3. Summary
      4. Programming Problems
    7. Chapter 7: Local Variables and Procedures
      1. Local Variables
      2. Hidden Variables
      3. Procedures
      4. Stack Frames
      5. Recursion
      6. Programming Style
      7. Summary
      8. Programming Problems
    8. Chapter 8: Complex Data Types
      1. Enums
      2. Preprocessor Tricks and Enums
      3. Structures
        1. Structures in Memory
        2. Accessing Unaligned Data
        3. Structure Initialization
        4. Structure Assignment
        5. Structure Pointers
        6. Structure Naming
      4. Unions
      5. Creating a Custom Type
      6. Structures and Embedded Programming
      7. typedef
        1. Function Pointers and typedef
        2. typedef and struct
      8. Summary
      9. Programming Problems
    9. Chapter 9: Serial Output on the STM
      1. Writing a String One Character at a Time
        1. Defining Our Own putchar
      2. Serial Output
        1. A Brief History of Serial Communications
        2. Serial Hello World!
        3. UART Initialization
        4. Transmitting a Character
      3. Communicating with the Device
        1. Windows
        2. Linux and macOS
      4. Summary
      5. Programming Problems
    10. Chapter 10: Interrupts
      1. Polling vs. Interrupts
      2. Interrupts for Serial I/O
      3. Interrupt Routines
      4. Writing a String with Interrupts
        1. Program Details
        2. Interrupt Hell
      5. Using a Buffer to Increase Speed
        1. Sending Function
        2. Interrupt Routine
        3. Full Program
        4. The Problem
      6. Summary
      7. Programming Problems
    11. Chapter 11: The Linker
      1. The Linker’s Job
      2. Compilation and Linking Memory Models
        1. The Ideal C Model
        2. Nonstandard Sections
      3. The Linking Process
      4. Symbols Defined by the Linker
      5. Relocation and Linking Object Files
      6. The Linker Map
      7. Advanced Linker Usage
        1. Flash Memory for “Permanent” Storage
        2. Multiple Configuration Items
        3. Field Customization Example
        4. Firmware Upgrade
      8. Summary
      9. Programming Problems
    12. Chapter 12: The Preprocessor
      1. Simple Macros
        1. Parameterized Macros
        2. Code Macros
      2. Conditional Compilation
      3. Where Symbols Get Defined
        1. Command Line Symbols
        2. Predefined Symbols
      4. Include Files
      5. Other Preprocessor Directives
      6. Preprocessor Tricks
      7. Summary
      8. Programming Problems
  6. Part II: C for Big Machines
    1. Chapter 13: Dynamic Memory
      1. Basic Heap Allocation and Deallocation
      2. Linked Lists
        1. Adding a Node
        2. Printing the Linked List
        3. Deleting a Node
        4. Putting It All Together
      3. Dynamic Memory Problems
      4. Valgrind and the GCC Address Sanitizer
      5. Summary
      6. Programming Problems
    2. Chapter 14: Buffered File I/O
      1. The printf Function
        1. Writing the ASCII Table
        2. Writing to Predefined Files
      2. Reading Data
      3. The Evil gets Function
      4. Opening Files
      5. Binary I/O
      6. Copying a File
      7. Buffering and Flushing
      8. Closing Files
      9. Summary
      10. Programming Problems
    3. Chapter 15: Command Line Arguments and Raw I/O
      1. Command Line Arguments
      2. Raw I/O
        1. Using Raw I/O
        2. Using Binary Mode
      3. ioctl
      4. Summary
      5. Programming Problems
    4. Chapter 16: Floating-Point Numbers
      1. What Is a Floating-Point Number?
        1. Floating-Point Types
        2. Automatic Conversions
      2. Problems with Floating-Point Numbers
        1. Rounding Errors
        2. Digits of Precision
      3. Infinity, NaN, and Subnormal Numbers
      4. Implementation
      5. Alternatives
      6. Summary
      7. Programming Problems
    5. Chapter 17: Modular Programming
      1. Simple Modules
        1. Problems with the Simple Module
        2. Making the Module
      2. What Makes Good Modules
      3. Namespaces
      4. Libraries
        1. ranlib and Library Linking
        2. Deterministic vs. Nondeterministic Libraries
      5. Weak Symbols
      6. Summary
      7. Programming Problems
  7. Afterword
    1. Learn How to Write
    2. Learn How to Read
    3. Collaboration and Creative Theft
    4. Useful Open Source Tools
      1. Cppcheck
      2. Doxygen
      3. Valgrind
      4. SQLite
    5. Never Stop Learning
  8. Appendix: Project Creation Checklist
    1. Native C Project
    2. STM32 Workbench Embedded Project
  9. Index

Product information

  • Title: Bare Metal C
  • Author(s): Steve Oualline
  • Release date: July 2022
  • Publisher(s): No Starch Press
  • ISBN: 9781718501621