TLA+ is a formal specification language that is used for modeling the behavior of systems and algorithms, especially distributed and concurrent systems.
Embedded Swift
8 May 2024 by Phillip JohnstonSwift is one of the newest languages to enter the embedded space thanks to a new, experimental compilation mode. You can now use Swift to target ARM and RISC-V microcontrollers. This new compilation mode disables certain language features, like runtime reflection and ABI stability to produce standalone, statically linked binaries that can be run on a microcontroller. While this mode requires disabling certain language features, the aim is to not make it a separate Swift dialect, but to rather be a subset that feels very close to the “full” Swift language. For now, Embedded Swift …
Continue reading “Embedded Swift”
Python
Python is a widely used general-purpose scripting language.
Table of Contents:
Resources
Learning Python
- The Official Python 3 Tutorial
- Dive Into Python – for experienced devs learning Python
- Python Practice Problems for Beginner Coders – Berkeley I School Online
- Python Cheat Sheets is a great summary resource. Start with the basics.
- James Powell: So you want to be a Python expert? | PyData Seattle 2017
- Automate the Boring Stuff with Python Programming (Udemy Course)
Advanced
Style Guides
- PEP 8 – Style Guide for Python Code
- Google Python Style Guide
- The Hitchhiker’s Guide to Python: Code Style
Quality Enforcement
- Linters are tools for finding potential bugs and style problems in Python source code. They find problems that are typically caught by a compiler for less dynamic languages like C and C++.
- Ruff is a Python linter and code formatter that people prefer for its speed of execution. This is currently our choice of tool.
- Pylint is a tool for finding bugs and style problems in Python source code. It finds problems that are typically caught by a compiler for less dynamic languages like C and C++.
- Flake8 is another popular linter and formatter that supports plugins to add additional checks.
- Black is a Python formatter
- Type Checking
- Mypy is a static type checker for Python that aims to add compile-time type checking with no runtime overhead.
On Embedded Artistry
libc
The C standard library is commonly known as “libc”. This library provides the macros, types, and function implementations for the C programming language.
Markdown
5 November 2019 by Phillip Johnston • Last updated 9 June 2021Markdown is a lightweight markup language with plain text formatting syntax. Its design allows it to be converted to many output formats. Markdown is often used to format in-source documentation (e.g., a README). There are many different flavors of Markdown, each using a common core specification but adding additional features of their own. Table of Contents: Specifications Learning Markdown Specifications John Gruber’s Markdown page discusses the original flavor CommonMark is a proposal for a standard, unambiguous Markdown syntax specification GitHub Flavored Markdown Spec describes the dialect of Markdown supported …
YAML
YAML is a human-readable data serialization standard for all programming languages.
Ada
Ada is a statically-typed, imperative, object-oriented programming language. Ada’s design philosophy is to favor compile-time errors over run-time errors. It is primarily used to develop safety critical systems.
Rust
Rust is a systems programming language that places an emphasis on memory and concurrency safety. Because of these factors, it is gaining traction in the embedded community.
Table of Contents:
Books
- Official Rust Programming Book [free online]
- This book is approachable and worth spending a few hours reading and working through it.
- Embedded Rust Book [free online] is meant for embedded developers learning rst.
- Discovery is a variant intended for those also learning embedded at the same time.
- Programming Rust: Fast, Safe Systems Development
- Rust Atomics and Locks: Low-Level Concurrency in Practice by Mara Bos (Book)
- Awesome Embedded Rust collection
From Around the Web
- Cliffle: Prefer Rust to C/C++ for New Code
- Dion analyzes the state of Rust for embedded development as of September 2023.
Learning Rust
- Rust by Example
- Learn Rust the Dangerous Way – Cliffle
- Learning Rust for Embedded Systems by Steve Branam
- Niko’s intro_rust – screencasts for learning Rust
- Cheatsheet for Confusing Rust Terms
- Ferrous Systems: Rust Training Material
- Rust ownership, the hard way – an article by Chris Morgan
- Stratify Labs shared an extensive comparison between C++ and Rust, which will be useful learning shortcut for any C++ developer that’s making a foray into Rust land.
- A Firehose of Rust for People Who Know C++ (video)
Code Analysis
- This series, from Ferrous Systems, describes the performance improvement work done on
rustls. - Lock-free ring buffer implementation for maximum throughput | Maya’s Programming & Electronics Blog
Rust for Embedded
-
Embedded Rust Book [free online]
-
Accessing Hardware in Rust by Ferrous Systems
-
Rust embedded-hal v1.0 was released in 2024
-
repository, the API documentation and the migration guide
The
embedded-halcrates provide traits (interfaces) for using peripherals commonly available in microcontrollers such as GPIO, UART, SPI or I2C. They allow writing drivers (for sensors, displays, actuators, network adapters, etc.) in a generic way, so they work on any microcontroller with anembedded-halimplementation without modifying them. It’s a central piece of the Embedded Rust ecosystem, ensuring interoperability throughout.The 1.0 release has been in the works since 2020. Now that it’s out, we consider all traits in it to be stable. The plan is to extend
embedded-halwith more traits in future 1.x releases, not doing more breaking changes (i.e. there are no plans for a 2.0 release). This will provide a stable base for building HALs and drivers.
-
Safety-Critical Rust
Ferrous Systems has been working on qualifying Rust for safety-critical system development with its Ferrocene toolchain.
Rust Patterns
- Cliffle: The Typestate Pattern in Rust
- Using
mem::taketo reduce heap allocations - Brave new I/O | Embedded in Rust
- Memory safe DMA transfers | Embedded in Rust
Mixing Languages
- Mixing C++ and Rust for Fun and Profit: Part 1 by Loren Burkholder
- Deploying Rust in Existing Firmware Codebases, by Ivan Lozano and Dominik Maier, Android Team, describes a strategy for gradually integrating rust components.
C
C is a general-purpose imperative programming language widely used for embedded systems development.
Table of Contents:
- From Around the Web
- Tools
- Advanced Techniques
- Exceptions
- Objects in C
- Information Hiding in C
- Polymorphism and Inheritance in C
- From Embedded Artistry
- Recommended C Libraries
From Around the Web
Beginners
The classic introductory recommendation for programming in C is Brian W. Kernighan and Dennis M. Ritchie’s The C Programming Language.
If you prefer more of a course-based approach, Learn Code the Hard Way has a Learn C the Hard Waycourse. We recommend this course because it provides hands-on demos and examples.
An excellent C crash course can be found on Embedded.fm in the Embedded Wednedsays series:
- Programming C Without Knowing Assembly
- A Crash Course in C – Part 1 – Integers
- A Crash Course in C – Part 2 – Floating Point
- A Crash Course in C – Part 3 – Characters
- A Crash Course in C – Part 4 – Booleans
- A Crash Course in C – Part 5 – Arrays
- A Crash Course in C – Part 6 – The Basic Structure of C Code
- A Crash Course in C – Part 7 – Flow Control Statements
- A Crash Course in C – Part 8 – Introduction to Functions
- A Crash Course in C – Part 9 – Functions, part 2
- Logic in C – Part 1
- Logic in C – Part II, bit manipulation
- How big is an enum?
- Shortcutting Conditionals
- Scope I
- Scope II
- The Case for the Default Case – On the importance of specifying the default switch case.
The following Embedded.fm articles can be used to build upon your new C knowledge:
- Explaining the C Keyword
volatile - Shifty Acceleration discusses shifting bits, as well as how it can be used to solve a problem with an accelerometer
- Number Representation discusses ASCII/numeric conversions
- New To Me – Range specifiers in switch statements.
- Ranges in Array Initializers – Ranges again, but this time in initializers.
- Exceptional code – an investigation into null pointers and GCC code generation
The Atoms of Confusion website provides information on making confusing code constructs more understandable. Review these common C confusion points to improve your programming abilities. Avoid unclear constructs whenever you’re able to.
For an in-depth test of your C knowledge, try figuring out the Bad C Analysis interview question.
Standard Library
The C standard library is commonly called libc, and occasionally stdlib or cstdlib.
For more information and relevant links, see the dedicated glossary entry.
Articles related to standard library evolution:
- Implementing #embed for C and C++ by JeanHeyd Menade
- The Defer Technical Specification: It Is Time by JeanHeyd Menade
Pointers
Pointers are the foundation of C, yet many developers are intimidated by them. These resources will help you better understand pointers.
- The Basics and Pitfalls of Pointers in C
- When 4 + 1 Equals 8: An Advanced Take on Pointers in C
- How to Create Jump Tables via Function Pointer Arrays in C and C++
- How to Use C’s
offsetof()Macro
Variadic Functions
- Stack Overflow: Forward Declaration of a Variadic Function in C
- C FAQ: Passing Around Variadic Arguments
Volatile
Embedded developers will defend their beloved volatile keyword to the death. Make sure you understand what the keyword actually does under the hood!
If in doubt, do not declare a variable volatile. Instead, cast it to volatile in the particular spot where you want to suppress optimizations.
- Embedded.fm: Explaining the C Keyword
volatile - The Trouble with Volatile explains common misconceptions about how volatile works
- Is volatile useful with threads? – the answer may suprise you
Security & Safety
MITRE outlines common weaknesses found in software written in C. Familiarize yourself with these common security flaws to improve your programming abilities.
No, strncpy() is not a “safer” strcpy() points out the weaknesses with strncpy().
Secure Coding in C and C++ (2nd Edition) (SEI Series in Software Engineering)
Microsoft is working on Checked C, a language extension project that adds static and dynamic (runtime) checking for common errors such as buffer overruns, out-of-bounds memory accesses, and incorrect type casts.
For C coding standards with a focus on safety and security, see:
Undefined Behavior
Undefined behavior abounds in the C programming language, and programmers easily trip over it. Here are some resources to improve your knowledge on undefined behavior:
- How Much memory does malloc(0) allocate? is a lightweight introduction to the topic, showing that we can’t really rely on a consistent result with
malloc(0) - Shafik Yaghmour’s Twitter Thread demonstrates a undefined behavior examples
- John Regehr has a series of articles on Undefined Behavior in C and C++:
C11
- C11: The New C Standard summarizes the changes between C99 and C11
- Stackoverflow: Multi-threading Support in C11 provides interesting discussion about new threading support
References
- C programming language reference site that’s run by the C Standards Committee.
- CppReference: C reference – the best C++ reference website contains a detailed reference on C as well
- GNU C Reference Manual documents C99 and the widely-used GNU C extensions
Tools
- Cscope is used to browse, search, and refactor a C project’s complete source tree
- cdecl can be used to figure out tricky declarations such as function pointers
Advanced Techniques
The following advanced techniques are reviewed below:
Bitwise Operations
The best resource for bit manipulation routines is Bit Twiddling Hacks. This code is in the public domain and is used in a variety of projects I’ve worked on.
Exceptions
You can implement exception-like behavior in C with libraries. Our favorite is CException, written by the Throw the Switch team.
Objects in C
For information on how to use “objects” in C, please see this Field Atlas entry.
Information Hiding in C
We can take the previous approach to objects in C further by applying information hiding and encapsulation using opaque pointers.
For a practical example that uses this technique, see Creating a Circular Buffer in C and C++.
Polymorphism and Inheritance in C
For information on how to use polymorphism and inheritance in C, please see this Field Atlas entry.
From Embedded Artistry
Recommended C Libraries
C++
C++ is a compiled programming language originally derived from C. C++ supports object-oriented, generic, and functional programming features.
Table of Contents:
Books
Here are our favorite C++ books:
- Real-Time C++: Efficient Object-Oriented and Template Microcontroller Programming by Christopher Kormanyos
- A Tour of C++ (2nd Edition) (C++ In-Depth Series) by Bjarne Stroustrup
- Effective Modern C++
by Scott Meyers - Modern C++ Programming with Test-Driven Development: Code Better, Sleep Betterby Jeff Langr
C++ YouTube Playlist
We have a Youtube Playlist with our favorite lectures on C++.
From Embedded Artistry
From Around the Web
References
- C++ Reference – the best online reference for the language
- ISO C++ Website – the official C++ website
- The Super-FAQ contains a treasure trove of knowledge that is especially useful to new developers
- C++ Core Guidelines is a collaborative effort to provide guidelines for the effective use of C++ (since C++11).
- C++ Best Practices – a “Collaborative Collection of C++ Best Practices” from Jason Turner
- Covers the safety, maintainability, portability, thread-ability, and performance of C++ code
Blogs
- Shafik Yaghmour’s Blog
- foonathan()::blog by Jonathan Mueller
- Fluent C++ by Jonathan Boccara
- Sutter’s Mill by Herb Sutter
- ISO C++ Blog – C++ articles from around the web
- Meeting C++ Blogroll
- Embedded Template Library blog by John Wellbelove
- Simplify C++ by Arne Mertz
- Modernes C++ by Rainer Grimm
- Kate Gregory’s Blog
- Bartek’s Coding Blog by Bartlomiej Filipek
- Andrzej’s C++ blog by Andrzej Krzemieński
- Sticky Bits by Feabhas Ltd.
Not active, but full of quality content:
On C++ Templates
Rainer Grimm, a C++ trainer, has a series that teaches you about C++ templates:
- Templates – First Steps
- Function Templates
- Function Templates – More Details About Explicit Template Arguments and Concepts
- Class Templates
- Surprise Included: Inheritance and Member Functions of Class Templates
- Alias Templates and Template Parameters
- Template Arguments
- Template Argument Deduction for Class Parameters
- Template Specialization
- Template Specialization – More Details About Class Templates
- Full Specialization of Function Templates
On C++ Concepts
Concepts are a new addition to C++20. The topic can be complex enough to deserve a bit of dedicated study.
- C++20 concepts are structural: What, why, and how to change it? by Jonathan Müller, looks at the concept of nominal vs structural concepts, explains by C++ used a structural approach, and describes how you can mimic nominal concepts in your code.
