The aim of this project is to demonstrate the use of RTOS in development of multitasking applications on embedded systems. Using an RTOS greatly enhances the functionality of an embedded system. It makes optimum utilization of the CPU time and resources.
However, this also increases the coding complexity of the project. As embedded systems continue to be more and more sophisticated, the number of tasks handled by them also goes increasing and so does the coding complexity.
Real Time Operating System
First let’s get down to some basics. A process is an abstraction of a running program and is the logical unit of work scheduled by the operating system. A thread is a lightweight process that shares resources with other processes or threads. Each thread must “reside” within some process and make use of the resources of that process. Threads that reside within the same process share that processes’ resources.
Now, what exactly is a real time system?
A real-time system is one whose logical correctness is based on both the correctness of the outputs and their timeliness.
Which means if an over temperature detection system in nuclear power plant detects the over temperature after the plant fails makes no sense. Here it requires a rapid correct response to events that occur at non-regular rates. This can be done by the real time system. Some more examples of real time systems are:
· Military radar and defence system.
· air traffic controllers, missile guidance systems or health monitoring systems
· Where a missed response can have catastrophic consequences.
· Aircraft uses a sequence of accelerometer pulses to determine its position.
Now, it is not always that a small error may cause some catastrophic failure so they are categorised into two
1. A soft real-time system is one in which performance is degraded but not destroyed by failure to meet response-time constraints.
2. A hard real-time system is one in which failure to meet a single deadline may lead to complete and catastrophic system failure.
This project is basically an application of soft real-time system
A short intro to Real-time operating system:
Real-time operating systems must provide three specific functions with respect to tasks: scheduling, dispatching, and intercommunication and synchronization. The kernel of the operating system is the smallest portion that provides for these functions. A scheduler determines which task will run next in a multitasking system, while a dispatcher performs the necessary information to start that task. Inter-task communication (like mailbox, pipes and queue) and synchronization (like events, semaphores and mutexes) assures that the tasks cooperate.
HARDWARE
1. STM32 DISCOVERY BOARD
The discovery board is a mid ranged board hardware for getting started with STMicroelectronics ARM based processors. The board has ST-Link programmer/debugger for programming the STM32 microcontroller via a micro USB port. This MCU has ARM cortex M3 processor at its core which is internally clocked at 24 MHz Several GPIO pins are available on the board for interfacing with other devices. The next crucial component after the MCU is the GLCD module.
Fig. 1: Pin Diagram of STM32 Discovery Board
2. GLCD 128 X 64 KS0108
We have used a 128×64 graphical LCD which is controlled by KS0108E controller. The GLCD screen is divided into two halves (called pages) horizontally. The left page is controlled by IC1 and the right one is controlled by IC2.So in order to print on either of these pages, the corresponding Chip Select pin (CS1 or CS2) must be set accordingly. Each of the pages has 64 columns, making 128 columns in total. Each page is further divided vertically into 8 rows, and each of these rows contains 8 pixels, making 64 pixels vertically. Thus in this way, we have 128 x 64 pixels in all. For printing anything on the screen, the following parameters have to be specified:
(a) Page number (using CS1 and CS2 pins).
(b) Column number.
(c) Row number. After that, the data corresponding to the selected row has to be passed into the GLCD. All these communications between the GLCD and MCU happens via the 8 data pins and 5 control pins.
Pin Configuration of 128×64 GLCD
Pin No. |
Function |
Name |
1 |
Ground(0V) |
VSS |
2 |
Supply(5V) |
VCC |
3 |
Contrast adjustment |
Vo |
4 |
High to display data, low-instruction |
Register select |
5 |
Low to write to register, high-read register |
Read/write |
6 |
Reads data-high to low transition |
enable |
7 |
Data pin |
DB0 |
8 |
Data pin |
DB1 |
9 |
Data pin |
DB2 |
10 |
Data pin |
DB3 |
11 |
Data pin |
DB4 |
12 |
Data pin |
DB5 |
13 |
Data pin |
DB6 |
14 |
Data pin |
DB7 |
15 |
Chip selection for IC1, active high |
CS1 |
16 |
Chip selection for IC2, active high |
CS2 |
17 |
Reset signal, active low |
RST |
18 |
Output voltage for lcd driving |
Vout |
19 |
Backlight Vcc (5V) |
LED A |
20 |
Backlight GND (0V) |
LED K |
Circuit Design and Software
CIRCUIT DESIGN
1. DIP-TRACE
Dip Trace is amazing, easy to use, PCB design and layout software which can be downloaded for free. While designing the board for this project, we had to custom create the footprint for the stm32vldiscovery board, as it was not available with the standard library package. It is EDA software for creating schematic diagrams and printed circuit boards. See the Circuit Diagram tabs for schematic and Trace.
2. Interfacing
The project hardware is built around the stm32lv discovery board. The whole unit is powered by the micro USB port of the discovery board, which has to be kept connected with a laptop or a PC to draw power.
The connections made in the PCB are as follows:
Microcontroller pin |
Peripheral (Annotation) |
PB0 |
S9 |
PB1 |
S8 |
PB2 |
S7 |
PD0 |
S2 |
PD1 |
S1 |
PD2 |
S6 |
PD3 |
S5 |
PD4 |
S4 |
PA0 to PA7 |
7 to 4 (data port for GLCD ) |
PC1 |
4(RS) |
PC2 |
5(R/W) |
PC3 |
6(EN) |
PC4 |
15(CS1) |
PC5 |
16(CS2) |
SOFTWARE
1. Keil uvision4
Keil is professional grade software for developing high-end Embedded Firmware. It supports a wide range of microcontrollers and processors. There is a code limited version of this MDK which is available for free download from Keils website. This version of the software limits the maximum size of the code to 32 KB. Since our Firmware is comfortably inside this range, we couldn’t ask for anything better. The most favourable feature of this software is that it comes built in with an amazing RTOS, RTX, which is royalty free. Moreover, all the functions of this RTOS are explained in great detail in Keils Help menu. There are also plenty of example programs to demonstrate the use of these functions.
2. ST-LINK UTILITY
The ST Link utility software is used for downloading the hex code generated by Keil in to the discovery board via micro USB cable. The STM32 ST-LINK Utility soft ware facilitates fast in-system programming of the STM32 microcontroller families in development environments via the tools, ST-LINK and ST-LINK/V2.
Fig. 2: Screenshot of STM32 ST-LINK Utility on Windows
Tank Game and Code Execution
THE TANK GAME
This project is a software major project, as the prime requirement here, is to synchronize all the tasks of a game. From the very beginning, it was evident to us, that the conventional Infinite loop approach won’t be as efficient, as the code length would be enormous since we would have to write the code for the scheduler ourselves.
Game Design:
The game consists of three horizontally moving sprites, which serve as the targets for the player. At the bottom of the screen, we have the tank, which the user can control. The tank is used to fire bullets at the target by pressing down the A button. The left/right motion of the tank is controlled by the directional arrow keys.
The objective of the game is to hit as much targets as possible before the gas runs out. The score increases with multiple of 10.
Fig. 3: Screenshot of Tank from the game
Fig. 4: Image showing initial messages of Tank Game on LCD
Fig. 5: Image of Tank Game based on RTOS
Fig. 6: Prototype of Game Console for Tank Game
Task Execution Flow
Fig. 7: Flowchart showing implementation of RTOS based Tank Game
The above flow chart describes the execution of tasks depending on the events that occur.
· When the processor switches its execution from one task to another, the previous task goes into wait state and the current task goes into running state.
· If the task is event driven (i.e. the task has to be triggered by some other task or interrupt) it waits for its execution till its particular event flag is set.
· Tasks t_enemyA,B,C execute in round robin style where as the t_tankD can be executed after any enemy task (the alternate way of doing this instead of hard coding it is by setting it to a higher priority )
· The Task t_bulletE is executed only after the external interrupt has occurred (‘A’ button is pressed).
· This is purely object-oriented way of multithreading i.e. each object that is displayed or used is a different thread which updates the screen on every execution. This project can also be done by giving each process a different thread like printing on GLCD, calculating positions of each object. This will reduce the number of threads but increase delay.
CODE
The source code consists of
1. main.c
2. GLCD.h
3. sprites.h
4. font5x8.h
main.c:- This file has the main technique of how the game works from the start of rtos to the end of the game.
GLCD.h:- This file has the driver functions that are required for initializing
The graphical lcd. It also has some extra functions which are helpful in making animations and custom graphics. You can modify or add many more functions to this file as per your requirement in project.
sprites.h:- This is a custom file in which image of each sprite is stored. You can create any arbitrary sprite or object and add it to this file. Note the size of obj. you add. It should be of what value you give in GLCD.h
font5x8:-This is same as sprites.h the only difference is that it has characters and symbols.
The code is self explanatory still I have put comments where ever necessary.
Circuit Diagrams
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.