Modern C++ for Embedded Systems

Similar documents
Digital Input and Output

STM32F100RB processor GPIO notes rev 2

Important From Last Time

ELEC 3040/3050 Lab Manual Lab 2 Revised 8/20/14. LAB 2: Developing and Debugging C Programs in MDK-ARM for the STM32L100RC Microcontroller

Developing Reusable Device Drivers for MCU's

MISRA-C. Subset of the C language for critical systems

C Refresher, Advance C, Coding Standard, Misra C Compliance & Real-time Programming

Real Time & Embedded Systems. STM32 GPIO and Timers

The xpcc microcontroller framework

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Using the Special Function Registers of the Digital I/O interface of STM32

Using the Special Function Registers of the Digital I/O interface of STM32

Developing and Debugging C Programs in MDK-ARM for the STM32L100RC Microcontroller

Input/Output Programming

TEVATRON TECHNOLOGIES PVT. LTD Embedded! Robotics! IoT! VLSI Design! Projects! Technical Consultancy! Education! STEM! Software!

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Explicit Conversion Operator Draft Working Paper

Advanced Operating Systems Embedded from Scratch: System boot and hardware access. Federico Terraneo

Tokens, Expressions and Control Structures

Casting in C++ (intermediate level)

HOW TO INTEGRATE NFC FRONTENDS IN LINUX

Basic Types, Variables, Literals, Constants

Special Topics for Embedded Programming

ST 软件 软件平台 2. TouchGFX

STM32F1xx motor-control firmware for dspin. Quick guide

AD916x API Specification Rev 1.0

AD9164 API Specification Rev 1.0

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

STM32 F0 Value Line. Entry-level MCUs

L2 - C language for Embedded MCUs

The Mercury project. Zoltan Somogyi

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Interfacing the RAD1419 A/D converter with the GRLIB IP core library

ECE 362 Experiment 3: General Purpose I/O

AN4838. Managing memory protection unit (MPU) in STM32 MCUs. Application note. Introduction

Newbie s Guide to AVR Interrupts

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Introduction to I2C & SPI. Chapter 22

18-642: Code Style for Compilers

Hands-On with STM32 MCU Francesco Conti

ECE 598 Advanced Operating Systems Lecture 8

Vector and Free Store (Pointers and Memory Allocation)

ARM Microcontroller Course

INDUSTRIAL TRAINING:6 MONTHS PROGRAM TEVATRON TECHNOLOGIES PVT LTD

ECE 362 Lab Verification / Evaluation Form Experiment 3

ECE 471 Embedded Systems Lecture 2

Network Embedded Systems Sensor Networks Fall Hardware. Marcus Chang,

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

RX Family APPLICATION NOTE. Simple I 2 C Module Using Firmware Integration Technology. Introduction. Target Device.

Four Components of a Computer System

Using a Segmented Memory System to Enforce Modularity between Programs. Rohit Rao March 20, 2003

Module Introduction. PURPOSE: The intent of this module is to explain MCU processing of reset and interrupt exception events.

Modern C++ White paper: Making things do stuff. Glennan Carnie Technical consultant, Feabhas

Embedded Systems Programming - PA8001

Multi-core Architectures. Dr. Yingwu Zhu

Serial Peripheral Interface Bus SPI

General Purpose I/O ARM University Program Copyright ARM Ltd

Using Enum Structs as Bitfields

Improving Enumeration Types [N1513= ] David E. Miller

MICROPROCESSOR BASED SYSTEM DESIGN

CS11 Advanced C++ Fall Lecture 7

Lab #4: GPIOs in Assembly Language Week of 18 February 2019

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

P1267R0: Custom Constraint Diagnostics

Embedded C. ECE Rick

Interconnects, Memory, GPIO

STM32F3. Cuauhtémoc Carbajal ITESM CEM 12/08/2013

RASPBERRY PI EXAMPLES WITH CLICK BOARDS

EECS 373 Design of Microprocessor-Based Systems

Digital Control for Space Power Management Devices

Functions in C C Programming and Software Tools

Chapter 17 vector and Free Store. Bjarne Stroustrup

ECE332, Week 8. Topics. October 15, Exceptions. Hardware Interrupts Software exceptions

RX Family APPLICATION NOTE. I 2 C Bus Interface (RIIC) Module Using Firmware Integration Technology. Introduction. Target Device.

AT03262: SAM D/R/L/C System Pin Multiplexer (SYSTEM PINMUX) Driver. Introduction. SMART ARM-based Microcontrollers APPLICATION NOTE

MODERNIZATION C++ Bjarne Stroustrup, creator of C++

ECE2049 E17 Lecture 4 MSP430 Architecture & Intro to Digital I/O

Embedded Elixir. Elixir Taiwan Meetup July 25, 2016 Jake Morrison

C-LANGUAGE CURRICULAM

CUDA Toolkit CUPTI User's Guide. DA _v01 September 2012

Lecture 5: Computing Platforms. Asbjørn Djupdal ARM Norway, IDI NTNU 2013 TDT

Introduce C# as Object Oriented programming language. Explain, tokens,

External memory code execution on STM32F7x0 Value line and STM32H750 Value line MCUs

Using the MPU with an RTOS to Enhance System Safety and Security

SPI on RTU (plus other fun features & peripherals)

Learning Module 9. Managing the Sensor: Embedded Computing. Paul Flikkema. Department of Electrical Engineering Northern Arizona University

S32 SDK for Power Architecture Release Notes Version EAR

Modern and Lucid C++ Advanced for Professional Programmers. Part 7 Compile-Time Computation. Department I - C Plus Plus

Hello, and welcome to this presentation of the STM32 I²C interface. It covers the main features of this communication interface, which is widely used

Francesco Nidito. Programmazione Avanzata AA 2007/08

Hello, and welcome to this presentation of the STM32 Reset and Clock Controller.

The UtePC/Yalnix Memory System

System Energy Efficiency Lab seelab.ucsd.edu

Semantic Analysis. Lecture 9. February 7, 2018

EECS 373 Design of Microprocessor-Based Systems

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

A Fast Review of C Essentials Part I

Advanced Embedded Systems

EECS 373 Design of Microprocessor-Based Systems

Section 5 SERCOM. Tasks SPI. In this section you will learn:

Transcription:

Modern C++ for Embedded Systems Part 1 Compile-Time Device Models Alexander Graf January 25th, 2019 1/27

Outline 1 Terms and Conditions :) 2 C-World Problems 3 C++ Solutions 4 Restraints in the Industry 5 Conclusions and Discussion 2/27

Terms and Conditions :) Defining Embedded interact with physical environment via sensors and actuators cost and resource constrained talk assumes typical MCUs < 1MB internal program memory AVR, ARMv7M based (e.g. STM32, LPC5), etc. bare-metal or small RTOS or true kernel space programming on larger systems 3/27

Terms and Conditions :) Todays Focus The Register Access Layer 4/27

Terms and Conditions :) Todays Focus The Register Access Layer Example Code for STM32F107 https://github.com/alibabashack/cxx-device-models 4/27

C-World Problems Typical Register-Level Mistakes Intention: Set Pin B5 high ODR = Output Data Register IDR = Input Data Register 5/27

C-World Problems Typical Register-Level Mistakes Intention: Set Pin B5 high ODR = Output Data Register IDR = Input Data Register Mask Confusion 1 1 2 3 # define GPIO_ODR_ODR_5 (( uint32_t ) 0 x00000020 ) # define GPIO_IDR_IDR_5 (( uint32_t ) 0 x00000020 ) # define GPIOB (( GPIO_t *) GPIOB_BASE ) 4 5 GPIOB - > ODR = GPIO_IDR_IDR_5 ; 5/27

C-World Problems Typical Register-Level Mistakes Mask Confusion 1 1 2 3 # define GPIO_ODR_ODR_5 (( uint32_t ) 0 x00000020 ) # define GPIO_IDR_IDR_5 (( uint32_t ) 0 x00000020 ) # define GPIOB (( GPIO_t *) GPIOB_BASE ) 4 5 GPIOB - > ODR = GPIO_IDR IDR_5 ; X X 7 7 compiles works (by accident) but it is not correct! And it is non-optimal (on STM32) 6/27

C-World Problems Typical Register-Level Mistakes Intention: Enable clock for blocks SPI2 and GPIOB RCC = Reset and clock control APB = Advanced Peripheral Bus 7/27

C-World Problems Typical Register-Level Mistakes Intention: Enable clock for blocks SPI2 and GPIOB RCC = Reset and clock control APB = Advanced Peripheral Bus Mask Confusion 2 1 2 RCC - > APB1ENR = ( RC C_ AP B1 EN R_ SP I2 EN R C C _A P B 2E N R _G P I OB E N ) ; 7/27

C-World Problems Typical Register-Level Mistakes Mask Confusion 2 1 2 RCC - > APB1ENR = ( RC C_ AP B1 EN R_ SP I2 EN RCC_APB2ENR_GPIOBEN ) ; X 7 7 7 compiles but does not work may introduces side effects on existing behavior may invoke undefined hardware behavior (e.g. unexpected power consumption) 8/27

C-World Problems Resulting Issues 7 Creates lots of problems for future-homer! 9/27

C-World Problems Resulting Issues time wasted for flashing to target creating stimuli/test fixtures lots of manual work remaining hard-to-find non-functional bugs undetected usage of undefined behavior catastrophic if the chip die changes YES, this happens! 10/27

C-World Problems Resulting Issues time wasted for flashing to target creating stimuli/test fixtures lots of manual work remaining hard-to-find non-functional bugs undetected usage of undefined behavior catastrophic if the chip die changes YES, this happens! Our aim: The compiler shall detect illegal/non-optimal register usage! 10/27

C-World Problems Reasons for Compiler Blindness GPIO memory map definition 1 2 3 4 5 6 7 8 typedef struct { volatile uint32_t volatile uint32_t volatile uint32_t volatile uint32_t //... } GPIO_TypeDef ; CRL ; CRH ; IDR ; ODR ; everything is of the same data type (integer) thus, we can assign apples to oranges 11/27

C-World Problems Reasons for Compiler Blindness GPIO Output Data Register bit definition 1 2 3 4 # define GPIO_ODR_ODR0 (( uint16_t ) 0 x0001 ) # define GPIO_ODR_ODR1 (( uint16_t ) 0 x0002 ) # define GPIO_ODR_ODR2 (( uint16_t ) 0 x0004 ) //... bits of the same field are unrelated language-wise 12/27

C-World Problems Reasons for Compiler Blindness ODR, IDR: different bits for the same logical pin 1 2 # define GPIO_ODR_ODR_5 (( uint32_t ) 0 x00000020 ) # define GPIO_IDR_IDR_5 (( uint32_t ) 0 x00000020 ) no differentiation between logical and physical structures mechanism and policy1 1 see Linux Device Drivers Corbet et al. 13/27

C-World Problems Reasons for Compiler Blindness mechanism the intention of an action policy (Verfahrensweise) how the action is achieved no differentiation between logical and physical structures mechanism and policy self-documenting code 14/27

C-World Problems Lets Discuss What can we do to engage the compiler? one type per register cluster related bits separate mechanism from policy check invalid value combinations 15/27

C-World Problems The True Challenge enum and typedef are unsafe: implicit conversions bit fields also suffer from implicit conversions no mechanism to convert types (except casting) non-trivial value checks must be performed at runtime 16/27

C-World Problems The True Challenge enum and typedef are unsafe: implicit conversions bit fields also suffer from implicit conversions no mechanism to convert types (except casting) non-trivial value checks must be performed at runtime Conclusion The C type system is to weak for our purpose. 16/27

C++ Solutions New Tools enum class (Scoped Enumerations) define types without default operators or conversions user-defined operators define exactly the operations you need constexpr running code at compile-time static assert checking conditions at compile-time 17/27

C++ Solutions Sorting Apples and Oranges GPIO registers with individual types 1 2 3 4 5 6 7 8 struct GpioRegs { PortConfigLowReg CRL ; Por tconfi ghighr eg CRH ; InputDataReg IDR ; OutputDataReg ODR ; SetResetMaskReg BSRR ; //... }; 18/27

C++ Solutions Separating Mechanism and Policy 1 2 class OutputDataReg { public : 3 4 5 6 7 OutputDataReg & operator =( PinId pid ) { rawreg = (1 < < static_cast < size_t > ( pid ) ) ; return * this ; } 8 9 10 11 12 OutputDataReg & operator =( PinMask pm ) { rawreg = static_cast < uint32_t > ( pm ) ; return * this ; } 13 14 15 16 private : volatile uint32_t rawreg ; }; 19/27

C++ Solutions Defining Value Types 1 2 3 4 enum class PinId { Px0, Px1, Px2, Px3, Px4, Px5, Px6, Px7, Px8, Px9, Px10, Px11, Px12, Px13, Px14, Px15, }; 5 6 7 8 enum class PinMask : uint32_t { // PinMasks are created indirectly from PinId using logic operators }; 9 10 11 gpioeregs - > ODR = PinId :: Px2 ; gpioeregs - > ODR = ( PinId :: Px2 PinId :: Px3 ) ; 20/27

C++ Solutions Converting Value Types 1 2 3 4 5 6 constexpr PinMask operator ( PinId l, PinId r ) { return static_cast < PinMask > ( (1 << static_cast < size_t > ( l ) ) (1 << static_cast < size_t > ( r ) ) ); } 7 8 9 10 11 12 13 constexpr PinMask operator ( PinId l, PinMask r ) { return static_cast < PinMask > ( (1 << static_cast < size_t > ( l ) ) static_cast < size_t > ( r ) ); } 21/27

C++ Solutions Checking Value Ranges 1 2 3 4 5 6 7 template < PinId p, PinMode m, PinConfig c > constexpr PinSetupHigh cr ea te Pi ns et up Hi gh () { static_assert ( static_cast < size_t > ( p ) > 7, " selected PinId invalid " ) ; //... } 8 9 10 11 12 gpioeregs - > CRL = createpinsetuplow < PinId :: Px2, PinMode :: Output2Mhz, PinConfig :: GeneralPushPullOutput >() ; 22/27

C++ Solutions What did we gain? Compiler can throw errors for X mixed-up bit masks X illegal value ranges X suboptimal register access X impossible to use reserved/undefined behavior X zero runtime overhead 7 Need to invest in a lot of new code 23/27

Restraints in the Industry C as Dominating Language 70% of Embedded Software is written in C 23% is written in C++ Industry is havily focused to support C everything else is optional primary solution: building on top of C 2 2 Barr Group Embedded Systems Safety & Security Survey 2018 24/27

Restraints in the Industry Building on Top of C Peripheral Library with assertions (e.g. ST) assertions only thrown at runtime runtime overhead Automatic Code Generation (e.g. ST CubeMx) vendor-dependent tools in build integration of automatic and manual code support stops at the chip boundary 25/27

Restraints in the Industry CubeMx 26/27

Conclusions and Discussion C++ is also suiteable for lowest code levels can do powerfull things without run time overhead can offer advanced diagnostics at compile time The industry will likely not support this approach. Lets make it free software then! 27/27

Conclusions and Discussion Q/A 28/27

Conclusions and Discussion Q/A possible future talks: Test-Driven Development for Embedded Systems Using Smart Pointers with Memory Pools Device Driver Architecture 28/27