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