embedded programming in c/c++: lesson-1: programming

55
2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Upload: others

Post on 10-Feb-2022

61 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1

Embedded Programming in C/C++:

Lesson-1: Programming Elements and

Programming in C

Page 2: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 2

• An essential part of any embedded system design

ProgrammingProgramming

Page 3: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 3

• 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

Programming in Assembly or HLLProgramming in Assembly or HLL

Page 4: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 4

1. Assembly Language Programming1. Assembly Language Programming

Page 5: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 5

Advantage of Assembly Language Advantage of Assembly Language Programming 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

Page 6: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 6

Advantage of Assembly Language Advantage of Assembly Language ProgrammingProgramming

� 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 rule-declarations

� Not the compiler specific and library-functions specific

Page 7: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 7

Advantage of Assembly Language Advantage of Assembly Language ProgrammingProgramming

� Device driver codes may need only a few assembly instructions.

� Bottom-up-design approach

Page 8: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 8

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

Page 9: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 9

Short Development CycleShort 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

Page 10: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 10

• 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

Short Development CycleShort Development Cycle── BottomBottom--up up designdesign

Page 11: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 11

• First design of main program (blue-print), 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

Short Development CycleShort Development Cycle── TopTop--down down Design:Design:

Page 12: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 12

Use of Data Type and DeclarationsUse 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.

Page 13: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 13

Use of Type CheckingUse of Type Checking

• Type checking during compilationmakes the program less prone to errors.

• Example─ type checking on a char data type variable (a character) does not permit subtraction, multiplication and division.

Page 14: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 14

Use of Control Structures, loops and Use of Control Structures, loops and ConditionsConditions

• 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 program-

flow design

Page 15: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 15

Use of Data StructuresUse 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.

Page 16: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 16

• Queue• Stack• Array – one dimensional as a vector• Multidimensional• List • Tree

Standard Data structure Standard Data structure

Page 17: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 17

Use of ObjectsUse 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

Page 18: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 18

3. Advantage of 3. Advantage of using C C for ProgrammingProgramming

Page 19: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 19

CC• Procedure oriented language (No

objects)• Provision of inserting the assembly

language codes in between (called in-line assembly) to obtain a direct hardware control.

Page 20: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 20

Procedure oriented languageProcedure oriented language• A large program in ‘C’ splits into the

declarations for variables, functions and data structure, simpler functional blocks and statements.

Page 21: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 21

• Processor and memory sensitive part of the program within the in-line assembly, and the complex part in the HLL codes.

• Example function outportb (q, p)• Example─ Mov al, p; out q, al

InIn--line assembly codes of C functionsline assembly codes of C functions

Page 22: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 22

4. 4. C Program ElementsC Program Elements

Page 23: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 23

Preprocessor include DirectivePreprocessor include Directive

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

Page 24: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 24

# include "VxWorks.h" /* Include VxWorks functions*/

# include "semLib.h" /* Include Semaphore functions Library */

# include "taskLib.h" /* Include multitasking functions Library */

Examples of Preprocessor include Examples of Preprocessor include DirectivesDirectives

Page 25: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 25

# 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. */

Examples of Preprocessor include Examples of Preprocessor include DirectivesDirectives

Page 26: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 26

Preprocessor Directive for the Preprocessor Directive for the definitionsdefinitions

• Global Variables ─ # define volatile boolean IntrEnable

• Constants ─ # define false 0• Strings─ # define welcomemsg

"Welcome To ABC Telecom"

Page 27: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 27

• 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.

Preprocessor MacrosPreprocessor Macros

Page 28: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 28

• 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.

Difference between Macro and Difference between Macro and FunctionFunction

Page 29: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 29

• When a function call is used instead of macro, the overheads (context saving and return) will take a time, Toverheadsthat is the same order of magnitude as the time, Texec for execution of short codes within a function.

• Use the function when the Toverheads << Texec and macro when Toverheads ~= or > Texec.

Difference between Macro and FunctionDifference between Macro and Function

Page 30: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 30

Use of ModifiersUse of Modifiers

• auto • unsigned• static• const• register

Page 31: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 31

• interrupt • extern• volatile• volatile static

Use of ModifiersUse of Modifiers

Page 32: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 32

Use of infinite loopsUse 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!

Page 33: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 33

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.

Page 34: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 34

# define false 0# define true 1void main (void) { /* Call RTOS run here */ rtos.run ( );/* Infinite while loops follows in each

task. So never there is return from the RTOS. */

}

Page 35: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 35

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 ( ); } }

Page 36: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 36

Use of Use of typedeftypedef� 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

Page 37: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 37

Use of PointersUse of Pointers

� Pointers are powerful tools when used correctly and according to certain basic principles.

Page 38: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 38

# define COM ((struct sio near*) 0x2F8);

This statement with a single master stroke assigns the addresses to all 8 variables

Example Example

Page 39: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 39

0x2F8: Byte at RBR/THR /DLATCH-LByte0x2F9: Byte at DLATCH-HByte0x2FA: Byte at IER; 0x2FB: Byte at LCR; 0x2FC: Byte at MCR; 0x2FD: Byte at LSR; 0x2FE: Byte at MSR0x2FF: Byte Dummy Character

Byte at the Byte at the siosio Addresses Addresses

Page 40: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 40

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.

Example Example

Page 41: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 41

• 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

Data structureData structure

Page 42: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 42

• 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.

Example of Data structure declaration Example of Data structure declaration

Page 43: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 43

# 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

Example of Data structure declaration Example of Data structure declaration

Page 44: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 44

Use of functionsUse 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.

Page 45: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 45

(ii) Passing the ReferencesWhen 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.

Page 46: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 46

Use of Reentrant FunctionUse 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.

Page 47: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 47

Three conditions for a function called Three conditions for a function called as as reentrant functionreentrant function

1. All the arguments pass the values and none of the argument is a pointer (address) whenever a calling function calls that function.

Page 48: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 48

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.]

Page 49: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 49

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

Page 50: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 50

SummarySummary

Page 51: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 51

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.

Page 52: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 52

• 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

Page 53: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 53

We learnt� C language supports in-line

assembly (fragments of codes in assembly) gives the benefits of assembly as well as high-level language

Page 54: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 54

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,

Page 55: Embedded Programming in C/C++: Lesson-1: Programming

2008 Chapter-5L01: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 55

End of Lesson 1 of Chapter 5End of Lesson 1 of Chapter 5