home

=//Contents//= =**__ 1.Introduction :- __**= =**__ 2. __****__ Modification required to Convert existing freeRTOS Demo (msp430f5438) to use it for __****__ msp430f5435 :- __**= =**__ 3.Some simple tests and results :- __**= //(To check whether freeRTOS has been successfully ported)// =**__ 4.Demo of test1:- __**= =__**5.How can freeRTOS be used for Enviromote project :-**__= =__ **6.Some basic freeRTOS concept :-** __= = =

 This freertos development for msp430f5435 is aiming to assist an ongoing project ‘Enviromote – Wireless Pollution Monitoring’ (a project concerned about pollution monitoring at different places of in Bangalore, [] ) in IISc.  My journey started without any clear idea like how to proceed, just it was known that I have to find some type of Embedded OS which is able to do multitasking like a tinyOS. Initially I searched about many different Embedded OS’s to decide which is suitable for our project, I found many interesting concepts about tinyOS (since I was heard of only this OS at very beginning) but due to unavailability of tinyOS for any of msp430f54xx, I just gave up the idea of making a tinyOS from scratch, then similar story with ucOS. Finally in 3-4 days, I came to know about demo application of freeRTOS which was available for msp430f5438 and hence I started to work on it.
 * __Introduction :- __**

====FreeRTOS package along with demo application is available on internet for almost every type of specific microcontroller (concerning almost all type of development tool) and source code can be downloaded from download section of [|www.freertos.org] . The available demo application is for only a specific hardware platform of a specific microcontroller (prototyping or evaluation board) like in case of msp430 micontroller using IAR workbench, the demo application is available only for msp430f5438. In our ‘Enviromote’ project, we are using msp430f5435 evaluation board, so I modified it to make it compatible with msp430f5435. ====
 * __Modification required to Convert an existing freeRTOS Demo (msp430f5438) to use it for msp430f5435 :- __**

So this section documents the FreeRTOS demo application for the MSP430f5435 microcontroller. We are using the IAR Embedded Workbench for MSP430 from IAR Systems. According to officials It is recommended not to attempt to open the demo project in an IAR Embedded Workbench version that is earlier than V5.20 as to do so can silently corrupt the project file. Here I am using IAR V7.1.0 and all the tests are done in this version only.

 Here are some simple steps to modify the existing freeRTOS.  The very first task is to download the freeRTOS package from freeRTOS site ( [|www.freeRTOS.org] ). First check you can successfully compile the existing demo application exactly as downloaded - before making any modifications. In most cases the demo application would compile without any errors or warnings.

 There is a function in the package, is defined like :-
 * STEP 1 :- Modification in partest.c **

void vParTestInitialise( void ) {

/* The port used by the two LEDs is configured from halBoardInit so <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">nothing needs to be done here. */

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Here the function halBoardInit has not been called, so call it. This function basically configures the GPIO ports and some other peripheral ports. So replace this with following code.

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void vParTestInitialise( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halBoardInit; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> The experimenter board msp430f5438 have a lcd screen as a peripheral. These files just defines some lcd parameters and lcd functions. Our board (msp430f5435) doesn’t have any lcd, so no need to include these files.
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">STEP 2 :- Remove hal_lcd.c and hal_lcd_fonts.c files from project. **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> There is a function in the package, is defined like :-
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">STEP 3 :- Modification in main.c **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static void prvSetupHardware( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/* Convert a Hz value to a KHz value, as required by the Init_FLL_Settle <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">function. */

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">unsigned long ulCPU_Clock_KHz = ( configCPU_CLOCK_HZ / 1000UL ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halBoardInit; seting up the port pins configuaration <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">LFXT_Start( XT1DRIVE_0 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Init_FLL_Settle( ( unsigned short ) ulCPU_Clock_KHz, 488 ); /* <span style="color: black; font-family: Arial,sans-serif;">setting up the clock system*/

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halButtonsInit( BUTTON_ALL ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halButtonsInterruptEnable( BUTTON_SELECT );

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/* Initialise the LCD, but note that the backlight is not used as the <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">library function uses timer A0 to modulate the backlight, and this file <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">defines vApplicationSetupTimerInterrupt to also use timer A0 to generate <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">the tick interrupt. If the backlight is required, then change either the <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halLCD library or vApplicationSetupTimerInterrupt to use a different <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">timer. Timer A1 is used for the run time stats time base6. */

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halLcdInit; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halLcdSetContrast( 100 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halLcdClearScreen; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halLcdPrintLine( " www.FreeRTOS.org", 0, OVERWRITE_TEXT );

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Here in this function also every peripheral port related to our demo application is being initialized. In the existing demo application package, some functions related to lcd and joystick peripheral have been initialized, which haven’t any use to us. So just comment them out.

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Now also comment Init_FLL_Settle( ( unsigned short ) ulCPU_Clock_KHz, 488 ); function as well. I don’t know the exact reason behind it, I come to know to this point by executing and experimenting several times. I searched about it but couldn’t find any clue about this function. The following tests give positive results even without this function. So at that time I just skipped to search further about this function and just commented it out for time being. Now I’ll set it right as soon as I’ll get to know the details about this function.

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Hence effectively prvSetupHardware functions looks like :-

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static void prvSetupHardware( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">halBoardInit; /* seting up the port pins configuaration*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">LFXT_Start( XT1DRIVE_0 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Note:- This function prvSetupHardware still is not fully correct. Some demo application may require the use of Init_FLL_Settle( ( unsigned short ) ulCPU_Clock_KHz, 488 ); function.

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Here a function named void LFXT_Start(uint16_t xtdrive) is defined which sets Startup routine for 32kHz Crystal on LFXT1. This is like
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">STEP 4 :- Modification in hal_UCS.c **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void LFXT_Start(uint16_t xtdrive) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">UCSCTL6_L |= XT1DRIVE1_L+XT1DRIVE0_L; //<span style="color: black; font-family: Arial,sans-serif;">Highest drive setting for XT1 startup // /*check OFIFG fault flag*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">while (SFRIFG1 & OFIFG) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">UCSCTL7 &= ~(DCOFFG+XT1LFOFFG+XT1HFOFFG+XT2OFFG); /*<span style="color: black; font-family: Arial,sans-serif;">Clear OSC flaut Flags fault flags*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">SFRIFG1 &= ~OFIFG; /*<span style="color: black; font-family: Arial,sans-serif;">Clear OFIFG fault flag*/ //<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">} // <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">UCSCTL6 = (UCSCTL6 & ~(XT1DRIVE_3)) //<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> |(xtdrive); //<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/* set Drive mode*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Here comment out the whole while loop thing. Again this problem is related to the problem we encountered in STEP 3. So it is just a temporary solution, need to set it right. Here set the parameters related to unified clock system like Highest drive setting for XT1 startup and to set Drive mode. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Hence our function will look similar to this :-

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void LFXT_Start(uint16_t xtdrive) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">UCSCTL6_L |= XT1DRIVE1_L+XT1DRIVE0_L; <span style="color: black; font-family: Arial,sans-serif;">Highest drive setting for XT1 startup <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">UCSCTL6 = (UCSCTL6 & ~(XT1DRIVE_3)) |(xtdrive); set Drive mode <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Note :- Same problem (which we encountered in STEP 3) goes here also.

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> This step have two parts <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">I. Modification in hal_board.h <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">II. Modification in hal_board.c
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">STEP 5 :- Modification in hal_board.h and hal_board.c **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> __Modification in hal_board.h :-__ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> In this header file, port related to system clock are configured. In case of msp430f5438 11th port have peripheral function of system clock (P11.x, x = pin 1, 2, 3) but msp430f5435 have only eight ports. On msp430f5435 microcontroller, the MCLK and ACLK pins are on port 2 (P2.0 and P2.6 respectively) and SMCLK is present on port 1 (P1.6). <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Thus the changes are as follows :- <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> The definitions are given as -

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_PORT_DIR P11DIR <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_PORT_OUT P11OUT <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_PORT_SEL P11SEL

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> After modifying these definitions should look like this –

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_ACLK_MCLK_DIR P2DIR <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_SMCLK_DIR P1DIR

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_ACLK_MCLK_OUT P2OUT <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_SMCLK_OUT P1OUT

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_ACLK_MCLK_SEL P2SEL <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define CLK_SMCLK_SEL P1SEL

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> __Modification in hal_board.c :-__ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Make the corresponding changes in hal_board.c file also. There exists two functions related clock peripherals which are –

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void halBoardOutputSystemClock(void) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_PORT_DIR |= 0x07; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_PORT_SEL |= 0x07; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void halBoardStopOutputSystemClock(void) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_PORT_OUT &= ~0x07; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_PORT_DIR |= 0x07; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_PORT_SEL &= ~0x07; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Now they will look like –

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void halBoardOutputSystemClock(void) /* <span style="color: black; font-family: Arial,sans-serif;">outputs clock to testpoints*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_ACLK_MCLK_DIR |= 0X41; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_SMCLK_DIR |= 0X40;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_ACLK_MCLK_SEL |= 0X41; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_SMCLK_SEL |= 0X40; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void halBoardStopOutputSystemClock(void)

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_ACLK_MCLK_OUT &= ~0X41; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_SMCLK_OUT &= ~0X40;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_ACLK_MCLK_DIR |= 0X41; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_SMCLK_DIR |= 0X40;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_ACLK_MCLK_SEL &= ~0X41; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">CLK_SMCLK_SEL &= ~0X40; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Note :- All the changes related to hardware setup have been made in these steps (STEP 1 to STEP 5). Now the following changes are just for the sake of the demo application.

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> __In hal_board.h__ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> If you are using 3 led’s for GPIO test then define third led in this header file –
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">STEP 6 :- Modification for test 1 (GPIO test) **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">#define LED_3 BIT2

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> __In Partest.c__ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Define case 2 in function void vParTestToggleLED( unsigned portBASE_TYPE uxLED) as follows –

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">case 2: if( ( LED_PORT_OUT & LED_3 ) == 0 ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">LED_PORT_OUT |= LED_3; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">} <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">else <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">LED_PORT_OUT &= ~LED_3; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">} <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">break;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Note :- If port 1 and port 2 are engaged in some other peripheral application then define these leds on port 4.


 * __<span style="color: black; font-family: Arial,sans-serif; font-size: 16pt;">Some simple tests and results :- __**
 * //<span style="color: black; font-family: Arial,sans-serif;">(To check whether freeRTOS has been successfully ported) //**

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> To blink 3 leds with 3 different frequencies simultaneously. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> There are two methods of calling a function of created the task. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">I. Using prototype macros and making separate .c file <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">II. Directly defining the function in main.c file and creating the task in main function
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">1. Multitasking test at GPIO ports (flash test) :- **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> The first method has been used by freertos package. There exists a flash.c file in package, so just call the function vStartLEDFlashTasks in our main function. This function creates three tasks of toggling the three leds with three different frequencies. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Now write a simple program of toggling the leds with general and very crude mechanism to check that the LED outputs are working. This simple program need not make use of FreeRTOS - all that is of interest at this stage is ensuring the LEDs work - so for now comment out the existing main function and replace it with something similar to th //<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">e //<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">following example //<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">: //

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">int main( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">volatile unsigned long ul; /* volatile so it is not optimized away. */ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">prvSetupHardware;/* initialize the led outputs*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/* Toggle the LEDs repeatedly. */ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">for { <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/* We don't want to use the RTOS features yet, so just use a very crude delay mechanism instead. */ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">for( ul = 0; ul < 0xfff; ul++ ) { }

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">vParTestToggleLED( 0 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">vParTestToggleLED( 1 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">vParTestToggleLED( 2 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">} <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">return 0; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Once the LEDs are known to be working the dummy main function can be removed, and the original main function restored. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Now the work of scheduler starts from here. The standard 'flash test' tasks are a set of 3 very simple tasks - each of which toggles a single LED at a fixed frequency, with each task using a different frequency. These tasks should be started within main by a call to the function vStartLEDFlashTasks and simply add the file FreeRTOS/Demo/Common/Minimal/Flash.c to your build. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> Comment out every function that is used to start one or more demo tasks, other than the call to vStartLEDFlashTasks. It is likely that main will then only call three functions:

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void main( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">prvSetupHardware; /*setting the hardware setup*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">vStartLEDFlashTasks; /*<span style="color: black; font-family: Arial,sans-serif;">creating the tasks*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">vTaskStartScheduler; /*Start the scheduler.*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

__<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Alternate:- __ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">This can be done alternatively using 2nd option (Directly defining the function in main.c file and creating the task in main function) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">In this case, create three separate tasks in main function and define corresponding functions in main.c itself. It will look like similar to this -

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Format of creating task is : - <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/*Task to be created*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void vTaskCode( void * pvParameters ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">for <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif;">/*Task code goes here.*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">} <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/*Function that creates a task. */ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void main( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static unsigned char ucParameterToPass; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">xTaskHandle xHandle; <span style="color: black; font-family: Arial,sans-serif;">/*Create the task, storing the handle. Note that the passed parameter ucParameterToPass <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">must exist for the lifetime of the task, so in this case is declared static. If it was just an <span style="color: black; font-family: Arial,sans-serif;">an automatic stack variable it might no longer exist, or at least have been corrupted, by the time <span style="color: black; font-family: Arial,sans-serif;">the new task attempts to access it.*/

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/* Use the handle to delete the task. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">vTaskDelete( xHandle ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">An simple example :- <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Define some parameters in main.c related to our task as :-

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static void vLED1FlashTask( void * pvParameters ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static void vLED2FlashTask( void * pvParameters ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static void vLED3FlashTask( void * pvParameters );

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Creation of the tasks as :- <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void main( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> prvSetupHardware;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xTaskCreate( vLED1FlashTask, ( signed char * ) "LED.1", ledSTACK_SIZE, NULL, 0, ( xTaskHandle * ) NULL ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xTaskCreate( vLED2FlashTask, ( signed char * ) "LED.2", ledSTACK_SIZE, NULL, 0, ( xTaskHandle * ) NULL ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xTaskCreate( vLED3FlashTask, ( signed char * ) "LED.3", ledSTACK_SIZE, NULL, 0, ( xTaskHandle * ) NULL );

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> /* Start the scheduler. */ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> vTaskStartScheduler; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Task code is like this:- <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static void vLED1FlashTask( void * pvParameters ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> { <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> portTickType xFlashRate1, xLastFlashTime; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ( void ) pvParameters; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xFlashRate1 = ledFLASH_RATE_BASE; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xFlashRate1 /= portTICK_RATE_MS; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xFlashRate1 /= ( portTickType ) 2;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xLastFlashTime = xTaskGetTickCount;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> for <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> { <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> vTaskDelayUntil( &xLastFlashTime, xFlashRate1 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> vParTestToggleLED( 0 );

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> vTaskDelayUntil( &xLastFlashTime, xFlashRate1 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> vParTestToggleLED( 0 ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> } <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> }

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">Similarly create other two task (vLED2FlashTask and vLED3FlashTask ) as well.

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> To blink an led and to read and print the digital output of ADC simultaneously. The function corresponding to first task may be vLED1FlashTask and second function need to be created like this :- static void vADCtask( void * pvParameters );
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">2. GPIO and ADC multitasking test :- **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void main( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> prvSetupHardware; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xTaskCreate( vLED1FlashTask, ( signed char * ) "LED.1", ledSTACK_SIZE, NULL, 0, ( xTaskHandle * ) NULL ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> xTaskCreate( vADCtask, ( signed char * ) "ADC", adcSTACK_SIZE, NULL, 0, ( xTaskHandle * ) NULL ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> /* Start the scheduler. */ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> vTaskStartScheduler; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static void vADCtask( void * pvParameters ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> volatile unsigned int results; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> /*Needs to be global in this example. Otherwise, the<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> compiler removes it because it is not used for anything. */ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> WDTCTL = WDTPW+WDTHOLD; /*Stop watchdog timer*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> P6SEL |= 0x01; /* Enable A/D channel A0*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ( void ) pvParameters;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> for{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ADC12CTL0 = ADC12ON+ADC12SHT0_8+ADC12MSC+ADC12SHT02+ADC12REFON+ADC12REF2_5V; //Turn on ADC12, set sampling time// <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> /* set multiple sample conversion*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ADC12CTL1 = ADC12SHP+ADC12CONSEQ_2; /*Use sampling timer, set mode*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ADC12MCTL0 = ADC12SREF_1;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ADC12IE = 0x01; /* Enable ADC12IFG.0*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ADC12CTL0 |= ADC12ENC; /* Enable conversions*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> ADC12CTL0 |= ADC12SC; /* Start conversion*/

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> results = ADC12MEM0; /*Move results*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> printf("%d, \n", ADC12MEM0); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> }

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> To blink an led and to do serial communication between microcontroller and the DAC chip (AD5662).
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">3. Multitasking test at GPIO and SPI ports :- **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> To blink an led and
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">4. Multitasking test at ****<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">GPIO and UART ports ****<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">:- **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> I would like to share a video clip of demonstration which I made during I was doing test 1 (GPIO test), it is about multitasking on GPIO port using freeRTOS. <span style="color: blue; font-family: Arial,sans-serif;">[]
 * __<span style="color: black; font-family: Arial,sans-serif; font-size: 16pt;">Demo of test1:- __**

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> This freeRTOS may be useful in future if we are using many enviromotes implemented over many different locations. At that time multitasking can be done using freeRTOS, for example every module have zigbee communication protocol, then we may needn’t be having different gsm modules. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> So whenever freeRTOS is needed, we just have to create a task in the following format and call it in main function before calling vStartScheduler.
 * __<span style="color: black; font-family: Arial,sans-serif; font-size: 16pt;">How can freeRTOS be used for Enviromote project :- __**

/*Task to be created*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void vTaskCode( void * pvParameters ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">for <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ /*Task code goes here.*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">} <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

/*Function that creates a task.*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void vOtherFunction( void ) <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">static unsigned char ucParameterToPass ;

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">xTaskHandle xHandle;

<span style="color: black; font-family: Arial,sans-serif;">the new task attempts to access it.*/
<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">/*Use the handle to delete the task. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">vTaskDelete( xHandle );*/

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> I searched many sites for basic concepts of real time OS and the for freeRTOS. I found many interesting concepts about how real time OS works. I’m sharing some basic concepts here. I made a good ppt of these concepts but I’m not able to upload it here due to some file uploading capacity restriction on this wikispace. These are some basic points about real time OS.
 * __<span style="color: black; font-family: Arial,sans-serif; font-size: 16pt;">Some basic freeRTOS concept :- __**

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> **__A simplified scenario__** of an application using freeRTOS is - <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">int main <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">{ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">init_OS; <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">createTask( LCD_ task ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">createTask( Keypad_task ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">createTask( SD_task ); <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">start_OS; contains start_scheduler etc... <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● CreateTask( x ) would tell the OS to update itsinternal data structures and create a task whosecode section is pointed by 'x'. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● 'x' is actually a function ! <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">void x { <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">for { /*do something*/ <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">} <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">}
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> __Task__ **

<span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● Actual OS have a much more complicated'createTask' function. (example: ucOS ). <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● The clock is the heart beat of the system. The scheduler is invoked every time we get a timerinterrupt. It then 'context switches' between different tasks. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● Use of assembly language is unavoidable while writing the scheduler for a particular uP/uC OS code vs Application code. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● OS code is executed only during the interrupts and during system calls. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● Typically system calls on real time OS are just plain functions which change the internal kernel data structures. <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;">● System calls MIGHT be implemented using software interrupts on more complicated platforms(example linux on x86).
 * <span style="color: black; font-family: Arial,sans-serif; font-size: 12pt;"> __Scheduler__ **