1. Infinite loops are commonly used in embedded system programming where tasks continuously loop waiting for events or messages from an RTOS.
2. The main function passes control to an RTOS which controls and signals tasks. Each task contains an infinite while loop waiting to execute code for events or send messages.
3. Functions can be made reentrant by passing all arguments by value rather than reference, not operating on global variables, and not calling non-reentrant functions. This allows tasks to safely call the same function simultaneously.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
100 views
Unit II - Loops and Function Pointers, Queues
1. Infinite loops are commonly used in embedded system programming where tasks continuously loop waiting for events or messages from an RTOS.
2. The main function passes control to an RTOS which controls and signals tasks. Each task contains an infinite while loop waiting to execute code for events or send messages.
3. Functions can be made reentrant by passing all arguments by value rather than reference, not operating on global variables, and not calling non-reentrant functions. This allows tasks to safely call the same function simultaneously.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34
Use 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! Programming using Events and Messages Polling Function main with a waiting loop • main ( ) passes the control to an RTOS • Each task controlled by RTOS and • Each task will also have the codes in an infinite loop • A waiting task is passed a signal by the RTOS to start # define false 0 # define true 1 void main (void) { /* Call RTOS run here */ rtos.run ( ); /* Infinite while loops follows in each task. So never there is return from the RTOS. */ } 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 ( ); } } • void task2 (....) { /* Declarations */ while (true) { /* Codes that repeatedly execute */ /* Codes that execute on an event */ if (flag2) {....;}; flag2 =0; /* Codes that execute for message to the kernel */ message2 ( ); } } Polling for events and messages Example – Mobile Phone Polling for a menu selection from screen state void poll_menuK { /* Code for polling for choice from menu m for screen state K*/ } } Polling for an event like mice click or menu select One of the following functions (s,m) gets executed Use 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. (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. Use 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. Three conditions for a function called as reentrant function 1. All the arguments pass the values and none of the argument is a pointer (address) whenever a calling function calls that function. 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.] 3. That function does not call any other function that is not itself Reentrant. Programming using functions and function queues Programming using functions and function queues • Use of multiple function calls in the main ( ) • Use of multiple function calls in cyclic order • use of pointer to a function • Use of function queues 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 ( ). Use of 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. • 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. Consider a declaration in the example, 1. void inPortA (unsigned char *); inPortA means a pointer to the statements of the function. Inside the parenthesis there is a character pointed by some pointer. 2. *inPortA will refer to all the compiled statements of inPortA.
3. (*inPortA) will refer to calling of statements of
interrupt service routine is executed first and the lowest priority. • The ISRs insert the function pointers into a priority queue of function pointers.