Ada for the Embedded C Developer Quentin Ochem Robert Tice Gustavo A

Total Page:16

File Type:pdf, Size:1020Kb

Ada for the Embedded C Developer Quentin Ochem Robert Tice Gustavo A Ada for the Embedded C Developer Quentin Ochem Robert Tice Gustavo A. Hoffmann Patrick Rogers Ada for the Embedded C Developer Release 2021-09 Quentin Ochem Robert Tice Gustavo A. Hoffmann Patrick Rogers. Sep 17, 2021 CONTENTS 1 Introduction 3 1.1 So, what is this Ada thing anyway? ............................... 3 1.2 Ada — The Technical Details .................................. 5 2 The C Developer's Perspective on Ada 7 2.1 What we mean by Embedded Software ............................ 7 2.2 The GNAT Toolchain ....................................... 7 2.3 The GNAT Toolchain for Embedded Targets ......................... 7 2.4 Hello World in Ada ........................................ 8 2.5 The Ada Syntax .......................................... 9 2.6 Compilation Unit Structure ................................... 10 2.7 Packages .............................................. 10 2.7.1 Declaration Protection ................................. 11 2.7.2 Hierarchical Packages ................................. 12 2.7.3 Using Entities from Packages ............................. 12 2.8 Statements and Declarations .................................. 13 2.9 Conditions ............................................. 18 2.10 Loops ............................................... 21 2.11 Type System ........................................... 27 2.11.1 Strong Typing ...................................... 27 2.11.2 Language-Defined Types ................................ 31 2.11.3 Application-Defined Types ............................... 31 2.11.4 Type Ranges ....................................... 34 2.11.5 Unsigned And Modular Types ............................. 36 2.11.6 Attributes ......................................... 40 2.11.7 Arrays and Strings ................................... 41 2.11.8 Heterogeneous Data Structures ........................... 48 2.11.9 Pointers .......................................... 50 2.12 Functions and Procedures ................................... 54 2.12.1 General Form ...................................... 54 2.12.2 Overloading ....................................... 57 2.12.3 Aspects .......................................... 60 3 Concurrency and Real-Time 63 3.1 Understanding the various options .............................. 63 3.2 Tasks ................................................ 63 3.3 Rendezvous ............................................ 66 3.4 Selective Rendezvous ...................................... 67 3.5 Protected Objects ........................................ 69 3.6 Ravenscar ............................................. 73 4 Writing Ada on Embedded Systems 77 4.1 Understanding the Ada Run-Time ............................... 77 4.2 Low Level Programming ..................................... 78 i 4.2.1 Representation Clauses ................................ 78 4.2.2 Embedded Assembly Code .............................. 79 4.3 Interrupt Handling ........................................ 80 4.4 Dealing with Absence of FPU with Fixed Point ........................ 82 4.5 Volatile and Atomic data .................................... 86 4.5.1 Volatile .......................................... 86 4.5.2 Atomic .......................................... 88 4.6 Interfacing with Devices ..................................... 89 4.6.1 Size aspect and attribute ............................... 90 4.6.2 Register overlays .................................... 91 4.6.3 Data streams ...................................... 94 4.7 ARM and svd2ada ........................................ 99 5 Enhancing Verification with SPARK and Ada 101 5.1 Understanding Exceptions and Dynamic Checks ...................... 101 5.2 Understanding Dynamic Checks versus Formal Proof ................... 107 5.3 Initialization and Correct Data Flow .............................. 110 5.4 Contract-Based Programming ................................. 111 5.5 Replacing Defensive Code ................................... 114 5.6 Proving Absence of Run-Time Errors ............................. 116 5.7 Proving Abstract Properties .................................. 117 5.8 Final Comments ......................................... 117 6 C to Ada Translation Patterns 119 6.1 Naming conventions and casing considerations ....................... 119 6.2 Manually interfacing C and Ada ................................ 119 6.3 Building and Debugging mixed language code ....................... 121 6.4 Automatic interfacing ...................................... 122 6.5 Using Arrays in C interfaces ................................... 122 6.6 By-value vs. by-reference types ................................ 124 6.7 Naming and prefixes ...................................... 125 6.8 Pointers .............................................. 126 6.9 Bitwise Operations ........................................ 128 6.10 Mapping Structures to Bit-Fields ................................ 130 6.10.1 Overlays vs. Unchecked Conversions ........................ 143 7 Handling Variability and Re-usability 147 7.1 Understanding static and dynamic variability ........................ 147 7.2 Handling variability & reusability statically .......................... 147 7.2.1 Genericity ........................................ 147 7.2.2 Simple derivation .................................... 150 7.2.3 Configuration pragma files .............................. 155 7.2.4 Configuration packages ................................ 157 7.3 Handling variability & reusability dynamically ........................ 161 7.3.1 Records with discriminants .............................. 161 7.3.2 Variant records ..................................... 163 7.3.2.1 Variant records and unions .......................... 165 7.3.2.2 Optional components ............................. 166 7.3.2.3 Optional output information ......................... 167 7.3.3 Object orientation .................................... 170 7.3.3.1 Type extension ................................. 170 7.3.3.2 Overriding subprograms ........................... 171 7.3.3.3 Comparing untagged and tagged types .................. 171 7.3.3.4 Dispatching calls ................................ 174 7.3.3.5 Interfaces .................................... 175 7.3.3.6 Deriving from multiple interfaces ...................... 177 7.3.3.7 Abstract tagged types ............................. 178 7.3.3.8 From simple derivation to OOP ....................... 180 7.3.3.9 Further resources ............................... 182 ii 7.3.4 Pointer to subprograms ................................ 182 7.4 Design by components using dynamic libraries ....................... 187 8 Performance considerations 191 8.1 Overall expectations ....................................... 191 8.2 Switches and optimizations ................................... 191 8.2.1 Optimizations levels .................................. 191 8.2.2 Inlining .......................................... 192 8.3 Checks and assertions ...................................... 193 8.3.1 Checks .......................................... 193 8.3.2 Assertions ........................................ 196 8.4 Dynamic vs. static structures .................................. 197 8.5 Pointers vs. data copies ..................................... 198 8.5.1 Function returns .................................... 201 9 Argumentation and Business Perspectives 205 9.1 What's the expected ROI of a C to Ada transition? ..................... 205 9.2 Who is using Ada today? .................................... 206 9.3 What is the future of the Ada technology? .......................... 206 9.4 Is the Ada toolset complete? .................................. 207 9.5 Where can I find Ada or SPARK developers? ......................... 207 9.6 How to introduce Ada and SPARK in an existing code base? ................ 208 10 Conclusion 209 11 Appendix A: Hands-On Object-Oriented Programming 213 11.1 System Overview ......................................... 213 11.2 Non Object-Oriented Approach ................................ 214 11.2.1 Starting point in C .................................... 214 11.2.2 Initial translation to Ada ................................ 217 11.2.3 Improved Ada implementation ............................ 221 11.3 First Object-Oriented Approach ................................ 225 11.3.1 Interfaces ......................................... 225 11.3.2 Base type ......................................... 225 11.3.3 Derived types ...................................... 226 11.3.4 Subprograms from parent ............................... 227 11.3.5 Type AB .......................................... 228 11.3.6 Updated source-code .................................. 228 11.4 Further Improvements ..................................... 232 11.4.1 Dispatching calls .................................... 232 11.4.2 Dynamic allocation ................................... 233 11.4.3 Limited controlled types ................................ 234 11.4.4 Updated source-code .................................. 235 Bibliography 241 iii iv Ada for the Embedded C Developer, Release 2021-09 Copyright © 2020 – 2021, AdaCore This book is published under a CC BY-SA license, which means that you can copy, redistribute, remix, transform, and build upon the content for any purpose, even commercially, as long as you give appropriate credit, provide a link to the license, and indicate if changes were made. If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the
Recommended publications
  • Nintendo Wii Software
    Nintendo wii software A previous update, ( U) introduced the ability to transfer your data from one Wii U console to another. You can transfer save data for Wii U software, Mii. The Wii U video game console's built-in software lets you watch movies and have fun right out of the box. BTW, why does it sound like you guys are crying about me installing homebrew software on my Wii console? I am just wondering because it seems a few of you. The Nintendo Wii was introduced in and, since then, over pay for any of this software, which is provided free of charge to everyone. Perform to Popular Chart-Topping Tunes - Sing up to 30 top hits from Season 1; Gleek Out to Never-Before-Seen Clips from the Show – Perform to video. a wiki dedicated to homebrew on the Nintendo Wii. We have 1, articles. Install the Homebrew Channel on your Wii console by following the homebrew setup tutorial. Browse the Homebrew, Wii hardware, Wii software, Development. The Wii was not designed by Nintendo to support homebrew. There is no guarantee that using homebrew software will not harm your Wii. A crazy software issue has come up. It's been around 10 months since the wii u was turned on at all. Now that we have, it boots up fine and you. Thus, we developed a balance assessment software using the Nintendo Wii Balance Board, investigated its reliability and validity, and. In Q1, Nintendo DS software sales were million, up million units Wii software sales reached million units, a million.
    [Show full text]
  • Also Includes Slides and Contents From
    The Compilation Toolchain Cross-Compilation for Embedded Systems Prof. Andrea Marongiu ([email protected]) Toolchain The toolchain is a set of development tools used in association with source code or binaries generated from the source code • Enables development in a programming language (e.g., C/C++) • It is used for a lot of operations such as a) Compilation b) Preparing Libraries Most common toolchain is the c) Reading a binary file (or part of it) GNU toolchain which is part of d) Debugging the GNU project • Normally it contains a) Compiler : Generate object files from source code files b) Linker: Link object files together to build a binary file c) Library Archiver: To group a set of object files into a library file d) Debugger: To debug the binary file while running e) And other tools The GNU Toolchain GNU (GNU’s Not Unix) The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and software for embedded systems. The GNU project produced a set of programming tools. Parts of the toolchain we will use are: -gcc: (GNU Compiler Collection): suite of compilers for many programming languages -binutils: Suite of tools including linker (ld), assembler (gas) -gdb: Code debugging tool -libc: Subset of standard C library (assuming a C compiler). -bash: free Unix shell (Bourne-again shell). Default shell on GNU/Linux systems and Mac OSX. Also ported to Microsoft Windows. -make: automation tool for compilation and build Program development tools The process of converting source code to an executable binary image requires several steps, each with its own tool.
    [Show full text]
  • Embedded Linux Systems with the Yocto Project™
    OPEN SOURCE SOFTWARE DEVELOPMENT SERIES Embedded Linux Systems with the Yocto Project" FREE SAMPLE CHAPTER SHARE WITH OTHERS �f, � � � � Embedded Linux Systems with the Yocto ProjectTM This page intentionally left blank Embedded Linux Systems with the Yocto ProjectTM Rudolf J. Streif Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City São Paulo • Sidney • Hong Kong • Seoul • Singapore • Taipei • Tokyo Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales depart- ment at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions about sales outside the U.S., please contact [email protected]. Visit us on the Web: informit.com Cataloging-in-Publication Data is on file with the Library of Congress.
    [Show full text]
  • Introduction to Embedded C
    INTRODUCTION TO EMBEDDED C by Peter J. Vidler Introduction The aim of this course is to teach software development skills, using the C programming language. C is an old language, but one that is still widely used, especially in embedded systems, where it is valued as a high- level language that provides simple access to hardware. Learning C also helps us to learn general software development, as many of the newer languages have borrowed from its concepts and syntax in their design. Course Structure and Materials This document is intended to form the basis of a self-study course that provides a simple introduction to C as it is used in embedded systems. It introduces some of the key features of the C language, before moving on to some more advanced features such as pointers and memory allocation. Throughout this document we will also look at exercises of varying length and difficulty, which you should attempt before continuing. To help with this we will be using the free RapidiTTy® Lite IDE and targeting the TTE®32 microprocessor core, primarily using a cycle-accurate simulator. If you have access to an FPGA development board, such as the Altera® DE2-70, then you will be able to try your code out in hardware. In addition to this document, you may wish to use a textbook, such as “C in a Nutshell”. Note that these books — while useful and well written — will rarely cover C as it is used in embedded systems, and so will differ from this course in some areas. Copyright © 2010, TTE Systems Limited 1 Getting Started with RapidiTTy Lite RapidiTTy Lite is a professional IDE capable of assisting in the development of high- reliability embedded systems.
    [Show full text]
  • Embedded C Programming I (Ecprogrami)
    To our customers, Old Company Name in Catalogs and Other Documents On April 1st, 2010, NEC Electronics Corporation merged with Renesas Technology Corporation, and Renesas Electronics Corporation took over all the business of both companies. Therefore, although the old company name remains in this document, it is a valid Renesas Electronics document. We appreciate your understanding. Renesas Electronics website: http://www.renesas.com April 1st, 2010 Renesas Electronics Corporation Issued by: Renesas Electronics Corporation (http://www.renesas.com) Send any inquiries to http://www.renesas.com/inquiry. Notice 1. All information included in this document is current as of the date this document is issued. Such information, however, is subject to change without any prior notice. Before purchasing or using any Renesas Electronics products listed herein, please confirm the latest product information with a Renesas Electronics sales office. Also, please pay regular and careful attention to additional and different information to be disclosed by Renesas Electronics such as that disclosed through our website. 2. Renesas Electronics does not assume any liability for infringement of patents, copyrights, or other intellectual property rights of third parties by or arising from the use of Renesas Electronics products or technical information described in this document. No license, express, implied or otherwise, is granted hereby under any patents, copyrights or other intellectual property rights of Renesas Electronics or others. 3. You should not alter, modify, copy, or otherwise misappropriate any Renesas Electronics product, whether in whole or in part. 4. Descriptions of circuits, software and other related information in this document are provided only to illustrate the operation of semiconductor products and application examples.
    [Show full text]
  • Design Languages for Embedded Systems
    Design Languages for Embedded Systems Stephen A. Edwards Columbia University, New York [email protected] May, 2003 Abstract ward hardware simulation. VHDL’s primitive are assign- ments such as a = b + c or procedural code. Verilog adds Embedded systems are application-specific computers that transistor and logic gate primitives, and allows new ones to interact with the physical world. Each has a diverse set be defined with truth tables. of tasks to perform, and although a very flexible language might be able to handle all of them, instead a variety of Both languages allow concurrent processes to be de- problem-domain-specific languages have evolved that are scribed procedurally. Such processes sleep until awak- easier to write, analyze, and compile. ened by an event that causes them to run, read and write variables, and suspend. Processes may wait for a pe- This paper surveys some of the more important lan- riod of time (e.g., #10 in Verilog, wait for 10ns in guages, introducing their central ideas quickly without go- VHDL), a value change (@(a or b), wait on a,b), ing into detail. A small example of each is included. or an event (@(posedge clk), wait on clk un- 1 Introduction til clk='1'). An embedded system is a computer masquerading as a non- VHDL communication is more disciplined and flexible. computer that must perform a small set of tasks cheaply and Verilog communicates through wires or regs: shared mem- efficiently. A typical system might have communication, ory locations that can cause race conditions. VHDL’s sig- signal processing, and user interface tasks to perform.
    [Show full text]
  • Computer Architecture and Assembly Language
    Computer Architecture and Assembly Language Gabriel Laskar EPITA 2015 License I Copyright c 2004-2005, ACU, Benoit Perrot I Copyright c 2004-2008, Alexandre Becoulet I Copyright c 2009-2013, Nicolas Pouillon I Copyright c 2014, Joël Porquet I Copyright c 2015, Gabriel Laskar Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with the Invariant Sections being just ‘‘Copying this document’’, no Front-Cover Texts, and no Back-Cover Texts. Introduction Part I Introduction Gabriel Laskar (EPITA) CAAL 2015 3 / 378 Introduction Problem definition 1: Introduction Problem definition Outline Gabriel Laskar (EPITA) CAAL 2015 4 / 378 Introduction Problem definition What are we trying to learn? Computer Architecture What is in the hardware? I A bit of history of computers, current machines I Concepts and conventions: processing, memory, communication, optimization How does a machine run code? I Program execution model I Memory mapping, OS support Gabriel Laskar (EPITA) CAAL 2015 5 / 378 Introduction Problem definition What are we trying to learn? Assembly Language How to “talk” with the machine directly? I Mechanisms involved I Assembly language structure and usage I Low-level assembly language features I C inline assembly Gabriel Laskar (EPITA) CAAL 2015 6 / 378 I Programmers I Wise managers Introduction Problem definition Who do I talk to? I System gurus I Low-level enthusiasts Gabriel Laskar (EPITA) CAAL
    [Show full text]
  • Compiler Construction
    Compiler Construction Chapter 11 Compiler Construction Compiler Construction 1 A New Compiler • Perhaps a new source language • Perhaps a new target for an existing compiler • Perhaps both Compiler Construction Compiler Construction 2 Source Language • Larger, more complex languages generally require larger, more complex compilers • Is the source language expected to evolve? – E.g., Java 1.0 ! Java 1.1 ! . – A brand new language may undergo considerable change early on – A small working prototype may be in order – Compiler writers must anticipate some amount of change and their design must therefore be flexible – Lexer and parser generators (like Lex and Yacc) are therefore better than hand- coding the lexer and parser when change is inevitable Compiler Construction Compiler Construction 3 Target Language • The nature of the target language and run-time environment influence compiler construction considerably • A new processor and/or its assembler may be buggy Buggy targets make it difficult to debug compilers for that target! • A successful source language will persist over several target generations – E.g., 386 ! 486 ! Pentium ! . – Thus the design of the IR is important – Modularization of machine-specific details is also important Compiler Construction Compiler Construction 4 Compiler Performance Issues • Compiler speed • Generated code quality • Error diagnostics • Portability • Maintainability Compiler Construction Compiler Construction 5 Compiler Speed • Reduce the number of modules • Reduce the number of passes Perhaps generate machine
    [Show full text]
  • Sr. Firmware - Embedded Software Engineer
    SR. FIRMWARE - EMBEDDED SOFTWARE ENGINEER Atel USA is a leading provider of high-performance, small form-factor tracking systems for the vehicle recovery, fleet, heavy trucks, and asset tracking industries. Our team has years of experience in developing complex wireless and communications systems. We have shipped several millions of vehicle tracking devices operating on all major cellular networks. We are seeking an enthusiastic embedded software engineer to be a key team member in the design, implementation, and verification of our asset tracking devices and sensory accessories. The Sr. Firmware Engineer will become an integral part of our company and will work as part of a small, multi-disciplinary development team to design, construct and deliver software/firmware for our current and next generation products. Primary Responsibilities: • Work independently on project tasks as well as work as a team member of a larger project team. • Collaborate with hardware/system design engineers to define the product feature set and work within a product development team to deliver firmware that meets or exceeds product requirements. • Engage with customers and product managers to define requirements, develop software architecture, and plan development in dynamic, evolving customer driven environment. • Deliver innovative solutions from concept to prototype to production. • Conduct/participate in engineering reviews to provide technical input on product designs and quality. • Conduct software unit tests to exercise implemented functionality. • Document software designs. • Troubleshoot and remove defects from production software. • Communicate and interact with team and customers to clearly set expectations, share technical details, resolve issues, and report progress. • Participate in brainstorms and otherwise contribute outside your area of expertise.
    [Show full text]
  • Porting Musl to the M3 Microkernel TU Dresden
    Porting Musl to the M3 microkernel TU Dresden Sherif Abdalazim, Nils Asmussen May 8, 2018 Contents 1 Abstract 2 2 Introduction 3 2.1 Background.............................. 3 2.2 M3................................... 4 3 Picking a C library 5 3.1 C libraries design factors . 5 3.2 Alternative C libraries . 5 4 Porting Musl 7 4.1 M3andMuslbuildsystems ..................... 7 4.1.1 Scons ............................. 7 4.1.2 GNUAutotools........................ 7 4.1.3 Integrating Autotools with Scons . 8 4.2 Repositoryconfiguration. 8 4.3 Compilation.............................. 8 4.4 Testing ................................ 9 4.4.1 Syscalls ............................ 9 5 Evaluation 10 5.1 PortingBusyboxcoreutils . 10 6 Conclusion 12 1 Chapter 1 Abstract Today’s processing workloads require the usage of heterogeneous multiproces- sors to utilize the benefits of specialized processors and accelerators. This has, in turn, motivated new Operating System (OS) designs to manage these het- erogeneous processors and accelerators systematically. M3 [9] is an OS following the microkernel approach. M3 uses a hardware/- software co-design to exploit the heterogeneous systems in a seamless and effi- cient form. It achieves that by abstracting the heterogeneity of the cores via a Data Transfer Unit (DTU). The DTU abstracts the heterogeneity of the cores and accelerators so that they can communicate systematically. I have been working to enhance the programming environment in M3 by porting a C library to M3. I have evaluated different C library implementations like the GNU C Library (glibc), Musl, and uClibc. I decided to port Musl as it has a relatively small code base with fewer configurations. It is simpler to port, and it started to gain more ground in embedded systems which are also a perfect match for M3 applications.
    [Show full text]
  • Sr. Embedded Software/ Firmware Engineer This Position Is Located at the Corporate Headquarters in Henderson, Nevada
    Sr. Embedded Software/ Firmware Engineer This position is located at the Corporate Headquarters in Henderson, Nevada VadaTech, Inc. is seeking experienced candidates for Senior Embedded Software/Firmware Engineer. Primary Responsibilities: • Responsible for the focus on BIOS and board bring up • Responsible for the coordinating and prioritizing application modifications and bug fixes • Responsible for working with the customer support team to troubleshoot and resolve production issues • Responsible for the support, maintenance and documentation of software functionality • Responsible for Contributing to the development of internal tools to streamline the engineering and build and release processes • Responsible for the production of functional specifications and design documents Desired Skills and Experience • Bachelor’s degree in computer science or related field; master’s degree preferred • 8+ years of related work experience • Fluency in C programming required • Experience with Embedded Bootloader/OS porting required • Experience with U-Boot/Embedded Linux porting and PC BIOS maintenance preferred • Experience with new/untested hardware board bring-up required • Demonstrated ability to read hardware schematics and use lab instruments such as oscilloscopes, multimeters and JTAG probes to resolve problems required • Must be knowledgeable in low-level board-support software/hardware interfacing for DDR3 memory, local bus timings, Ethernet PHYs, NOR/NAND flash, I2C/SPI, and others • Experience with diverse CPU types such as PowerPC/PowerQUICC/QorIQ,
    [Show full text]
  • Embedded Linux Systems
    Dpto Sistemas Electrónicos y de Control Universidad Politécnica de Madrid Embedded Linux Systems Using Buildroot for building Embedded Linux Systems with the Raspberry-PI V1.2 Mariano Ruiz 2014 EUIT Telecomunicación Dpto. Sistemas Electrónicos y de C o n t r o l Page 1 of 41 Page 2 of 41 Table of contents 1 SCOPE ........................................................................................................................................ 6 1.1 Document Overview .............................................................................................................. 6 1.2 Acronyms .............................................................................................................................. 6 2 REFERENCED DOCUMENTS ......................................................................................................... 7 2.1 References ............................................................................................................................. 7 3 LAB1: BUILDING LINUX USING BUILDROOT ................................................................................. 8 3.1 Elements needed for the execution of these LABS. .................................................................. 8 3.2 Starting the VMware .............................................................................................................. 8 3.3 Configuring Buildroot. .......................................................................................................... 11 3.4 Compiling buildroot. ...........................................................................................................
    [Show full text]