May 2018: C++ Embedded Frameworks

Welcome to the May 2018 edition of the Embedded Artistry Newsletter! This is a monthly newsletter of curated and original content to help you build superior embedded systems. This newsletter is intended to supplement the website and covers topics not mentioned there.

This month we'll cover:

  • Four open-source C++ embedded frameworks that can jump-start your next project:
    • Kvasir
    • modm
    • crect
    • EMB2
  • Interesting links from around the web
  • Embedded Artistry website updates and popular posts

Open-Source Embedded Frameworks

In last month's newsletter we recommended quality C and C++ libraries to give your next project a boost.

This month we are taking things a step further by diving into embedded C++ frameworks which can be used as the foundation for your next product or project. While each framework is focused on solving a distinct problem, there is at least one that will benefit your team.

We will cover the following four embedded frameworks:

  1. Kvasir, which specializes in compile-time checking of Special Function Register usage
  2. modm, which utilizes vendor-provided data to generate drivers and BSPs
  3. crect, which generates a deadlock-free and data-race-free scheduler for real-time systems at compile time
  4. EMB2, which provides a paradigm for developing multi-core and mutli-chip embedded programs

Kvasir

Kvasir is a C++11 framework created by Odin Holmes. Kvasir is suitable for tiny bare-metal applications such as ARM cores. By using C++11’s constexpr feature and template meta-programming, Kvasir’s features come at no cost. In some cases, the meta-programming magic provides superior implementations to common hand-coded approaches!

The primary feature is the Kvasir::Register abstraction, which enables strongly typed register and bit-field definitions and provides SFR read/write methods. These abstractions improve type safety and enable static checking of register interactions during compilation. As long as the register definitions are correct, Kvasir will eliminate off-by-one errors, prevent users from writing to reserved bits, ensure that bit-field accesses don't overflow into other fields, and generate warnings if you are incorrectly using a register that performs a clear-on-read operation.

In addition to SFR abstractions, Kvasir also supplies the Kvasir::IO abstraction to provide similar benefits with GPIO. The IO abstraction utilizes a pinLocation concept to define GPIOs and provides constexpr meta functions such as makeOpenDrain(), makeOutput(), set(), clear(), and read().

SFR bugs are extremely common on embedded systems. Eliminate them altogether by building your next system with Kvasir.

For more on Kvasir:

modm

modm (Modular Object-oriented Development for Microcontrollers) is a C++14 framework built by Niklas Hauser and Fabian Grief. The modm project uses vendor-provided chip data with a library builder, enabling modm to automatically generate startup code, chip-specific drivers, external drivers, and BSPs for your platform. Since modm provides a portable HAL, you can easily migrate your software from one processor to another supported processor with minimal effort.

modm provides a framework which is suitable for bare-metal systems ranging from the ATtiny to a 32-bit ARM Cortex-M. The HAL features no memory allocations, low RAM consumption, and lightweight stack-less threads. The framework also provides useful algorithms suitable for bare-metal systems, as well as drivers for a wide variety of SPI and I2C peripherals. modm is well-tested, featuring 64 test groups with 343 test cases and over 4000 assertions. While the HAL is not fully tested in an automated manner, a variety of example hardware projects are regularly checked by the CI server.

The modm framework currently supports ~1350 AVR and ARM Cortex-M microcontrollers from Atmel, ST, and NXP. If you are using a processor from of those vendors, modm can provide your team with a stable foundation of drivers and the advantage of being able to quickly migrate your software to another processor.

For more on modm:

crect

The crect project (pronounced correct) is a C++14 framework for generating a scheduler for Cortex-M microcontrollers at compile-time. crect uses the Cortex-M's Nested Vector Interrupt Controller (NVIC) to implement a Stack Resource Policy (SRP) scheduler which guarantees deadlock-free and data-race-free execution.

crect is built upon the Kvasir Meta-programming Library, which is also the foundation of the Kvasir framework. The use of C++ meta-programming and C++14 features allows priority ceilings and interrupt masks to be calculated at compile time. Resource locks are handled through RAII, and resource access is handled using a monitor pattern).

The most impressive thing about this framework is the minimal resource requirements:

  • ~400 bytes of memory for the static elements (linked list, SysTick, time implementation)
  • 4-5 instructions per job for initializing the NVIC
  • 2-3 instructions per queue element for initializing the asynchronous queue
  • 3-4 instructions + 4 bytes of stack space for a lock
  • 1-3 instructions for an unlock
  • 2-4 instructions for a pend/clear
  • 20-30 instructions per item in the queue for async

If you are working on a bare-metal ARM program with real-time concerns, crect is an RTOS alternative that provides protection against common multithreading concerns like priority inversion, deadlocks, and race conditions.

For more on crect:

EMB2

We discussed the EMB2 project in the November 2017 newsletter in the context of Industry Standard Multicore APIs. I am including EMB2 again here to emphasize its importance as a modern embedded systems framework.

EMB2 is a framework developed by Siemens and the University of Houston. EMB2 provides generic building blocks for building multi-core or multi-chip embedded applications, including basic parallel algorithms, concurrent data structures, and application skeletons. Since EMB2 is targeted for embedded applications, it provides soft-real-time support, predictable memory consumption (no dynamic memory allocations after startup), non-blocking APIs, and support for task priorities and affinities.

The framework utilizes the MTAPI abstraction layer, enabling EMB2 programs to be easily ported to new operating systems and processor architectures. Heterogeneous and distributed embedded programming is simplified by utilizing MTAPI. Developers can easily distribute work across processor cores, hardware accelerators, GPUs, DSPs, FPGAs, or networked devices.

The EMB2 base library is implemented as a C API with C++ wrappers. While the parallel algorithms, dataflow patterns, and concurrent containers are implemented in C++. C99 and C++03 are used as the implementation standard to provide maximum usability in the embedded world. C11 and C++11 are also supported.

If you are building a product which uses a multi-core processor, multiple processors, or hardware accelerators, EMB2 provides a solid and portable foundation that will enable your team to take full advantage of your system's hardware resources.

For more on EMB2:

Around the Web

The Industrial Internet Consortium published their IoT Security Maturity Model. If your team is working on an IoT device, this model will help you figure out where you need to be on the security spectrum and how to properly invest in the relevant security requirements.

Thinking about cutting corners or ignoring security in your IoT product? You might be featured in the IoT Hall of Shame.

Niklas Hauser, the author of modm, has two great articles for improving your embedded C++ skills:

Website Updates

The Archive now has a Guest Posts section so our readers can find our external publications.

Additional terms have been added to the Glossary: SRP, SFR, Cortex-A, Cortex-M, RTFM, NVIC

The Software References page was expanded with a variety of embedded and software development links.

Hardware rules of thumb were added to the Hardware References page.

The For Beginners page was expanded with C pointer references, code review articles, and introductory GitHub resources.

New Articles

These articles were published on our website in April:

I also shared some of my personal business experience with the Harpoon team in a Customer Spotlight.

These were our most popular articles in April:

  1. Circular Buffers in C/C++
  2. Installing LLVM/Clang on OSX
  3. Jenkins: Configuring a Linux Slave Node
  4. C++ Casting, or: Oh No, They Broke Malloc!"
  5. std::string vs C-strings
  6. An Overview of C++ STL Containers
  7. Jenkins: Running Steps as sudo
  8. Demystifying ARM Floating Point Compiler Options
  9. Implementing Malloc: First-fit Free List
  10. A Simple Consulting Services Agreement

Thanks for Reading!

Have any feedback, questions, suggestions, interesting articles, or resources to recommend to other developers? Simply reply to this email!

While you wait on the next edition, check out the website or follow us on Twitter

Happy hacking!

-Phillip