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

Similar documents
Keil C51 Cross Compiler

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

The C Programming Language course syllabus associate level

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

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

C++ INTERVIEW QUESTIONS

1 Abstract Data Types Information Hiding

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

How to design and implement firmware for embedded systems

El Dorado Union High School District Educational Services

An Overview of Java. overview-1

Sources: On the Web: Slides will be available on:

Lecture 22: C Programming 4 Embedded Systems

ELEG3924 Microprocessor Ch.7 Programming In C

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

Linux Driver Devices. Why, When, Which, How?

Object Oriented Software Design

C++ Programming Language

Operating System Manual. Realtime Communication System for netx. Kernel API Function Reference.

Embedded C Programming, Linux, and Vxworks. Synopsis

Freescale Semiconductor, I

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

13 Classes & Objects with Constructors/Destructors

Object Oriented Software Design II

Top 10 Bug-Killing Coding Standard Rules

Basic Programming and PC Skills: Basic Programming and PC Skills:

Object Oriented Software Design

Java Interview Questions and Answers

Chapter 12. Development Tools for Microcontroller Applications

A deeper look at Inline functions

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Adapting the PowerPC 403 ROM Monitor Software for a 512Kb Flash Device

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

How To Write Portable Programs In C

MPLAB Harmony System Service Libraries Help

An Incomplete C++ Primer. University of Wyoming MA 5310

Operating Systems. Privileged Instructions

The BSN Hardware and Software Platform: Enabling Easy Development of Body Sensor Network Applications

An Implementation Of Multiprocessor Linux

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

OKLAHOMA SUBJECT AREA TESTS (OSAT )

Introduction to Java

OpenACC 2.0 and the PGI Accelerator Compilers

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Introduction. What is an Operating System?

VB.NET Programming Fundamentals

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

Embedded Software development Process and Tools: Lesson-1

Lecture 7: Programming for the Arduino

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

Instruction Set Architecture (ISA)

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

MQX Lite Real-Time Operating System User Guide

Stacks. Linear data structures

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

TivaWare Utilities Library

Embedded Software development Process and Tools:

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

C++FA 3.1 OPTIMIZING C++

Illustration 1: Diagram of program function and data flow

CS 241 Data Organization Coding Standards

Programming Database lectures for mathema

Comp151. Definitions & Declarations

Programming languages C

Middleware Lou Somers

13. Publishing Component Information to Embedded Software

Lumousoft Visual Programming Language and its IDE

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

Using the RDTSC Instruction for Performance Monitoring

Java the UML Way: Integrating Object-Oriented Design and Programming

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

BCS2B02: OOP Concepts and Data Structures Using C++

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Habanero Extreme Scale Software Research Project

Glossary of Object Oriented Terms

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

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

Threads Scheduling on Linux Operating Systems

Research and Design of Universal and Open Software Development Platform for Digital Home

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

50 Computer Science MI-SG-FLD050-02

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

RS-485 Protocol Manual

Introduction to Embedded Systems. Software Update Problem

C++FA 5.1 PRACTICE MID-TERM EXAM

Kernel Synchronization and Interrupt Handling

AN1754 APPLICATION NOTE

Serial Communications

ADL User Guide for Open AT V4.10

Database Toolkit: Portable and Cost Effective Software

RTOS Debugger for ecos

Mouse Programming Mouse Interrupts Useful Mouse functions Mouselib.h Mouselib.c

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Crash Course in Java

Transcription:

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

An essential part of any embedded system design Programming 2

Programming in Assembly or HLL Processor and memory-sensitive instructions: Program codes may be written in assembly Most of codes: Written in a high level language (HLL), C, C++ or Java 3

1. Assembly Language Programming 4

Advantage of Assembly Language Programming Assembly codes sensitive to the processor, memory, ports and devices hardware Gives a precise control of the processor internal devices Enables full use of processor specific features in its instruction set and its addressing modes 5

Advantage of Assembly Language Programming Machine codes are compact, processor and memory sensitive System needs a smaller memory. Memory needed does not depend on the programmer data type selection and ruledeclarations Not the compiler specific and libraryfunctions specific 6

Advantage of Assembly Language Programming Device driver codes may need only a few assembly instructions. Bottom-up-design approach 7

2. Advantage of using high level language (HLL) for Programming 8

Short Development Cycle Code reusability A function or routine can be repeatedly used in a program Standard library functions For examples, the mathematical functions and delay ( ), wait ( ), sleep ( ) functions Use of the modular building blocks 9

Short Development Cycle Bottom-up design Sub-modules are designed first for specific and distinct set of actions, then the modules and finally integration into complete design. First code the basic functional modules and then build a bigger module and then integrate into the final system 10

Short Development Cycle Top-down Design: First design of main program (blueprint), then its modules and finally the sub-modules are designed for specific and distinct set of actions. Top-down design Most favoured program design approach 11

Use of Data Type and Declarations Examples, char, int, unsigned short, long, float, double, boolean. Each data type provides an abstraction of the (i) methods to use, manipulate and represent, and (ii) set of permissible operations. 12

Use of Type Checking Type checking during compilation makes the program less prone to errors. Example type checking on a char data type variable (a character) does not permit subtraction, multiplication and division. 13

Use of Control Structures, loops and Conditions Control Structures and loops Examples while, do-while, break and for Conditional Statements examples if, if- else, else - if and switch - case) Makes tasks simple for the programflow design 14

Use of Data Structures Data structure- A way of organizing large amounts of data. A data elements collection Data element in a structure identified and accessed with the help of a few pointers and/or indices and/or functions. 15

Queue Stack Array one dimensional as a vector Multidimensional List Tree Standard Data structure 16

Use of Objects Objects bind the data fields and methods to manipulate those fields Objects reusability Provide inheritance, method overloading, overriding and interfacing Many other features for ease in programming 17

3. Advantage of using C for Programming 18

C Procedure oriented language (No objects) Provision of inserting the assembly language codes in between (called inline assembly) to obtain a direct hardware control. 19

Procedure oriented language A large program in C splits into the declarations for variables, functions and data structure, simpler functional blocks and statements. 20

In-line assembly codes of C functions Processor and memory sensitive part of the program within the inline assembly, and the complex part in the HLL codes. Example function outportb (q, p) Example Mov al, p; out q, al 21

4. C Program Elements 22

Preprocessor include Directive Header, configuration and other available source files are made the part of an embedded system program source file by this directive 23

Examples of Preprocessor include Directives # include "VxWorks.h" /* Include VxWorks functions*/ # include "semlib.h" /* Include Semaphore functions Library */ # include "tasklib.h" /* Include multitasking functions Library */ 24

Examples of Preprocessor include Directives # include "syslib.c" /* Include system library for system functions */ # include "netdrvconfig.txt" /* Include a text file that provides the 'Network Driver Configuration'. */ # include "prctlhandlers.c" /* Include file for the codes for handling and actions as per the protocols used for driving streams to the network. */ 25

Preprocessor Directive for the definitions Global Variables # define volatile boolean IntrEnable Constants # define false 0 Strings # define welcomemsg "Welcome To ABC Telecom" 26

Preprocessor Macros Macro - A named collection of codes that is defined in a program as preprocessor directive. Differs from a function in the sense that once a macro is defined by a name, the compiler puts the corresponding codes at the macro at every place where that macro-name appears. 27

Difference between Macro and Function The codes for a function compiled once only On calling that function, the processor has to save the context, and on return restore the context. Macros are used for short codes only. 28

Difference between Macro and Function When a function call is used instead of macro, the overheads (context saving and return) will take a time, T overheads that is the same order of magnitude as the time, T exec for execution of short codes within a function. Use the function when the T overheads << T exec and macro when T overheads ~= or > T exec. 29

auto unsigned static const register Use of Modifiers 30

interrupt extern volatile volatile static Use of Modifiers 31

Use of infinite loops Infinite loops- Never desired in usual programming. Why? The program will never end and never exit or proceed further to the codes after the loop. Infinite loop is a feature in embedded system programming! 32

Example: A telephone is never switching off. The system software in the telephone has to be always in a waiting loop that finds the ring on the line. An exit from the loop will make the system hardware redundant. 33

# define false 0 # define true 1 void main (void) { /* Call RTOS run here */ rtos.run ( ); /* Infinite while loops follows in each task. So never there is return from the RTOS. */ } 34

void task1 (...) { /* Declarations */. while (true) { /* Run Codes that repeatedly execute */ /* Run Codes that execute on an event*/ if (flag1) {...;}; flag1 =0; /* Codes that execute for message to the kernel */ message1 ( ); } } 35

Use of typedef Example A compiler version may not process the declaration as an unsigned byte The 'unsigned character' can then be used as a data type. Declared as follows: typedef unsigned character portadata Used as follows: #define Pbyte portadata 0xF1 36

Use of Pointers Pointers are powerful tools when used correctly and according to certain basic principles. 37

# define COM ((struct sio near*) 0x2F8); This statement with a single master stroke assigns the addresses to all 8 variables Example 38

Byte at the sio Addresses 0x2F8: Byte at RBR/THR /DLATCH-LByte 0x2F9: Byte at DLATCH-HByte 0x2FA: Byte at IER; 0x2FB: Byte at LCR; 0x2FC: Byte at MCR; 0x2FD: Byte at LSR; 0x2FE: Byte at MSR 0x2FF: Byte Dummy Character 39

Example Free the memory spaces allotted to a data structure. #define NULL (void*) 0x0000 Now statement & COM ((struct sio near*) = NULL; assigns the COM to Null and make free the memory between 0x2F8 and 0x2FF for other uses. 40

Data structure Example structure sio Eight characters Seven for the bytes in BR/THR/DLATCHLByte, IER, IIR, LCR, MCR, LSR, MSR registers of serial line device and one dummy variable 41

Example of Data structure declaration Assume structured variable COM at the addresses beginning 0x2F8. # define COM ((struct sio near*) 0x2F8) COM is at 8 addresses 0x2F8-0x2FF and is a structure consisting of 8 character variables structure for the COM port 2 in the UART serial line device at an IBM PC. 42

Example of Data structure declaration # define COM1 ((struct sio near*) 0x3F8); It will give another structured variable COM1 at addresses beginning 0x3F8 using the data structure declared earlier as sio 43

Use of functions (i) Passing the Values (elements): The values are copied into the arguments of the functions. When the function is executed in this way, it does not change a variable's value at the function, which calls new function. 44

(ii) Passing the References When an argument value to a function passes through a pointer, the called function can change this value. On returning from this function, the new value may be available in the calling program or another function called by this function. 45

Use of Reentrant Function Reentrant function- A function usable by the several tasks and routines synchronously (at the same time). This is because all the values of its argument are retrievable from the stack. 46

Three conditions for a function called as reentrant function 1. All the arguments pass the values and none of the argument is a pointer (address) whenever a calling function calls that function. 47

2. When an operation is not atomic, that function should not operate on any variable, which is declared outside the function or which an interrupt service routine uses or which is a global variable but passed by reference and not passed by value as an argument into the function. [The value of such a variable or variables, which is not local, does not save on the stack when there is call to another program.] 48

3. That function does not call any other function that is not itself Reentrant. 49

Summary 50

We learnt An embedded system programming in the assembly language- only when a precise control of the processors internal devices and full use of processor specific features in its instruction set and addressing modes needed. 51

Program in a high-level language features- A short development cycle for a complex system and portability to the system hardware modifications. It easily makes larger program development feasible. Top-down design approach C or C++ or JAVA mainly used for embedded system codes development 52

We learnt C language supports in-line assembly (fragments of codes in assembly) gives the benefits of assembly as well as high-level language 53

We learnt Important programming elements in C- preprocessor directives, library functions, header files, source and configuration files, data type declarations, macros, data structures, infinite loops, reentrant functions, 54

End of Lesson 1 of Chapter 5 55