Showing posts with label uCOS-II. Show all posts
Showing posts with label uCOS-II. Show all posts

Saturday, May 29, 2010

Adding Interrupts to uCOS-II Running on Microsoft Windows

How to Simulate Interrupts?

In the past few days, I was asked that question many times. So, I decided to make a small software example to show how uCOS-II interrupts can be simulated under Windows.

I modified the uCOS-II port to support 8 interrupts at the same level of priority. Moreover, these interrupts can occur simultaneously but their ISR execution order depends on the implementation. I followed the uCOS-II tasks's priority schema with interrupts (i.e. the smaller the interrupt number, the faster it will be executed).

The implementation core is done by creating a Win32 thread as a generic IRQ handler in os_cpu.c. In addition, 2 utilities were added in the file pc.c. They are used to register and unregister user ISRs. ISRs will be written only in C.

Testing the Interrupts Implementation

My test code is based on the 1st example in uCOS-II book. I modified this code to have 5 tasks with the highest priority to be interrupt-driven. Each task of the 5 will wait for a semaphore before printing its number on the screen. ISRs will trigger these tasks by signaling the semaphores. The test code can be found here.

To automate this test, I wrote a utility that generates these 5 interrupts randomly every 2 milliseconds. The code for this utility can be found here. I just run my test code then run the interrupt generation utility. To distinguish the interrupt-driven tasks from others, I modified their code to print their IDs in a new color scheme. The interrupt generation utility code is found here.

Fig 1 shows the application running with interrupts. Numbers with blue background are those printed by tasks driven by interrupts.

Figure 1: Example 1 Modified to Run with Interrupts


Friday, May 28, 2010

Running Timer's of uCOS-II to Measure Cycle Average Execution Time

Software Timers

Embedded applications need to schedule future events. This can be accomplished using timers and timers' services.

Timers are an integral part of any real-time system. A timer can be seen as an event scheduling according to a predefined time value in the future, exactly like setting an alarm clock.

Embedded systems that have time-sensitive activities use 2 types of timers; hard-timers and soft-timers. Hard timers are based on physical timers on the chip that directly interrupt the CPU. A hard-timer is a must in case of an application demanding high precision and predication. On the other hand soft-timers are scheduled through a software facility that enables soft-timer's maintenance, installation, and removal.

But if hard-timers are more accurate than their soft counterparts, why are they used? The answer is in their nature. The can be programmed with timeouts of course granularity. In addition, the high-precision is not always needed in most of embedded systems. Another prevailing reason is reducing system interrupt overhead. Facilities of soft-timers are built using only 1 hardware-timer.

Software Timers in uCOS-II

Starting from uCOS-II V2.8x, uCOS-II added the soft-timers feature. uCOS-II timers have 2 modes of operations; periodic and 1-shot modes. Whenever a timer timeouts, a callback function is executed. This service is implemented as a uCOS-II task. This task is assigned a stack size and priority defined the macros OS_TASK_TMR_STK_SIZE and OS_TASK_TMR_PRIO respectively. This task is signaled from the tick ISR at a constant rate defined by the macro OS_TMR_CFG_TICKS_PER_SEC. Whenever signaled, it updates the soft timers created by the user and make calls to callback functions if they expire. The design structure used for timers implementation is wheel spoke. Like any other uCOS-II service, this service is highly configurable. In order to use it you have to:

1. Define OS_TMR_EN to a value greater than zero

2. Define OS_TASK_TMR_STK_SIZE and OS_TASK_TMR_PRIO

3. Define frequency of timers update through OS_TMR_CFG_TICKS_PER_SEC

4. Enable hooks

5. Modify OSTimeTickHook to signals the timers manager task as explained here

6. Use any of timers management APIs in your application

Measuring Execution Time

To test the execution time of a cyclic task, you should use the OSTimeGet API at its start and end to measure the execution time. You should accumulate the total execution time and the number of cycles executed in order to calculate average cycle execution time. The soft-timer will be used to calculate this average at periodic rates. A code example is can be found here.

Fig 1 shows the result of running that code.

Figure 1: Timer Measuring Execution Time


Saturday, April 10, 2010

Running uCOS-II and uC/GUI on Mini STM32 (ARM Cortex M3)

Development Environment

I decided to test the two gifts, I earlier had from micro4you. These gifts were a Mini STM32 board and a Colink ARM USB JTAG. In addition, I use RVMDK4 as a cross compiler. In order to run this demo successfully, you should have a full version of this tool.

Installing Colink Driver

1. Go to http://www.ftdichip.com/Documents/InstallGuides.htm

2. Follow the installation guide matching your OS

3. Or go to http://www.ftdichip.com/Drivers/D2XX.htm to install the driver directly

Installing Colink ARM USB for RVMDK

1. Go to http://www.coocox.org/

2. Click on "Download Colink for MDK"

3. Save CooCox_Colink_MDK_Plugin_V1.81_Setup.exe to your PC

4. Run CooCox_Colink_MDK_Plugin_V1.81_Setup.exe and follow the installation. Install the plug-in in the same directory where Keil (RVMDK) is installed.

For screenshots for step 4, you can refer to http://www.coocox.org/CoLinkGuide/Installation.htm.

Testing the Application Using Colink

1. Open your project using RVMDK. The project contains uC/GUI demo application, uC/GUI ported to my TFT LCD, and uCOS-II ported to my board.

2. Build the target

3. Configure that target options to use the Colink for debugging and flash programming

For screenshots for step 3, you can refer to http://www.coocox.org/CoLinkGuide/Use.htm.

4. Make sure that the flash settings are STM32F10x On-Chip Flash 128K and the download function is Erase Sectors

5. Connect the board to its USB power cable and Colink ARM USB JTAG

6. Download the compiled program to the flash

7. Run the demo

Repeat steps from 2 to 7 with different configurations. To change demos running:

1. Open GUIDEMO.c

2. For the variable _apfTest, just comment the demos you want to stop and uncomment the demos you want run

3. Save the file

4. Repeat steps from 2 to 7

Next Steps

1. Run every possible demo of uC/GUI on the board

2. Use Arabic fonts with Arabic orientation for the complete demo finished above

Wednesday, April 7, 2010

My 2nd Visit to Anzma

About the Visit


My visit, this time, was more focused on uCOS-II. I challenged their students to finish a 4-hours lab based on the example, I developed earlier. The goal was to finish the lab in 4 hours and with maximum 8 questions. They were 5 students. Amazingly, the first finished his work after 2 hours only. It ran successfully from the first time. Other 3 students finished after 3 hours and half while the last finished just before the given time.


It was a great transition for the students and they gain confidence in themselves to code in a multi-task style. They proved the effectiveness and efficiency of Anzma's RTOS courses. In less than 16 hours, students were able to program a real application using uCOS-II.


Figure 1 shows me and the students after finishing the lab.

Figure 1: From the left to the right (Khaled, Ahmed, Me, Islam, Ahmed, and finally the champ Ibrahim)

Sunday, March 28, 2010

Running uCOS-II with Microsoft Windows

uCOS-II Win32 Simulation with Windows
Most of the previous tests of the Win32 ports were never done, until writing this document, with Windows Vista and Windows 7. Lately, I have tested the previous work I have done in here and here on Windows 7 and Windows XP. It worked successfully on Windows 7 and Windows XP.

Sunday, February 21, 2010

Running PC Utility for uCOS-II with Microsoft Windows Vista in 1 Hour

uCOS-II the Real-Time Kernel

uCOS-II is a small, yet powerful real-time operating system developed by Jean J. Labrosse and described in his book "MicroC/OS-II - The Real-Time-Kernel", Second Edition, published by CMP Books. The operating system is highly CPU independent and has been ported to numerous microprocessor platforms. The source code as provided together with the book and via the web site may be freely used for non-commercial applications and educational purposes. Licenses for commercial use are available too.

PC Utility for uCOS-II x86 Win32 Simulation

In a previous post, I managed to simulate uCOS-II with Windows Vista. The example used was a very simple one that used to display simple messages on the console. The next step was to create a PC utility to access some of the PC capabilities. By this, I am completing the simulation environment of uCOS-II on Windows Vista and making it similar to the environment described in uCOS-II books by J. Labrosse.

This utility is encapsulated in a file called PC.C and called from the test code. This utility uses the Windows console driver. Encapsulation allows users to easily adapt the code to a different compiler or a different design. The utility contains 3 types of services: character-based display, elapsed-time measurement, and miscellaneous.

Most of the previous tests of the PC Utility were never done, until writing this document, with Windows Vista. In this document, I will show how you can run a PC Utility used with Windows XP under Windows Vista.

Building the PC Utility

My starting point was a PC utility written by Prof. Werner Zimmermann. He used the windows console driver for character based display and some APIs for time measurements.

In addition, he had 3 examples similar to those described in Labrosse's book. I used examples 1, 2, and 3 written for VC as a starting point for my test applications.

To build this tutorial on your own, you need to download Zimmermann's Windows port of uCOS-II from here.

Steps to Build the PC utility

1. Extract the Zimmermann's port

2. Following the directory structure we made in the previous post referenced above, create a directory for the PC utility files as follows C:\Micrium\Software\Blocks\PC\OpenWatcomC-C++1.8

3. Copy PC.C and PC.H from Zimmermann's port to the created directory above

4. In PC.C, remove anything related to interrupts.

5. Build 3 different projects as described in the previous post. The new projects should include the newly added PC files and source codes of examples 1, 2, and 3 from the Zimmermann's port.

6. In each file called test.c, change the PC_DispChar calls to PC_DispStr. PC_DispChar is buggy for this port.

7. In any example where hooks are needed, do the necessary changes to include them in the uCOS-II original code.

8. Compile and run your 3 examples.

Figure 1: Open Watcom C-C++ 1.8 Project Structure

Figure 2: Example 1 Running with the PC Utility

Figure 3: Example 2 Running with the PC Utility

Figure 4: Example 3 Running with the PC Utility