[go: up one dir, main page]

0% found this document useful (0 votes)
20 views37 pages

Unit 3 RTOS

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 37

UNIT 3

“Introduction to embedded systems “ – Shibu K V


INTRODUCTION

The embedded firmware is responsible for controlling the various


peripherals of the embedded hardware and generating response in
accordance with the functional requirements for a particular
embedded product.
Firmware is considered as a master brain of the embedded
system(during fabrication or in the later stage)
Firmware- ROM
EMBEDDED FIRMWARE DESIGN
APPROACHES

1. Conventional procedural based firmware design/ Super loop


model
2. Embedded Operating System based design
SUPER LOOP BASED APPROACH

- For applications that are not critical


- Similar to conventional procedural programming where code is
executed task by task.
- The task listed at the top of the program code is executed first and
task below the top are executed after completing the first task.
- In multiple task based system, each task is executed serially.
Firmware execution flow is
1. Configure the common parameters and perform initialization for
hardware components like memory, register etc.
SUPER LOOP APPROACH

2. Start the first task and execute it


3. Execute the second task
4. Execute the next task
5…
6…
7. Execute the last defined task
8. Jump back to the first task and follow the same
The operation is in infinite loop based approach, hence called as
Super Loop Based Approach
SUPER LOOP APPROACH

The only way to come out of the loop is either a hardware reset or an
interrupt assertion.
-A hardware reset brings the program execution back to the main
loop.
-An interrupt request suspends the task execution temporarily and
performs the corresponding interrupt routine and on completion of
routine it restarts the task execution from the point where it got
interrupted.
-The super loop approach does not require an operating system
(since no scheduling and priority is required)
-Example : Video Games keypad and display
SUPER LOOP APPROACH

Drawback
1.Any failure in any part of a single task may affect the total system
(hardware and software watchdogs can be used)
2.Lack of real timeliness – no of tasks increases , time at which each
task is repeated increases. Probable missing of some task . Ex:
pressing of the keys
solution: to introduce interrupts for events that require real
time attention
THE EMBEDDED OPERATING
SYSTEM BASED APPROACH

• OS based approach can contain either a General Purpose System (GPOS) or


RTOS
• GPOS is similar to conventional PC Ex. Microsoft Windows
• Use of GPOS in embedded product merges the demarcation of embedded
systems and general computing systems in terms of OS.
• For developing applications on top of the OS, the OS supported APIs are used.
• OS based applications require ‘Driver software’for different hardware present
on the board to communicate with them.
• RTOS respond in a timely and predictable manner to events.
• RTOS contains a Real Time Kernel for pre emptive multitasking, scheduling
task etc.
PROGRAMMING IN EMBEDDED C
-Whenever C language and its extensions are used for programming
embedded systems it is referred as ‘Embedded C’ programming.
C v/s Embedded C
-C is well structured, combination of the features of high level languages
and assembly and helps in hardware access programming.
-Conventional follows ANSI standard and has various libraries for different
operating systems. A compiler is required for conversion of programs
written in C to the target processor specific binary files. Hence it is platform
specific.
-Embedded C is subset of C language.
-A software program called ‘Cross-compiler’ is used for the conversion of
programs written in Embedded C to target processor specific instructions.
COMPILER VS CROSS-COMPILER
Compiler is a software tool that converts a source code written in a high level
language on top of a particular OS running on a specific target processor
architecture.
-OS, compiler program and source code run on the same target processor.
-Source code is converted to target processor specific machine instructions.
-Compilers are called ‘Native Compilers’
-Cross compilers are used in cross platform development applications
-A compiler converts the source code to machine code for a target processor
whose architecture an instruction set is different from the processor on which the
compiler is running
-Ex: Embedded system development ( firmware is developed on Intel/ machine
and converted to machine code for target processor like 8051, ARM
USING ‘C’ IN EMBEDDED ‘C’

Keywords and identifiers


-Keywords are reserved names, ANSI ‘C’ supports 32 keywords and
always written in lower case
-Identifiers are user defined
Names and labels. Identifiers can
Contain letters and numbers.
Only underscore is allowed in
Special characters.
USING ‘C’ IN EMBEDDED ‘C’

Data types
STORAGE CLASS

Key words related to storage class provide information on the scope


and lifetime of a variable. C supports 4 types, apart from this it
supports storage class ‘global’. An auto or static variable declared in
the public space of a file is accessible to all functions within that file.
No explicit storage class for ‘global’, only depends on the way the
variable is declared.
LOGICAL AND ARITHMETIC
OPERATIONS
BRANCHING INSTRUCTIONS
BRANCHING INSTRUCTIONS
LOOPING INSTRUCTIONS
ARRAYS AND POINTERS

Arrays is a collections of related elements


They are declared with data type of arrays, name of the array and
number related elements to be placed in the array
Char arr [5];
The index of the
first element of
the array is 0.
IMPORTANT POINTS ON ARRAYS
1. The sizeof() operator returns the size of a array as the number of bytes.
2. The sizeof() operator when used for retrieving the size of an array which is
passed as parameter to a function will only give the size of the data type of
the array.
3. Use the syntax extern array type array name [ ] to access an array which is
declared outside the current file.
4. Arrays are not equivalent to pointers. But the expression ‘array name’is
equivalent to a pointer, of type specified by the array, to the first element of
an array
5. Array subscripting is commutative in C language and arr[k] is same as *((arr)
+(k)) where arr[k] is the content of kth element of array arr and (arr) is the
starting address of the array arr and k is the index of the array or offset
address for the kth element from the base address of array.
POINTERS

Pointer is a flexible at the same time most dangerous feature,


capable of creating potential issues leading to firmware crash, if not
properly used.
Pointers are very helpful in
1.Accessing and modifying variables
2.Increasing speed of execution
3.Accessing contents within a block of memory
4.Passing variables to functions by eliminating the use of a local
copy of variables
5.Dynamic memory allocation
IMPORTANT POINTS ON POINTERS

1. The instruction *ptr++ increments only the pointer not the content
pointed by the pointer ptr and *ptr– decrements the pointer not the
contents pointed by the pointer ptr
2. The instruction (*ptr)++ increments the content pointed by the
pointer ptr and not the pointer ptr (*ptr)- - decrements the content
pointed by the pointer ptr and not the pointer ptr
3. A type casted pointer cannot be used in an assignment expression
and cannot be incremented or decremented
4. ‘Null Pointer’ is a ponter holding a special value called ‘NULL’which
is not the address of any variable or function or the start address of
the allocated memory block in dynamic memory allocations.
IMPORTANT POINTS ON POINTERS

5. Pointers of each type can have a related null pointer viz. there can
be character type null pointer, integer type null pointer.
6. NULL is a preprocessor macro which is literally defined as zero or
((void*)0). #define NULL 0 or #define NULL((void*)0)
7. NULL is a constant zero and both can be used interchangeably as
Null pointer constants
8. A NULL pointer can be checked by the operaor if()
9. Null pointer is a C language concept and whose internal value
does not matter to us . NULL always guarantee a 0 to you but Null
pointer need not be .
CHARACTERS AND STRINGS

Character is a one byte data type and it can hold values ranging
from 0 to 255 or -128 to +127. Character refers to the alpha numeric
characters A to Z and special characters like *, !, ? ,,,
•String is an array of characters. A group of characters defined within
a double quote represents a constant string.
•String always terminates with a \0 character.
Input and Output operations
Standard string input (scanf()) and string output(printf()) functions
from the platform specific I/O library.
CHARACTERS AND STRINGS
• String concatenation
It refers to the joining of two or more strings together to form a single string. To make use of the
built in string operation functions include the header file ‘string.h’ to the ‘.c’ file.
‘strcat()’ is the function used for concatenating two strings.
• String comparison
Is done by strcmp() function. If two strings are passed as arguments to strcmp() function are equal,
the return value is zero. The value can be greater or lesser than zero if the strings compared are
not equal.
• String length
it refers to the number of characters except the null terminator character \0 present in a string.
Strlen() gives the length of the string.
• Copying strings
Strcpy() is used for copying a string to another string
CHARACTERS AND STRINGS

• A few important points on characters and strings


1. The function strcat() is used for concatenating two string variable
and string constants. Characters cannot be appended to a string
variable using strcat() function.
2. Strings are character arrays and they cannot be assigned directly
to a character array
3. Whenever a character array is declared to hold a string , allocate
size for the null terminator character \0 also in the character array
FUNCTIONS

Two different types of functions are supported


Library functions and user defined functions
-Library functions are built in functions which are either part of the
standard library or user created library files. These library files are
categorized into various types like I/O library function, string
operation library functions, memory allocation library functions
-User should include the header file corresponding to a particular
library file for calling the functions from that library in C source file .
FUNCTIONS
User defined functions
-Are programmer created functions for various reasons like modularity, easy
understanding of code, code reusability etc. The syntax is
Return type function name (argument list)
{
//Function body (declarations & statements)
//Return statement
}
Arguments to functions are passed by two means pass by value and pass by
reference. Pass by value method passes the variables to the function using local
copies whereas in pass by reference variables are passed to the function using
pointers.
FUNCTION POINTERS

Function pointer is a pointer variable pointing to a function. When


the application is loaded in primary memory for execution, the code
corresponding to the function is also loaded into the memory and it
resides at a memory address provided by the application loader.
Return_type (*pointer_name) (argument list)
Typedef int (*funcptr)();
funcptr fptr;
STRUCTURES AND UNIONS

- Structure is a variable holding collection of data types. The data


types can be either unique or distinct. The tag ‘struct’ is used for
declaring a structure. The general form is given below
struct struct_name
{
// variable 1 declaration
// variable 2 declaration
//..
// variable n declaration
};
UNIONS

Union declaration follow the same syntax as that of structures.


Though union looks similar to structure in declaration, it differs from
structure in the memory allocation technique for the member
variables. Whenever a union variable is created, memory is allocated
only to the member variable of union requiring the max storage size.
But for structures, memory is allocated to each member variables.
The size of a union returns the storage size of its member variable
occupying the max storage size.
DYNAMIC MEMORY ALLOCATION

Static memory allocation: the memory required is fixed and


allocation is done before the execution of the application.
Allocating memory on demand and freeing the memory at run time
is the most advised method for handling the storage of dynamic
data and this memory allocation technique is known as ‘Dynamic
Memory Allocation’
DYNAMIC MEMORY ALLOCATION
-Actual program code is stored at the lower address of the memory
-Constant data related to the application is stored at the memory area just above the
executable code.
-The alterable data (global and static variables) are stored above constants data and is fixed.
-Static storage memory comprises of executable code, constant data and alterable data.
-The storage memory available within the memory segment excluding the fixed memory is the
Dynamic storage memory
-Dynamic storage memory area is divided into two separate entities namely ‘stack’and ‘heap’
-Stack memory starts at the high memory area and goes down, it
Holds auto variables, function parameters and function return values.
-The free memory region lying in between the stack and fixed memory a
Area is called ‘heap’. It grows upwards.
DYNAMIC MEMORY ALLOCATION

Malloc() allocates a block of memory dynamically. It reserves a block


of memory of size specified as parameter to the function, in the
heap memory and returns a pointer of type void.
Calloc() it allocates multiple blocks of storage bytes and initializes
each allocated byte to zero.
Free() is used for releasing or de-allocating the memory allocated in
the heap memory by malloc() or calloc() functions.
Realloc() is a function is used for changing the size of allocated bytes
in a dynamically allocated memory block.
Integrated development environment –self study
FREE RTOS

-FreeRTOS is an open source, real time operating system for


microcontrollers that makes small, low-power edge devices easy to
program, deploy, secure, connect, and manage
-FreeRTOS is a real-time kernel (or real-time scheduler) on top of which LPC17xx applications
can be built to meet their hard real-time requirements.
-It allows LPC17xx applications to be organized as a collection of independent threads of execution. As
the LPC17xx has only one core, in reality only a single thread can be executing at any one time. The
kernel decides which thread should be executing by examining the priority assigned to each thread by
the application designer.
THE LPC17XX PORT OF FREERTOS
1. The LPC17xx port includes all the standard FreeRTOS features:
2. Pre-emptive or co-operative operation
3. Very flexible task priority assignment
4. Queues
5. Binary semaphores
6. Counting semaphores
7. Recursive semaphores
8. Mutexes
9. Tick hook functions
10. Idle hook functions
11. Stack overflow checking
12. Trace hook macros
13. Optional commercial licensing and support
RESOURCES USED BY FREERTOS

-FreeRTOS has a very small footprint. A typical kernel build will consume approximately 6K bytes of
Flash space and a few hundred bytes of RAM. Each task also requires RAM to be allocated for use as
the task stack.
-FreeRTOS uses a modified GPL license. The modification is included to ensure:
1.FreeRTOS can be used in commercial applications.
2. FreeRTOS itself remains open source.
3.FreeRTOS users retain ownership of their intellectual property.
4.When you link FreeRTOS into an application, you are obliged to open source only the kernel,
5.including any additions or modifications you may have made. Components that merely use
6.FreeRTOS through its published API can remain closed source and proprietary.
THANK YOU

You might also like