]> git.sur5r.net Git - freertos/blob - Demo/MSP430X_MSP430F5438_IAR/main.c
Continue work on the MSP430X demo - still a work in progress.
[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 "comtest2.h"\r
69 \r
70 /* Codes sent within messages to the LCD task so the LCD task can interpret\r
71 exactly what the message it just received was.  These are sent in the\r
72 cMessageID member of the message structure (defined below). */\r
73 #define mainMESSAGE_BUTTON_UP                   ( 1 )\r
74 #define mainMESSAGE_BUTTON_SEL                  ( 2 )\r
75 #define mainMESSAGE_STATUS                              ( 3 )\r
76 \r
77 /* When the cMessageID member of the message sent to the LCD task is\r
78 mainMESSAGE_STATUS then these definitions are sent in the ulMessageValue member\r
79 of the same message and indicate what the status actually is. */\r
80 #define mainERROR_DYNAMIC_TASKS                 ( pdPASS + 1 )\r
81 #define mainERROR_COM_TEST                              ( pdPASS + 2 )\r
82 #define mainERROR_GEN_QUEUE_TEST                ( pdPASS + 3 )\r
83 #define mainERROR_REG_TEST                              ( pdPASS + 4 )\r
84 \r
85 /* The length of the queue (the number of items the queue can hold) that is used\r
86 to send messages from tasks and interrupts the the LCD task. */\r
87 #define mainQUEUE_LENGTH                                ( 5 )\r
88 \r
89 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
90 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
91 \r
92 /* The LED used by the comtest tasks. See the comtest.c file for more\r
93 information.  In this case it is deliberately out of range as there are only\r
94 two LEDs, and they are both already in use. */\r
95 #define mainCOM_TEST_LED                        ( 3 )\r
96 \r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 extern void vRegTest1Task( void *pvParameters );\r
101 extern void vRegTest2Task( void *pvParameters );\r
102 static void prvSetupHardware( void );\r
103 static void prvTerminalIOTask( void *pvParameters );\r
104 static void prvButtonPollTask( void *pvParameters );\r
105 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 volatile unsigned short usRegTest1Counter = 0, usRegTest2Counter = 0;\r
110 volatile unsigned long ulStatsOverflowCount = 0;\r
111 \r
112 /* The handle of the queue used to send messages from tasks and interrupts to\r
113 the LCD task. */\r
114 static xQueueHandle xLCDQueue = NULL;\r
115 \r
116 /* The definition of each message sent from tasks and interrupts to the LCD\r
117 task. */\r
118 typedef struct\r
119 {\r
120         char cMessageID;        /* << States what the message is. */\r
121         unsigned long ulMessageValue; /* << States the message value (can be an integer, string pointer, etc. depending on the value of cMessageID. */\r
122 } xQueueMessage;\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void main( void )\r
126 {\r
127         prvSetupHardware();\r
128 \r
129         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
130         task. */\r
131         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
132 \r
133         if( xLCDQueue != NULL )\r
134         {\r
135                 /* Add the created queue to the queue registry so it can be viewed in\r
136                 the IAR FreeRTOS state viewer plug-in. */\r
137                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
138 \r
139                 /* Create the standard demo tasks. */\r
140                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, 9600, mainCOM_TEST_LED );\r
141                 \r
142                 /* Create the terminal IO and button poll tasks, as described at the top\r
143                 of this file. */\r
144                 xTaskCreate( prvTerminalIOTask, ( signed char * ) "IO", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );\r
145                 xTaskCreate( prvButtonPollTask, ( signed char * ) "BPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
146 \r
147                 xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
148                 xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
149                 vTaskStartScheduler();\r
150         }\r
151         for( ;; );\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 static void prvTerminalIOTask( void *pvParameters )\r
156 {\r
157 xQueueMessage xReceivedMessage;\r
158 \r
159 /* Buffer into which strings are formatted and placed ready for display on the\r
160 LCD.  Note this is a static variable to prevent it being allocated on the task\r
161 stack, which is too small to hold such a variable.  The stack size is configured\r
162 when the task is created. */\r
163 static char cBuffer[ 512 ];\r
164 \r
165         /* This function is the only function that uses printf().  If printf() is\r
166         used from any other function then some sort of mutual exclusion on stdout\r
167         will be necessary.\r
168         \r
169         This is also the only function that is permitted to access the LCD.\r
170         \r
171         First print out the number of bytes that remain in the FreeRTOS heap.  This\r
172         can be viewed in the terminal IO window within the IAR Embedded Workbench. */\r
173         printf( "%d bytes of heap space remain unallocated\n", ( int ) xPortGetFreeHeapSize() );\r
174 \r
175         for( ;; )\r
176         {\r
177                 /* Wait for a message to be received.  Using portMAX_DELAY as the block\r
178                 time will result in an indefinite wait provided INCLUDE_vTaskSuspend is\r
179                 set to 1 in FreeRTOSConfig.h, therefore there is no need to check the\r
180                 function return value and the function will only return when a value\r
181                 has been received. */\r
182                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
183 \r
184                 /* What is this message?  What does it contain? */\r
185                 switch( xReceivedMessage.cMessageID )\r
186                 {\r
187                         case mainMESSAGE_BUTTON_UP              :       /* The button poll task has just\r
188                                                                                                 informed this task that the up\r
189                                                                                                 button on the joystick input has\r
190                                                                                                 been pressed or released. */\r
191                                                                                                 sprintf( cBuffer, "Button up = %d", ( int ) xReceivedMessage.ulMessageValue );\r
192                                                                                                 break;\r
193 \r
194                         case mainMESSAGE_BUTTON_SEL             :       /* The select button interrupt\r
195                                                                                                 just informed this task that the\r
196                                                                                                 select button was pressed.\r
197                                                                                                 Generate a table of task run time\r
198                                                                                                 statistics and output this to\r
199                                                                                                 the terminal IO window in the IAR\r
200                                                                                                 embedded workbench. */\r
201                                                                                                 printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************" );\r
202                                                                                                 fflush( stdout );\r
203                                                                                                 vTaskGetRunTimeStats( ( signed char * ) cBuffer );\r
204 //                                                                                              printf( cBuffer );\r
205                                                                                                 break;\r
206                                                                                                 \r
207                         case mainMESSAGE_STATUS                 :       /* The tick interrupt hook\r
208                                                                                                 function has just informed this\r
209                                                                                                 task of the system status.\r
210                                                                                                 Generate a string in accordance\r
211                                                                                                 with the status value. */\r
212                                                                                                 prvGenerateStatusMessage( cBuffer, xReceivedMessage.ulMessageValue );\r
213                                                                                                 break;\r
214                                                                                                 \r
215                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
216                                                                                                 break;\r
217                 }\r
218                 \r
219                 /* Output the message that was placed into the cBuffer array within the\r
220                 switch statement above. */\r
221                 printf( "%s : %u\n", cBuffer, ( unsigned int ) xTaskGetTickCount() );\r
222                 fflush( stdout );\r
223         }\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )\r
228 {\r
229         /* Just a utility function to convert a status value into a meaningful\r
230         string for output onto the LCD. */\r
231         switch( lStatusValue )\r
232         {\r
233                 case pdPASS                                             :       sprintf( pcBuffer, "Task status = PASS" );\r
234                                                                                         break;\r
235                 case mainERROR_DYNAMIC_TASKS    :       sprintf( pcBuffer, "Error: Dynamic tasks" );\r
236                                                                                         break;\r
237                 case mainERROR_COM_TEST                 :       sprintf( pcBuffer, "Err: COM test" ); /* Error in COM test - is the Loopback connector connected? */                                                                                                            \r
238                                                                                         break;\r
239                 case mainERROR_GEN_QUEUE_TEST   :       sprintf( pcBuffer, "Error: Gen Q test" );\r
240                                                                                         break;\r
241                 case mainERROR_REG_TEST                 :       sprintf( pcBuffer, "Error: Reg test" );\r
242                                                                                         break;\r
243                 default                                                 :       sprintf( pcBuffer, "Unknown status" );\r
244                                                                                         break;\r
245         }\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static void prvButtonPollTask( void *pvParameters )\r
250 {\r
251 unsigned char ucLastState = pdFALSE, ucState;\r
252 xQueueMessage xMessage;\r
253 \r
254         /* This tasks performs the button polling functionality as described at the\r
255         top of this file. */\r
256         for( ;; )\r
257         {\r
258                 /* Check the button state. */\r
259                 ucState = ( halButtonsPressed() & BUTTON_UP );\r
260                 \r
261                 if( ucState != 0 )\r
262                 {\r
263                         ucState = pdTRUE;\r
264                 }\r
265                 \r
266                 if( ucState != ucLastState )\r
267                 {\r
268                         /* The state has changed, send a message to the LCD task. */\r
269                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
270                         xMessage.ulMessageValue = ( unsigned long ) ucState;\r
271                         ucLastState = ucState;\r
272                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
273                 }\r
274                 \r
275                 /* Block for 10 milliseconds so this task does not utilise all the CPU\r
276                 time and debouncing of the button is not necessary. */\r
277                 vTaskDelay( 10 / portTICK_RATE_MS );\r
278         }\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 static void prvSetupHardware( void )\r
283 {\r
284 unsigned long ulCPU_Clock_KHz = ( configCPU_CLOCK_HZ / 1000UL );\r
285 \r
286         halBoardInit();\r
287         halButtonsInit( BUTTON_ALL );\r
288         halButtonsInterruptEnable( BUTTON_SELECT );\r
289         LFXT_Start( XT1DRIVE_0 );\r
290         Init_FLL_Settle( ( unsigned short ) ulCPU_Clock_KHz, 488 );\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 void vApplicationSetupTimerInterrupt( void )\r
295 {\r
296 const unsigned short usACLK_Frequency_Hz = 32768;\r
297 \r
298         /* Ensure the timer is stopped. */\r
299         TA0CTL = 0;\r
300 \r
301         /* Run the timer from the ACLK. */\r
302         TA0CTL = TASSEL_1;\r
303 \r
304         /* Clear everything to start with. */\r
305         TA0CTL |= TACLR;\r
306 \r
307         /* Set the compare match value according to the tick rate we want. */\r
308         TA0CCR0 = usACLK_Frequency_Hz / configTICK_RATE_HZ;\r
309 \r
310         /* Enable the interrupts. */\r
311         TA0CCTL0 = CCIE;\r
312 \r
313         /* Start up clean. */\r
314         TA0CTL |= TACLR;\r
315 \r
316         /* Up mode. */\r
317         TA0CTL |= MC_1;\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 void vApplicationMallocFailedHook( void )\r
322 {\r
323         for( ;; );\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
328 {\r
329         ( void ) pxTask;\r
330         ( void ) pcTaskName;\r
331         \r
332         for( ;; );\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 void vApplicationIdleHook( void )\r
337 {\r
338         /* Want to leave the SMCLK running so the COMTest tasks don't fail. */\r
339         __bis_SR_register( LPM1_bits + GIE );\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 void vApplicationTickHook( void )\r
344 {\r
345 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
346 static unsigned long ulCounter = 0;\r
347 static const unsigned long ulCheckFrequency = 5000UL / portTICK_RATE_MS;\r
348 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
349 \r
350 /* Define the status message that is sent to the LCD task.  By default the\r
351 status is PASS. */\r
352 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };\r
353 \r
354         /* This is called from within the tick interrupt and performs the 'check'\r
355         functionality as described in the comments at the top of this file.\r
356 \r
357         Is it time to perform the 'check' functionality again? */\r
358         ulCounter++;\r
359         if( ulCounter >= ulCheckFrequency )\r
360         {\r
361                 #ifdef LEFT_OVER_FROM_CUT_AND_PASTE\r
362                         /* See if the standard demo tasks are executing as expected, changing\r
363                         the message that is sent to the LCD task from PASS to an error code if\r
364                         any tasks set reports an error. */\r
365                         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
366                         {\r
367                                 xStatusMessage.lMessageValue = mainERROR_DYNAMIC_TASKS;\r
368                         }\r
369                         \r
370                         if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
371                         {\r
372                                 xStatusMessage.lMessageValue = mainERROR_GEN_QUEUE_TEST;\r
373                         }\r
374                 #else\r
375                         /* See if the standard demo tasks are executing as expected, changing\r
376                         the message that is sent to the LCD task from PASS to an error code if\r
377                         any tasks set reports an error. */\r
378                         if( xAreComTestTasksStillRunning() != pdPASS )\r
379                         {\r
380                                 xStatusMessage.ulMessageValue = mainERROR_COM_TEST;\r
381                         }\r
382 \r
383 \r
384                         /* Check the reg test tasks are still cycling.  They will stop incrementing\r
385                         their loop counters if they encounter an error. */\r
386                         if( usRegTest1Counter == usLastRegTest1Counter )\r
387                         {\r
388                                 xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
389                         }\r
390         \r
391                         if( usRegTest2Counter == usLastRegTest2Counter )\r
392                         {\r
393                                 xStatusMessage.ulMessageValue = mainERROR_REG_TEST;\r
394                         }\r
395         \r
396                         usLastRegTest1Counter = usRegTest1Counter;\r
397                         usLastRegTest2Counter = usRegTest2Counter;\r
398                 #endif\r
399                 \r
400                 /* As this is the tick hook the lHigherPriorityTaskWoken parameter is not\r
401                 needed (a context switch is going to be performed anyway), but it must\r
402                 still be provided. */\r
403                 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &xHigherPriorityTaskWoken );\r
404                 ulCounter = 0;\r
405         }\r
406 \r
407         if( ( ulCounter & 0xff ) == 0 )\r
408         {\r
409                 if( ( LED_PORT_OUT & LED_1 ) == 0 )\r
410                 {\r
411                         LED_PORT_OUT |= LED_1;\r
412                         LED_PORT_OUT &= ~LED_2;\r
413                 }\r
414                 else\r
415                 {\r
416                         LED_PORT_OUT &= ~LED_1;\r
417                         LED_PORT_OUT |= LED_2;\r
418                 }\r
419         }\r
420 }\r
421 /*-----------------------------------------------------------*/\r
422 \r
423 #pragma vector=PORT2_VECTOR\r
424 __interrupt static void prvSelectButtonInterrupt(void)\r
425 {\r
426 /* Define the message sent to the LCD task from this interrupt. */\r
427 static const xQueueMessage xMessage = { mainMESSAGE_BUTTON_SEL, ( unsigned long ) "Select Interrupt!" };\r
428 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
429 \r
430         /* This is the interrupt handler for the joystick select button input.\r
431         The button has been pushed, write a message to the LCD via the LCD task. */\r
432         xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
433 \r
434         P2IFG = 0;\r
435         \r
436         /* If writing to xLCDQueue caused a task to unblock, and the unblocked task\r
437         has a priority equal to or above the task that this interrupt interrupted,\r
438         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
439         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
440         interrupt returns directly to the higher priority unblocked task. */\r
441         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
442 }\r
443 /*-----------------------------------------------------------*/\r
444 \r
445 void vConfigureTimerForRunTimeStats( void )\r
446 {\r
447         /* Ensure the timer is stopped. */\r
448         TA1CTL = 0;\r
449 \r
450         /* Run the timer from the ACLK/4. */\r
451         TA1CTL = TASSEL_1 | ID__4;\r
452 \r
453         /* Clear everything to start with. */\r
454         TA1CTL |= TACLR;\r
455 \r
456         /* Enable the interrupts. */\r
457         TA1CCTL0 = CCIE;\r
458 \r
459         /* Start up clean. */\r
460         TA1CTL |= TACLR;\r
461 \r
462         /* Continuous mode. */\r
463         TA1CTL |= MC__CONTINOUS;\r
464 }\r
465 /*-----------------------------------------------------------*/\r
466 \r
467 #pragma vector=TIMER1_A0_VECTOR\r
468 static __interrupt void prvRunTimeStatsOverflowISR( void )\r
469 {\r
470         ulStatsOverflowCount++;\r
471 }\r
472 /*-----------------------------------------------------------*/\r
473 \r
474 inline unsigned long ulGetRunTimeStatsTime( void )\r
475 {\r
476 unsigned long ulReturn;\r
477 \r
478         TA1CTL &= ~MC__CONTINOUS;\r
479         ulReturn = ( ( ulStatsOverflowCount << 16UL ) | ( unsigned long ) TA1R );\r
480         TA1CTL |= MC__CONTINOUS;\r
481         \r
482         return ulReturn;\r
483 }\r
484 \r
485 \r
486 \r
487 \r