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