Notes

Powered by The LE Company

Tuesday, July 19, 2011

Porting Real Time OS "FreeRTOS" to AT91SAM7S256 Evaluation board

Le Trung Thang 2011 
Preparation.
Hardware:  
AT91SAM7S Evaluation Board or OLIMEX SAM7-P256 Development Board (www.olimex.com).
-  Segger J-link emulator (www.segger.com).
Software:
-  IAR Embedded workbench for ARM (www.iar.com).
-  FreeRTOS  RTOS (www.freertos.org).
To get FreeRTOS , you can either download from www.freertos.org directly or  download the demo project which included FreeRTOS v7.01. Thank to Mr. Richard Barry, author of FreeRTOS.

This article will conduct you how to port FreeRTOS to AT91SAM7S board. Basic about Evaluation Board and IAR Embedded Workbench for ARM, you can reach at link “Getting started IAR Embedded Workbench for ARMwith AT91SAM7S256”.
There is also assuming that you have had basic knowledge about Real Time OS.


FreeRTOS is a scale-able real time kernel designed specifically for small embedded systems. FreeRTOS supports a wide range of different CPUs: 8051, AVR, ARM,…
After download and unzip FreeRTOS, you will get source code of FreeRTOS in directory FreeRTOS.
The most important folder you need note is Source where stores all source file of FreeRTOS.
The first, to port FreeRTOS RTOS to the board, you will need to pay attention in the files following:
board_cstartup_iar.s : this file stores start-up code and interrupt handler of system.
portasm.s79 : this file is a part of the FreeRTOS, it stores the snippy code in Assembly language for context switching of the OS Scheduler.
ISR_Support.h : this file associates for file portasm.s79.
Port.c : this file stores interrupt handler of  timer which is used by the OS to generate timer ‘Tick’.
FreeRTOSConfig.h : this stores macros to configure OS operation.

To find place of above files, go to directory FreeRTOS-getting-started and open file FreeRTOS-getting-started.eww to start the IDE. All above file were already opened in the IDE.
Now, we will go to the detail of these files.

a). board_cstartup_iar.s :  This file stores the section code for start-up system and interrupt processing. As in previous article: “Getting started IAR Embedded Workbench for ARM with AT91SAM7S256”, we studied deeply about start-up code and interrupt processing in a non-OS program. However, we cannot use this start-up code file for a within-OS program. In a non-OS program, when an interrupt happens, the program will be stopped at position it is executing to jump to the interrupt handler. After finished the interrupt handler, program will go back position where it was interrupted previously to continue executing the next instructions.
However, in a program has the Real Time Operating System, Everything is not simply as such. In a within-OS program, OS will use some timer interrupt for operation of OS Scheduler, in our project that is PIT interrupt. When a PIT interrupt happens, a context switching need to be considered, program executing will be pended and program jumps to PIT interrupt handler which is either vPortPreemptiveTick or vPortNonPreemptiveTick depending on your configuration.  After finished the interrupt service, the program may doesn’t return previous position where the program was interrupted. This doesn’t comply with a C/C++ standard program. Due to this reason, we cannot use the interrupt handler in board_cstartup_iar.s as used to do with the non-OS program previously. Instead, we will jump directly to interrupt handler supplied by FreeRTOS is vPortPreemptiveTick. So, we need modify the line:
LDR PC, = irqHandler   to  LDR PC, [PC, #-0xF20].
Moreover, the context switching can occur by software. Program can force a context switching by call portSWITCH_CONTEXT, this causes a software interrupt is called. And which handler services for this event will be vPortYieldProcessor. So, we need add a code line for software interrupt vector:
LDR  PC, =vPortYieldProcessor.
Finally, before go to the main function, CPU must be in SuperVisor mode as on demand of FreeRTOS, so we add instruction to change mode of CPU to Supervisor mode.  
MSR     cpsr_c,  #ARM_MODE_SVC | I_BIT | F_BIT.
The sequence diagram of context switching mechanic is as figure 1. Assuming that there are 2 tasks are running.

Fig. 1 Context switch mechanism


b). portasm.s79 : this file and file ISR_Support.h store  all interrupt handler which were used by the OS for scheduling. General, we don’t need modify these files. However, we configured the AIC operate on Protect mode. So we need add some code lines to support protect mode of AIC. In file portasm.s79,  we add :
LDR R14, =AT91C_BASE_AIC;
STR     R14, [R14, #AIC_IVR];  // support protect mode of AIC

All another interrupt handlers in future if have, must add this snippy code at end of interrupt handler. However, instead writes in Assembly language, you can write in C/C++ style as:
AT91C_BASE_AIC->AIC_IVR = 0xFFFFF000;

c). Port.c : When OS Scheduler start, It calls prvSetupTimerInterrupt to setup hardware for timer “Tick”. In our board, we used PIT module for generation the timer “Tick”. So, we need modify function prvSetupTimerInterrupt. In function prvSetupTimerInterrupt, we configured to PIT triggers each every 500ms, hence 1 Tick = 500ms.

d). FreeRTOSConfig.h : this file is supplied by OS. It is used to configure CPU operation and other components of OS. Because our CPU run at speed 48Mhz, so, we will declare in macro configCPU_CLOCK_HZ  = 48000000.

After modified above files, we ported complete FreeRTOS to Evaluation board. And so now, you can begin to write an application with supporting of FreeRTOS.
Note: Because we don’t use interrupt handler irqHandler in section .vectors which declared in file board_cstartup_iar.s (line 108). So, this code is redundant. I tried to reuse it but wasn’t successful.

= = = =***= = = =***= = = =***= = = =
FreeRTOS is Copyright (C) Real Time Engineers Ltd.
IAR Embedded Workbench for ARM is Copyright (C) IAR Systems AB.

No comments: