Motorola/Freescale HCS12 RTOS Port
banked memory model
Thanks to Uri Kogan for providing the original MC9S12DP256B port.
From FreeRTOS V3.1.1 the HCS12 port supports both the small and banked memory models. This page demonstrates the usage of the banked
memory model on an MC9S12DP256B processor. See the MC9S12C32 RTOS port page
for an example that uses the small memory model.
The port was developed on a M68KIT912DP256
development kit from Freescale (instructions are provided should you wish to use an alternative development board). It uses the
CodeWarrior HC12 Development Tools
from Metrowerks and a P&E Multilink USB BDM interface - both
of which are included in the development kit.
IMPORTANT! Notes on using the Freescale (Motorola) HCS12 RTOS port
Please read all the following points before using this RTOS port.
See also the FAQ My application does not run, what could be wrong?
- Source Code Organisation
- The Demo Application
- Configuration and Usage Details
Source Code Organisation
The FreeRTOS source code download contains the source code for all the FreeRTOS ports.
See the Source Code Organization section for a description of the
downloaded files and information on creating a new project.
The HCS12 CodeWarrior banked memory model demo application project file is located in the FreeRTOS/Demo/HCS12_CodeWarrior_banked directory
and is called RTOSDemo.mcp.
The Demo Application
The FreeRTOS source code download includes a fully preemptive multitasking demo application for the banked mode HCS12 RTOS port.
Demo application hardware setup
The demo application includes tasks that send and receive characters over the serial port. The characters sent by one task
need to be received by another - if any character is missed or received out of sequence an error condition is flagged. A
loopback connector is required on the serial port for this mechanism to operate (simply connect pins 2 and 3 together on
the serial port connector labelled P1).
The demo application utilises the LEDs built onto the prototyping board.
The demo application creates 21 tasks - 20 standard demo application tasks and the idle
task. A further two tasks are periodically dynamically created and deleted by the standard demo 'create' task.
Some simple demo functionality is also included in the idle task by way of an idle task hook. See the
demo application section for details of the standard demo real time tasks.
When executing correctly the demo application will behave as follows:
Building the demo application
The RTOSDemo.mcp project file (located in the FreeRTOS/Demo/HCS12_CodeWarrior_banked directory) should be opened from within
the CodeWarrior IDE.
The project was originally created using the UNIS Processor Expert configuration
utility - resulting in a lot of automatically generated files and folders being included in the project.
The FreeRTOS real time kernel
source files are contained in the 'Source->Kernel Source' project folder, highlighted in red in the diagram below.
The demo application source files are contained in the 'Source->RTOS Demo' project folder highlighted in green
in the diagram below.
CodeWarrior project window showing the RTOSDemo source files
The demo application project contains two build configurations:
- Simulator build
To simulate the RTOSDemo project first select the 'Simulator' configuration from the drop down list highlighted in blue
within the diagram above. Following a successful build selecting Debug from the Project menu will automatically open
the simulator and start a debug session.
- Remote debugging build
The project can be executed and debugged directly on the HCS12 microcontroller using the USB BDM interface. To start
a remote debugging session first select 'P&E ICD' from the drop down list highlighted in blue within the diagram above.
Following a successful build selecting Debug from the Project menu will automatically load the program into the microcontroller
flash memory and start a debug session (assuming you have the target board powered and connected using the provided USB cable!).
As downloaded the MC9S12DP256B demo uses the banked memory model.
To use the banked memory model:
Only the PPAGE bank register is saved as part of the task context.
- BANKED_MODEL must be defined in the file FreeRTOS/Demo/HCS12_CodeWarrior_banked/FreeRTOSConfig.h.
- The CodeWarrior project must be configured to use the banked memory model.
Please note: The RTOSDemo.mcp project defines most memory banks as being only 256 bytes long. This is done solely to
force code to reside in different banks, and therefore test the RTOS kernel bank switching code. Users should modify this
memory map as necessary for their applications.
RTOS port specific configuration
Configuration items specific to this port are contained in FreeRTOS/Demo/HCS12_CodeWarrior_banked/FreeRTOSConfig.h. The
constants defined in this file can be edited to suit your application. In particular - the definition configTICK_RATE_HZ is
used to set the frequency of the RTOS tick. The supplied value of 1000Hz is useful for testing the RTOS kernel functionality
but is faster than most applications require. Lowering this value will improve efficiency.
Each port #defines 'BaseType_t' to equal the most efficient data type for that processor. This port defines
BaseType_t to be of type char.
Note that vPortEndScheduler() has not been implemented.
Interrupt service routines
The interrupt vector table is contained within the Vectors.c source file. Vectors.c was originally
created by the Processor Expert utility - but then manually modified to tailor it for use with the real time kernel and
the demo application. If the Processor Expert is used again it will recreate the file and overwrite these modifications.
It is therefore advisable to keep Vectors.c write protected and only update it manually.
An interrupt service routine that does not cause a context switch has no special requirements and can be written as per the
normal CodeWarrior syntax.
Often you will require an interrupt service routine to cause a context switch. For example a serial port character being
received may wake a high priority task that was blocked waiting for the character. If the ISR interrupted a lower priority
task then it should return immediately to the woken task. A macro portTASK_SWITCH_FROM_ISR() is provided to permit
this functionality. Note that portTASK_SWITCH_FROM_ISR() should only ever be used at the very end of an ISR and only when
the ISR does not declare any non static local variables. If the ISR does use local variables then a call to portYIELD() can
be used in place of the portTASK_SWITCH_FROM_ISR() macro.
example here is the function vButtonPush() from the demo application:
/* ISR connected to PP0 button input. */
void interrupt vButtonPush( void )
static unsigned BaseType_t uxValToSend = 0;
/* Send an incrementing value to the button push
task each time the button is pushed. */
/* Clear the interrupt flag. */
PIFP = 1;
/* Send the incremented value down the queue. The
button push task is blocked waiting for the data.
As the button push task is high priority it will
wake and a context switch should be performed before
leaving the ISR. */
if( xQueueSendFromISR( xButtonQueue, &uxValToSend, pdFALSE ) )
/* Posting the message caused a higher priority
task to unblock. This is the end of the ISR so
we can perform the task switch here. This can be
used as the only local variable is static. */
See the interrupt function vCOM0_ISR() within FreeRTOS/Demo/HCS12_CodeWarrior_banked/serial/serial.c for a full
example that uses local stack variables.
Switching between the pre-emptive and co-operative RTOS kernels
Set the definition configUSE_PREEMPTION within FreeRTOS/Demo/HCS12_CodeWarrior_banked/FreeRTOSConfig.h to 1 to use pre-emption or 0
to use co-operative.
As with all the ports, it is essential that the correct compiler options are used. The best way to ensure this is to base your
application on the provided demo application project file - as described in the
Source Organization section.
The SWI instruction is utilised by the RTOS kernel and cannot be used by application code.
The configuration of the timer used to generate the RTOS tick can be viewed in the TickTimer.c Processor Expert
generated source file.
FreeRTOS/Source/Portable/MemMang/heap_2.c is included in the HCS12 demo application project to provide the
memory allocation required by the RTOS kernel.
Please refer to the Memory Management section of the API documentation for
Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2013 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd..
See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.