EMBEDDED C and C++ PROGRAMMING T S PRADEEPKUMAR, SCS
OVERVIEW Embedded C Programming (introduction) Preprocessor Directives Scope of Variables and Parameter passing Embedded C++ Embedded Software Development
Embedded C Embedded C is a subset of the C programming language which is intended for embedded systems programming. It excludes size and/or speed consuming C features that are not relevant for embedded systems.
Embedded C….. It is midlevel with high level features. It is very efficient Good, well proven compilers are available for every embedded processors.(8 bit to 32 bit or more)
Preprocessor Directives #define #pragma To describe specific resources of your target hardware Example: #pragma portrw PORTA @ 0x0000 #pragma portrw PORTB @ 0x0001; #pragma portw DDRB @ 0x0005;
Preprocessor Directives #ifdef checks for the existence of macro definitions. Example: #ifdef EXTENDED #define MAX_LEN 74 #else #define MAX_LEN 50 #endif
Scope of Variables Local or Auto Global variables Volatile variables Register variables Static variables
Local and Global Auto or local variables Variables declared inside the function Stored in temporary memory location like Stack Extern variables The declaration is used to describe the variable that is externally defined.
Local Variables A temporary information used by only one software module or function Local variables usually stored in the Stack. To access local variables, the compiler use the stack pointer addressing mode.
Local Variables int a=100; void main() { int b=20; Printf(“%d”,b); }
Volatile Volatile variables volatile keyword informs the compiler that it can not depend upon the value of a variable and should not perform any optimizations based on assigned values.
Volatile Example unsigned char data[100]; #define PORTA *(unsigned char volatile *)(0x0000) #define DDRA *(unsigned char volatile *)(0x0004) void main(void) { short i; DDRA=0x00; /* make Port A an input */ for(i=0;i<100;i++){ /* collect 100 measurements */ data[i]=PORTA;  /* collect ith measurement */ }}
register Register variables To give a hint to the compiler that the variable should be allocated to a CPU register if possible. Some compilers allow global register variable declaration is helpful in case when there are more number of interrupt handlers
Static Variables Static variables Static local or static global, both are stored in the RAM File static and function static varibles are treated as global variables by most of the compilers
Parameter Passing Input parameter Data passed from the calling routine into the module during execution Output parameter Information returned from the module to the calling routine.
Parameter passing…. Pass by value Only passes the copies of the objects. Pass by reference References to the actual arguments are passed instead of the copies of the actual arguments.
Example Void main() { Unsigned short angle=0; Stepper_init(); While(true){ Stepper_step(); Next(&angle); } } Void next (unsigned short *pt) { (*pt)++; If((*pt)==200) (*pt)=0; }
Embedded C++ It is a subset of full draft C++ standard These are omitted in EC++ Multiple inheritance and virtual base classes Runtime type identification (RTTI) Exception handling, Templates, namespaces
Optimizing C and C++ Code Minimize local variables Declare local variables in the inner most scope Reduce the number of parameters Don't define a return value if not used Prefer int over char. Prefer initialization over assignment In-line 1 to 3 line functions
Some C Optimisations Non Optimised code Optimised code Unsigned short int a; a /=8; Unsigned short int a; a>>=3; a*=2; a+=a; for(i=0; i<10; i++) { b = k * c; p[i] = b; } b = k * c; /* constant code moved outside the loop */ for(i=0; i<10; i++) { p[i] = b; }
Super Loop Architecture void main(void) { // Prepare run function X X_Init(); while(1) // ‘for ever’ (Super Loop) { X(); // Run function X() } }
ECU Task: control injection time (3 sub-tasks) T S P compute air flow compute injection time drive actuators air flow injection time air temperature engine temperature engine speed throttle position look-up table PWM signals air pressure
Control Injection Time void main(void) { Control_ECU_Init(); while(1) // 'for ever' (Super Loop) { Control_ECU_Get_throttle_position(); Control_ECU_Get_Actual_speed(); Control_ECU_Get_Actual_temperature(); Control_ECU_Get_Actual_pressure(); Control_ECU_Control_airflow(); } }
Embedded Software Development
Embedded C Guidelines Comments Overall purpose of the software The names of the programmers Update dates Hardware/software configuration required to use the module Copyright information (if available)
Embedded C guidelines Included .h files Extern variables Preprocessor directives Struct, union, enum statements Global variables and constants Function prototypes Implementation of functions
Questions?

Embedded c programming22 for fdp

  • 1.
    EMBEDDED C andC++ PROGRAMMING T S PRADEEPKUMAR, SCS
  • 2.
    OVERVIEW Embedded CProgramming (introduction) Preprocessor Directives Scope of Variables and Parameter passing Embedded C++ Embedded Software Development
  • 3.
    Embedded C EmbeddedC is a subset of the C programming language which is intended for embedded systems programming. It excludes size and/or speed consuming C features that are not relevant for embedded systems.
  • 4.
    Embedded C….. Itis midlevel with high level features. It is very efficient Good, well proven compilers are available for every embedded processors.(8 bit to 32 bit or more)
  • 5.
    Preprocessor Directives #define#pragma To describe specific resources of your target hardware Example: #pragma portrw PORTA @ 0x0000 #pragma portrw PORTB @ 0x0001; #pragma portw DDRB @ 0x0005;
  • 6.
    Preprocessor Directives #ifdefchecks for the existence of macro definitions. Example: #ifdef EXTENDED #define MAX_LEN 74 #else #define MAX_LEN 50 #endif
  • 7.
    Scope of VariablesLocal or Auto Global variables Volatile variables Register variables Static variables
  • 8.
    Local and GlobalAuto or local variables Variables declared inside the function Stored in temporary memory location like Stack Extern variables The declaration is used to describe the variable that is externally defined.
  • 9.
    Local Variables Atemporary information used by only one software module or function Local variables usually stored in the Stack. To access local variables, the compiler use the stack pointer addressing mode.
  • 10.
    Local Variables inta=100; void main() { int b=20; Printf(“%d”,b); }
  • 11.
    Volatile Volatilevariables volatile keyword informs the compiler that it can not depend upon the value of a variable and should not perform any optimizations based on assigned values.
  • 12.
    Volatile Example unsignedchar data[100]; #define PORTA *(unsigned char volatile *)(0x0000) #define DDRA *(unsigned char volatile *)(0x0004) void main(void) { short i; DDRA=0x00; /* make Port A an input */ for(i=0;i<100;i++){ /* collect 100 measurements */ data[i]=PORTA;  /* collect ith measurement */ }}
  • 13.
    register Register variablesTo give a hint to the compiler that the variable should be allocated to a CPU register if possible. Some compilers allow global register variable declaration is helpful in case when there are more number of interrupt handlers
  • 14.
    Static Variables Staticvariables Static local or static global, both are stored in the RAM File static and function static varibles are treated as global variables by most of the compilers
  • 15.
    Parameter Passing Inputparameter Data passed from the calling routine into the module during execution Output parameter Information returned from the module to the calling routine.
  • 16.
    Parameter passing…. Passby value Only passes the copies of the objects. Pass by reference References to the actual arguments are passed instead of the copies of the actual arguments.
  • 17.
    Example Void main(){ Unsigned short angle=0; Stepper_init(); While(true){ Stepper_step(); Next(&angle); } } Void next (unsigned short *pt) { (*pt)++; If((*pt)==200) (*pt)=0; }
  • 18.
    Embedded C++ Itis a subset of full draft C++ standard These are omitted in EC++ Multiple inheritance and virtual base classes Runtime type identification (RTTI) Exception handling, Templates, namespaces
  • 19.
    Optimizing C andC++ Code Minimize local variables Declare local variables in the inner most scope Reduce the number of parameters Don't define a return value if not used Prefer int over char. Prefer initialization over assignment In-line 1 to 3 line functions
  • 20.
    Some C Optimisations Non Optimised code Optimised code Unsigned short int a; a /=8; Unsigned short int a; a>>=3; a*=2; a+=a; for(i=0; i<10; i++) { b = k * c; p[i] = b; } b = k * c; /* constant code moved outside the loop */ for(i=0; i<10; i++) { p[i] = b; }
  • 21.
    Super Loop Architecturevoid main(void) { // Prepare run function X X_Init(); while(1) // ‘for ever’ (Super Loop) { X(); // Run function X() } }
  • 22.
    ECU Task:control injection time (3 sub-tasks) T S P compute air flow compute injection time drive actuators air flow injection time air temperature engine temperature engine speed throttle position look-up table PWM signals air pressure
  • 23.
    Control Injection Timevoid main(void) { Control_ECU_Init(); while(1) // 'for ever' (Super Loop) { Control_ECU_Get_throttle_position(); Control_ECU_Get_Actual_speed(); Control_ECU_Get_Actual_temperature(); Control_ECU_Get_Actual_pressure(); Control_ECU_Control_airflow(); } }
  • 24.
  • 25.
    Embedded C GuidelinesComments Overall purpose of the software The names of the programmers Update dates Hardware/software configuration required to use the module Copyright information (if available)
  • 26.
    Embedded C guidelinesIncluded .h files Extern variables Preprocessor directives Struct, union, enum statements Global variables and constants Function prototypes Implementation of functions
  • 27.