]> git.sur5r.net Git - freertos/blob - Demo/MSP430X_MSP430F5438_IAR/main.c
Add the debugger settings to the MSP430X IAR demo directory.
[freertos] / Demo / MSP430X_MSP430F5438_IAR / main.c
1 /*\r
2     FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Standard includes. */\r
55 #include <stdio.h>\r
56 \r
57 /* FreeRTOS includes. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "queue.h"\r
61 \r
62 /* Hardware includes. */\r
63 #include "msp430.h"\r
64 #include "hal_MSP-EXP430F5438.h"\r
65 \r
66 /* Standard demo includes. */\r
67 #include "ParTest.h"\r
68 #include "dynamic.h"\r
69 #include "comtest2.h"\r
70 #include "GenQTest.h"\r
71 \r
72 /* Codes sent within messages to the LCD task so the LCD task can interpret\r
73 exactly what the message it just received was.  These are sent in the\r
74 cMessageID member of the message structure (defined below). */\r
75 #define mainMESSAGE_BUTTON_UP                   ( 1 )\r
76 #define mainMESSAGE_BUTTON_SEL                  ( 2 )\r
77 #define mainMESSAGE_STATUS                              ( 3 )\r
78 \r
79 /* When the cMessageID member of the message sent to the LCD task is\r
80 mainMESSAGE_STATUS then these definitions are sent in the ulMessageValue member\r
81 of the same message and indicate what the status actually is. */\r
82 #define mainERROR_DYNAMIC_TASKS                 ( pdPASS + 1 )\r
83 #define mainERROR_COM_TEST                              ( pdPASS + 2 )\r
84 #define mainERROR_GEN_QUEUE_TEST                ( pdPASS + 3 )\r
85 #define mainERROR_REG_TEST                              ( pdPASS + 4 )\r
86 \r
87 /* The length of the queue (the number of items the queue can hold) that is used\r
88 to send messages from tasks and interrupts the the LCD task. */\r
89 #define mainQUEUE_LENGTH                                ( 5 )\r
90 \r
91 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
92 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
93 #define mainGENERIC_QUEUE_TEST_PRIORITY ( tskIDLE_PRIORITY )\r
94 \r
95 /* The LED used by the comtest tasks. See the comtest.c file for more\r
96 information.  In this case it is deliberately out of range as there are only\r
97 two LEDs, and they are both already in use. */\r
98 #define mainCOM_TEST_LED                                ( 3 )\r
99 \r
100 /* The baud rate used by the comtest tasks described at the top of this file. */\r
101 #define mainCOM_TEST_BAUD_RATE                  ( 9600 )\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 extern void vRegTest1Task( void *pvParameters );\r
105 extern void vRegTest2Task( void *pvParameters );\r
106 static void prvSetupHardware( void );\r
107 static void prvTerminalIOTask( void *pvParameters );\r
108 static void prvButtonPollTask( void *pvParameters );\r
109 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 volatile unsigned short usRegTest1Counter = 0, usRegTest2Counter = 0;\r
114 volatile unsigned long ulStatsOverflowCount = 0;\r
115 \r
116 /* The handle of the queue used to send messages from tasks and interrupts to\r
117 the LCD task. */\r
118 static xQueueHandle xLCDQueue = NULL;\r
119 \r
120 /* The definition of each message sent from tasks and interrupts to the LCD\r
121 task. */\r
122 typedef struct\r
123 {\r
124         char cMessageID;        /* << States what the message is. */\r
125         unsigned long ulMessageValue; /* << States the message value (can be an integer, string pointer, etc. depending on the value of cMessageID. */\r
126 } xQueueMessage;\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 void main( void )\r
130 {\r
131         prvSetupHardware();\r
132 \r
133         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
134         task. */\r
135         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
136 \r
137         if( xLCDQueue != NULL )\r
138         {\r
139                 /* Add the created queue to the queue registry so it can be viewed in\r
140                 the IAR FreeRTOS state viewer plug-in. */\r
141                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
142 \r
143                 /* Create the standard demo tasks. */\r
144                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
145                 vStartDynamicPriorityTasks();\r
146                 vStartGenericQueueTasks( mainGENERIC_QUEUE_TEST_PRIORITY );\r
147                 \r
148                 /* Create the terminal IO and button poll tasks, as described at the top\r
149                 of this file. */\r
150                 xTaskCreate( prvTerminalIOTask, ( signed char * ) "IO", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );\r
151                 xTaskCreate( prvButtonPollTask, ( signed char * ) "BPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
152 \r
153                 /* Create the register test tasks as described at the top of this file. */\r
154                 xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
155                 xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
156                 vTaskStartScheduler();\r
157         }\r
158         \r
159         for( ;; );\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 static void prvTerminalIOTask( void *pvParameters )\r
164 {\r
165 xQueueMessage xReceivedMessage;\r
166 \r
167 /* Buffer into which strings are formatted and placed ready for display on the\r
168 LCD.  Note this is a static variable to prevent it being allocated on the task\r
169 stack, which is too small to hold such a variable.  The stack size is configured\r
170 when the task is created. */\r
171 static char cBuffer[ 512 ];\r
172 unsigned char ucLine = 1;\r
173 \r
174 \r
175         /* This function is the only function that uses printf().  If printf() is\r
176         used from any other function then some sort of mutual exclusion on stdout\r
177         will be necessary.\r
178         \r
179         This is also the only function that is permitted to access the LCD.\r
180         \r
181         First print out the number of bytes that remain in the FreeRTOS heap.  This\r
182         can be viewed in the terminal IO window within the IAR Embedded Workbench. */\r
183         printf( "%d bytes of heap space remain unallocated\n", ( int ) xPortGetFreeHeapSize() );\r
184 \r
185         for( ;; )\r
186         {\r
187                 /* Wait for a message to be received.  Using portMAX_DELAY as the block\r
188                 time will result in an indefinite wait provided INCLUDE_vTaskSuspend is\r
189                 set to 1 in FreeRTOSConfig.h, therefore there is no need to check the\r
190                 function return value and the function will only return when a value\r
191                 has been received. */\r
192                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
193 \r
194                 /* Clear the LCD if no room remains for any more text output. */\r
195                 if( ucLine > 8 )\r
196                 {\r
197                         halLcdClearScreen();\r
198                         ucLine = 0;\r
199                 }\r
200                 \r
201                 /* What is this message?  What does it contain? */\r
202                 switch( xReceivedMessage.cMessageID )\r
203                 {\r
204                         case mainMESSAGE_BUTTON_UP              :       /* The button poll task has just\r
205                                                                                                 informed this task that the up\r
206                                                                                                 button on the joystick input has\r
207                                                                                                 been pressed or released. */\r
208                                                                                                 sprintf( cBuffer, "Button up = %d", ( int ) xReceivedMessage.ulMessageValue );\r
209                                                                                                 break;\r
210 \r
211                         case mainMESSAGE_BUTTON_SEL             :       /* The select button interrupt\r
212                                                                                                 just informed this task that the\r
213                                                                                                 select button was pressed.\r
214                                                                                                 Generate a table of task run time\r
215                                                                                                 statistics and output this to\r
216                                                                                                 the terminal IO window in the IAR\r
217                                                                                                 embedded workbench. */\r
218                                                                                                 printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************" );\r
219                                                                                                 fflush( stdout );\r
220                                                                                                 vTaskGetRunTimeStats( ( signed char * ) cBuffer );\r
221                                                                                                 printf( cBuffer );\r
222                                                                                                 fflush( stdout );\r
223                                                                                                 \r
224                                                                                                 /* Also print out a message to\r
225                                                                                                 the LCD - in this case the\r
226                                                                                                 pointer to the string to print\r
227                                                                                                 is sent directly in the\r
228                                                                                                 lMessageValue member of the\r
229                                                                                                 message.  This just demonstrates\r
230                                                                                                 a different communication\r
231                                                                                                 technique. */\r
232                                                                                                 sprintf( cBuffer, "%s", ( char * ) xReceivedMessage.ulMessageValue );\r
233                                                                                                 break;\r
234                                                                                                 \r
235                         case mainMESSAGE_STATUS                 :       /* The tick interrupt hook\r
236                                                                                                 function has just informed this\r
237                                                                                                 task of the system status.\r
238                                                                                                 Generate a string in accordance\r
239                                                                                                 with the status value. */\r
240                                                                                                 prvGenerateStatusMessage( cBuffer, xReceivedMessage.ulMessageValue );\r
241                                                                                                 break;\r
242                                                                                                 \r
243                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
244                                                                                                 break;\r
245                 }\r
246                 \r
247                 halLcdPrintLine( cBuffer, ucLine,  OVERWRITE_TEXT );\r
248                 ucLine++;\r
249         }\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )\r
254 {\r
255         /* Just a utility function to convert a status value into a meaningful\r
256         string for output onto the LCD. */\r
257         switch( lStatusValue )\r
258         {\r
259                 case pdPASS                                             :       sprintf( pcBuffer, "Status = PASS" );\r
260                                                                                         break;\r
261                 case mainERROR_DYNAMIC_TASKS    :       sprintf( pcBuffer, "Err: Dynamic tsks" );\r
262                                                                                         break;\r
263                 case mainERROR_COM_TEST                 :       sprintf( pcBuffer, "Err: COM test" ); /* Error in COM test - is the Loopback connector connected? */                                                                                                            \r
264                                                                                         break;\r
265                 case mainERROR_GEN_QUEUE_TEST   :       sprintf( pcBuffer, "Error: Gen Q test" );\r
266                                                                                         break;\r
267                 case mainERROR_REG_TEST                 :       sprintf( pcBuffer, "Error: Reg test" );\r
268                                                                                         break;\r
269                 default                                                 :       sprintf( pcBuffer, "Unknown status" );\r
270                                                                                         break;\r
271         }\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 static void prvButtonPollTask( void *pvParameters )\r
276 {\r
277 unsigned char ucLastState = pdFALSE, ucState;\r
278 xQueueMessage xMessage;\r
279 \r
280         /* This tasks performs the button polling functionality as described at the\r
281         top of this file. */\r
282         for( ;; )\r
283         {\r
284                 /* Check the button state. */\r
285                 ucState = ( halButtonsPressed() & BUTTON_UP );\r
286                 \r
287                 if( ucState != 0 )\r
288                 {\r
289                         ucState = pdTRUE;\r
290                 }\r
291                 \r
292                 if( ucState != ucLastState )\r
293                 {\r
294                         /* The state has changed, send a message to the LCD task. */\r
295                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
296                         xMessage.ulMessageValue = ( unsigned long ) ucState;\r
297                         ucLastState = ucState;\r
298                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
299                 }\r
300                 \r
301                 /* Block for 10 milliseconds so this task does not utilise all the CPU\r
302                 time and debouncing of the button is not necessary. */\r
303                 vTaskDelay( 10 / portTICK_RATE_MS );\r
304         }\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 static void prvSetupHardware( void )\r
309 {\r
310 unsigned long ulCPU_Clock_KHz = ( configCPU_CLOCK_HZ / 1000UL );\r
311 \r
312         halBoardInit();\r
313 \r
314         LFXT_Start( XT1DRIVE_0 );\r
315         Init_FLL_Settle( ( unsigned short ) ulCPU_Clock_KHz, 488 );\r
316 \r
317         halButtonsInit( BUTTON_ALL );\r
318         halButtonsInterruptEnable( BUTTON_SELECT );\r
319         halLcdInit();\r
320         halLcdBackLightInit();\r
321         halLcdSetBackLight( 0 );\r
322         halLcdSetContrast( 100 );\r
323         halLcdClearScreen();\r
324         \r
325         halLcdPrintLine( " www.FreeRTOS.org", 0,  OVERWRITE_TEXT );\r
326         \r
327 while( ( halButtonsPressed() & BUTTON_UP ) == 0 );\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 void vApplicationSetupTimerInterrupt( void )\r
332 {\r
333 const unsigned short usACLK_Frequency_Hz = 32768;\r
334 \r
335         /* Ensure the timer is stopped. */\r
336         TA0CTL = 0;\r
337 \r
338         /* Run the timer from the ACLK. */\r
339         TA0CTL = TASSEL_1;\r
340 \r
341         /* Clear everything to start with. */\r
342         TA0CTL |= TACLR;\r
343 \r
344         /* Set the compare match value according to the tick rate we want. */\r
345         TA0CCR0 = usACLK_Frequency_Hz / configTICK_RATE_HZ;\r
346 \r
347         /* Enable the interrupts. */\r
348         TA0CCTL0 = CCIE;\r
349 \r
350         /* Start up clean. */\r
351         TA0CTL |= TACLR;\r
352 \r
353         /* Up mode. */\r
354         TA0CTL |= MC_1;\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 void vApplicationMallocFailedHook( void )\r
359 {\r
360         for( ;; );\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
365 {\r
366         ( void ) pxTask;\r
367         ( void ) pcTaskName;\r
368         \r
369         for( ;; );\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 void vApplicationIdleHook( void )\r
374 {\r
375         /* Want to leave the SMCLK running so the COMTest tasks don't fail. */\r
376         __bis_SR_register( LPM1_bits + GIE );\r
377 }\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 void vApplicationTickHook( void )\r
381 {\r
382 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
383 static unsigned long ulCounter = 0;\r
384 static const unsigned long ulCheckFrequency = 5000UL / portTICK_RATE_MS;\r
385 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
386 \r
387 /* Define the status message that is sent to the LCD task.  By default the\r
388 status is PASS. */\r
389 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };\r
390 \r
391         /* This is called from within the tick interrupt and performs the 'check'\r
392         functionality as described in the comments at the top of this file.\r
393 \r
394         Is it time to perform the 'check' functionality again? */\r
395         ulCounter++;\r
396         if( ulCounter >= ulCheckFrequency )\r
397         {\r
398                 /* See if the standard demo tasks are executing as expected, changing\r
399                 the message that is sent to the LCD task from PASS to an error code if\r
400                 any tasks set reports an error. */\r
401                 if( xAreComTestTasksStillRunning() != pdPASS )\r
402                 {\r
403                         xStatusMessage.ulMessageValue = mainERROR_COM_TEST;\r
404                 }\r
405 \r
406                 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
407                 {\r
408                         xStatusMessage.ulMessageValue = mainERROR_DYNAMIC_TASKS;\r
409                 }\r
410                 \r
411                 if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
412                 {\r
413                         xStatusMessage.ulMessageValue = mainERROR_GEN_QUEUE_TEST;\r
414                 }                       \r
415 \r
416                 /* Check the reg test tasks are still cycling.  They will stop incrementing\r
417                 their loop counters if they encounter an error. */\r
418                 if( usRegTest1Counter == usLastRegTest1Counter )\r
419                 {\r
420                         xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
421                 }\r
422 \r
423                 if( usRegTest2Counter == usLastRegTest2Counter )\r
424                 {\r
425                         xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
426                 }\r
427 \r
428                 usLastRegTest1Counter = usRegTest1Counter;\r
429                 usLastRegTest2Counter = usRegTest2Counter;\r
430                 \r
431                 /* As this is the tick hook the lHigherPriorityTaskWoken parameter is not\r
432                 needed (a context switch is going to be performed anyway), but it must\r
433                 still be provided. */\r
434                 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &xHigherPriorityTaskWoken );\r
435                 ulCounter = 0;\r
436         }\r
437 \r
438         if( ( ulCounter & 0xff ) == 0 )\r
439         {\r
440                 if( ( LED_PORT_OUT & LED_1 ) == 0 )\r
441                 {\r
442                         LED_PORT_OUT |= LED_1;\r
443                         LED_PORT_OUT &= ~LED_2;\r
444                 }\r
445                 else\r
446                 {\r
447                         LED_PORT_OUT &= ~LED_1;\r
448                         LED_PORT_OUT |= LED_2;\r
449                 }\r
450         }\r
451 }\r
452 /*-----------------------------------------------------------*/\r
453 \r
454 #pragma vector=PORT2_VECTOR\r
455 __interrupt static void prvSelectButtonInterrupt(void)\r
456 {\r
457 /* Define the message sent to the LCD task from this interrupt. */\r
458 static const xQueueMessage xMessage = { mainMESSAGE_BUTTON_SEL, ( unsigned long ) "Select Interrupt" };\r
459 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
460 \r
461         /* This is the interrupt handler for the joystick select button input.\r
462         The button has been pushed, write a message to the LCD via the LCD task. */\r
463         xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
464 \r
465         P2IFG = 0;\r
466         \r
467         /* If writing to xLCDQueue caused a task to unblock, and the unblocked task\r
468         has a priority equal to or above the task that this interrupt interrupted,\r
469         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
470         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
471         interrupt returns directly to the higher priority unblocked task. */\r
472         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
473 }\r
474 /*-----------------------------------------------------------*/\r
475 \r
476 void vConfigureTimerForRunTimeStats( void )\r
477 {\r
478         /* Ensure the timer is stopped. */\r
479         TA1CTL = 0;\r
480 \r
481         /* Run the timer from the ACLK/4. */\r
482         TA1CTL = TASSEL_1 | ID__4;\r
483 \r
484         /* Clear everything to start with. */\r
485         TA1CTL |= TACLR;\r
486 \r
487         /* Enable the interrupts. */\r
488         TA1CCTL0 = CCIE;\r
489 \r
490         /* Start up clean. */\r
491         TA1CTL |= TACLR;\r
492 \r
493         /* Continuous mode. */\r
494         TA1CTL |= MC__CONTINOUS;\r
495 }\r
496 /*-----------------------------------------------------------*/\r
497 \r
498 #pragma vector=TIMER1_A0_VECTOR\r
499 static __interrupt void prvRunTimeStatsOverflowISR( void )\r
500 {\r
501         ulStatsOverflowCount++;\r
502 }\r
503 /*-----------------------------------------------------------*/\r
504 \r
505 inline unsigned long ulGetRunTimeStatsTime( void )\r
506 {\r
507 unsigned long ulReturn;\r
508 \r
509         TA1CTL &= ~MC__CONTINOUS;\r
510         ulReturn = ( ( ulStatsOverflowCount << 16UL ) | ( unsigned long ) TA1R );\r
511         TA1CTL |= MC__CONTINOUS;\r
512         \r
513         return ulReturn;\r
514 }\r
515 \r
516 \r
517 \r
518 \r