]> git.sur5r.net Git - freertos/blob - Demo/MSP430X_MSP430F5438_IAR/main.c
Start to write the MSP430X demo.
[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 #include <stdio.h>\r
54 \r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 #include "queue.h"\r
58 \r
59 #include "msp430.h"\r
60 #include "hal_MSP-EXP430F5438.h"\r
61 \r
62 /* The rate at which mainCHECK_LED will toggle when all the tasks are running\r
63 without error.  Controlled by the check task as described at the top of this\r
64 file. */\r
65 #define mainNO_ERROR_CYCLE_TIME         ( 5000 / portTICK_RATE_MS )\r
66 \r
67 /* The rate at which mainCHECK_LED will toggle when an error has been reported\r
68 by at least one task.  Controlled by the check task as described at the top of\r
69 this file. */\r
70 #define mainERROR_CYCLE_TIME            ( 200 / portTICK_RATE_MS )\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 cMessageValue 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 \r
86 /* The length of the queue (the number of items the queue can hold) that is used\r
87 to send messages from tasks and interrupts the the LCD task. */\r
88 #define mainQUEUE_LENGTH                                ( 5 )\r
89 \r
90 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 extern void vRegTest1Task( void *pvParameters );\r
95 extern void vRegTest2Task( void *pvParameters );\r
96 static void prvCheckTask( void *pvParameters );\r
97 static void prvSetupHardware( void );\r
98 static void prvTerminalIOTask( void *pvParameters );\r
99 static void prvButtonPollTask( void *pvParameters );\r
100 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 volatile unsigned short usRegTest1Counter = 0, usRegTest2Counter = 0;\r
105 \r
106 /* The handle of the queue used to send messages from tasks and interrupts to\r
107 the LCD task. */\r
108 static xQueueHandle xLCDQueue = NULL;\r
109 \r
110 /* The definition of each message sent from tasks and interrupts to the LCD\r
111 task. */\r
112 typedef struct\r
113 {\r
114         char cMessageID;        /* << States what the message is. */\r
115         char cMessageValue; /* << States the message value (can be an integer, string pointer, etc. depending on the value of cMessageID. */\r
116 } xQueueMessage;\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void main( void )\r
120 {\r
121         prvSetupHardware();\r
122         \r
123         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
124         task. */\r
125         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
126         \r
127         if( xLCDQueue != NULL )\r
128         {\r
129                 /* Add the created queue to the queue registry so it can be viewed in\r
130                 the IAR FreeRTOS state viewer plug-in. */\r
131                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
132 \r
133                 /* Create the terminal IO and button poll tasks, as described at the top\r
134                 of this file. */\r
135                 xTaskCreate( prvTerminalIOTask, ( signed char * ) "LCD", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );\r
136                 xTaskCreate( prvButtonPollTask, ( signed char * ) "ButPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
137 \r
138                 xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
139                 xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
140                 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
141                 vTaskStartScheduler();\r
142         }\r
143         for( ;; );\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 static void prvTerminalIOTask( void *pvParameters )\r
148 {\r
149 xQueueMessage xReceivedMessage;\r
150 \r
151 /* Buffer into which strings are formatted and placed ready for display on the\r
152 LCD.  Note this is a static variable to prevent it being allocated on the task\r
153 stack, which is too small to hold such a variable.  The stack size is configured\r
154 when the task is created. */\r
155 static char cBuffer[ 512 ];\r
156 \r
157         /* This function is the only function that uses printf().  If printf() is\r
158         used from any other function then some sort of mutual exclusion on stdout\r
159         will be necessary.\r
160         \r
161         This is also the only function that is permitted to access the LCD.\r
162         \r
163         First print out the number of bytes that remain in the FreeRTOS heap.  This\r
164         can be viewed in the terminal IO window within the IAR Embedded Workbench. */\r
165         printf( "%d bytes of heap space remain unallocated\n", ( int ) xPortGetFreeHeapSize() );\r
166 \r
167         for( ;; )\r
168         {\r
169                 /* Wait for a message to be received.  Using portMAX_DELAY as the block\r
170                 time will result in an indefinite wait provided INCLUDE_vTaskSuspend is\r
171                 set to 1 in FreeRTOSConfig.h, therefore there is no need to check the\r
172                 function return value and the function will only return when a value\r
173                 has been received. */\r
174                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
175 \r
176                 /* What is this message?  What does it contain? */\r
177                 switch( xReceivedMessage.cMessageID )\r
178                 {\r
179                         case mainMESSAGE_BUTTON_UP              :       /* The button poll task has just\r
180                                                                                                 informed this task that the up\r
181                                                                                                 button on the joystick input has\r
182                                                                                                 been pressed or released. */\r
183                                                                                                 sprintf( cBuffer, "Button up = %d", xReceivedMessage.cMessageValue );\r
184                                                                                                 break;\r
185 \r
186                         case mainMESSAGE_BUTTON_SEL             :       /* The select button interrupt\r
187                                                                                                 just informed this task that the\r
188                                                                                                 select button was pressed.\r
189                                                                                                 Generate a table of task run time\r
190                                                                                                 statistics and output this to\r
191                                                                                                 the terminal IO window in the IAR\r
192                                                                                                 embedded workbench. */\r
193                                                                                                 printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************" );\r
194 //                                                                                              vTaskGetRunTimeStats( ( signed char * ) cBuffer );\r
195 //                                                                                              printf( cBuffer );\r
196                                                                                                 break;\r
197                                                                                                 \r
198                         case mainMESSAGE_STATUS                 :       /* The tick interrupt hook\r
199                                                                                                 function has just informed this\r
200                                                                                                 task of the system status.\r
201                                                                                                 Generate a string in accordance\r
202                                                                                                 with the status value. */\r
203                                                                                                 prvGenerateStatusMessage( cBuffer, xReceivedMessage.cMessageValue );\r
204                                                                                                 break;\r
205                                                                                                 \r
206                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
207                                                                                                 break;\r
208                 }\r
209                 \r
210                 /* Output the message that was placed into the cBuffer array within the\r
211                 switch statement above. */\r
212                 printf( "%s", cBuffer );\r
213         }\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )\r
218 {\r
219         /* Just a utility function to convert a status value into a meaningful\r
220         string for output onto the LCD. */\r
221         switch( lStatusValue )\r
222         {\r
223                 case pdPASS                                             :       sprintf( pcBuffer, "Task status = PASS" );\r
224                                                                                         break;\r
225                 case mainERROR_DYNAMIC_TASKS    :       sprintf( pcBuffer, "Error: Dynamic tasks" );\r
226                                                                                         break;\r
227                 case mainERROR_COM_TEST                 :       sprintf( pcBuffer, "Err: loop connected?" ); /* Error in COM test - is the Loopback connector connected? */                                                                                                             \r
228                                                                                         break;\r
229                 case mainERROR_GEN_QUEUE_TEST   :       sprintf( pcBuffer, "Error: Gen Q test" );\r
230                                                                                         break;\r
231                 default                                                 :       sprintf( pcBuffer, "Unknown status" );\r
232                                                                                         break;\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvButtonPollTask( void *pvParameters )\r
238 {\r
239 unsigned char ucLastState = pdFALSE, ucState;\r
240 xQueueMessage xMessage;\r
241 \r
242         /* This tasks performs the button polling functionality as described at the\r
243         top of this file. */\r
244         for( ;; )\r
245         {\r
246                 /* Check the button state. */\r
247                 ucState = ( halButtonsPressed() & BUTTON_UP );\r
248                 if( ucState != ucLastState )\r
249                 {\r
250                         /* The state has changed, send a message to the LCD task. */\r
251                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
252                         xMessage.cMessageValue = ucState;\r
253                         ucLastState = ucState;\r
254                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
255                 }\r
256                 \r
257                 /* Block for 10 milliseconds so this task does not utilise all the CPU\r
258                 time and debouncing of the button is not necessary. */\r
259                 vTaskDelay( 10 / portTICK_RATE_MS );\r
260         }\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 static void prvSetupHardware( void )\r
265 {\r
266         halBoardInit();\r
267         halButtonsInit( BUTTON_ALL );\r
268         halButtonsInterruptEnable( BUTTON_SELECT );\r
269         LFXT_Start (XT1DRIVE_0);\r
270         Init_FLL_Settle( 25000, 488 );\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void prvCheckTask( void *pvParameters )\r
275 {\r
276 volatile unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
277 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;\r
278 const char *pcStatusMessage = "OK";\r
279 \r
280         /* Initialise xNextWakeTime - this only needs to be done once. */\r
281         xNextWakeTime = xTaskGetTickCount();\r
282 \r
283         for( ;; )\r
284         {\r
285                 /* Place this task in the blocked state until it is time to run again. */\r
286                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
287 \r
288                 /* Check the reg test tasks are still cycling.  They will stop incrementing\r
289                 their loop counters if they encounter an error. */\r
290                 if( usRegTest1Counter == usLastRegTest1Counter )\r
291                 {\r
292                         pcStatusMessage = "Error: RegTest1";\r
293                 }\r
294 \r
295                 if( usRegTest2Counter == usLastRegTest2Counter )\r
296                 {\r
297                         pcStatusMessage = "Error: RegTest2";\r
298                 }\r
299 \r
300                 usLastRegTest1Counter = usRegTest1Counter;\r
301                 usLastRegTest2Counter = usRegTest2Counter;\r
302                 \r
303                 printf( "%s, tick count = %u\n", pcStatusMessage, ( unsigned int ) xTaskGetTickCount() );\r
304                 fflush( stdout );\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 void vApplicationSetupTimerInterrupt( void )\r
310 {\r
311 const unsigned short usACLK_Frequency_Hz = 32768;\r
312 \r
313         /* Ensure the timer is stopped. */\r
314         TA0CTL = 0;\r
315 \r
316         /* Run the timer of the ACLK. */\r
317         TA0CTL = TASSEL_1;\r
318 \r
319         /* Clear everything to start with. */\r
320         TA0CTL |= TACLR;\r
321 \r
322         /* Set the compare match value according to the tick rate we want. */\r
323         TA0CCR0 = usACLK_Frequency_Hz / configTICK_RATE_HZ;\r
324 \r
325         /* Enable the interrupts. */\r
326         TA0CCTL0 = CCIE;\r
327 \r
328         /* Start up clean. */\r
329         TA0CTL |= TACLR;\r
330 \r
331         /* Up mode. */\r
332         TA0CTL |= MC_1;\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 void vApplicationMallocFailedHook( void )\r
337 {\r
338         for( ;; );\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
343 {\r
344         ( void ) pxTask;\r
345         ( void ) pcTaskName;\r
346         \r
347         for( ;; );\r
348 }\r
349 /*-----------------------------------------------------------*/\r
350 \r
351 void vApplicationIdleHook( void )\r
352 {\r
353         __bis_SR_register( LPM3_bits + GIE );\r
354 }\r
355 \r
356 \r
357 \r