OSs Micrium Learning+Centre Application+Notes Micrium - Application - Notes - 17
OSs Micrium Learning+Centre Application+Notes Micrium - Application - Notes - 17
µC/OS-II
μC/Probe
and
The Microchip dsPIC33
(Using the Explorer 16 Evaluation Board)
Application Note
AN-1016
www.Micrium.com
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
Table Of Contents
1.00 Introduction 3
1.01 Port Specific Details 4
1.02 µC/Probe 5
1.03 Directories and Files 8
1.04 MPLab IDE 11
2.00 Example Code 13
2.01 Example Code, app.c 13
2.02 Example Code, app_cfg.h 16
2.03 Example Code, includes.h 16
2.04 Example Code, os_cfg.h 16
2.05 Example Code, OS-Probe.* 16
3.00 Board Support Package (BSP) 17
3.01 Board Support Package, p33FJ256GP710.gld 17
3.02 Board Support Package, bsp*.* 18
Licensing 22
References 22
Contacts 22
2
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
1.00 Introduction
This document shows example code for using µC/OS-II and µC/Probe on a Microchip
dsPIC33 processor. To demonstrate the dsPIC33, we used a Microchip Explorer 16 Evaluation
Board as shown in Figure 1-1. µC/LCD is used to drive the Hitachi complaint LCD controller.
We used the Microchip MPLab IDE and C30 Compiler Tools to demonstrate this application. The
software versions used were MPLAB v8.02, C30 v3.01, and ASM v3.01. µC/OS-II has also been
ported to the Hi-Tech dsPICC and IAR iccDSPIC compilers. Support for IAR EWDSPIC is also
available.
ICD2
Connector
dsPIC33
RS232 Connection to running at 40MHZ
µC/Probe
Default Baud 38,400 BPS
8 LEDs
Power (+9Vdc)
The application code is downloaded into Flash using an ICD2 or Real ICE in ‘Programming
Mode’. When the application is started, the 8 onboard LED’s switch on and off from left to right
producing a scrolling like effect. Additionally, the LCD screen is initialized and displays various
messages thoughout the run-time of the application.
3
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
Furthermore, in order to utilize all of the onboard LEDs, it is critical that the optional JTAG port be
disabled. This can be done from the configuration bit screen within MPLab, via software during
runtime, or via Macro in source code. Each compiler has different requirements for setting the
configuration bits. In most cases, the configuration bits are set via macro from the top of BSP.H.
However, some circumstances require the use of the MPLAB configuration bits as an alternative.
Both methods should not be used simulataneously.
The OS port makes use of the on chip PLL. In order to select the XT/HS/EC with PLL oscillator
source, an MPLab macro has been used at the top of BSP.C.
The OS port assumes the availability of Timer number 2, a B-type timer for use with the OS
Ticker. If your application absolutely requires this timer, then you may optionally select Timer
number 4, also a B-type timer, for use with the OS Ticker by changing BSP_OS_TMR_SEL in
BSP.H from 2 to 4.
ISR handlers are defined using predefined compiler specific keyword and names such that
MPLab automatically populates the vector table upon loading the executable into flash memory.
Every vendors compiler handles vector table population differently. Users should not use IDE or
compiler specific keywords in order to create ISRs written entirely in C unless the necessary
steps to ensure that the OS is properly informed of the interrupt are taken. This usually involves
inline assembly. A better method is to populate the ISR table with the address of a first level ISR
handler written in assembly which calls a user specified ‘C’ code handler function. An example
assembly ISR may be copied from the OS tick ISR with minimal changes. All ISRs should be
written as specified in the dsPIC33 OS port application note, see AN-1033.
Lastly, µC/Probe requires a free running timer for making time measurements. Since the
dsPIC33 does not provide a free-running timer mode, the example described herein cannot share
a timer with the µC/OS-II Ticker. As a result, a second timer is configured from BSP.C with
interrupts disabled and a Period register value of 0xFFFF. This simulates a free-running timer
that can be utilized by µC/Probe without having any effect on the µC/OS-II Tick interrupt
source.
4
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
1.02 µC/Probe
µC/Probe is a Microsoft Windows program that displays the content of system variables on
various user definable graphical elements such as simulated mechanical counters, graphs,
on-screen LEDs and so on.
In order for µC/Probe to display information about your application, an ELF file, must be
generated by the user’s compiler. The ELF file contains the names and addresses of all the
global symbols referenced within the users embedded application. Only symbols that have been
allocated memory, e.g. not allocated on the stack, are able to be monitored by µC/Probe. Global
and static variables are examples of variables that may be monitored.
The user places components (such as gauges, labels, and charts) into a Data Screen in a
µC/Probe workspace. Each one of these controls is then assigned to one or more of the
variables from the Symbol Browser. The Symbol Browser lists all symbols referenced from within
the ELF file. Symbols associated with components placed on an open Data Screen will be
updated after the user presses the start button (assuming the user’s PC is connected to the
target and the target is running).
µC/Probe currently interfaces with a target processor via JTAG, RS-232, UDP and USB. A
small section of code resident on the target receives commands from the Windows application
and responds to those commands. The commands ask for a certain number of bytes located at a
certain address, for example, “Send 16 bytes beginning at 0x0040102C”. The Windows
application, upon receiving the response, updates the appropriate component(s) on the data
screen(s) with the new values.
5
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
Data Screen.
Components are placed onto the data screen and
assigned symbols during Design View. During
Run-Time View, these components are updated
with values of those symbols from the target
Symbol Browser.
Contains all symbols from the ELF
files added to the workspace.
To use µC/Probe with the example project (or your application), do the following:
http://www.micrium.com/products/probe/probe.html
6
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
2. Open µC/Probe. After downloading and installing this program, open the example
µC/Probe workspace for µC/OS-II, named OS-Probe.wsp, which should be located in
the AN-1208 Codewarrior project directory.
You may also open one of the sample workspaces that comes with µC/Probe. The
sample workspaces, located in the µC/Probe target directory, contains generic
workspaces for µC/OS-II, as well as other Micrium software modules.
3. Connect Target to PC. Currently, µC/Probe can use RS-232 to retrieve information
from the target. You should connect a RS-232 cable between your target and computer.
Load Your ELF File. The example projects included with this application note are
already configured to output an ELF file. (If you are using your own project, please refer
to Appendix A of the µC/Probe user manual for directions for generating an ELF file with
your compiler.) Codewarrior generates an ELF file with a .abs extension. This file is
located in a directory named BIN within the sample project directory.
To load this ELF file, right-click on the symbol browser and choose “Add Symbols”.
Navigate to the file directory, select the file, and choose “OK”.
4. Configure the RS-232 Options. In µC/Probe, choose the “Options” menu item on the
“Tools” menu. A dialog box as shown in Figure 6-2 (left) should appear. Choose the
“RS-232” radio button. Next, select the “RS-232” item in the options tree, and choose the
appropriate COM port and baud rate. The baud rate for the projects accompanying this
application note is 38,400 baud.
5.
6. Start Running. You should now be ready to run µC/Probe. Just press the run button
to see the variables in the open data screens update.
7
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
µC/OS-II:
\Micrium\Software\uCOS-II\Source
This directory contains the processor independent code for µC/OS-II. The version used
was 2.86.
\Micrium\Software\uCOS-II\Ports\Microchip\PIC33FJ256\C30
This directory contains the standard processor specific files for a µC/OS-II port
assuming the Microchip MPLab IDE and C30 Compiler Tools. In fact, these files could
easily be modified to work with other tool chains. However, you would place the modified
files in a different directory. Specifically, this directory contains the following files:
os_cpu.h
os_cpu_a.s
os_cpu_c.c
os_cpu_util.s
os_dbg_c
µC/OS-II ports for the Hi-Tech iccDSPIC and IAR dsPICC compilers are available upon
request.
µC/Probe:
\Micrium\Software\uC-Probe\Target\Communication\Generic\OS\uCOS-II
This directory contains the OS dependent interface for the communication layer of
µC/Probe. If you plan to run µC/Probe with a different RTOS, or without any RTOS,
the following files would have to be adjusted accordingly:
probe_com_os.c
\Micrium\Software\uC-Probe\Target\Communication\Generic
\RS-232\OS\uCOS-II
This directory contains OS dependent interface code for the RS-232 specific portion of
µC/Probe, specifically the code necessary to generate an optional Rx packet parse
task. If you plan to run µC/Probe with a different RTOS, modifications to the files listed
below will have to be made. If you are not running an RTOS, the following files may be
excluded from the build.
probe_rs232_os.c
8
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
\Micrium\Software\uC-Probe\Target\Communication\Generic\RS-
232\Ports\Microchip\dsPIC33\C30
This directory contains the µC/Probe hardware port files for the dsPIC33 processor.
probe_rs232c.c
probe_rs232_a.s
\Micrium\Software\uC-Probe\Target\Communication\Generic\RS-232\Source
This directory contains target independent source code for the µC/Probe RS-232
communication layer. Specifically, this directory contains the following files:
probe_rs232.c
probe_rs232.h
\Micrium\Software\uC-Probe\Target\Communication\Generic\Source
This directory contains target independent source code for the µC/Probe
communication layer. Specifically, this directory contains the following files:
probe_com.c
probe_com.h
\Micrium\Software\uC-Probe\Target\Plugins\uCOS-II
This directory contains the target independent source code for µC/Probe. Specifically,
this directory contains the following files:
os_probe.c
os_probe.h
µC/CPU:
\Micrium\Software\uC-CPU\
This directory contains processor independent files for µC/CPU. µC/CPU contains code
for entering and existing critical sections, as well as macro definitions for the ‘C’
programming datatypes used in most Micrium products. Specifically, this directory
includes:
cpu_def.h
\Micrium\Software\uC-CPU\Microchip\PIC33FJ256\C30
This directory contains processor port files for µC/CPU. µC/CPU contains code for
entering and existing critical sections, as well as macro definitions for the ‘C’
programming datatypes used in most Micrium products.
cpu.h
9
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
μC/LIB:
\Micrium\Software\uC-LIB
This directory contains lib_def.h, which provides #defines for useful constants (like
DEF_TRUE and DEF_DISABLED) and macros.
\Micrium\Software\uC-LIB\Doc
This directory contains the documentation for μC/LIB.
Application Code:
\Micrium\Software\Evalboards\Microchip\Explorer16\PIC33FJ256
\MPLAB-C30\OS-Probe
This directory contains the application source code and MPLAB project files for this
application note. The following files are located within the above directory:
app.c
app_cfg.h
app_hooks.c
app_probe.c
includes.h
os_cfg.h
OS-Probe.*
Prob_com_cfg.h
includes.h contains a master include file used by the application, os_cfg.h is the
µC/OS-II configuration file and probe_com_cfg.h contains user modifiable
configuration constants for µC/Probe. Lastly, OS-Probe.* are the Microchip MPLab
IDE and C30 Compiler Tools project files.
\Micrium\Software\Evalboards\Microchip\Explorer16\PIC33FJ256
\MPLAB_C30\BSP
This directory contains the Board Support Package for the Explorer16 evaluation board
and the dsPIC33 MCU. While some of the code in this directory may work on other
dsPIC33 derivatives, routines that are hardware dependent such as LED_On() will
require modification depending on the hardware design of your EVB. This directory
contains:
bsp.c
bsp.h
bsp_a.s
bsp_lcd.c
p33FJ256GP710.gld
p33FJ256GP710.h
p33FJ256GP710.inc
10
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
11
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
12
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
\Micrium\Software\Evalboards\Microchip\Explorer16\PIC33FJ256
\MPLAB_C30\OS-Probe
It should be noted that the file p33FJ256GP710.gld (BSP directory) is critical for linking the
example code to the correct address ranges suitable for the dsPIC33. This file should not need to
be modified.
BSP_IntDisAll(); (2)
OSInit(); (3)
OSTaskCreateExt(AppStartTask, (4)
(void *)0,
(OS_STK *)& AppStartTaskStk[APP_START_TASK_STK_SIZE - 1],
APP_START_TASK_PRIO,
APP_START_TASK_PRIO,
(OS_STK *)&AppStartTaskStk[0],
APP_START_TASK_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
OSStart(); (6)
}
L2-1(2) We start off by calling a BSP function (see bsp.c) that will disable all interrupts. We
do this to ensure that initialization doesn’t get interrupted in case we do a ‘warm
restart’.
L2-1(3) As will all µC/OS-II applications, you need to call OSInit() before creating any
task or other kernel objects.
13
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
L2-1(4) We then create at least one task (in this case we used OSTaskCreateExt() to
specify additional information about your task to µC/OS-II). It turns out that
µC/OS-II creates one and possibly two tasks in OSInit(). As a minimum,
µC/OS-II creates an idle task (OS_TaskIdle() which is internal to µC/OS-II) and
OS_TaskStat() (if you set OS_TASK_STAT_EN to 1 in OS_CFG.H).
OS_TaskStat() is also an internal task in µC/OS-II.
L2-1(5) As of V2.6x, you can now name µC/OS-II tasks (and other kernel objects) and be
able to display task names at run-time or, with a debugger. In this case, we name
our first task ‘Start Task’.
L2-1(6) We finally start µC/OS-II by calling OSStart(). µC/OS-II will then start executing
AppStartTask() since that’s the highest priority task created. OSStart() does
not return.
BSP_Init(); (1)
AppTaskCreate(); (4)
LED_Off(0); (5)
14
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
}
}
L2-2(1) BSP_Init() is called to initialize the Board Support Package – the I/Os, the tick
interrupt, and so on. BSP_Init() will be discussed in the next section.
L2-2(2) OSStatInit() computes how fast the CPU runs when OS_TASK_STAT_EN is set
to 1 in os_cfg.h.
L2-2(3) Initialize µC/Probe. Probe communication port settings may be adjusted from within
probe_com_cfg.h as well as app_probe.c.
L2-2(4) AppTaskCreate() is a user defined function for creating additional µC/OS-II tasks.
This function is not required and additional tasks could have been created directly
within AppStartTask(). This function has been used to create the AppLCDTask()
which drives the LCD for this example.
L2-2(6) As with all task managed by µC/OS-II, the task body must be in the form of an
infinite loop. Tasks managed by µC/OS-II must never be allowed to exit. Instead,
tasks should be deleted using OSTaskDel() when they are no longer desired. This
task performs the LED illumination and scrolling effect used within this application.
L2-2(7) As µC/OS-II tasks must either enter an infinite loop ‘waiting’ for some event to occur
or terminate itself. In this case, we wait for time to expire as the ‘event’. This is
accomplished by calling OSTimeDlyHMSM().
15
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
The reason this is done here is to make it easier to configure your application from a single file.
16
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
The BSP for the Explorer 16 Evaluation Board is found in the follow directory.
\Micrium\Software\EvalBoards\Microchip\Explorer16\PIC33FJ256
\MPLAB_C30\BSP
bsp.c
bsp.h
bsp_a.s
bsp_lcd.c
p33FJ256GP710.gld
p33FJ256GP710.h
p33FJ256GP710.inc
17
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
L3-1(1) This macro disables watchdog timer capabilities of the PIC24. This capability may
be enabled, however, the user application must take responsibility for clearing the
watchdog timer periodically.
L3-1(2) This macro selects the primary oscillator and PLL as the dsPIC33 core clock source.
Your application code must call BSP_Init() to initialize the BSP. BSP_Init() in turn calls
other functions as needed.
L3-2(1) This function initializes the on chip PLL by setting the multiplier and divider, then
enabling and switching to the PLL.
L3-2(2) We then call LED_Init() to initialize the onboard LED I/O Pins as outputs.
18
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
L3-3(1) Get the CPU operating frequency in Hz in order to calculate the correct number of
timer increments for the desired OS Tick rate.
L3-3(2) Compute the number of timer increments necessary to generate the desired OS Tick
rate.
L3-3(4) Set default settings for the timer. Stop the timer, set the timer to derive its clock from
Fcy (PLL Output), configure the timer with a prescaler of 1, and set the timer to
operate in 16 bit mode.
L3-3(6) Set the compare value that the counter will increment to before generating a match
interrupt. This was computed in L3-2(2).
L3-3(7) Clear the interrupt priority bits so that the correct priority may be written in L3-2(9).
L3-3(12) Perform the same procedure for timer #4 instead of timer #2 If BSP_OS_TMR_SEL in
BSP.H is defined as 4.
19
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
#if BSP_OS_TMR_SEL == 4
IFS1 &= ~T41F;
#endif
OSTimeTick(); (2)
}
This function is called from an assembly interrupt service routine which properly informs
µC/OS-II of the interrupt condition, calls the interrupt handler, and clears the interrupt source.
See AN-1033 and the file description BSP_A.S for more details.
The on chip timer generates a time tick by letting the up-counter run from 0x0000 to the
computed match value configured in Tmr_TickInit(). After an interrupt is generated, the
counter resets to zero and continues counting until the next match occurs.
0x0000
1 Millisecond with
OS_TICKS_PER_SEC set to 1000
20
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
When the selected Timer issues an interrupt, the processor vectors to __T2Interrupt() or
__T4Interrupt() depending on which timer is enabled for use with the OS Ticker. These ISR
functions both call Tmr_TickISR_Handler() as described above in Listing 3-3. Only 1 timer
for the OS Ticker may be enabled at a time.
You should note that ALL of your ISRs should be written in assembly where OS related
processing may take place before calling an interrupt handler function of the form ‘interrupt
void MyISR_Handler(void)’ Refer to AN-1033 for details.
21
Micriµm
µC/OS-II and µC/Probe for the Microchip dsPIC33
Licensing
If you intend to use μC/OS-II in a commercial product, remember that you need to contact
Micriμm to properly license its use in your product. The use of μC/OS-II in commercial
applications is NOT-FREE. Your honesty is greatly appreciated.
References
MicroC/OS-II, The Real-Time Kernel, 2nd Edition
Jean J. Labrosse
CMP Technical Books, 2002
ISBN 1-5782-0103-9
Contacts
CMP Books, Inc.
6600 Silacci Way
Gilroy, CA 95020 USA
Phone Orders: 1-800-500-6875
or 1-408-848-3854
Fax Orders: 1-408-848-5784
e-mail: rushorders@cmpbooks.com
WEB: http://www.cmpbooks.com
Micriµm
949 Crestview Circle
Weston, FL 33327
USA
954-217-2036
954-217-2037 (FAX)
e-mail: Jean.Labrosse@Micrium.com
WEB: www.Micrium.com
22