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