International Journal of Micro and Nano Systems, 2(1), 2011, pp. 19-25 IMPLEMENTATION OF REAL TIME SYSTEMS USING PREEMPTIVE REAL TIME KERNEL Anitha, K. *1, P. A. Vijaya 2 and V. Siddalingappa 3 1 Dept. of Electronics & Communication Engineering, GSSSIETW, Mysore, E-mail: 1 ckmanitha@gmail.com 2 Dept. of Electronics & Communication Engineering, Malnad College of Engineering, Hassan 3 Scientist F, CETE Banglore Abstract: Real time systems are those systems in which the correctness of the system depends not only on the logical results of computation but also on the time at which the results are produced. The kernel provides the basic task level services. These services of the custom real time kernel can be employed as a platform for the development of real time applications. The list of kernel services can be increased to meet the customization requirement and performance of the product. In this paper the development of two real time systems using preemptive real time kernel is described. Pre-emptive Real Time Kernel has the features of multitasking, time scheduling and preemption with multiple priority level for each task and inter task communication. Two applications which use these features are automatic street light controller show the reliability of multitasking of kernel and automatic railway gate controller application describes the multitasking of the kernel. Experimental result shows that our real time model is easy to use and efficient while being able to provide accurate results. Keywords: Rtos, Automated Street light controller I. INTRODUCTION Real time systems are those systems in which the correctness of the system depends not only on the logical results of computation but also on the time at which the results are produced. They span a broad spectrum of complexity from very simple microcontrollers in embedded systems (a microprocessor controlling an automobile engine) to highly sophisticated complex and distributed systems (air traffic control for continental United States) other real time systems include command and control systems, and multimedia and high speed communication systems. Some real time systems are beginning to add expert system and other AI technology, creating additional requirements and complexities. At least three major trends in the real time and embedded systems field have had a major impact on its technology: the increased growth and sophistication of embedded systems the development of more scientific and technological results for hard real-time systems, and the advent of distributed multimedia. Most embedded systems consist of a microcontroller and limited software situated with in some product such as microwave oven or automobile car. Hard real-time systems are those in which missing an important deadline can cause severe consequences, even death. In this area, many fundamental results have been developed. For example in real-time scheduling, rate monotonic analysis has enabled careful evaluation of many practical systems; the concept and analysis of competitive algorithms has provided important scheduling bounds and limits: and on-line planning has added flexible and dynamic capabilities to realtime systems. Many other results exist in real time architecture, fault tolerance, communication protocols, specification and design tools, formal verification, databases and object-oriented systems. Emphasis on all these areas is expected to increase in the foreseeable future. Many hard real time systems are embedded systems. Typically, a real-time system consists of a controlling and controlled system. For example in an automated factory, the control system is the factory floor with its robots, assembling stations, and the assembled parts, while the controlling system is the computer and human interfaces that manage and coordinate the activities on the factory floor. Thus, the controlled system can be viewed as
20 International Journal of Micro and Nano Systems the environment with which the computer interacts. The controlling system interacts with its environment using information about the environment available from various sensors. It is imperative that the state of the environment as perceived by the controlling system be consistent with the actual state of the environment. Hence periodic monitoring of the environment as well as timely processing of the sensed information is necessary. Timing correctness requirements in a real-time system arise because of the physical impact of the controlling system s activities upon its environment via actuators. The most common timing constraints for tasks are periodic, aperiodic and sporadic. A periodic task is one that is activated every T units. The deadline for each activated instance may be less than, equal to, or greater than the period T. An aperiodic task is activated at unpredictable times. A sporadic task may be external interrupts which could be of higher priority. Hence there is requirement of preemption and suspend other tasks and execute the higher priority tasks. Hence it is proposed to develop an RTOS involving Preemptive multitasking capability for RISC microcontrollers using embedded C, GNU and C compiler. II. REAL TIME KERNEL Kernel is a part of an operating system that provides the most basic services to application software running on a processor. The kernel of a real time operating system (RTOS) provides an abstraction layer that hides from application software the hardware details of the processor(or set of processor) upon which the application software will run. The basic services provided by a RTOS Kernel is as shown in Figure 1. (1) Task Management This set of services allows application software developers to design their software as a number of separate chunks of software, each handling a distinct topic, a distinct goal, and perhaps its own real time deadline. Each separate chunk of software is called task. Services in this category include the ability to launch tasks and assign priorities to them. The main RTOS service in this category is the scheduling of tasks as an embedded system is in operation. The Task Scheduler controls the execution of application software tasks, and can make them run in a very timely and responsive fashion. Figure 1: Basic Services Provided by a Real-Time Operating System Kernel (2) Intertask Communication and Synchronization These services make it possible for tasks to pass information from one to another, without danger of that information ever being damaged. They also make it possible for tasks to coordinate, so that they can productively cooperate with one another. Without the help of these RTOS services, tasks might well communicate corrupted information or otherwise interfere with each other. A. Device I/O Supervisor Device I/O Supervisor category of services provide a uniform framework for organizing and accessing the many hardware device drivers that are typical for an embedded system In addition to kernel services, many of these add-on components are complex. Each of these addon components is included in an embedded system only if its services are needed for implementing the embedded application, in order to keep program memory consumption to a minimum III. IMPLEMENTATION OF REAL TIME SYSTEMS USING REAL TIME KERNEL This section explains about the preemptive real time kernel services used to develop two real time systems automatic street light controller and automatic railway gate controller which are implemented using kernel services. (1) Kernel Services Implemented AVRX_GCC_TASKDEF (task name, int bytes, int priority).
Implementation of Real Time Systems using Preemptive Real Time Kernel 21 AvrXRunTask (task_name). Epilog ( ). IntProlog ( ). AvrXStartTimer (&TCB, int time); TCB (Timer control block). AvrXWaitTimer (Timer). AvrXSetKernelStack (int stack size). A. AVRX_GCC_TASKDEF(task name, int bytes, int priority) Here is a task named demo, RAM of the task stack size say 200 byte, priority of the task (say 1) Example of usage AVRX_GCC_TASKDEF(demo, 200, 1) { char ch; // and so on, as in any function puts_p(pstr( Hello From Demo Task\r\n ));... the rest of the tasks s code, usually an infinite loop... with wait for message, or some such. } B. AVRXRUNTASK (TASK_NAME) Format: AvrRunTask(&task); Starts a task by first calling RTOS initialization followed by calling the task as defined in the task definition. This must be called by main() to get things going. C. Epilog ( ) Format: Epilog( ); Restore previous context of all variable values kernel or user. generate an interrupt before returning to the task. Internal use or in ISRs D. IntProlog ( ) Format: IntProlog( ); second parameter as the wait time (number of timer ticks). The 2 nd parameter is the timeout tick count F. AvrXWaitTimer (Timer) Format: AvrXWaitTimer (&TCB); Waits on a timer to expire the number of ticks passed on in the function AvrxStartTimer. G. AvrXSetKernelStack (int stack size) Format: AvrXSetKernelStack (&stack); This function initializes the stack for different tasks. IV. IMPLEMENTATION OF AN AUTOMATIC STREET LIGHT CONTROLLER The automatic streetlight controller senses the filtered sunlight by a photo electric device which generates a voltage proportional to illumination level and after passing through the low pass filter which is connected to an ADC of the Microcontroller. The control system or controller is based on RISC microcontroller ATmega32. The system will have digital display and a keypad to set time of activation and deactivation of the light switching with feed back from LDR. A. Algorithm of the Program Step 1: Declaration of LCD variables, Real Time Clock (RTC) setting variables, keyboard variables and ADC variables are declared. Select 4 bit LCD operation and increment display characters. Step 2: Using keyboard set RTC for the present time. Set the time for switching ON lights in the evening. Set the time for switching OFF lights in the morning Step 3: Read the values of LDR for different lighting condition using ADC Pushes entire register context onto the stack, returning a frame pointer to the saved context. If running in user mode upon entry then switches the stack to the kernel and stores the frame pointer in the current processes. Internal use or in ISRs E. AvrXStartTimer (&TCB, int time); TCB (Timer control block) This is function call for timer ticks assignment and it will add the TCB into the timer queue using the Figure 2. Block Diagram of an Automatic Street Light
22 International Journal of Micro and Nano Systems by calculating the time taken at a certain speed to cross at least one compartment of standard minimum size of the Indian railway. Sensors can be fixed at predetermined distance on both sides of the gate. The sensor along the train direction as onward sensor and the other as depart sensor. When onward sensor receiver gets activated, the gate motor is turned on in one direction and the gate is closed and stays closed until the train crosses the gate and reaches depart sensor. When depart sensor receiver gets activated motor turns in opposite direction and gate opens and motor stops. Buzzer will immediately sound at the onward receiver activation and gate will close after 5 seconds, so giving time to drivers to clear gate area in order to avoid trapping between the gates and stop sound after the train has crossed. And a track switching using a relay switch, which will be operated manually when experimenting. This avoids accidents in case of two trains coming in the opposite direction but on the same track. Here we are using traffic light indicator for the people who wants to cross the railway gate using LEDs. Figure 3 : Flow Chart of an Automatic Street Light Step 4: Depending upon the time and depending on the lighting condition and if it is evening, after checking the ON time, switch ON the street lights or if it is morning, after checking the OFF time switch OFF the street light. V. IMPLEMENTATION OF AN AUTOMATIC RAILWAY GATE CONTROLLER This implementation utilizes two powerful IR transmitters and two receivers; one pair of transmitter and receiver is fixed at up side (from where the train comes) at a level higher than a human being in exact alignment and similarly the other pair is fixed at down side of the train direction. In this implementation a time period of 5 seconds are considered. Sensor activation time is so adjusted Figure 4 : Block Diagram of an Automatic Railway Gate Figure 4 shows the block diagram of automatic railway gate controller. ATmega32 microcontroller (8-bit) consists of 40 pin and has 4 ports (A, B, C, and D) each of 8 bits. In Port B first 4 bits are connected to the key pad through a resistor of 220 ohms. When the key pad is pressed it is connected to the 5volts power supply and connected to ground
Implementation of Real Time Systems using Preemptive Real Time Kernel 23 when released. The 4 th and 5 th bit of port B is connected to 2 IR sensors. Here the sensor consists of two parts-transmitter and receiver, where the transmitter will be continuously transmitting pulse to the receiver. The transmitter has a 555 timer which generates a square wave for IR LED to work. When the train cuts the pulses emitted the output of the IR sensor momentarily transits through a low state as a result a monostable is triggered and a short pulses are applied to the 4 th and 5 th bit of Port B. Micro switches used are ordinary switches, when pressed they are connected to 5V power supply and connected to ground when released. Here micro switch is connected to track (real world implementation) to detect the arrival of train. Here DC motor is used for opening and closing of the railway gate located near the track. It is connected to Port D of ATmega32 through H-Bridge. It is a high voltage, current dual full bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC motor and stepper motor. Two enable inputs C and D are provided to enable or disable the device independently of the input signal. An additional supply input is provided so that logic works at a lower voltage. Depending on the values to the input (C&D) the motor rotates in forward and reverse direction. The 3 bits of Port C are connected to green, red LEDs and buzzer. Here the buzzer will be used to indicate the riders the arrival of the train. LEDs are used for traffic light indication near gate. LCD is used to display real time clock and text as given in pseudo code. It is connected to Port A of ATmega32. Figure 5: Step 4: When the train goes away from the railway gate crossing followed by other infrared rays is sensed, now the gate is open, during opening the buzzer becomes ON, when the gate is completely open the buzzer goes off, red LED becomes 0ff and green LED gets ON. VI. RESULTS Flow Chart of an Automatic Railway Gate The result obtained after the design and implementation of the real time systems have been discussed in this section, which shows the reliability of Kernel on Multitasking, Intertask communication and Preemption feature of the kernel. A. Algorithm of the Program Step 1: Declaration LCD, RTC setting variables, display ON and OFF variables, ADC variables, keyboard variables, micro switch variables, motor variables, LED variables and buzzer variables are declared. Step 2: Detects micro switch and infrared sensors in both the direction if train is moving, train wheel will press micro switch first followed by sensing of infrared rays, if both the condition satisfied then it indicates arrival of train. Step 3: The red LED glows and railway gate is closed by the motor. During closing of the gate one buzzer is made ON continuously. When railway gate is completely closed buzzer is switched OFF. Figure 6: Snapshot of the Setup of an Automatic Street Light
24 International Journal of Micro and Nano Systems All the hardware components are installed in cover box which are Keyboard, LCD LM018, ATmega32 IC, LED s etc. Figure 9 shows the snapshot of the result of a street light controller system when there is a fully dark (i.e night). Figure 10 shows the snapshot of the result of a street light controller system when there is a partial bright (i.e early morning) Figure 7: Snapshot of the Result of an Automatic Street Light when there is Partial Darkness (i.e Evening) A. Automatic Railway Gate The connections are made as per the Block diagram shown in figure 6 and coding is done which is further burn in ATmega32 IC via AVR Studio 4 software. Sensors are fixed at predetermined distance on both sides of the gate. The sensor along the train direction as onward sensor and the other as depart sensor. When onward sensor receiver and micro switch gets activated, the gate motor is turned Figure 8: Snapshot of the Result of an Automatic Street Light when there is Fully Darkness (i.e night) Figure 10: Snapshotof an Automatic Railway Gate Setup Figure 9 : Photograph of the Result of an Automatic Street Light when there is Partial Brightness (i.e Early Morning) Figure 11: Snapshot of an Automatic Railway Gate Result when the Motor Runs in Clockwise Direction
Implementation of Real Time Systems using Preemptive Real Time Kernel 25 on in one direction and the gate is closed and stays closed until the train crosses the gate and reaches depart sensor. This result photograph is as shown in figure 11. When depart sensor receiver gets activated motor turns in opposite direction and gate opens and motor stops. Photograph of this result is as shown in figure 12. Figure 12: Snapshot of an Automatic Railway Gate Result when the Motor Runs in Anticlockwise Direction CONCLUSION The intention of preemptive real time kernel is to support multitasking for a maximum of 128 tasks. Since time taken by individual tasks is different, the time response is very complex during multitasking. Practically providing multitasking for all 128 tasks may be very hard to achieve. Multitasking of simple applications can be provided very easily, in this project up to 5 multitasking are implemented but complex applications take longer time and if it is inter dependent upon other applications then it will be even difficult to implement. The kernel provides abundant services for the applications, I tried my best to implement few of the kernel services for RISC controllers, in this project two real time systems automatic street light controller and automatic railway gate controller has been implemented using kernel services to test the reliability of the kernel features and they are working properly. The number of applications can be extended depending upon the requirement and at the same time care has to be taken for time management. In this paper multitasking and intertask communication features of real time kernel is used for the implementation of real time system called automatic street light controller and preemption feature of real time kernel is used in the implementation of the real time system automatic railway gate controller. It has been successfully tested in the laboratory. ACKNOWLEDGEMENT It is a pleasure to recognize the many individual who have helped me in completing this technical paper. I sincerely express heartiest thanks to my guide Dr. P.A Vijaya and Mr.Siddalingappa for all the technical guidance, encouragement and support throughout this process. REFRENCES [1] Luis E. Leyva-del-Foyo, Pedro Mejia-Alvarez, and Dionisio de Niz, Predictable Interrupt Management for Real Time Kernels over conventional PC Hardware, IEEE Journal on Embedded System, pp. 921-926, 2006. [2] Jaehwan Lee and Vincent John Mooney, Anders Daleby, Karl Ingstrom, Tommy Klevin* and Lennart Lindh*, A Comparison of the RTU Hardware RTOS with a Hardware/Software RTOS, IEEE Journal on Embedded System, pp. 683-688, 2003. [3] Su-Lim TAN and Tran Nguyen Bao Anh, Realtime operating system (RTOS) for small (16-bit) microcontroller, The 13th IEEE International Symposium on Consumer Electronics (ISCE2009), pp. 1007-1011, 2009. [4] B. Nicolescu, N. Ignat, Y. Savaria, and G. Nicolescu, Analysis of Real-Time Systems Sensitivity to Transient Faults Using MicroC Kernel, IEEE Transaction on Nuclear Science, Vol. 53, No. 4, pp. 1902-1909, 2006. [5] Achyut S. Godbole, Operating Systems, TMH,13th reprint, 2002. [6] Tanenbaum, Operating system design implementation, Prentice-Hall, 1987. [7] Dhamdhere D. M., Introduction to system software, Tata McGraw-Hill, New Delhi, 1986. [8] Kenneth J Ayala, The 8051 microcontroller architecture, programming and applications, Pearson Publication, Second edition, 2005. [9] Frank Vahid embedded system design, Wiley- India Publishers, Edition-1, 2007. [10] Details of ATmega32 microcontroller: www.atmel.com [11] Richard M. Stallman and the GCC Developer Community, Using the GNU Compiler Collection, Last updated 23 May 2004. [12] Doxygen, avr-libc Reference Manual, Jan 2006. STK500 Starter Kit User s Guide.