Comparing RTOS to Infinite Loop Designs



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

COMPUTER HARDWARE. Input- Output and Communication Memory Systems

Freescale Semiconductor, I

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

快 速 porting μc/os-ii 及 driver 解 說

How to design and implement firmware for embedded systems

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

Have both hardware and software. Want to hide the details from the programmer (user).

SYSTEM ecos Embedded Configurable Operating System

A Practical Approach to Education of Embedded Systems Engineering

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

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

µtasker Document FTP Client

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

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

S7 for Windows S7-300/400

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

Animated Lighting Software Overview

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

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Tutorial for MPLAB Starter Kit for PIC18F

WA Manager Alarming System Management Software Windows 98, NT, XP, 2000 User Guide

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Python Loops and String Manipulation

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

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

Chapter 5 Real time clock by John Leung

Kepware Technologies Optimizing KEPServerEX V5 Projects

Gentran Integration Suite. Archiving and Purging. Version 4.2

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

8051 MICROCONTROLLER COURSE

(Cat. No SI) Product Data

Using the CoreSight ITM for debug and testing in RTX applications

EE8205: Embedded Computer System Electrical and Computer Engineering, Ryerson University. Multitasking ARM-Applications with uvision and RTX

DAC Digital To Analog Converter

Embedded Systems Design Course Applying the mbed microcontroller

Microtronics technologies Mobile:

Using the NicheStack TCP/IP Stack - Nios II Edition Tutorial

AN1754 APPLICATION NOTE

Exception and Interrupt Handling in ARM

Keil C51 Cross Compiler

Notes and terms of conditions. Vendor shall note the following terms and conditions/ information before they submit their quote.

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Introduction to Synoptic

XMOS Programming Guide

DEVICE DRIVERS AND TERRUPTS SERVICE MECHANISM Lesson-14: Device types, Physical and Virtual device functions

Open Flow Controller and Switch Datasheet

TCP/IP Networking, Part 2: Web-Based Control

Real Time Programming: Concepts

SYMETRIX SOLUTIONS: TECH TIP February 2014

Introduction. How does FTP work?

Designing a Home Alarm using the UML. And implementing it using C++ and VxWorks

Developing an Application on Core8051s IP-Based Embedded Processor System Using Firmware Catalog Drivers. User s Guide

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

Operating Systems 4 th Class

Digital Guitar Effects Pedal

Computer and Set of Robots

3. Programming the STM32F4-Discovery

AN141 SMBUS COMMUNICATION FOR SMALL FORM FACTOR DEVICE FAMILIES. 1. Introduction. 2. Overview of the SMBus Specification. 2.1.

ENGI E1112 Departmental Project Report: Computer Science/Computer Engineering

Lab Experiment 1: The LPC 2148 Education Board

BLUETOOTH SERIAL PORT PROFILE. iwrap APPLICATION NOTE

From Control Loops to Software

Introduction the Serial Communications Huang Sections 9.2, 10.2 SCI Block User Guide SPI Block User Guide

the high-performance embedded kernel User Guide Version 5.0 Express Logic, Inc Toll Free 888.THREADX FAX

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

North Texas FLL Coaches' Clinics. Beginning Programming October Patrick R. Michaud republicofpi.org

Embedded Component Based Programming with DAVE 3

Assignment # 2: Design Patterns and GUIs

Integration for X-Tools and X32

Real-Time Component Software. slide credits: H. Kopetz, P. Puschner

Experiments: Labview and RS232

4 Character 5x7 LED Matrix Display

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

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

Programmable Logic Controller PLC

Integrity Checking and Monitoring of Files on the CASTOR Disk Servers

Parallel and Distributed Computing Programming Assignment 1

File Transfers. Contents

Application Bulletin

Virtual Integrated Design Getting started with RS232 Hex Com Tool v6.0

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

Course Project Documentation

ET-BASE AVR ATmega64/128

Hello, and welcome to this presentation of the STM32L4 reset and clock controller.

The programming language C. sws1 1

Cyclic Architectures in UML

Lab 3: Introduction to Data Acquisition Cards

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

73M1866B/73M1966B FXOCTL Application User Guide November 2, 2009 Rev. 4.1 UG_1x66B_009

E-Blocks Easy Internet Bundle

Automatic Scheduling Using AutoTrack Command Line Options

-Helping to make your life betterwww.person-to-person.net

International Journal of Advancements in Research & Technology, Volume 2, Issue3, March ISSN

The Real-Time Operating System ucos-ii

Transcription:

Comparing RTOS to Infinite Loop Designs If you compare the way software is developed for a small to medium sized embedded project using a Real Time Operating System (RTOS) versus a traditional infinite loop type implementation, you will find that they are very similar. Yet, using a RTOS can provide significant advantages by reducing code complexity, increasing reliability and making the maintenance and addition of new features easier. Some embedded developers who have not used an RTOS may disagree with one or more of these benefits, but this document will provide an objective comparison for each point. Lets say our embedded project is a data logger. When the project begins, we know it will have a keypad, simple LCD display, status LEDs, Analog-To-Digital sampling capability, and a RS-232 serial port. A typical development approach would be to implement and test each feature one at a time. This iterative procedure is the same whether or not a RTOS is used. So lets implement each task showing how it would be done both with and without a RTOS. Serial Port Processor Analog to Digital LCD Display Keypad LEDs System Block Diagram The Beginning The first step is to just get a simple program to compile. At this point we don t have any functional code, just the program shell so we can begin to add our functions. iprintf( Starting Program\r\n ); The first difference you will notice is the name of the main function. When using the RTOS, you begin in a task that the RTOS initialization code has set up. In this case it is called. From this point forward you could actually use in the exact same way as and there would be virtually no difference between them (other than the RTOS system clock tick interrupt routine running in the background). With no other tasks running, the task would have complete control over the system just as does in the non-rtos version.

Add Serial Port Functionality Now lets add the code required to make the serial port operational. The serial port can be used for both a command interface and debugging during development. The serial port should be able to send and receive serial data as well as parse commands. For example, lets say that we want to have a simple interactive menu such as the following: MAIN MENU --------- 1. Read A/D 2. Write GPIO outputs 3. Read GPIO inputs 4. Set LEDs 5. Write string to LCD display The incoming command parser can be as simple as: c = getchar(), where c would be the value of 1-5. The difference in the function code below is that for the RTOS you would enclose the parsing code with an infinite while loop. More about this difference later. char s[80]; // for RTOS version only char c = getchar(); switch c case 1: // Read and display ADC value case 2: // Prompt for value and write to GPIO outputs printf( Enter GPIO value: ); fgets(s, 80, stdin); // will block until user hits <enter> // Convert s to a number and write GPIOs case 3: // Read and display GPIO input values case 4: // Prompt for value and write to LEDs printf( Enter LED value: ); fgets(s, 80, stdin); // will block until user hits <enter> // Convert s to a number and write LEDs case 5: // Prompt for value and write to LCD display printf( Enter display string: ); fgets(s, 80, stdin); // will block until user hits <enter> // Write display string to LCD default: // print error message The complication here is that getchar() and all the functions that require the user to input a value (such as writing to the LCD display) are blocking functions, and in a non-rtos environment we cannot block or the

main while loop will stop which will cause the system to be unresponsive. We can solve part of this problem by using a function to check whether or not a serial port character is available before we call the function, but that will only work for the command in the process of writing a string to the LCD display will be a blocking function for a string input. In contrast, we can use a RTOS and simply make the serial command parser into a task, which will only become active if there is a command to parse and process. if ( SerialCharAvail() ) ProcessSerialPort(); iprintf( Starting Program\r\n ); OSCreateTask( ProcessSerialPort, SerialPrio ); In the above code sets you can see that the ProcessSerialPort() function is declared for both methods. The difference is that the non-rtos version requires the check for an available character before deciding to call the function. The problem remains that inside ProcessSerialPort() there are function calls that block in order to get text data to write the GPIOs, write the LEDs, and write to the LCD display. For example, in case 2 where a user can enter GPIO settings, the fgets() function will block until the user enters the data and hits the <enter> key. During this data entry period, the system is unresponsive using the non-rtos implementation. In the RTOS version, the OSCreateTask() function uses the ProcessSerialPort() function as a parameter. In effect, the ProcessSerialPort() function becomes the task. This new task will block on the getchar() function until data is received by the serial port. We do not need to add any code to the infinite while loop, the RTOS will handle calling the ProcessSerialPort() code when necessary! Looking again at the case 2 example, a blocking function such as fgets() is not a problem since the RTOS will enable other tasks to run. This is accomplished without any coding on our part. Add Keypad Functionality The 4 x 4 keypad implementation will consist of 16 contacts in a matrix with 4 outputs and 4 inputs to the processor. Whenever a key is pressed it will create a signal on one of the 4 inputs, which will generate an interrupt and set a flag telling the system a key was pressed. The keypad matrix is then scanned by driving the outputs and reading the inputs in sequence to determine which key was pressed. All this must happen before the person releases the depressed key. The functions for RTOS and non-rtos are shown below. They both rely on an interrupt service routine to set a flag called Keypressed. In the non-rtos version the flag is checked in the main while loop. The RTOS implementation uses ProcessKeypad() as a task with it s own infinite while loop, and the check on the Keypressed flag is inside the ProcessKeypad() function. In RTOS lingo we use a Semaphore to be the flag and a blocking function called OSSemPend() to wait for the Semaphore to be set.

char key = ScanKeypad(); // code to process key goes here if ( SerialCharAvail() ) ProcessSerialPort(); if ( Keypressed ) ProcessKeypad(); OSSemPend( Keypressed ); char key = ScanKeypad(); // code to process key goes here OSSemPost( Keypressed ); iprintf( Starting Program\r\n ); OSCreateTask( ProcessSerialPort, SerialPrio ); OSCreateTask( ProcessKeypad, KeypadPrio ); Again, the above code sets are similar in that they both create a function called ProcessKeypad() that scans the keypad and processes the keypress. In the non-rtos implementation we check the keypressed flag that would be set the an interrupt service routine to decide whether or not to call the ProcessKeypad() function. In the RTOS implementation, we simply add another task that is ProcessKeypad(). There is a major difference here worth noting. Let s say that we are using the non-rtos implementation. The serial port interface is used to select menu item #5 to enter a text string that will be sent to the LCD display, and the user has only entered half of the characters. This is a blocked state and the main while loop is completely stalled. If a user types characters on the keypad, they will be missed. One way to around this would be to write a more complex interrupt service routine that scanned the keypad and buffered the keypress (essentially what the ProcessKeypad() function does) but now the interrupt routine is taking a lot of time and the system latency goes up. While this might be acceptable if all we needed was to handle the keypad, what about all the other interrupts such as a system timer, analog to digital conversion, serial port, etc.? The further a programmer goes down this path, the closer he is to creating a task scheduler, which is one of the primary features of an RTOS! The major difference is that the RTOS has a large user base and has been debugged. If we consider the RTOS implementation in which the task that processes keypad input is a higher priority than the serial port, the system will immediately switch from the ProcessSerialPort() task to the ProcessKeypad() task, process the keypad entry, and jump right back to the ProcessSerialPort() task so the operator can continue to enter characters that will be sent to the LCD display. This should happen so fast that it is transparent to the user.

Add Analog To Digital Functionality To implement the ADC we will use an interrupt to signal that a sample reading is available. An interrupt service routine will then read the value and set a flag indicating a new value is ready to be read. As with the keypad processing, the non-rtos version can use a flag, and the RTOS version can use a semaphore. Once a ADC value has been read it is multiplied by a calibration value and stored in an array. The functions are shown below: if ( SampleReady ) // code to read and store ADC value OSSemPend( SampleReady ); // code to read and store ADC value The program now looks like: // code to store ADC value // code to store ADC value if ( SerialCharAvail() ) ProcessSerialPort(); if ( Keypressed ) ProcessKeypad(); if ( SampleReady ) ProcessADC(); OSCreateTask( ProcessSerialPort, SerialPrio ); OSCreateTask( ProcessKeypad, KeypadPrio ); OSCreateTask( ProcessADC, ADCPrio ); There are two concerns here for the non-rtos implementation. First, there is the possibility a reading could be lost because the main while loop did not call the ProcessADC() function before the next ADC reading was done. Second, there is the possibility of contention or data corruption if both the interrupt routine and the ProcessADC

code access the global adc_val at the same time. A mechanism for mutual exclusion to adc_val is required to prevent this situation. The RTOS implementation solves both these problems. As with the keypad, the priority of the ProcessADC() task would be set so that it will take momentary control from the serial port task to quickly process the sample. Mutual exclusion of the adc_val is handled by the OSSemPend() and OSSemPost(), so data corruption cannot occur. Then end result is that with very little coding we have guaranteed the sample will be processed in time, and the sample data will not be corrupted. As a side note, the RTOS also provides a more advantageous way to handle this case. Instead of a semaphore or flag that gets set, we could read the ADC values in the ISR and store them in a Queue. Calling OSQPend() will block until one or more samples have been read and are in the Queue. The code would look like this: OSQPend( Sample, timeout, status ); // code to store the Sample value obtained above All we had to do was change the OSSemPend() to OSQPend(). If for any reason the ProcessADC() task could not catch up with the interrupt service routine, multiple readings would be stored in the Queue. If more than 1 reading is in the Queue, then the OSQPend() function would return immediately so the additional readings could be processed. Add GPIO, LED and LCD Display Functionality The GPIO, LED and LCD display functions in this example would be called from the functions we have already defined in response to key presses, serial input or sample readings. They probably would not need to be called directly from the main while loop or be tasks of their own. CONCLUSION Writing a small to medium size embedded application can be nearly identical from a code function standpoint whether an infinite main loop or RTOS is used. In many cases when a RTOS is not used, a programmer must write the equivalent of a limited scheduler to handle events. The advantage of a RTOS is that a programmer can use a tested and reliable solution to achieve the same goal. In addition, the RTOS supplies features such as priority, semaphores and queues that make the structure of the application much easier than if a RTOS was not used.