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