You are on page 1of 42

Hardware Design Languages

Verilog VHDL

You will study by yourself!!!

Programming in C
Programming: An essential part of any embedded system design. Processor and memory-sensitive instructions: Program codes may be written in assembly Most of the codes written in a high level language (HLL): e.g., C, C++ or Java

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

Advantages of an HLL
1. 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

Advantages of an HLL
Top-down Design: First design of main program (blue-print), then its modules and finally the sub-modules are designed for specific and distinct set of actions. This is most favored program design approach.

Advantages of an HLL
2. Data type declaration: 4 types of integer int, unsigned int, short(16 bits), long(64 bits) unsigned int : +ve values signed int : 32bits Char to manipulate text & string Each data type is an abstraction for the methods, which are permitted for using, manipulating, representing and for defining a set of permissible operations on that data.

Advantages of an HLL
3. Type checking: Type checking during compilation makes the program less prone to errors. For example, type checking on a char data type variable (a character) does not permit subtraction, multiplication and division.

Advantages of an HLL
4. Control Structures, Conditions and loops make the program-flow designing tasks simple.
Examples of Control Structures: while, do-while, break and for Examples of Conditional Statements: if, if - else, else - if and switch - case.

Uses of C as an HLL
C is a language between low level(assembly) & high level language. C an HLL with provision of inserting the assembly language codes in between (called in-line assembly) to obtain a direct hardware control. In the in-line assembly the complex part is in HLL codes. Example: Mov al, p; out q, al are the in-line assembly codes of the C function outportb (q, p);

C Program Elements: Header, Source Files, Pre-processor Directive


Structural elements of C program: 1. Pre-processor declarations, definitions & statements 2. Main function 3. Functions, exceptions and ISRs Pre-processor structural elements: 1. Include : directive for file inclusion 2. Definitions for pre-processor global variables 3. Definitions of constants 4. Declarations for global data type, type declaration and data structure, macros and functions

C Program Elements
1. Preprocessor include Directive:
to include the contents(data & code) of a file. i. Including code files: files for which codes are already available e.g. #include prctHandlers,c ii. Including constant data files: extension .const iii. Including string data files: extension, .strings , .str or .txt e.g. #include netDrvConfig.txt iv. Including initial data files: initial or default data for the shadow of ROM. Extension, .init (for ROM) , .data(for RAM)

C Program Elements
v. Including basic variables files : files for local or global static variables stored in RAM static variable: one instance having one variable address with a static memory allocation e.g: system real time clock (with extension .bss) vi. Including header files: includes the contents(code or data) of a set of source files e.g: string manipulation function- string.h mathematical function- math.h Pre-processor directive- #include <string.h> #include <math.h> #include <stdio.h> , #include <conio.h>

C Program Elements
1. Preprocessor include Directive:
# include "VxWorks.h" /* Include VxWorks functions */ # include "semLib.h" /* Include Semaphore functions Library */ # include "taskLib.h" /* Include multitasking functions Library */

C Program Elements
1. Preprocessor include Directive:
# include "sysLib.c" /* Include system library for system functions */ # include "netDrvConfig.txt" /* Include a text file that provides the 'Network Driver Configuration' */

C Program Elements
1. Preprocessor include Directive:
# include "prctlHandlers.c" /* Include file for the codes for handling and actions as per the protocols used for driving streams to the network */

C Program Elements
2. Preprocessor Directive:
(pre-processor constants, variables, text files, configuration files, header files, library functions) a) Global Variables: IntrDisable, IntrPortAEnable, IntrPortBDisable, STAF, STAI # define volatile boolean IntrEnable (Volatile: a directive to the compiler not to take this variable into account while compacting & optimizing the code)

a) Constants: # define false 0 #define portA(volatile unsigned char*) 0x1000 # define PIOC(volatile unsigned char*) 0x1001 0x1000 and 0x1001 are address for portA & PIOC(port input-output control register) are constants defined for the 68HC11 register address a) Strings: # define welcome "Welcome To ABC Telecom"

C Program Elements 2. Source Files

C Program Elements
3. Configuration files: For configuration of the system Device configuration code put in a file of basic variables e.g: serialLine_cfg.h , os_cfg.h

C Program Elements
3. Preprocessor Macros:
Macro - A named collection of codes that is defined in a program as preprocessor directive. It 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.

C Program Elements
3. Preprocessor Macros:
Macro differs from a function as follows: The codes for a function are 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.

C Program Elements
3. Preprocessor Macros:
When a function call is used instead of macro, the overheads (context saving and return) will take a time, Toverheads that is the same order of magnitude as the time, Texec for execution of short codes within a function. Use a function when the Toverheads << Texec, and a macro when Toverheads ~= or > Texec.

C Program Elements Data Types:


Primitive Data types:
Char, byte 8 bits, unsigned short, short 16bits, unsigned int, int, float 32bits, long double, double -64 bits typedef: create a boolean type variable as a data type To use unsigned character as a data type typedef unsigned character portAdata # define Pbyte portAdata 0xF1

C Program Elements Pointers & Null pointers:


Pointer- reference to the starting address refers to variable, data structure & function E.g. unsigned char *0x1000 character of 8bit at address 0x1000 #define NULL (void*) 0x0000 ???

unsigned short *timer1;


timer1++ =??

unsigned byte *portA - *means the contents at (portA have a buffer register storing a byte) void *portAdata compiler allocate address for the *portAdata without type check Pointer can be assigned a constant fixed address #define portA(volatile unsigned byte*)0x1000 or volatile unsigned byte*portA = (unsigned byte*) 0x1000 unsigned byte *portA = &portAdata

C Program Elements
4. Data Structures:
Data structure - A way of organising large amounts of data. A data elements collection can then be identified and accessed with the help of a few pointers and/or indices and/or functions.

C Program Elements
4. Data Structures:
It a way of organizing several data elements of same types together at consecutive memory address. Standard Data Structure Examples: Queue Stack Array one dimensional as a vector Multidimensional List Tree

C Program Elements
5. Modifiers:
auto, unsigned, static, const, register, interrupt, extern, volatile, volatile static auto: no modifier ROM allocation if the variable is initialized in the program RAM allocation if the variable is not initialized in the program Unsigned : to permit only +ve values for short(16 bits) , int(32 bits), long(64 bits) data type.

C Program Elements
Modifiers
Static: declaration inside a function block, such that a variable can be accessible outside the function static variable has reserve memory for it Const: outside a function block, initialized by the program ROM allocation for this code #define const welcome_msg good morning Extern : directs the compiler to look for the data type declaration or a function in the module other than the one in currently in use extern long volatile time_value

C Program Elements
Volatile : outside a function block is a warning to the compiler that an event can change its value or that its change represents an event. e.g. - volatile variable is not optimized ; Volatile static : inside the function block static- variable should be accessible outside the function block with reserved memory volatile- cannot optimize e.g.- volatile static boolean RTIEnable= true

C Program Elements
6. Infinite loops:
Infinite loop 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!

C Program Elements
8. 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.

C Program Elements
8. Functions:
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.

C Program Elements
9. 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.

C Program Elements
Three conditions for a function called as reentrant function :
i. All the arguments pass the values and none of the argument is a pointer (address) whenever a calling function calls that function.

C Program Elements
ii. 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.]

C Program Elements
iii. That function does not call any other function that is not itself Reentrant.

10. Multiple function calls in Cyclic Order:


One of the most common methods is the use of multiple function-calls in a cyclic order in an infinite loop of the main ( ).

C Program Elements

C Program Elements
11.Function Pointers: * sign when placed before the function
name then it refers to all the compiled form of the statements in the memory that are specified within the curly braces when declaring the function.

C Program Elements
A returning data type specification (for example, void) followed by '(*functionName) (functionArguments)' calls the statements of the functionName using the functionArguments, and on a return, it returns the specified data object. We can thus use the function pointer for invoking a call to the function.

Summary
We learnt
i. 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. 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. Therefore, C or C++ or JAVA is used for embedded system development.

ii.

Summary
We also learnt
iii. 'C' language supports in-line assembly (fragments of codes in assembly) gives the benefits of assembly as well as high-level language. iv. the important programming elements in C preprocessor directives, library functions, header files, source and configuration files, data type declarations, data structures, infinite loops, reentrant functions, macros, multiple function calls, pointers and function pointers.

You might also like