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 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.

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.

Functionality

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:

LEDs D0, D1 and D2 are under control of the 'flash' tasks. Each will flash at a constant frequency, with LED D0
being the fastest and LED D2 being the slowest.

When the loopback connector is in place every character transmitted by the ComTest Tx task is received by the ComTest
Rx task. Each transmitted character causes LED D3 to toggle, each correctly received character causes LED D4 to
toggle. LED D4 will only toggle if the received character matches the character the ComTest Rx task expected to receive.

Not all the tasks update an LED so have no visible indication that they are operating correctly.
Therefore a 'Check' task is created whose job it is to ensure that no errors have been detected in any of the other tasks.

LED D7 is under control of the 'Check' task. Every three
seconds the 'Check' task examines all the tasks in the system to ensure they are executing without error. It
then toggles LED D7. If LED D7 is toggling every three seconds then no errors have ever been detected.
The toggle rate increasing to 500ms indicates that the 'Check' task has
discovered at least one error.

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!).

Memory Model

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.

Only the PPAGE bank register is saved as part of the task context.

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.

As an
example here is the function vButtonPush() from the demo application:

/* ISR connected to PP0 button input. */
void interrupt vButtonPush( void )
{
static UBaseType_t uxValToSend = 0;
/* Send an incrementing value to the button push
task each time the button is pushed. */
uxValToSend++;
/* 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. */
portTASK_SWITCH_FROM_ISR();
}
}

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.

Compiler options

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.

SWI instructions

The SWI instruction is utilised by the RTOS kernel and cannot be used by application code.

Timer usage

The configuration of the timer used to generate the RTOS tick can be viewed in the TickTimer.c Processor Expert
generated source file.

Memory allocation

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
full information.