Ramy Souody
Introduction:
What does the term Embedded C mean?
Embedded C is a proper subset from the C language g g suitable for Embedded Systems. y Some features exist in C and not used in Embedded C.
Introduction:
Why do we use Embedded C?
Because Embedded Systems are not like general purpose computers since we have limited hardware resources. Embedded C is limited by
Hardware capabilities Compiler capabilities
Compilation
Each .c file, plus any headers it includes, is called a compilation il ti unit it Compiler turns compilation unit into an object file Object j files are: Relocatable Code and data addresses are symbolic not yet bound to physical addresses Contain unresolved references
Linking
Linker functions 1. Merge text, data, BSS segments of individual 2. object files Including libraries, Including processor boot code (startup code) 3. Resolve references to code and data 4. Report any errors 5. Locate relocatable code 6. Follow instructions in linker script 7 Report any errors 7. 8. Result: Binary image ready
Operators:
Precedence of Operations:
Casting Parentheses () Negative Multiplication and division Addition and subtraction
Arrays
Array is a grouping of elements of the same type. Array name is a pointer to the first element Example:
Int I t Arr[10]; A [10] Int *Ptr; Ptr = &Arr[0]; X = *(Ptr+2); Actually now: x = Arr[2];
Copyright 2009 Embedded Systems Committee
Structures:
Structure is a complex data type to group elements of different types. Declaration: struct Str_Name Str Name
{ datatype Var Var_Name; Name; };
Copyright 2009 Embedded Systems Committee
Structures:
Example: struct student { char name[20]; short id; };
(This is only a declaration and doesn doesnt t reserve any memory space)
Structures:
Definition:
main() () { short id; struct student S1; // here we reserved memory space for the structure S1 of type student S1 id=20; S1.id=20; id=30; }
Copyright 2009 Embedded Systems Committee
Structures:
Notes:
It is legal to name variables in different structures by the same name. Structure can include another structure but must be declared first. Structure declaration doesnt reserve any memory space.
Interactive Questions
what will be the output of the following program? main() { int i=4; switch(i) { default: printf(default); case 1: printf(case 1); break; case 2: printf(case 2); break; } }
Copyright 2009 Embedded Systems Committee
Interactive Questions
point out the error, if any, in the for loop () main() { a. int i=1; b. for ( ; ; ) { c. i++; d. if(i>10)break; ( ) ; } }
the condition is a must the two semicolons should be dropped replace for loop with while loop no error
Interactive Questions
point out the error, if any, in the while loop () main() { a. int i=1; b. while( () { c. i++; d. if(i>10)break; ( ) ; } }
the condition is a must there should be a semicolon in while() replace while loop with for loop no error
Interactive Questions
point out the error, if any, in the following program main() { int i=4,j=2; switch(i) { case 1: printf(case 1); break; case j: printf(case printf( case 2 2); ); break; } }
Copyright 2009 Embedded Systems Committee
Interactive Questions
point out the error, if any, in the following program main() { int i=1; switch(i) { case 1: printf(case 1); break; case 1 1*2+4: 2+4: printf( printf(case case 2 2); ); break; } }
Copyright 2009 Embedded Systems Committee
Interactive Questions
point out the error, if any, in the following program main() { int a=10,b; a>=5? b=100: b=200; }
Interactive Questions
what will be the output of the following program? main() { int Arr[2][2][2]={10,20,30,40,50,60,70,80}; printf(%d,Arr[1][1][1]); }
Type Casting:
Type casting is to put variable in different type during run time Example:
main() { char y; short x; x = (short) y; }
Copyright 2009 Embedded Systems Committee
Functions:
Function prototype:
Return_type Return type function function_name name (argument (argument_type type Argument_name);
Example: p short myFunc(char x); Argument name in the function prototype is optional, only the argument type is a must. short myFunc(char);
Copyright 2009 Embedded Systems Committee
Functions:
Function Body:
Example: p short myFunc(char x) { short y; y=x*x; return(y); }
Copyright 2009 Embedded Systems Committee
Functions:
Function Calling:
Example: p main()
{ short y; char x=5; y= myFunc(x); y y ( ) }
Functions:
Function input parameters:
there are two methods for passing parameters for a function:
Stack CPU register
Interactive Questions
Point out the error in the following code? ( a, int b) ) int func(int { int a; a=20; return a; }
Interactive Questions
There is a mistake in the following code. Add a statement in it to correct the mistake:
main() { int a; a=(int)f(10,3.14); } float f(int aa, float bb) { return((float)aa + bb); }
Interactive Questions
Point out the error in the following code?
void f(void); main() { int a=10; a=f(); } void f() { printf(Hi); }
Interactive Questions
Point out the error in the following code? int f(int a); main() () { int b; b=f(20); } int f(int a) { a>20: return(10) : return(20); }
Copyright 2009 Embedded Systems Committee
Interactive Questions
How many times the following program would print error? main() { printf(\n error); main(); } Infinite number of times 32767 times 65535 times Till the stack overflow
Copyright 2009 Embedded Systems Committee
A. B. C. D.
Pointers:
Pointer is a variable in memory where we can store address. Pointer scope and lifetime are same as normal variables. Pointer size is not standard and it depends on the memory architecture and memory space.
Pointers:
Pointer definition:
base_type * pointer_name;
Example:
char * ptr;
Pointers:
Example:
char x,z; ptr = &x;
Address of x
*ptr = 5;
Value at ptr
x = 5; z = x;
z= *ptr; * t
Pointers:
Pointer Arithmetic:
short x; char * pc; pc = &x;
(Error)
Step size:
short * pc; pc++; (step size is according to the base type)
Copyright 2009 Embedded Systems Committee
Pointers:
Operators that we can use with pointers:
+ , - , ++ , - Note:
ptr2 ptr2- ptr1 = number of elements not bytes
Pointers:
Example:
char n = 10; // address 100 char * p; p = &n; (*p)++; printf(%p,p); // 100 printf(%d, i tf(%d * *p); ) // 11
Pointers:
Pointers and arrays:
char st[4] = {0,1,2,3}; char * pc; pc = st; // pc = &st[0]; printf(%d, *(pc+2));
Pointers:
Example:
char ch; ch = st[1]; // array indexing ch = pc[1]; // pointer indexing ch = *(pc+1); //pointer arithmetic
Pointers:
Note:
short s[10], *ptr; ptr = s; ptr += 3; This statement adds 3 elements (3*step size) not 3 bytes. bytes
Pointers:
Pointer as a function parameter:
void fill_array (char Ar[], char size, char value) {
char i; for (i=0;i<size;i++) Ar[i] = value;
}
Copyright 2009 Embedded Systems Committee
Pointers:
Pointer as a function parameter:
void fill_array (char * p, char size, char value) {
char i; for (i=0;i<size;i++) p[i] = value; // we can use pointer arithmetic here
}
Copyright 2009 Embedded Systems Committee
Pointers:
Note:
Pointers can be used for more than one return value from a function. void id f func_arth th ( (char h x, char h * square, char h * cube) b ) {
*square square = x x*x; x; *cube = x*x*x;
}
Copyright 2009 Embedded Systems Committee
Pointers:
Pointer to function:
Definition:
Return_type (*ptr_name) (argument_type argument_name);
Initialization:
Pt Ptr_name = func_name; f
Calling:
Same as function call;
Pointers:
Example:
char square, cube; void (*ptr) (char x, char * square, char * cube); ptr = func_arth ; ptr(3 , &square, &cube);
Interactive Questions
In the following program, how would you print 50 using p? void main (void) {
char a[] = {10,20,30,40,50}; char * p; p = a; ; printf(.);
Interactive Questions
In the following program, add a statement in the function fun() such that address of a is stored in j;
void fun (int **); main() {
int * j; f (&j) fun(&j);
}
Copyright 2009 Embedded Systems Committee
Interactive Questions
Would the following program give any warning on compilation?
main() {
float * p1, i = 25.5; char * p2; p1 = &i; p2 = &i;
Startup Code
Startup code is a small block of assembly language code that prepares the hardware to start executing higher level language code (C language) Usually startup code is automatically inserted by software development tools. Most cross-compilers for embedded systems include an assembly language file called startup.asm or something similar where the startup code is written
Startup Code
Startup p code for C/C++ p programs g usually y consists of the following actions, performed in the order described: 1. 1 2. 3. 4. 5. 6 6. 7. 8. 9. Disable all interrupts interrupts. Clock initialization and stabilization. Copy any initialized data from ROM(BSS) to RAM. Zero the uninitialized data area. Allocate space for and initialize the stack. Initialize the processor's stack pointer pointer. Initialize the peripheral registers p Enable interrupts. Call main.
Copyright 2009 Embedded Systems Committee
Volatile keyword
No optimization by y the compiler Volatile variables are variables whose values may change without a direct instruction. For example, example a variable which contains data received from a port will change as the port value changes.
Used to define & access hardware registers: volatile PORTA @0x011; PORTA = 3; X = PORTA; This code intend to output 3 on PORTA then keep the variable X updated with the value currently on PORTA If PORTA is not volatile then the compiler will optimize this code by combining the last 2 statements into -> x = 3;
Copyright 2009 Embedded Systems Committee
Bit field
struct { unsigned int shortElement : 1; // 1 bit in size unsigned int longElement : 8; // 8 bits in size i } myBitField;
Flow of code
All embedded code is called within an Infinite loop in main Except: Initialization code Interrupt service routine ISR Example:
Main() {
Init_Timer(); // Initialization code While (1) { // Call y your application pp }
Macros
Use #define to define any repeatedly used values with expressive names
Example #define true 1 #define Clock 4000
#ifdef UART_ENABLED Void SendByte(Byte y ( y *Data); ) Void SendByte(Byte *Data) { } #endif #ifdef ERROR LogError(); #end
Macros
Masking #define MASK_BIT_3 0x08 #d fi SET_BIT_3(X) #define SET BIT 3(X) (X | MASK_BIT_3) MASK BIT 3) PORTA = SET_BIT_3(PORTA);
Include path
#include filename.h h
Search for header in the same location as the source file
Comments
Using meaningful comments is very useful It helps you to fix your code faster It h helps l you t to modify dif someone else l code d It helps others understand your code and learn from it
Comments
/*************************************************************************************** * Filename: main.c * Author: you * Date: 1/1/09 * Version: 2.1 * Description: p main application pp ***************************************************************************************/ /*includes*/ /*Global variables*/ /*Initialization*/ /*Enable interrupts*/ Void main() { While(1) { /*Call Application*/ } }
Copyright 2009 Embedded Systems Committee