2 FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 #include "FreeRTOS.h"
\r
60 #include "hal_MSP-EXP430F5438.h"
\r
62 /* Codes sent within messages to the LCD task so the LCD task can interpret
\r
63 exactly what the message it just received was. These are sent in the
\r
64 cMessageID member of the message structure (defined below). */
\r
65 #define mainMESSAGE_BUTTON_UP ( 1 )
\r
66 #define mainMESSAGE_BUTTON_SEL ( 2 )
\r
67 #define mainMESSAGE_STATUS ( 3 )
\r
69 /* When the cMessageID member of the message sent to the LCD task is
\r
70 mainMESSAGE_STATUS then these definitions are sent in the cMessageValue member
\r
71 of the same message and indicate what the status actually is. */
\r
72 #define mainERROR_DYNAMIC_TASKS ( pdPASS + 1 )
\r
73 #define mainERROR_COM_TEST ( pdPASS + 2 )
\r
74 #define mainERROR_GEN_QUEUE_TEST ( pdPASS + 3 )
\r
75 #define mainERROR_REG_TEST ( pdPASS + 4 )
\r
77 /* The length of the queue (the number of items the queue can hold) that is used
\r
78 to send messages from tasks and interrupts the the LCD task. */
\r
79 #define mainQUEUE_LENGTH ( 5 )
\r
81 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
83 /*-----------------------------------------------------------*/
\r
85 extern void vRegTest1Task( void *pvParameters );
\r
86 extern void vRegTest2Task( void *pvParameters );
\r
87 static void prvSetupHardware( void );
\r
88 static void prvTerminalIOTask( void *pvParameters );
\r
89 static void prvButtonPollTask( void *pvParameters );
\r
90 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );
\r
92 /*-----------------------------------------------------------*/
\r
94 volatile unsigned short usRegTest1Counter = 0, usRegTest2Counter = 0;
\r
96 /* The handle of the queue used to send messages from tasks and interrupts to
\r
98 static xQueueHandle xLCDQueue = NULL;
\r
100 /* The definition of each message sent from tasks and interrupts to the LCD
\r
104 char cMessageID; /* << States what the message is. */
\r
105 char cMessageValue; /* << States the message value (can be an integer, string pointer, etc. depending on the value of cMessageID. */
\r
107 /*-----------------------------------------------------------*/
\r
111 prvSetupHardware();
\r
113 /* Create the queue used by tasks and interrupts to send strings to the LCD
\r
115 xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );
\r
117 if( xLCDQueue != NULL )
\r
119 /* Add the created queue to the queue registry so it can be viewed in
\r
120 the IAR FreeRTOS state viewer plug-in. */
\r
121 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );
\r
123 /* Create the terminal IO and button poll tasks, as described at the top
\r
125 xTaskCreate( prvTerminalIOTask, ( signed char * ) "IO", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );
\r
126 xTaskCreate( prvButtonPollTask, ( signed char * ) "ButPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
128 xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
129 xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
130 vTaskStartScheduler();
\r
134 /*-----------------------------------------------------------*/
\r
136 static void prvTerminalIOTask( void *pvParameters )
\r
138 xQueueMessage xReceivedMessage;
\r
140 /* Buffer into which strings are formatted and placed ready for display on the
\r
141 LCD. Note this is a static variable to prevent it being allocated on the task
\r
142 stack, which is too small to hold such a variable. The stack size is configured
\r
143 when the task is created. */
\r
144 static char cBuffer[ 512 ];
\r
146 /* This function is the only function that uses printf(). If printf() is
\r
147 used from any other function then some sort of mutual exclusion on stdout
\r
150 This is also the only function that is permitted to access the LCD.
\r
152 First print out the number of bytes that remain in the FreeRTOS heap. This
\r
153 can be viewed in the terminal IO window within the IAR Embedded Workbench. */
\r
154 printf( "%d bytes of heap space remain unallocated\n", ( int ) xPortGetFreeHeapSize() );
\r
158 /* Wait for a message to be received. Using portMAX_DELAY as the block
\r
159 time will result in an indefinite wait provided INCLUDE_vTaskSuspend is
\r
160 set to 1 in FreeRTOSConfig.h, therefore there is no need to check the
\r
161 function return value and the function will only return when a value
\r
162 has been received. */
\r
163 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );
\r
165 /* What is this message? What does it contain? */
\r
166 switch( xReceivedMessage.cMessageID )
\r
168 case mainMESSAGE_BUTTON_UP : /* The button poll task has just
\r
169 informed this task that the up
\r
170 button on the joystick input has
\r
171 been pressed or released. */
\r
172 sprintf( cBuffer, "Button up = %d", xReceivedMessage.cMessageValue );
\r
175 case mainMESSAGE_BUTTON_SEL : /* The select button interrupt
\r
176 just informed this task that the
\r
177 select button was pressed.
\r
178 Generate a table of task run time
\r
179 statistics and output this to
\r
180 the terminal IO window in the IAR
\r
181 embedded workbench. */
\r
182 printf( "\nTask\t Abs Time\t %%Time\n*****************************************" );
\r
183 // vTaskGetRunTimeStats( ( signed char * ) cBuffer );
\r
184 // printf( cBuffer );
\r
187 case mainMESSAGE_STATUS : /* The tick interrupt hook
\r
188 function has just informed this
\r
189 task of the system status.
\r
190 Generate a string in accordance
\r
191 with the status value. */
\r
192 prvGenerateStatusMessage( cBuffer, xReceivedMessage.cMessageValue );
\r
195 default : sprintf( cBuffer, "Unknown message" );
\r
199 /* Output the message that was placed into the cBuffer array within the
\r
200 switch statement above. */
\r
201 printf( "%s : %u\n", cBuffer, ( unsigned int ) xTaskGetTickCount() );
\r
205 /*-----------------------------------------------------------*/
\r
207 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )
\r
209 /* Just a utility function to convert a status value into a meaningful
\r
210 string for output onto the LCD. */
\r
211 switch( lStatusValue )
\r
213 case pdPASS : sprintf( pcBuffer, "Task status = PASS" );
\r
215 case mainERROR_DYNAMIC_TASKS : sprintf( pcBuffer, "Error: Dynamic tasks" );
\r
217 case mainERROR_COM_TEST : sprintf( pcBuffer, "Err: loop connected?" ); /* Error in COM test - is the Loopback connector connected? */
\r
219 case mainERROR_GEN_QUEUE_TEST : sprintf( pcBuffer, "Error: Gen Q test" );
\r
221 case mainERROR_REG_TEST : sprintf( pcBuffer, "Error: Reg test" );
\r
223 default : sprintf( pcBuffer, "Unknown status" );
\r
227 /*-----------------------------------------------------------*/
\r
229 static void prvButtonPollTask( void *pvParameters )
\r
231 unsigned char ucLastState = pdFALSE, ucState;
\r
232 xQueueMessage xMessage;
\r
234 /* This tasks performs the button polling functionality as described at the
\r
235 top of this file. */
\r
238 /* Check the button state. */
\r
239 ucState = ( halButtonsPressed() & BUTTON_UP );
\r
246 if( ucState != ucLastState )
\r
248 /* The state has changed, send a message to the LCD task. */
\r
249 xMessage.cMessageID = mainMESSAGE_BUTTON_UP;
\r
250 xMessage.cMessageValue = ucState;
\r
251 ucLastState = ucState;
\r
252 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
255 /* Block for 10 milliseconds so this task does not utilise all the CPU
\r
256 time and debouncing of the button is not necessary. */
\r
257 vTaskDelay( 10 / portTICK_RATE_MS );
\r
260 /*-----------------------------------------------------------*/
\r
262 static void prvSetupHardware( void )
\r
265 halButtonsInit( BUTTON_ALL );
\r
266 halButtonsInterruptEnable( BUTTON_SELECT );
\r
267 LFXT_Start (XT1DRIVE_0);
\r
268 Init_FLL_Settle( 18000, 488 );
\r
270 /*-----------------------------------------------------------*/
\r
272 void vApplicationSetupTimerInterrupt( void )
\r
274 const unsigned short usACLK_Frequency_Hz = 32768;
\r
276 /* Ensure the timer is stopped. */
\r
279 /* Run the timer of the ACLK. */
\r
282 /* Clear everything to start with. */
\r
285 /* Set the compare match value according to the tick rate we want. */
\r
286 TA0CCR0 = usACLK_Frequency_Hz / configTICK_RATE_HZ;
\r
288 /* Enable the interrupts. */
\r
291 /* Start up clean. */
\r
297 /*-----------------------------------------------------------*/
\r
299 void vApplicationMallocFailedHook( void )
\r
303 /*-----------------------------------------------------------*/
\r
305 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
308 ( void ) pcTaskName;
\r
312 /*-----------------------------------------------------------*/
\r
314 void vApplicationIdleHook( void )
\r
316 __bis_SR_register( LPM3_bits + GIE );
\r
318 /*-----------------------------------------------------------*/
\r
320 void vApplicationTickHook( void )
\r
322 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;
\r
323 static unsigned long ulCounter = 0;
\r
324 static const unsigned long ulCheckFrequency = 5000UL / portTICK_RATE_MS;
\r
325 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
327 /* Define the status message that is sent to the LCD task. By default the
\r
329 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };
\r
331 /* This is called from within the tick interrupt and performs the 'check'
\r
332 functionality as described in the comments at the top of this file.
\r
334 Is it time to perform the 'check' functionality again? */
\r
336 if( ulCounter >= ulCheckFrequency )
\r
338 #ifdef LEFT_OVER_FROM_CUT_AND_PASTE
\r
339 /* See if the standard demo tasks are executing as expected, changing
\r
340 the message that is sent to the LCD task from PASS to an error code if
\r
341 any tasks set reports an error. */
\r
342 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
344 xStatusMessage.lMessageValue = mainERROR_DYNAMIC_TASKS;
\r
347 if( xAreComTestTasksStillRunning() != pdPASS )
\r
349 xStatusMessage.lMessageValue = mainERROR_COM_TEST;
\r
352 if( xAreGenericQueueTasksStillRunning() != pdPASS )
\r
354 xStatusMessage.lMessageValue = mainERROR_GEN_QUEUE_TEST;
\r
357 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
358 their loop counters if they encounter an error. */
\r
359 if( usRegTest1Counter == usLastRegTest1Counter )
\r
361 xStatusMessage.cMessageValue = mainERROR_REG_TEST;
\r
364 if( usRegTest2Counter == usLastRegTest2Counter )
\r
366 xStatusMessage.cMessageValue = mainERROR_REG_TEST;
\r
369 usLastRegTest1Counter = usRegTest1Counter;
\r
370 usLastRegTest2Counter = usRegTest2Counter;
\r
373 /* As this is the tick hook the lHigherPriorityTaskWoken parameter is not
\r
374 needed (a context switch is going to be performed anyway), but it must
\r
375 still be provided. */
\r
376 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &xHigherPriorityTaskWoken );
\r
380 if( ( ulCounter & 0xff ) == 0 )
\r
382 if( ( LED_PORT_OUT & LED_1 ) == 0 )
\r
384 LED_PORT_OUT |= LED_1;
\r
385 LED_PORT_OUT &= ~LED_2;
\r
389 LED_PORT_OUT &= ~LED_1;
\r
390 LED_PORT_OUT |= LED_2;
\r