]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_CrossWorks/main.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / msp430_CrossWorks / 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  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
98  * documentation provides more details of the demo application tasks.\r
99  *\r
100  * This demo is configured to execute on the ES449 prototyping board from\r
101  * SoftBaugh. The ES449 has a built in LCD display and a single built in user\r
102  * LED.  Therefore, in place of flashing an LED, the 'flash' and 'check' tasks\r
103  * toggle '*' characters on the LCD.  The left most '*' represents LED 0, the\r
104  * next LED 1, etc.\r
105  *\r
106  * Main. c also creates a task called 'Check'.  This only executes every three\r
107  * seconds but has the highest priority so is guaranteed to get processor time.\r
108  * Its main function is to check that all the other tasks are still operational.\r
109  * Each task that does not flash an LED maintains a unique count that is\r
110  * incremented each time the task successfully completes its function.  Should\r
111  * any error occur within such a task the count is permanently halted.  The\r
112  * 'check' task inspects the count of each task to ensure it has changed since\r
113  * the last time the check task executed.  If all the count variables have\r
114  * changed all the tasks are still executing error free, and the check task\r
115  * toggles an LED with a three second period.  Should any task contain an error\r
116  * at any time the LED toggle rate will increase to 500ms.\r
117  *\r
118  * Please read the documentation for the MSP430 port available on\r
119  * http://www.FreeRTOS.org.\r
120  */\r
121 \r
122 /* Standard includes. */\r
123 #include <stdlib.h>\r
124 \r
125 /* Scheduler includes. */\r
126 #include "FreeRTOS.h"\r
127 #include "task.h"\r
128 \r
129 /* Demo application includes. */\r
130 #include "partest.h"\r
131 #include "flash.h"\r
132 #include "integer.h"\r
133 #include "comtest2.h"\r
134 #include "PollQ.h"\r
135 \r
136 /* Constants required for hardware setup. */\r
137 #define mainALL_BITS_OUTPUT             ( ( unsigned char ) 0xff )\r
138 #define mainMAX_FREQUENCY               ( ( unsigned char ) 121 )\r
139 \r
140 /* Constants that define the LED's used by the various tasks. [in this case\r
141 the '*' characters on the LCD represent LED's] */\r
142 #define mainCHECK_LED                   ( 4 )\r
143 #define mainCOM_TEST_LED                ( 10 )\r
144 \r
145 /* Demo task priorities. */\r
146 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
147 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
148 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
149 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
150 \r
151 /* Baud rate used by the COM test tasks. */\r
152 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 19200 )\r
153 \r
154 /* The frequency at which the 'Check' tasks executes.  See the comments at the\r
155 top of the page.  When the system is operating error free the 'Check' task\r
156 toggles an LED every three seconds.  If an error is discovered in any task the\r
157 rate is increased to 500 milliseconds.  [in this case the '*' characters on the\r
158 LCD represent LED's]*/\r
159 #define mainNO_ERROR_CHECK_DELAY                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
160 #define mainERROR_CHECK_DELAY                   ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
161 \r
162 /* The constants used in the calculation. */\r
163 #define intgCONST1                              ( ( long ) 123 )\r
164 #define intgCONST2                              ( ( long ) 234567 )\r
165 #define intgCONST3                              ( ( long ) -3 )\r
166 #define intgCONST4                              ( ( long ) 7 )\r
167 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
168 \r
169 /*\r
170  * The function that implements the Check task.  See the comments at the head\r
171  * of the page for implementation details.\r
172  */\r
173 static void vErrorChecks( void *pvParameters );\r
174 \r
175 /*\r
176  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
177  * to be operating without error - otherwise returns pdFAIL.\r
178  */\r
179 static short prvCheckOtherTasksAreStillRunning( void );\r
180 \r
181 /*\r
182  * Perform the hardware setup required by the ES449 in order to run the demo\r
183  * application.\r
184  */\r
185 static void prvSetupHardware( void );\r
186 \r
187 \r
188 portBASE_TYPE xLocalError = pdFALSE;\r
189 volatile unsigned long ulIdleLoops = 0UL;\r
190 \r
191 /*-----------------------------------------------------------*/\r
192 \r
193 /*\r
194  * Start the demo application tasks - then start the real time scheduler.\r
195  */\r
196 int main( void )\r
197 {\r
198         /* Setup the hardware ready for the demo. */\r
199         prvSetupHardware();\r
200         vParTestInitialise();\r
201 \r
202         /* Start the standard demo application tasks. */\r
203         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
204         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
205         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
206         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
207 \r
208         /* Start the 'Check' task which is defined in this file. */\r
209         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
210 \r
211         /* Start the scheduler. */\r
212         vTaskStartScheduler();\r
213 \r
214         /* As the scheduler has been started the demo applications tasks will be\r
215         executing and we should never get here! */\r
216         return 0;\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
221 {\r
222 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY;\r
223 \r
224         /* Cycle for ever, delaying then checking all the other tasks are still\r
225         operating without error. */\r
226         for( ;; )\r
227         {\r
228                 /* Wait until it is time to check again.  The time we wait here depends\r
229                 on whether an error has been detected or not.  When an error is\r
230                 detected the time is shortened resulting in a faster LED flash rate. */\r
231                 vTaskDelay( xDelayPeriod );\r
232 \r
233                 /* See if the other tasks are all ok. */\r
234                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
235                 {\r
236                         /* An error occurred in one of the tasks so shorten the delay\r
237                         period - which has the effect of increasing the frequency of the\r
238                         LED toggle. */\r
239                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
240                 }\r
241 \r
242                 /* Flash! */\r
243                 vParTestToggleLED( mainCHECK_LED );\r
244         }\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 static short prvCheckOtherTasksAreStillRunning( void )\r
249 {\r
250 static short sNoErrorFound = pdTRUE;\r
251 static unsigned long ulLastIdleLoopCount = 0UL;\r
252 \r
253         /* The demo tasks maintain a count that increments every cycle of the task\r
254         provided that the task has never encountered an error.  This function\r
255         checks the counts maintained by the tasks to ensure they are still being\r
256         incremented.  A count remaining at the same value between calls therefore\r
257         indicates that an error has been detected.  Only tasks that do not flash\r
258         an LED are checked. */\r
259 \r
260         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
261         {\r
262                 sNoErrorFound = pdFALSE;\r
263         }\r
264 \r
265         if( xAreComTestTasksStillRunning() != pdTRUE )\r
266         {\r
267                 sNoErrorFound = pdFALSE;\r
268         }\r
269 \r
270         if( xArePollingQueuesStillRunning() != pdTRUE )\r
271         {\r
272                 sNoErrorFound = pdFALSE;\r
273         }\r
274 \r
275         if( xLocalError == pdTRUE )\r
276         {\r
277                 sNoErrorFound = pdFALSE;\r
278         }\r
279 \r
280     if( ulIdleLoops == ulLastIdleLoopCount )\r
281     {\r
282         sNoErrorFound = pdFALSE;\r
283     }\r
284     else\r
285     {\r
286         ulLastIdleLoopCount = ulIdleLoops;\r
287     }\r
288 \r
289         return sNoErrorFound;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static void prvSetupHardware( void )\r
294 {\r
295         /* Stop the watchdog. */\r
296         WDTCTL = WDTPW + WDTHOLD;\r
297 \r
298         /* Setup DCO+ for ( xtal * D * (N + 1) ) operation. */\r
299         FLL_CTL0 |= DCOPLUS + XCAP18PF;\r
300 \r
301         /* X2 DCO frequency, 8MHz nominal DCO */\r
302         SCFI0 |= FN_4;\r
303 \r
304         /* (121+1) x 32768 x 2 = 7.99 Mhz */\r
305         SCFQCTL = mainMAX_FREQUENCY;\r
306 \r
307         /* Setup the IO.  This is just copied from the demo supplied by SoftBaugh\r
308          for the ES449 demo board. */\r
309         P1SEL = 0x32;\r
310         P2SEL = 0x00;\r
311         P3SEL = 0x00;\r
312         P4SEL = 0xFC;\r
313         P5SEL = 0xFF;\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 /* The idle hook is just a copy of the standard integer maths tasks.  See\r
318 Demo/Common/integer.c for rationale. */\r
319 \r
320 void vApplicationIdleHook( void ) __toplevel\r
321 {\r
322 /* These variables are all effectively set to constants so they are volatile to\r
323 ensure the compiler does not just get rid of them. */\r
324 volatile long lValue;\r
325 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
326 \r
327         /* Keep performing a calculation and checking the result against a constant. */\r
328         for( ;; )\r
329         {\r
330                 /* Perform the calculation.  This will store partial value in\r
331                 registers, resulting in a good test of the context switch mechanism. */\r
332                 lValue = intgCONST1;\r
333                 lValue += intgCONST2;\r
334 \r
335                 /* Yield in case cooperative scheduling is being used. */\r
336                 #if configUSE_PREEMPTION == 0\r
337                 {\r
338                         taskYIELD();\r
339                 }\r
340                 #endif\r
341 \r
342                 /* Finish off the calculation. */\r
343                 lValue *= intgCONST3;\r
344                 lValue /= intgCONST4;\r
345 \r
346                 /* If the calculation is found to be incorrect we stop setting the\r
347                 TaskHasExecuted variable so the check task can see an error has\r
348                 occurred. */\r
349                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
350                 {\r
351                         /* Don't bother with mutual exclusion - it is only read from the\r
352                         check task and never written. */\r
353                         xLocalError = pdTRUE;\r
354                 }\r
355                 /* Yield in case cooperative scheduling is being used. */\r
356                 #if configUSE_PREEMPTION == 0\r
357                 {\r
358                         taskYIELD();\r
359                 }\r
360                 #endif\r
361 \r
362         ulIdleLoops++;\r
363 \r
364         /* Place the processor into low power mode. */\r
365         LPM3;\r
366         }\r
367 }\r
368 \r
369 \r
370 \r
371 \r
372 \r