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