How to design and implement firmware for embedded systems



Similar documents
Serial Communications

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

The Secrets of RS-485 Half-duplex Communication

Computer Organization and Components

Freescale Semiconductor, I

An Introduction to MPLAB Integrated Development Environment

Am186ER/Am188ER AMD Continues 16-bit Innovation

Designing VM2 Application Boards

BLUETOOTH SERIAL PORT PROFILE. iwrap APPLICATION NOTE

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Lab Experiment 1: The LPC 2148 Education Board

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

AVR Butterfly Training. Atmel Norway, AVR Applications Group

AN4664 Application note

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

UNIT 4 Software Development Flow

Microprocessor & Assembly Language

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

Exception and Interrupt Handling in ARM

Digitale Signalverarbeitung mit FPGA (DSF) Soft Core Prozessor NIOS II Stand Mai Jens Onno Krah

Andreas Läng. Hilscher Gesellschaft für Systemautomation mbh Intelligent solutions for industrial communication.

Chapter 1 Lesson 3 Hardware Elements in the Embedded Systems Chapter-1L03: "Embedded Systems - ", Raj Kamal, Publs.: McGraw-Hill Education

Accessing I2C devices with Digi Embedded Linux 5.2 example on Digi Connect ME 9210

Embedded Component Based Programming with DAVE 3

Serial Communications

The Secrets of Flow Control in Serial Communication

Hello, and welcome to this presentation of the STM32 SDMMC controller module. It covers the main features of the controller which is used to connect

Eliminate Risk of Contention and Data Corruption in RS-485 Communications

Software User Guide UG-461

Welcome to the Introduction to Controller Area Network web seminar My name is William Stuart, and I am a Applications Engineer for the Automotive

Comparing RTOS to Infinite Loop Designs

1. Computer System Structure and Components

Implementing a Data Logger with Spansion SPI Flash

Tutorial for MPLAB Starter Kit for PIC18F

An Introduction to the ARM 7 Architecture

Embedded Systems on ARM Cortex-M3 (4weeks/45hrs)

DKWF121 WF121-A B/G/N MODULE EVALUATION BOARD

Measuring Resistance Using Digital I/O

EE 472 Lab 2 (Group) Scheduling, Digital I/O, Analog Input, and Pulse Generation University of Washington - Department of Electrical Engineering

Microtronics technologies Mobile:

Waspmote. Quickstart Guide

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

- 35mA Standby, mA Speaking pre-defined phrases with up to 1925 total characters.

Chapter 13. PIC Family Microcontroller

Fondamenti su strumenti di sviluppo per microcontrollori PIC

Computer and Set of Robots

S7 for Windows S7-300/400

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

AN10866 LPC1700 secondary USB bootloader

DAC Digital To Analog Converter

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Building Blocks for PRU Development

Software based Finite State Machine (FSM) with general purpose processors

AN LPC1700 timer triggered memory to GPIO data transfer. Document information. LPC1700, GPIO, DMA, Timer0, Sleep Mode

Lab 3 Microcontroller programming Interfacing to Sensors and Actuators with irobot

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

UPS PIco. to be used with. Raspberry Pi B+, A+, B, and A. HAT Compliant. Raspberry Pi is a trademark of the Raspberry Pi Foundation

Soft processors for microcontroller programming education

Real Time Programming: Concepts

The I2C Bus. NXP Semiconductors: UM10204 I2C-bus specification and user manual HAW - Arduino 1

Debouncing Switches. Mechanical switches are one of the most common interfaces to a uc.

Lab 1 Course Guideline and Review

Application Note. EtherCAT Master Architecture. Applicable Products: Yaskawa Servodrives with CANopen over EtherCAT

System Design Issues in Embedded Processing

Hello, and welcome to this presentation of the STM32 Serial Audio Interface. I will present the features of this interface, which is used to connect

White Paper. Real-time Capabilities for Linux SGI REACT Real-Time for Linux

How To Program A Microcontroller Board (Eb064) With A Psp Microcontroller (B064-74) With An Ios 2.5V (Power) And A Ppt (Power Control) (Power Supply) (

RTOS Debugger for ecos

Lesson-16: Real time clock DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK

System-on-a-Chip with Security Modules for Network Home Electric Appliances

Pre-tested System-on-Chip Design. Accelerates PLD Development

Application Note: AN00141 xcore-xa - Application Development

Chapter 11 I/O Management and Disk Scheduling

What is LOG Storm and what is it useful for?

Programming Flash Microcontrollers through the Controller Area Network (CAN) Interface

Introduction. What is an Operating System?

SPI I2C LIN Ethernet. u Today: Wired embedded networks. u Next lecture: CAN bus u Then: wireless embedded network

Description of High Accuracy Digital Pressure Gauge Design

Computer Systems Structure Input/Output

UNIVERSITY OF CALIFORNIA. Los Angeles. Software for A Fault Tolerant. Microcontroller Network. A thesis submitted in partial satisfaction

A Design of Video Acquisition and Transmission Based on ARM. Ziqiang Hao a, Hongzuo Li b

XMOS Programming Guide

Introduction to Operating Systems. Perspective of the Computer. System Software. Indiana University Chen Yu

ARM Cortex -A8 SBC with MIPI CSI Camera and Spartan -6 FPGA SBC1654

Wireless monitoring system for temperature and humidity based on ZigBee

PIC Programming in Assembly. (

Axia IP-Audio Driver Update v June, 2013

Von der Hardware zur Software in FPGAs mit Embedded Prozessoren. Alexander Hahn Senior Field Application Engineer Lattice Semiconductor

CAN bus board. EB018

PLC training panel (Twido version)

White Paper. Technical Capabilities of the DF1 Half-Duplex Protocol

USB Audio Simplified

E-Blocks Easy Internet Bundle

Performance Comparison of RTOS

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

Storage. The text highlighted in green in these slides contain external hyperlinks. 1 / 14

AN3252 Application note

Going Linux on Massive Multicore

Transcription:

How to design and implement firmware for embedded systems Last changes: 17.06.2010 Author: Rico Möckel

The very beginning: What should I avoid when implementing firmware for embedded systems? Writing code for embedded systems is not the same as writing the user code for a PC (personal computer). It is more like writing a driver for a PC. If no special embedded operation system (OS) like TinyOS is used the firmware engineer has to take care of all basic things like setting up hardware registers himself. The code should be minimalistic, efficient, real-time, stable, easy to read etc. Your system is embedded meaning that it is typically connected to real hardware like motors, sensors, communication busses etc. The means that software errors can have dramatic and expensive results. Pins can get shorted in software leading to high currents and damage of the electronics. Batteries that should be monitored can be under- and overcharged and explode. Sensors can get destroyed when not being operated correctly. Gear boxes of motors can break without proper control. So test you system first in a safe environment using power supplies with selectable current limit and make sure that the pins of your microcontroller are correctly configured as inputs and outputs. Never ever do busy waiting with wile loops. Always use timers! Why: your busy waiting will keep the processor waiting in a busy state. It will not be able to do anything useful during that time. Remember: Embedded systems have to run in real time. There is typically no scheduler like when writing software for an operating system that automatically switches between different processes. Make yourself familiar with design strategies like interrupts and DMA (direct memory access). Interrupts will help you to avoid actively waiting for a condition like a change of an input signal but will instead allow you to react only when the change happens. DMA will free your processor from the dump work of just waiting to be ready to transfer some data from one memory to another. Never ever put a lot of code into an interrupt service routine. These call back functions are only there as signals and should be as short as possible. So if you need to do a lot of work after an interrupt: set a flag and get the work done in the main loop. Be aware of the fact that embedded systems have to face unknown time conditions. E.g. you never know when and in which precise order a user will use a switch or push button or you will receive some data through a UART interface. So be prepared for this. To make your code readable and reusable take care of coding standards and be consistent. Separate your code into different files and functions in a meaningful manner. Do not write spaghetti code. Very good C-code looks like C++. Make use of structs where useful. Do not make extensive use of functions where it is not necessary. Each call requires the current variables to be copied on the stack and after the call to be copied back. This is time consuming and can seriously decrease the speed of your code. Use pointers and buffers to generate efficient code. Avoid copying data. Make use of macros to design efficient and readable code. You can even implement simple functions with macros since macros take parameters. Document your code. Write the documentation is a way that you can use automatic extraction tools like Doxygen. It is always a good idea to use a subversion (SVN) system to keep track of the different versions of your code.

Microcontrollers and their development environments typically come with debugging functionalities. Use them! If useful think about using some highly efficient ASEMBLER code where useful.

First step: design your firmware Before even starting to implement any code think, plan and write a documentation of your code. Writing firmware for embedded systems is not a matter of trial and error but of thinking and coding. Without a good implementation strategy and a properly set up firmware architecture your code will be unreadable, difficult to update and debug and full of bugs and design errors. Nobody will be willing to reuse your code. And this simply means that your firmware was a waste of time. So this is what you need to write down carefully: What is roughly the purpose of your firmware? E.g. I want to write the firmware that allows an RS232 interface to talk to an RS485 interface and vice versa. What are the challenges you are facing with regard to the purpose of your firmware? E.g. RS485 is a half-duplex point-to-multipoint communication interface where only one communication partner is allowed to send data at a time while RS232 is a full-duplex pointto-point communication interface. Problems I need to handle are: (1) How to ensure that only one communication partner is sending data at a time? (2) What to do with data that I received over RS232 that cannot be forwarded to the RS485 since the bus is currently used by someone else? (3) How to avoid deadlocks meaning that the RS485 bus is blocked forever? How do you plan to deal with the challenges you are facing? Which strategies do you plan to follow? E.g. (1) I will implement a TDMA (time division multiple access) protocol with one master where only one communication partner can send data at a time. The master will ensure that only one communication partner will send data at a time by asking for the data. No slave is allowed to send data without being asked for it. (2) I will implement a ring buffer that can store the data that cannot be forwarded. Once the bus is free I will empty the buffer by sending the stored data. (3) I will use a timer that ensures that the RS485 bus is used again if a timeout occurs. If the answer of a slave is not received in time I will assume that there is a problem with this device and I will send an error message to the host. Make a detailed plan for the firmware architecture that you will be using. Which files do I need? Which peripheries of the microcontroller are used? How do I set up these peripheries correctly? Which functions need to be implemented? E.g. I need one source and header file for each periphery I am using. Furthermore, I need a main file as well as a configuration file where I am storing general configurations that I will need to change more often. Since I want to make my code as reusable as possible I will implement a special header and source file as well where I place configurations that are specific for my particular microcontroller like the configuration of IO ports. There will be initialization functions for each of the peripheries that are called from the main init function in the main file. I will have to set up the system clock correctly. I plan to use the full speed of 40MIPS (mega instructions per second). That is why I need to set up the PLL (phase locked loop). I will need to UARTs (universal asynchronous receiver transmitter). I do not plan to use the ADC. So I will ensure that this is switched off. I will need some GPIOs (general purpose input output) for debugging and to talk to the LEDs. And so on. Make drawings!

After and only after all these things are planned and carefully written down we start writing the implementation.

Second step: implementation of your firmware After your documentation is ready you may start the implementation. Remember: think first and do not just load your code into the microcontroller and try some stuff. You are working with an embedded system. 1. Set up your framework of (empty) header and source files. Commit your files to the SVN system. 2. Set up the clock of your microcontroller as well as the other basic configuration registers. 3. Create the main() and the main_init() functions. 4. Properly set up the pins of your microcontroller. Make sure all pins are correctly configured as inputs and outputs. Create macros to talk to your pins. It is much easier to set a GPIO port to one by writing PORT_LED = 1; then by writing (PORTA << 9) & 1; 5. You will need a timer. Set this up correctly. 6. Now it would be a good idea to test your timer and clock by driving a GPIO port with that timer and carefully measuring the port with an oscilloscope. Make sure that the period is correct and that there is not jitter. Once this is working it is a good time to commit your files to the SVN system. 7. You may now want to set up a UART to have some extra debugging possibility. Try to send and receive data with the UART. 8. After all these things are working correctly you may start implementing your specific firmware. Do not forget to commit your files to the SVN system.