]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430F5438_IAR/main.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / MSP430X_MSP430F5438_IAR / 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 and\r
108  * tests 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 and run time stats display - The select button on the\r
131  * joystick input device is configured to generate an external interrupt.  The\r
132  * handler for this interrupt sends a message to LCD task, which interprets the\r
133  * message to mean, firstly write a message to the LCD, and secondly, generate\r
134  * a table of run time statistics.  The run time statistics are displayed as a\r
135  * table that contains information on how much processing time each task has\r
136  * been allocated since the application started to execute.  This information\r
137  * is provided both as an absolute time, and as a percentage of the total run\r
138  * time.  The information is displayed in the terminal IO window of the IAR\r
139  * embedded workbench.  The online documentation for this demo shows a screen\r
140  * shot demonstrating where the run time stats can be viewed.\r
141  *\r
142  * Idle Hook - The idle hook is a function that is called on each iteration of\r
143  * the idle task.  In this case it is used to place the processor into a low\r
144  * power mode.  Note however that this application is implemented using standard\r
145  * components, and is therefore not optimised for low power operation.  Lower\r
146  * power consumption would be achieved by converting polling tasks into event\r
147  * driven tasks, and slowing the tick interrupt frequency.\r
148  *\r
149  * "Check" function called from the tick hook - The tick hook is called during\r
150  * each tick interrupt.  It is called from an interrupt context so must execute\r
151  * quickly, not attempt to block, and not call any FreeRTOS API functions that\r
152  * do not end in "FromISR".  In this case the tick hook executes a 'check'\r
153  * function.  This only executes every five seconds.  Its main function is to\r
154  * check that all the standard demo tasks are still operational.  Each time it\r
155  * executes it sends a status code to the LCD task.  The LCD task interprets the\r
156  * code and displays an appropriate message - which will be PASS if no tasks\r
157  * have reported any errors, or a message stating which task has reported an\r
158  * error.\r
159  *\r
160  * "Reg test" tasks - These fill the registers with known values, then check\r
161  * that each register still contains its expected value.  Each task uses\r
162  * different values.  The tasks run with very low priority so get preempted\r
163  * very frequently.  A check variable is incremented on each iteration of the\r
164  * test loop.  A register containing an unexpected value is indicative of an\r
165  * error in the context switching mechanism and will result in a branch to a\r
166  * null loop - which in turn will prevent the check variable from incrementing\r
167  * any further and allow the check task (described a above) to determine that an\r
168  * error has occurred.  The nature of the reg test tasks necessitates that they\r
169  * are written in assembly code.\r
170  *\r
171  * *NOTE 1* vApplicationSetupTimerInterrupt() is called by the kernel to let\r
172  * the application set up a timer to generate the tick interrupt.  In this\r
173  * example a timer A0 is used for this purpose.\r
174  *\r
175 */\r
176 \r
177 /* Standard includes. */\r
178 #include <stdio.h>\r
179 \r
180 /* FreeRTOS includes. */\r
181 #include "FreeRTOS.h"\r
182 #include "task.h"\r
183 #include "queue.h"\r
184 \r
185 /* Hardware includes. */\r
186 #include "msp430.h"\r
187 #include "hal_MSP-EXP430F5438.h"\r
188 \r
189 /* Standard demo includes. */\r
190 #include "ParTest.h"\r
191 #include "dynamic.h"\r
192 #include "comtest2.h"\r
193 #include "GenQTest.h"\r
194 \r
195 /* Codes sent within messages to the LCD task so the LCD task can interpret\r
196 exactly what the message it just received was.  These are sent in the\r
197 cMessageID member of the message structure (defined below). */\r
198 #define mainMESSAGE_BUTTON_UP                   ( 1 )\r
199 #define mainMESSAGE_BUTTON_SEL                  ( 2 )\r
200 #define mainMESSAGE_STATUS                              ( 3 )\r
201 \r
202 /* When the cMessageID member of the message sent to the LCD task is\r
203 mainMESSAGE_STATUS then these definitions are sent in the ulMessageValue member\r
204 of the same message and indicate what the status actually is. */\r
205 #define mainERROR_DYNAMIC_TASKS                 ( pdPASS + 1 )\r
206 #define mainERROR_COM_TEST                              ( pdPASS + 2 )\r
207 #define mainERROR_GEN_QUEUE_TEST                ( pdPASS + 3 )\r
208 #define mainERROR_REG_TEST                              ( pdPASS + 4 )\r
209 \r
210 /* The length of the queue (the number of items the queue can hold) that is used\r
211 to send messages from tasks and interrupts the the LCD task. */\r
212 #define mainQUEUE_LENGTH                                ( 5 )\r
213 \r
214 /* Priorities used by the test and demo tasks. */\r
215 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
216 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
217 #define mainGENERIC_QUEUE_TEST_PRIORITY ( tskIDLE_PRIORITY )\r
218 \r
219 /* The LED used by the comtest tasks. See the comtest.c file for more\r
220 information.  */\r
221 #define mainCOM_TEST_LED                                ( 1 )\r
222 \r
223 /* The baud rate used by the comtest tasks described at the top of this file. */\r
224 #define mainCOM_TEST_BAUD_RATE                  ( 38400 )\r
225 \r
226 /* The maximum number of lines of text that can be displayed on the LCD. */\r
227 #define mainMAX_LCD_LINES                               ( 8 )\r
228 \r
229 /* Just used to ensure parameters are passed into tasks correctly. */\r
230 #define mainTASK_PARAMETER_CHECK_VALUE  ( ( void * ) 0xDEAD )\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /*\r
234  * The reg test tasks as described at the top of this file.\r
235  */\r
236 extern void vRegTest1Task( void *pvParameters );\r
237 extern void vRegTest2Task( void *pvParameters );\r
238 \r
239 /*\r
240  * Configures clocks, LCD, port pints, etc. necessary to execute this demo.\r
241  */\r
242 static void prvSetupHardware( void );\r
243 \r
244 /*\r
245  * Definition of the LCD/controller task described in the comments at the top\r
246  * of this file.\r
247  */\r
248 static void prvLCDTask( void *pvParameters );\r
249 \r
250 /*\r
251  * Definition of the button poll task described in the comments at the top of\r
252  * this file.\r
253  */\r
254 static void prvButtonPollTask( void *pvParameters );\r
255 \r
256 /*\r
257  * Converts a status message value into an appropriate string for display on\r
258  * the LCD.  The string is written to pcBuffer.\r
259  */\r
260 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );\r
261 \r
262 /*-----------------------------------------------------------*/\r
263 \r
264 /* Variables that are incremented on each iteration of the reg test tasks -\r
265 provided the tasks have not reported any errors.  The check task inspects these\r
266 variables to ensure they are still incrementing as expected.  If a variable\r
267 stops incrementing then it is likely that its associate task has stalled. */\r
268 volatile unsigned short usRegTest1Counter = 0, usRegTest2Counter = 0;\r
269 \r
270 /* The handle of the queue used to send messages from tasks and interrupts to\r
271 the LCD task. */\r
272 static QueueHandle_t xLCDQueue = NULL;\r
273 \r
274 /* The definition of each message sent from tasks and interrupts to the LCD\r
275 task. */\r
276 typedef struct\r
277 {\r
278         char cMessageID;                                /* << States what the message is. */\r
279         unsigned long ulMessageValue;   /* << States the message value (can be an integer, string pointer, etc. depending on the value of cMessageID). */\r
280 } xQueueMessage;\r
281 \r
282 /*-----------------------------------------------------------*/\r
283 \r
284 /* The linker script can be used to test the FreeRTOS ports use of 20bit\r
285 addresses by locating all code in high memory.  The following pragma ensures\r
286 that main remains in low memory when that is done.  The ISR_CODE segment is used\r
287 for convenience as ISR functions are always placed in low memory. */\r
288 #pragma location="ISR_CODE"\r
289 void main( void )\r
290 {\r
291         /* Configure the peripherals used by this demo application.  This includes\r
292         configuring the joystick input select button to generate interrupts. */\r
293         prvSetupHardware();\r
294 \r
295         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
296         task. */\r
297         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
298 \r
299         /* If the queue could not be created then don't create any tasks that might\r
300         attempt to use the queue. */\r
301         if( xLCDQueue != NULL )\r
302         {\r
303                 /* Add the created queue to the queue registry so it can be viewed in\r
304                 the IAR FreeRTOS state viewer plug-in. */\r
305                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
306 \r
307                 /* Create the standard demo tasks. */\r
308                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
309                 vStartDynamicPriorityTasks();\r
310                 vStartGenericQueueTasks( mainGENERIC_QUEUE_TEST_PRIORITY );\r
311 \r
312                 /* Create the LCD, button poll and register test tasks, as described at\r
313                 the top of this file. */\r
314                 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE * 2, mainTASK_PARAMETER_CHECK_VALUE, mainLCD_TASK_PRIORITY, NULL );\r
315                 xTaskCreate( prvButtonPollTask, "BPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
316                 xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
317                 xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
318 \r
319                 /* Start the scheduler. */\r
320                 vTaskStartScheduler();\r
321         }\r
322 \r
323         /* If all is well then this line will never be reached.  If it is reached\r
324         then it is likely that there was insufficient (FreeRTOS) heap memory space\r
325         to create the idle task.  This may have been trapped by the malloc() failed\r
326         hook function, if one is configured. */\r
327         for( ;; );\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 static void prvLCDTask( void *pvParameters )\r
332 {\r
333 xQueueMessage xReceivedMessage;\r
334 \r
335 /* Buffer into which strings are formatted and placed ready for display on the\r
336 LCD.  Note this is a static variable to prevent it being allocated on the task\r
337 stack, which is too small to hold such a variable.  The stack size is configured\r
338 when the task is created. */\r
339 static char cBuffer[ 512 ];\r
340 unsigned char ucLine = 1;\r
341 \r
342 \r
343         /* This function is the only function that uses printf().  If printf() is\r
344         used from any other function then some sort of mutual exclusion on stdout\r
345         will be necessary.\r
346 \r
347         This is also the only function that is permitted to access the LCD.\r
348 \r
349         First print out the number of bytes that remain in the FreeRTOS heap.  This\r
350         can be viewed in the terminal IO window within the IAR Embedded Workbench. */\r
351         printf( "%d bytes of heap space remain unallocated\n", ( int ) xPortGetFreeHeapSize() );\r
352 \r
353         /* Just as a test of the port, and for no functional reason, check the task\r
354         parameter contains its expected value. */\r
355         if( pvParameters != mainTASK_PARAMETER_CHECK_VALUE )\r
356         {\r
357                 halLcdPrintLine( "Invalid parameter", ucLine,  OVERWRITE_TEXT );\r
358                 ucLine++;\r
359         }\r
360 \r
361         for( ;; )\r
362         {\r
363                 /* Wait for a message to be received.  Using portMAX_DELAY as the block\r
364                 time will result in an indefinite wait provided INCLUDE_vTaskSuspend is\r
365                 set to 1 in FreeRTOSConfig.h, therefore there is no need to check the\r
366                 function return value and the function will only return when a value\r
367                 has been received. */\r
368                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
369 \r
370                 /* Clear the LCD if no room remains for any more text output. */\r
371                 if( ucLine > mainMAX_LCD_LINES )\r
372                 {\r
373                         halLcdClearScreen();\r
374                         ucLine = 0;\r
375                 }\r
376 \r
377                 /* What is this message?  What does it contain? */\r
378                 switch( xReceivedMessage.cMessageID )\r
379                 {\r
380                         case mainMESSAGE_BUTTON_UP              :       /* The button poll task has just\r
381                                                                                                 informed this task that the up\r
382                                                                                                 button on the joystick input has\r
383                                                                                                 been pressed or released. */\r
384                                                                                                 sprintf( cBuffer, "Button up = %d", ( int ) xReceivedMessage.ulMessageValue );\r
385                                                                                                 break;\r
386 \r
387                         case mainMESSAGE_BUTTON_SEL             :       /* The select button interrupt\r
388                                                                                                 just informed this task that the\r
389                                                                                                 select button was pressed.\r
390                                                                                                 Generate a table of task run time\r
391                                                                                                 statistics and output this to\r
392                                                                                                 the terminal IO window in the IAR\r
393                                                                                                 embedded workbench. */\r
394                                                                                                 printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************" );\r
395                                                                                                 vTaskGetRunTimeStats( cBuffer );\r
396                                                                                                 printf( cBuffer );\r
397 \r
398                                                                                                 /* Also print out a message to\r
399                                                                                                 the LCD - in this case the\r
400                                                                                                 pointer to the string to print\r
401                                                                                                 is sent directly in the\r
402                                                                                                 ulMessageValue member of the\r
403                                                                                                 message.  This just demonstrates\r
404                                                                                                 a different communication\r
405                                                                                                 technique. */\r
406                                                                                                 sprintf( cBuffer, "%s", ( char * ) xReceivedMessage.ulMessageValue );\r
407                                                                                                 break;\r
408 \r
409                         case mainMESSAGE_STATUS                 :       /* The tick interrupt hook\r
410                                                                                                 function has just informed this\r
411                                                                                                 task of the system status.\r
412                                                                                                 Generate a string in accordance\r
413                                                                                                 with the status value. */\r
414                                                                                                 prvGenerateStatusMessage( cBuffer, xReceivedMessage.ulMessageValue );\r
415                                                                                                 break;\r
416 \r
417                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
418                                                                                                 break;\r
419                 }\r
420 \r
421                 /* Output the message that was placed into the cBuffer array within the\r
422                 switch statement above, then move onto the next line ready for the next\r
423                 message to arrive on the queue. */\r
424                 halLcdPrintLine( cBuffer, ucLine,  OVERWRITE_TEXT );\r
425                 ucLine++;\r
426         }\r
427 }\r
428 /*-----------------------------------------------------------*/\r
429 \r
430 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )\r
431 {\r
432         /* Just a utility function to convert a status value into a meaningful\r
433         string for output onto the LCD. */\r
434         switch( lStatusValue )\r
435         {\r
436                 case pdPASS                                             :       sprintf( pcBuffer, "Status = PASS" );\r
437                                                                                         break;\r
438                 case mainERROR_DYNAMIC_TASKS    :       sprintf( pcBuffer, "Err: Dynamic tsks" );\r
439                                                                                         break;\r
440                 case mainERROR_COM_TEST                 :       sprintf( pcBuffer, "Err: COM test" );\r
441                                                                                         break;\r
442                 case mainERROR_GEN_QUEUE_TEST   :       sprintf( pcBuffer, "Error: Gen Q test" );\r
443                                                                                         break;\r
444                 case mainERROR_REG_TEST                 :       sprintf( pcBuffer, "Error: Reg test" );\r
445                                                                                         break;\r
446                 default                                                 :       sprintf( pcBuffer, "Unknown status" );\r
447                                                                                         break;\r
448         }\r
449 }\r
450 /*-----------------------------------------------------------*/\r
451 \r
452 static void prvButtonPollTask( void *pvParameters )\r
453 {\r
454 unsigned char ucLastState = pdFALSE, ucState;\r
455 xQueueMessage xMessage;\r
456 \r
457         /* This tasks performs the button polling functionality as described at the\r
458         top of this file. */\r
459         for( ;; )\r
460         {\r
461                 /* Check the button state. */\r
462                 ucState = ( halButtonsPressed() & BUTTON_UP );\r
463 \r
464                 if( ucState != 0 )\r
465                 {\r
466                         /* The button was pressed. */\r
467                         ucState = pdTRUE;\r
468                 }\r
469 \r
470                 if( ucState != ucLastState )\r
471                 {\r
472                         /* The state has changed, send a message to the LCD task. */\r
473                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
474                         xMessage.ulMessageValue = ( unsigned long ) ucState;\r
475                         ucLastState = ucState;\r
476                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
477                 }\r
478 \r
479                 /* Block for 10 milliseconds so this task does not utilise all the CPU\r
480                 time and debouncing of the button is not necessary. */\r
481                 vTaskDelay( 10 / portTICK_PERIOD_MS );\r
482         }\r
483 }\r
484 /*-----------------------------------------------------------*/\r
485 \r
486 static void prvSetupHardware( void )\r
487 {\r
488         halBoardInit();\r
489 \r
490         LFXT_Start( XT1DRIVE_0 );\r
491         hal430SetSystemClock( configCPU_CLOCK_HZ, configLFXT_CLOCK_HZ );\r
492 \r
493         halButtonsInit( BUTTON_ALL );\r
494         halButtonsInterruptEnable( BUTTON_SELECT );\r
495 \r
496         /* Initialise the LCD, but note that the backlight is not used as the\r
497         library function uses timer A0 to modulate the backlight, and this file\r
498         defines vApplicationSetupTimerInterrupt() to also use timer A0 to generate\r
499         the tick interrupt.  If the backlight is required, then change either the\r
500         halLCD library or vApplicationSetupTimerInterrupt() to use a different\r
501         timer.  Timer A1 is used for the run time stats time base6. */\r
502         halLcdInit();\r
503         halLcdSetContrast( 100 );\r
504         halLcdClearScreen();\r
505 \r
506         halLcdPrintLine( " www.FreeRTOS.org", 0,  OVERWRITE_TEXT );\r
507 }\r
508 /*-----------------------------------------------------------*/\r
509 \r
510 void vApplicationTickHook( void )\r
511 {\r
512 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
513 static unsigned long ulCounter = 0;\r
514 static const unsigned long ulCheckFrequency = 5000UL / portTICK_PERIOD_MS;\r
515 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
516 \r
517 /* Define the status message that is sent to the LCD task.  By default the\r
518 status is PASS. */\r
519 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };\r
520 \r
521         /* This is called from within the tick interrupt and performs the 'check'\r
522         functionality as described in the comments at the top of this file.\r
523 \r
524         Is it time to perform the 'check' functionality again? */\r
525         ulCounter++;\r
526         if( ulCounter >= ulCheckFrequency )\r
527         {\r
528                 /* See if the standard demo tasks are executing as expected, changing\r
529                 the message that is sent to the LCD task from PASS to an error code if\r
530                 any tasks set reports an error. */\r
531                 if( xAreComTestTasksStillRunning() != pdPASS )\r
532                 {\r
533                         xStatusMessage.ulMessageValue = mainERROR_COM_TEST;\r
534                 }\r
535 \r
536                 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
537                 {\r
538                         xStatusMessage.ulMessageValue = mainERROR_DYNAMIC_TASKS;\r
539                 }\r
540 \r
541                 if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
542                 {\r
543                         xStatusMessage.ulMessageValue = mainERROR_GEN_QUEUE_TEST;\r
544                 }\r
545 \r
546                 /* Check the reg test tasks are still cycling.  They will stop\r
547                 incrementing their loop counters if they encounter an error. */\r
548                 if( usRegTest1Counter == usLastRegTest1Counter )\r
549                 {\r
550                         xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
551                 }\r
552 \r
553                 if( usRegTest2Counter == usLastRegTest2Counter )\r
554                 {\r
555                         xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
556                 }\r
557 \r
558                 usLastRegTest1Counter = usRegTest1Counter;\r
559                 usLastRegTest2Counter = usRegTest2Counter;\r
560 \r
561                 /* As this is the tick hook the lHigherPriorityTaskWoken parameter is not\r
562                 needed (a context switch is going to be performed anyway), but it must\r
563                 still be provided. */\r
564                 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &xHigherPriorityTaskWoken );\r
565                 ulCounter = 0;\r
566         }\r
567 \r
568         /* Just periodically toggle an LED to show that the tick interrupt is\r
569         running.  Note that this access LED_PORT_OUT in a non-atomic way, so tasks\r
570         that access the same port must do so from a critical section. */\r
571         if( ( ulCounter & 0xff ) == 0 )\r
572         {\r
573                 if( ( LED_PORT_OUT & LED_1 ) == 0 )\r
574                 {\r
575                         LED_PORT_OUT |= LED_1;\r
576                 }\r
577                 else\r
578                 {\r
579                         LED_PORT_OUT &= ~LED_1;\r
580                 }\r
581         }\r
582 }\r
583 /*-----------------------------------------------------------*/\r
584 \r
585 #pragma vector=PORT2_VECTOR\r
586 __interrupt static void prvSelectButtonInterrupt(void)\r
587 {\r
588 /* Define the message sent to the LCD task from this interrupt. */\r
589 static const xQueueMessage xMessage = { mainMESSAGE_BUTTON_SEL, ( unsigned long ) "Select Interrupt" };\r
590 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
591 \r
592         /* This is the interrupt handler for the joystick select button input.\r
593         The button has been pushed, write a message to the LCD via the LCD task. */\r
594         xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
595 \r
596         P2IFG = 0;\r
597 \r
598         /* If writing to xLCDQueue caused a task to unblock, and the unblocked task\r
599         has a priority equal to or above the task that this interrupt interrupted,\r
600         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
601         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
602         interrupt returns directly to the higher priority unblocked task. */\r
603         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
604 }\r
605 /*-----------------------------------------------------------*/\r
606 \r
607 /* The MSP430X port uses this callback function to configure its tick interrupt.\r
608 This allows the application to choose the tick interrupt source.\r
609 configTICK_VECTOR must also be set in FreeRTOSConfig.h to the correct\r
610 interrupt vector for the chosen tick interrupt source.  This implementation of\r
611 vApplicationSetupTimerInterrupt() generates the tick from timer A0, so in this\r
612 case configTICK_VECTOR is set to TIMER0_A0_VECTOR. */\r
613 void vApplicationSetupTimerInterrupt( void )\r
614 {\r
615 const unsigned short usACLK_Frequency_Hz = 32768;\r
616 \r
617         /* Ensure the timer is stopped. */\r
618         TA0CTL = 0;\r
619 \r
620         /* Run the timer from the ACLK. */\r
621         TA0CTL = TASSEL_1;\r
622 \r
623         /* Clear everything to start with. */\r
624         TA0CTL |= TACLR;\r
625 \r
626         /* Set the compare match value according to the tick rate we want. */\r
627         TA0CCR0 = usACLK_Frequency_Hz / configTICK_RATE_HZ;\r
628 \r
629         /* Enable the interrupts. */\r
630         TA0CCTL0 = CCIE;\r
631 \r
632         /* Start up clean. */\r
633         TA0CTL |= TACLR;\r
634 \r
635         /* Up mode. */\r
636         TA0CTL |= MC_1;\r
637 }\r
638 /*-----------------------------------------------------------*/\r
639 \r
640 void vApplicationIdleHook( void )\r
641 {\r
642         /* Called on each iteration of the idle task.  In this case the idle task\r
643         just enters a low power mode. */\r
644         __bis_SR_register( LPM3_bits + GIE );\r
645 }\r
646 /*-----------------------------------------------------------*/\r
647 \r
648 void vApplicationMallocFailedHook( void )\r
649 {\r
650         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
651         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
652         internally by FreeRTOS API functions that create tasks, queues or\r
653         semaphores. */\r
654         taskDISABLE_INTERRUPTS();\r
655         for( ;; );\r
656 }\r
657 /*-----------------------------------------------------------*/\r
658 \r
659 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
660 {\r
661         ( void ) pxTask;\r
662         ( void ) pcTaskName;\r
663 \r
664         /* Run time stack overflow checking is performed if\r
665         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
666         function is called if a stack overflow is detected. */\r
667         taskDISABLE_INTERRUPTS();\r
668         for( ;; );\r
669 }\r
670 /*-----------------------------------------------------------*/\r
671 \r