]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430F5438_IAR/main.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / MSP430X_MSP430F5438_IAR / main.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  * The documentation page for this demo available on http://www.FreeRTOS.org\r
71  * documents the hardware configuration required to run this demo.  It also\r
72  * provides more information on the expected demo application behaviour.\r
73  *\r
74  * main() creates all the demo application tasks, then starts the scheduler.\r
75  * A lot of the created tasks are from the pool of "standard demo" tasks.  The\r
76  * web documentation provides more details of the standard demo tasks, which\r
77  * provide no particular functionality but do provide good examples of how to\r
78  * use the FreeRTOS API.\r
79  *\r
80  * In addition to the standard demo tasks, the following tasks, interrupts and\r
81  * tests are defined and/or created within this file:\r
82  *\r
83  * "LCD" task - The LCD task is a 'gatekeeper' task.  It is the only task that\r
84  * is permitted to access the LCD and therefore ensures access to the LCD is\r
85  * always serialised and there are no mutual exclusion issues.  When a task or\r
86  * an interrupt wants to write to the LCD, it does not access the LCD directly\r
87  * but instead sends the message to the LCD task.  The LCD task then performs\r
88  * the actual LCD output.  This mechanism also allows interrupts to, in effect,\r
89  * write to the LCD by sending messages to the LCD task.\r
90  *\r
91  * The LCD task is also a demonstration of a 'controller' task design pattern.\r
92  * Some tasks do not actually send a string to the LCD task directly, but\r
93  * instead send a command that is interpreted by the LCD task.  In a normal\r
94  * application these commands can be control values or set points, in this\r
95  * simple example the commands just result in messages being displayed on the\r
96  * LCD.\r
97  *\r
98  * "Button Poll" task - This task polls the state of the 'up' key on the\r
99  * joystick input device.  It uses the vTaskDelay() API function to control\r
100  * the poll rate to ensure debouncing is not necessary and that the task does\r
101  * not use all the available CPU processing time.\r
102  *\r
103  * Button Interrupt and run time stats display - The select button on the\r
104  * joystick input device is configured to generate an external interrupt.  The\r
105  * handler for this interrupt sends a message to LCD task, which interprets the\r
106  * message to mean, firstly write a message to the LCD, and secondly, generate\r
107  * a table of run time statistics.  The run time statistics are displayed as a\r
108  * table that contains information on how much processing time each task has\r
109  * been allocated since the application started to execute.  This information\r
110  * is provided both as an absolute time, and as a percentage of the total run\r
111  * time.  The information is displayed in the terminal IO window of the IAR\r
112  * embedded workbench.  The online documentation for this demo shows a screen\r
113  * shot demonstrating where the run time stats can be viewed.\r
114  *\r
115  * Idle Hook - The idle hook is a function that is called on each iteration of\r
116  * the idle task.  In this case it is used to place the processor into a low\r
117  * power mode.  Note however that this application is implemented using standard\r
118  * components, and is therefore not optimised for low power operation.  Lower\r
119  * power consumption would be achieved by converting polling tasks into event\r
120  * driven tasks, and slowing the tick interrupt frequency.\r
121  *\r
122  * "Check" function called from the tick hook - The tick hook is called during\r
123  * each tick interrupt.  It is called from an interrupt context so must execute\r
124  * quickly, not attempt to block, and not call any FreeRTOS API functions that\r
125  * do not end in "FromISR".  In this case the tick hook executes a 'check'\r
126  * function.  This only executes every five seconds.  Its main function is to\r
127  * check that all the standard demo tasks are still operational.  Each time it\r
128  * executes it sends a status code to the LCD task.  The LCD task interprets the\r
129  * code and displays an appropriate message - which will be PASS if no tasks\r
130  * have reported any errors, or a message stating which task has reported an\r
131  * error.\r
132  *\r
133  * "Reg test" tasks - These fill the registers with known values, then check\r
134  * that each register still contains its expected value.  Each task uses\r
135  * different values.  The tasks run with very low priority so get preempted\r
136  * very frequently.  A check variable is incremented on each iteration of the\r
137  * test loop.  A register containing an unexpected value is indicative of an\r
138  * error in the context switching mechanism and will result in a branch to a\r
139  * null loop - which in turn will prevent the check variable from incrementing\r
140  * any further and allow the check task (described a above) to determine that an\r
141  * error has occurred.  The nature of the reg test tasks necessitates that they\r
142  * are written in assembly code.\r
143  *\r
144  * *NOTE 1* vApplicationSetupTimerInterrupt() is called by the kernel to let\r
145  * the application set up a timer to generate the tick interrupt.  In this\r
146  * example a timer A0 is used for this purpose.\r
147  *\r
148 */\r
149 \r
150 /* Standard includes. */\r
151 #include <stdio.h>\r
152 \r
153 /* FreeRTOS includes. */\r
154 #include "FreeRTOS.h"\r
155 #include "task.h"\r
156 #include "queue.h"\r
157 \r
158 /* Hardware includes. */\r
159 #include "msp430.h"\r
160 #include "hal_MSP-EXP430F5438.h"\r
161 \r
162 /* Standard demo includes. */\r
163 #include "ParTest.h"\r
164 #include "dynamic.h"\r
165 #include "comtest2.h"\r
166 #include "GenQTest.h"\r
167 \r
168 /* Codes sent within messages to the LCD task so the LCD task can interpret\r
169 exactly what the message it just received was.  These are sent in the\r
170 cMessageID member of the message structure (defined below). */\r
171 #define mainMESSAGE_BUTTON_UP                   ( 1 )\r
172 #define mainMESSAGE_BUTTON_SEL                  ( 2 )\r
173 #define mainMESSAGE_STATUS                              ( 3 )\r
174 \r
175 /* When the cMessageID member of the message sent to the LCD task is\r
176 mainMESSAGE_STATUS then these definitions are sent in the ulMessageValue member\r
177 of the same message and indicate what the status actually is. */\r
178 #define mainERROR_DYNAMIC_TASKS                 ( pdPASS + 1 )\r
179 #define mainERROR_COM_TEST                              ( pdPASS + 2 )\r
180 #define mainERROR_GEN_QUEUE_TEST                ( pdPASS + 3 )\r
181 #define mainERROR_REG_TEST                              ( pdPASS + 4 )\r
182 \r
183 /* The length of the queue (the number of items the queue can hold) that is used\r
184 to send messages from tasks and interrupts the the LCD task. */\r
185 #define mainQUEUE_LENGTH                                ( 5 )\r
186 \r
187 /* Priorities used by the test and demo tasks. */\r
188 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
189 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
190 #define mainGENERIC_QUEUE_TEST_PRIORITY ( tskIDLE_PRIORITY )\r
191 \r
192 /* The LED used by the comtest tasks. See the comtest.c file for more\r
193 information.  */\r
194 #define mainCOM_TEST_LED                                ( 1 )\r
195 \r
196 /* The baud rate used by the comtest tasks described at the top of this file. */\r
197 #define mainCOM_TEST_BAUD_RATE                  ( 38400 )\r
198 \r
199 /* The maximum number of lines of text that can be displayed on the LCD. */\r
200 #define mainMAX_LCD_LINES                               ( 8 )\r
201 \r
202 /* Just used to ensure parameters are passed into tasks correctly. */\r
203 #define mainTASK_PARAMETER_CHECK_VALUE  ( ( void * ) 0xDEAD )\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 /*\r
207  * The reg test tasks as described at the top of this file.\r
208  */\r
209 extern void vRegTest1Task( void *pvParameters );\r
210 extern void vRegTest2Task( void *pvParameters );\r
211 \r
212 /*\r
213  * Configures clocks, LCD, port pints, etc. necessary to execute this demo.\r
214  */\r
215 static void prvSetupHardware( void );\r
216 \r
217 /*\r
218  * Definition of the LCD/controller task described in the comments at the top\r
219  * of this file.\r
220  */\r
221 static void prvLCDTask( void *pvParameters );\r
222 \r
223 /*\r
224  * Definition of the button poll task described in the comments at the top of\r
225  * this file.\r
226  */\r
227 static void prvButtonPollTask( void *pvParameters );\r
228 \r
229 /*\r
230  * Converts a status message value into an appropriate string for display on\r
231  * the LCD.  The string is written to pcBuffer.\r
232  */\r
233 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );\r
234 \r
235 /*-----------------------------------------------------------*/\r
236 \r
237 /* Variables that are incremented on each iteration of the reg test tasks -\r
238 provided the tasks have not reported any errors.  The check task inspects these\r
239 variables to ensure they are still incrementing as expected.  If a variable\r
240 stops incrementing then it is likely that its associate task has stalled. */\r
241 volatile unsigned short usRegTest1Counter = 0, usRegTest2Counter = 0;\r
242 \r
243 /* The handle of the queue used to send messages from tasks and interrupts to\r
244 the LCD task. */\r
245 static xQueueHandle xLCDQueue = NULL;\r
246 \r
247 /* The definition of each message sent from tasks and interrupts to the LCD\r
248 task. */\r
249 typedef struct\r
250 {\r
251         char cMessageID;                                /* << States what the message is. */\r
252         unsigned long ulMessageValue;   /* << States the message value (can be an integer, string pointer, etc. depending on the value of cMessageID). */\r
253 } xQueueMessage;\r
254 \r
255 /*-----------------------------------------------------------*/\r
256 \r
257 /* The linker script can be used to test the FreeRTOS ports use of 20bit \r
258 addresses by locating all code in high memory.  The following pragma ensures \r
259 that main remains in low memory when that is done.  The ISR_CODE segment is used \r
260 for convenience as ISR functions are always placed in low memory. */\r
261 #pragma location="ISR_CODE"\r
262 void main( void )\r
263 {\r
264         /* Configure the peripherals used by this demo application.  This includes\r
265         configuring the joystick input select button to generate interrupts. */\r
266         prvSetupHardware();\r
267 \r
268         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
269         task. */\r
270         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
271 \r
272         /* If the queue could not be created then don't create any tasks that might\r
273         attempt to use the queue. */\r
274         if( xLCDQueue != NULL )\r
275         {\r
276                 /* Add the created queue to the queue registry so it can be viewed in\r
277                 the IAR FreeRTOS state viewer plug-in. */\r
278                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
279 \r
280                 /* Create the standard demo tasks. */\r
281                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
282                 vStartDynamicPriorityTasks();\r
283                 vStartGenericQueueTasks( mainGENERIC_QUEUE_TEST_PRIORITY );\r
284                 \r
285                 /* Create the LCD, button poll and register test tasks, as described at\r
286                 the top of this file. */\r
287                 xTaskCreate( prvLCDTask, ( signed char * ) "LCD", configMINIMAL_STACK_SIZE * 2, mainTASK_PARAMETER_CHECK_VALUE, mainLCD_TASK_PRIORITY, NULL );\r
288                 xTaskCreate( prvButtonPollTask, ( signed char * ) "BPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
289                 xTaskCreate( vRegTest1Task, ( signed char * ) "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
290                 xTaskCreate( vRegTest2Task, ( signed char * ) "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
291 \r
292                 /* Start the scheduler. */\r
293                 vTaskStartScheduler();\r
294         }\r
295         \r
296         /* If all is well then this line will never be reached.  If it is reached\r
297         then it is likely that there was insufficient (FreeRTOS) heap memory space\r
298         to create the idle task.  This may have been trapped by the malloc() failed\r
299         hook function, if one is configured. */ \r
300         for( ;; );\r
301 }\r
302 /*-----------------------------------------------------------*/\r
303 \r
304 static void prvLCDTask( void *pvParameters )\r
305 {\r
306 xQueueMessage xReceivedMessage;\r
307 \r
308 /* Buffer into which strings are formatted and placed ready for display on the\r
309 LCD.  Note this is a static variable to prevent it being allocated on the task\r
310 stack, which is too small to hold such a variable.  The stack size is configured\r
311 when the task is created. */\r
312 static char cBuffer[ 512 ];\r
313 unsigned char ucLine = 1;\r
314 \r
315 \r
316         /* This function is the only function that uses printf().  If printf() is\r
317         used from any other function then some sort of mutual exclusion on stdout\r
318         will be necessary.\r
319         \r
320         This is also the only function that is permitted to access the LCD.\r
321         \r
322         First print out the number of bytes that remain in the FreeRTOS heap.  This\r
323         can be viewed in the terminal IO window within the IAR Embedded Workbench. */\r
324         printf( "%d bytes of heap space remain unallocated\n", ( int ) xPortGetFreeHeapSize() );\r
325         \r
326         /* Just as a test of the port, and for no functional reason, check the task\r
327         parameter contains its expected value. */\r
328         if( pvParameters != mainTASK_PARAMETER_CHECK_VALUE )\r
329         {\r
330                 halLcdPrintLine( "Invalid parameter", ucLine,  OVERWRITE_TEXT );\r
331                 ucLine++;               \r
332         }\r
333 \r
334         for( ;; )\r
335         {\r
336                 /* Wait for a message to be received.  Using portMAX_DELAY as the block\r
337                 time will result in an indefinite wait provided INCLUDE_vTaskSuspend is\r
338                 set to 1 in FreeRTOSConfig.h, therefore there is no need to check the\r
339                 function return value and the function will only return when a value\r
340                 has been received. */\r
341                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
342 \r
343                 /* Clear the LCD if no room remains for any more text output. */\r
344                 if( ucLine > mainMAX_LCD_LINES )\r
345                 {\r
346                         halLcdClearScreen();\r
347                         ucLine = 0;\r
348                 }\r
349                 \r
350                 /* What is this message?  What does it contain? */\r
351                 switch( xReceivedMessage.cMessageID )\r
352                 {\r
353                         case mainMESSAGE_BUTTON_UP              :       /* The button poll task has just\r
354                                                                                                 informed this task that the up\r
355                                                                                                 button on the joystick input has\r
356                                                                                                 been pressed or released. */\r
357                                                                                                 sprintf( cBuffer, "Button up = %d", ( int ) xReceivedMessage.ulMessageValue );\r
358                                                                                                 break;\r
359 \r
360                         case mainMESSAGE_BUTTON_SEL             :       /* The select button interrupt\r
361                                                                                                 just informed this task that the\r
362                                                                                                 select button was pressed.\r
363                                                                                                 Generate a table of task run time\r
364                                                                                                 statistics and output this to\r
365                                                                                                 the terminal IO window in the IAR\r
366                                                                                                 embedded workbench. */\r
367                                                                                                 printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************" );\r
368                                                                                                 vTaskGetRunTimeStats( ( signed char * ) cBuffer );\r
369                                                                                                 printf( cBuffer );\r
370                                                                                                 \r
371                                                                                                 /* Also print out a message to\r
372                                                                                                 the LCD - in this case the\r
373                                                                                                 pointer to the string to print\r
374                                                                                                 is sent directly in the\r
375                                                                                                 ulMessageValue member of the\r
376                                                                                                 message.  This just demonstrates\r
377                                                                                                 a different communication\r
378                                                                                                 technique. */\r
379                                                                                                 sprintf( cBuffer, "%s", ( char * ) xReceivedMessage.ulMessageValue );\r
380                                                                                                 break;\r
381                                                                                                 \r
382                         case mainMESSAGE_STATUS                 :       /* The tick interrupt hook\r
383                                                                                                 function has just informed this\r
384                                                                                                 task of the system status.\r
385                                                                                                 Generate a string in accordance\r
386                                                                                                 with the status value. */\r
387                                                                                                 prvGenerateStatusMessage( cBuffer, xReceivedMessage.ulMessageValue );\r
388                                                                                                 break;\r
389                                                                                                 \r
390                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
391                                                                                                 break;\r
392                 }\r
393                 \r
394                 /* Output the message that was placed into the cBuffer array within the\r
395                 switch statement above, then move onto the next line ready for the next\r
396                 message to arrive on the queue. */\r
397                 halLcdPrintLine( cBuffer, ucLine,  OVERWRITE_TEXT );\r
398                 ucLine++;\r
399         }\r
400 }\r
401 /*-----------------------------------------------------------*/\r
402 \r
403 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )\r
404 {\r
405         /* Just a utility function to convert a status value into a meaningful\r
406         string for output onto the LCD. */\r
407         switch( lStatusValue )\r
408         {\r
409                 case pdPASS                                             :       sprintf( pcBuffer, "Status = PASS" );\r
410                                                                                         break;\r
411                 case mainERROR_DYNAMIC_TASKS    :       sprintf( pcBuffer, "Err: Dynamic tsks" );\r
412                                                                                         break;\r
413                 case mainERROR_COM_TEST                 :       sprintf( pcBuffer, "Err: COM test" );\r
414                                                                                         break;\r
415                 case mainERROR_GEN_QUEUE_TEST   :       sprintf( pcBuffer, "Error: Gen Q test" );\r
416                                                                                         break;\r
417                 case mainERROR_REG_TEST                 :       sprintf( pcBuffer, "Error: Reg test" );\r
418                                                                                         break;\r
419                 default                                                 :       sprintf( pcBuffer, "Unknown status" );\r
420                                                                                         break;\r
421         }\r
422 }\r
423 /*-----------------------------------------------------------*/\r
424 \r
425 static void prvButtonPollTask( void *pvParameters )\r
426 {\r
427 unsigned char ucLastState = pdFALSE, ucState;\r
428 xQueueMessage xMessage;\r
429 \r
430         /* This tasks performs the button polling functionality as described at the\r
431         top of this file. */\r
432         for( ;; )\r
433         {\r
434                 /* Check the button state. */\r
435                 ucState = ( halButtonsPressed() & BUTTON_UP );\r
436                 \r
437                 if( ucState != 0 )\r
438                 {\r
439                         /* The button was pressed. */\r
440                         ucState = pdTRUE;\r
441                 }\r
442                 \r
443                 if( ucState != ucLastState )\r
444                 {\r
445                         /* The state has changed, send a message to the LCD task. */\r
446                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
447                         xMessage.ulMessageValue = ( unsigned long ) ucState;\r
448                         ucLastState = ucState;\r
449                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
450                 }\r
451                 \r
452                 /* Block for 10 milliseconds so this task does not utilise all the CPU\r
453                 time and debouncing of the button is not necessary. */\r
454                 vTaskDelay( 10 / portTICK_RATE_MS );\r
455         }\r
456 }\r
457 /*-----------------------------------------------------------*/\r
458 \r
459 static void prvSetupHardware( void )\r
460 {\r
461         halBoardInit();\r
462 \r
463         LFXT_Start( XT1DRIVE_0 );\r
464         hal430SetSystemClock( configCPU_CLOCK_HZ, configLFXT_CLOCK_HZ );\r
465 \r
466         halButtonsInit( BUTTON_ALL );\r
467         halButtonsInterruptEnable( BUTTON_SELECT );\r
468 \r
469         /* Initialise the LCD, but note that the backlight is not used as the\r
470         library function uses timer A0 to modulate the backlight, and this file\r
471         defines vApplicationSetupTimerInterrupt() to also use timer A0 to generate\r
472         the tick interrupt.  If the backlight is required, then change either the\r
473         halLCD library or vApplicationSetupTimerInterrupt() to use a different\r
474         timer.  Timer A1 is used for the run time stats time base6. */\r
475         halLcdInit();\r
476         halLcdSetContrast( 100 );\r
477         halLcdClearScreen();\r
478         \r
479         halLcdPrintLine( " www.FreeRTOS.org", 0,  OVERWRITE_TEXT );\r
480 }\r
481 /*-----------------------------------------------------------*/\r
482 \r
483 void vApplicationTickHook( void )\r
484 {\r
485 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
486 static unsigned long ulCounter = 0;\r
487 static const unsigned long ulCheckFrequency = 5000UL / portTICK_RATE_MS;\r
488 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
489 \r
490 /* Define the status message that is sent to the LCD task.  By default the\r
491 status is PASS. */\r
492 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };\r
493 \r
494         /* This is called from within the tick interrupt and performs the 'check'\r
495         functionality as described in the comments at the top of this file.\r
496 \r
497         Is it time to perform the 'check' functionality again? */\r
498         ulCounter++;\r
499         if( ulCounter >= ulCheckFrequency )\r
500         {\r
501                 /* See if the standard demo tasks are executing as expected, changing\r
502                 the message that is sent to the LCD task from PASS to an error code if\r
503                 any tasks set reports an error. */\r
504                 if( xAreComTestTasksStillRunning() != pdPASS )\r
505                 {\r
506                         xStatusMessage.ulMessageValue = mainERROR_COM_TEST;\r
507                 }\r
508 \r
509                 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
510                 {\r
511                         xStatusMessage.ulMessageValue = mainERROR_DYNAMIC_TASKS;\r
512                 }\r
513                 \r
514                 if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
515                 {\r
516                         xStatusMessage.ulMessageValue = mainERROR_GEN_QUEUE_TEST;\r
517                 }                       \r
518 \r
519                 /* Check the reg test tasks are still cycling.  They will stop\r
520                 incrementing their loop counters if they encounter an error. */\r
521                 if( usRegTest1Counter == usLastRegTest1Counter )\r
522                 {\r
523                         xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
524                 }\r
525 \r
526                 if( usRegTest2Counter == usLastRegTest2Counter )\r
527                 {\r
528                         xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
529                 }\r
530 \r
531                 usLastRegTest1Counter = usRegTest1Counter;\r
532                 usLastRegTest2Counter = usRegTest2Counter;\r
533                 \r
534                 /* As this is the tick hook the lHigherPriorityTaskWoken parameter is not\r
535                 needed (a context switch is going to be performed anyway), but it must\r
536                 still be provided. */\r
537                 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &xHigherPriorityTaskWoken );\r
538                 ulCounter = 0;\r
539         }\r
540 \r
541         /* Just periodically toggle an LED to show that the tick interrupt is\r
542         running.  Note that this access LED_PORT_OUT in a non-atomic way, so tasks\r
543         that access the same port must do so from a critical section. */\r
544         if( ( ulCounter & 0xff ) == 0 )\r
545         {\r
546                 if( ( LED_PORT_OUT & LED_1 ) == 0 )\r
547                 {\r
548                         LED_PORT_OUT |= LED_1;\r
549                 }\r
550                 else\r
551                 {\r
552                         LED_PORT_OUT &= ~LED_1;\r
553                 }\r
554         }\r
555 }\r
556 /*-----------------------------------------------------------*/\r
557 \r
558 #pragma vector=PORT2_VECTOR\r
559 __interrupt static void prvSelectButtonInterrupt(void)\r
560 {\r
561 /* Define the message sent to the LCD task from this interrupt. */\r
562 static const xQueueMessage xMessage = { mainMESSAGE_BUTTON_SEL, ( unsigned long ) "Select Interrupt" };\r
563 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
564 \r
565         /* This is the interrupt handler for the joystick select button input.\r
566         The button has been pushed, write a message to the LCD via the LCD task. */\r
567         xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
568 \r
569         P2IFG = 0;\r
570         \r
571         /* If writing to xLCDQueue caused a task to unblock, and the unblocked task\r
572         has a priority equal to or above the task that this interrupt interrupted,\r
573         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
574         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
575         interrupt returns directly to the higher priority unblocked task. */\r
576         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
577 }\r
578 /*-----------------------------------------------------------*/\r
579 \r
580 /* The MSP430X port uses this callback function to configure its tick interrupt.\r
581 This allows the application to choose the tick interrupt source.\r
582 configTICK_VECTOR must also be set in FreeRTOSConfig.h to the correct\r
583 interrupt vector for the chosen tick interrupt source.  This implementation of\r
584 vApplicationSetupTimerInterrupt() generates the tick from timer A0, so in this\r
585 case configTICK_VECTOR is set to TIMER0_A0_VECTOR. */\r
586 void vApplicationSetupTimerInterrupt( void )\r
587 {\r
588 const unsigned short usACLK_Frequency_Hz = 32768;\r
589 \r
590         /* Ensure the timer is stopped. */\r
591         TA0CTL = 0;\r
592 \r
593         /* Run the timer from the ACLK. */\r
594         TA0CTL = TASSEL_1;\r
595 \r
596         /* Clear everything to start with. */\r
597         TA0CTL |= TACLR;\r
598 \r
599         /* Set the compare match value according to the tick rate we want. */\r
600         TA0CCR0 = usACLK_Frequency_Hz / configTICK_RATE_HZ;\r
601 \r
602         /* Enable the interrupts. */\r
603         TA0CCTL0 = CCIE;\r
604 \r
605         /* Start up clean. */\r
606         TA0CTL |= TACLR;\r
607 \r
608         /* Up mode. */\r
609         TA0CTL |= MC_1;\r
610 }\r
611 /*-----------------------------------------------------------*/\r
612 \r
613 void vApplicationIdleHook( void )\r
614 {\r
615         /* Called on each iteration of the idle task.  In this case the idle task\r
616         just enters a low power mode. */\r
617         __bis_SR_register( LPM3_bits + GIE );\r
618 }\r
619 /*-----------------------------------------------------------*/\r
620 \r
621 void vApplicationMallocFailedHook( void )\r
622 {\r
623         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
624         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
625         internally by FreeRTOS API functions that create tasks, queues or\r
626         semaphores. */\r
627         taskDISABLE_INTERRUPTS();\r
628         for( ;; );\r
629 }\r
630 /*-----------------------------------------------------------*/\r
631 \r
632 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
633 {\r
634         ( void ) pxTask;\r
635         ( void ) pcTaskName;\r
636         \r
637         /* Run time stack overflow checking is performed if\r
638         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
639         function is called if a stack overflow is detected. */\r
640         taskDISABLE_INTERRUPTS();\r
641         for( ;; );\r
642 }\r
643 /*-----------------------------------------------------------*/\r
644 \r