EEE435A Lab 02

Introduction to MicroC/OS-II


First Version of this lab
Created August 2008 lab developed for the Wytec Dragon12-Plus hosting a HC12 microprocessor.

LAB OBJECTIVE
The primary aim of this lab is to provide you with an introduction to MicroC/OS-II Real-Time Kernel. A secondary objective is to get familiar with the Freescale CodeWarrior. As well you will get to become familiar with the Dragon12-P teaching board.


Part 1: Setting Up Your Work Environment
When working in this lab you should work with the Micrium directory that is provided as a resource from the course website. You can put this directory on any drive including a USB memory stick. However you must keep the directory structure the way it was provided to you. If you change it you will have to recreate a project with your new structure (don't). The same goes for the file names and directory names. The directory structure may seem cumbersome at first but it was created this way by Micrium (the company that was created by Mr. Jean Labrosse author of MicroC/OS) so that it could contain various solutions for different microcontrollers in the same file structure without having to maintain several copies of the operating system. The project that you will be working on is located at:

Drive:\Micrium\SOFTWARE\EvalBoards\Freescale\MC9S12DG256B\Wytec Dragon12\Metrowerks\Paged\OS-Probe-LCD

The icon that you will see for the project looks like this: Project Icon

Double-click on the icon and this will automatically open the CodeWarrior IDE. You should see something like this:



If you doe not see the code as shown here, double-click on app.c (application.c) to open the code edit window. Before proceeding with the practical portion of the lab, read Part 2 below.

Part 2: The MC9S12DG256 and the Dragon12-Plus EVB
The objective of this part is to provide a step-by-step introduction to connecting and communicating with your Dragon12 Evaluation Board. In this lab, you will connect your evaluation board (EVB) to a lab computer and begin communicating with it.  We will be checking to see that it is working, explore some features of the CodeWarrior IDE, and then we will compile, download and run a sample program to the board.

The MC9S12DG256: An Overview
The MC9S12DG256 (9S12 for short from now on) is one of many currently available micro-controllers available from Motorola. You can find this device or its family members almost anywhere from microwaves to the fuel injection system in cars.   If you connect this micro-controller to some external devices, you can make almost anything happen as most of you have seen in your EEE351 course. You could build your own customize alarm clock, build a security system for your house, or build a generic remote control.

Your 9S12 sits atop a powerful evaluation board the Dragon12-Plus. First the EVB exposes some of the microcontroller I/O pins to make them available for external use. The board has all of the components required to make the 9S12 run and also to provide an interface for you to program. The 9S12 can be programmed and monitored from the command line. It can also be programmed in symbolic (assembly) language or in a variety of high level languages. In our labs we will be using ANSI C to experiment with the concepts and principles of operating systems.

The main features of the MC9S12DG256 are listed below:
• Powerful 16-bit CPU
• 256K bytes of flash memory
• 12K bytes of RAM
• 4K bytes of EEPROM
• SCI ports
• SPI ports
• CAN 2.0 ports
• I2C interface
• 8-ch 16-bit timers
• 8-ch 8-bit or 4-ch 16 bit PWM
• 16-channel 10-bit A/D converter
• Fast 25 MHz bus speed via on-chip Phase Lock Loop
• BDM for in-circuit programming and debugging
• 112-pin LQFP package offers up to 91 I/O in a small footprint

Dragon12-Plus Features

Below are two pictures of your Dragon12 board. You will notice that the prototyping board is not on the top picture to expose the 9S12. As well the 2X16 LCD Display is only featured on the second picture. If you are curious about the specifications of the 9S12 or the Dragon12 board you can read the documentation on the L: drive.



The Dragon12-Plus board includes the following features:
1. Dual RS232 communication ports
2. RS485 communication port
3. DS1307 RTC with backup battery included for testing I2C interface
4. I2C expansion port for interfacing external I2C devices
5. CAN port
6. SPI expansion port for interfacing external SPI devices
7. Dual 10-bit DAC for testing SPI interface and generating analog waveforms
8. Four robot servo controllers with terminal block for external 5V
9. Four digit 7-segment LED display for learning multiplexing technique
10. Eight LEDs
11. Eight-position DIP switch
12. Four push button switches
13. 5V regulator with DC jack and terminal block for external 9V battery input
14. Speaker to be driven by timer, or DAC or PWM signal for alarm or music applications.
15. Dual H-Bridge motor driver with motor feedback or rotary encoder interface for controlling two DC motors or one Stepper motor
16. Power-On LED indicator
17. IR transceiver with on-board 38KHz oscillator
18. BDM-in connector to be connected with a BDM from multiple vendors for debugging
19. BDM POD mode for programming other HCS12 boards. No extra hardware needed
20. Opto-coupler output
21. Logic probe with LED indicator
22. Abort switch for stopping program when program is hung in a dead loop
23. Mode switch for selecting 4 operating modes: EVB, Jump-to-EEPROM, BDM POD and Bootloader
24. 4 X 4 keypad
25. Form C relay output rated at 3A/30V or 1A/125V
26. Relay-On LED indicator
27. X-Y-Z accelerometer interface or GP2-D12 distance measuring sensor interface for distance measurement
28. Potentiometer trimmer pot for analog input
29. Temperature sensor
30. Communication port for VGA camera with built-in JPEG compression. (Camera is optional)
31. Light sensor
32. Female and male headers provide shortest distance (great for high speed applications!) from bread board to every I/O pin of the MC9S12DG256
33. PC board size is 8.4" X 5.35"


Part 3: Compiling, Downloading and Executing the Sample Code

As indicated in the comments in the code, you will be doing most of your work in the file app.c.

First attach the AC/DC 9v adapter to your board (circle 1). Always use the adapter provided.

Then attach the Background Debug Mode (BDM) adapter to your board on the pins (2). Ask the instructor or the technician if you are not sure. Use the USB cable provided and connect the BDM to your computer. The BDM displayed here is blue, most of the BDMs used in the lab are red. The BDM is actually a microcontroller that also uses the same architecture as your 9S12.

The BDM has to indicator LEDs. The blue LED is an indicator that the USB is connected to the host computer. The yellow LED indicates that the EVB is well connected to the BDM.

Always use the BDM that is matched to the host computer. For some reason P&E have a code that identifies each BDM as a different device that must be installed. You need to have administrator priviledges to install this device. Each BDM has the host name taped to it.

Compile the sample program by clicking on the make icon or by choosing the Make option on the Project menu or alternatively by pressing <F7>

Once the code is compiled you will get a few warnings, but that is OK.

Now start the downloading and flashing process by pressing the icon. (again you can use the shortcut key or menu.)

During the debug mode progress you will see a pop-up window appearing warning you about mass erase of the non-volatile memory. Select OK and you will get the debug mode window. Take time to look at all the sub-windows and see the information that is provided by CodeWarrior.

Start the program by selecting the execute icon .

Observe your EVB and take notice of some of the events that occur as the code executes.
 
Part 4: MicroC/OS-II

For the reminder of the semester you will be using MicroC/OS-II. We chose this operating system in the mid 1990s for many reasons. Mr Jean Labrosse gives free access to the code for educational purposes (if you are going to build a system for sale you must obtain a license). The coding standard that is used in the entire OS reflects our vision of proper documentation, naming conventions and coding standard. The book that accompanies the operating system can be used both as a real-time operating system text and as a reference for the API.

Procedure

1. Intro: First take a look at the app.c file. This file as indicated earlier is going to be where you will be doing most of your work. The first thing we are going to look at is how MicroC/OS takes control of the microcontroller. There are several ways of using the microcontroller:
A question you may ask yourself is: "how does MicroC/OS start". Well it starts in the same way as any other program that you would write in C to execute on the microcontroller.
Find the main() function in app.c. You will notice three function calls. OSInit(), OSTaskCreateExt() and OSStart().
Requirement 1 : Go to chapter 16 in your MicroC/OS book find the meaning for each of the parameters for OSTaskCreateExt(). Comment each of the parameter with its use.

Question 1 : With the knowledge that a process is a program in execution. What is a task in the scope of MicroC/OS?

Question 2 : With your knoweldge of C why is the parameter pdata a pointer to void?

Requirement 2 : OSTaskCreateExt()returns a code. It is always a good practice to ensure that call to the OS worked. Declare a variable of appropriate type (or use a variable that is already declared and appropriate), get the return value of  OSTaskCreateExt()for the last task that is created in the AppTaskCreate() function and check if there was no error reported by MicroC/OS. Note that there are pre-defined constants that name the various error message returns. Output an error on the LCD using the functions of the BDP (Board Development Package). After you output the error, do an infinite loop that will stop your application. To check if your error message works change the priority of the last task to that of another task already created. Do not forget after this test to change the priority back.

2. Important Files: As discussed in class, some operating systems use copious amounts of memory. Microcontrollers may not have the memory or power to host large operating systems like MSWindows. Real-Time Operating Systems (RTOS) such as MicroC/OS can be customized to limit the footprint taken by the OS. MicroC/OS can be taylored using the os_cfg.h header file. Open this file and study its content to feel comfortable with the intent.



Question 3 : What do the constants that end in "_EN" mean? Provide your interpretation of these constants.

Question 4 : OS_MAX_TASKS seems to limit the number of tasks in your application. There can be up to 254 application tasks for MicroC/OS (two are reserved). Why not simply declare 254 tasks always (you may take a look at pp. 112 and 113 in your ref)?

Question 5 :
OS_MAX_EVENTS limits the number of events in the system. What are events?

The operating system is included in the folder uC/OS-II\Source. Throughout the semester we will be using several of the functions from the API.

The BSP folder stands for Board Support Package and contains pre-made functions that are specific to the Dragon12 EVB. the uC/LCD folder contains the functions for the LCD display.

There are many other folders in the project. Feel free to navigate and inspect the content of various files. However we will only directly use the folders listed above.

3. Your First Task: In this section you will take control of the LCD display.

Requirement 3 : Write a function that displays a string of character name on the LCD. Create two tasks in the AppTaskCreate function that take the function as a parameter. Delay the task function for one second. Select the priorities appropriately by declaring constants in the app_cfg.h header file. In the
AppTaskCreate function declare an array that contains two strings one for your first mane and one for your last name. Use the pointer named data to pass each string to the two tasks. Print your first name on the first line of the lcd with the first task and your last name on the second line of the lcd with your second task.

Requirement 4
: Research the OS system calls: OSTaskSuspend and OSTaskResume. Immediately after you create each of your two tasks suspend them (in the
AppTaskCreate).

Requirement 5 : Modify the SevenSegTestTask so that when the counter reaches 200 the task suspends the LCD_TestTask and the KeypadRdTask and resumes your two tasks to write your name on the LCD display.