]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_CrossWorks/main.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / msp430_CrossWorks / main.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 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     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
72  * documentation provides more details of the demo application tasks.\r
73  *\r
74  * This demo is configured to execute on the ES449 prototyping board from\r
75  * SoftBaugh. The ES449 has a built in LCD display and a single built in user\r
76  * LED.  Therefore, in place of flashing an LED, the 'flash' and 'check' tasks\r
77  * toggle '*' characters on the LCD.  The left most '*' represents LED 0, the\r
78  * next LED 1, etc.\r
79  *\r
80  * Main. c also creates a task called 'Check'.  This only executes every three\r
81  * seconds but has the highest priority so is guaranteed to get processor time.\r
82  * Its main function is to check that all the other tasks are still operational.\r
83  * Each task that does not flash an LED maintains a unique count that is\r
84  * incremented each time the task successfully completes its function.  Should\r
85  * any error occur within such a task the count is permanently halted.  The\r
86  * 'check' task inspects the count of each task to ensure it has changed since\r
87  * the last time the check task executed.  If all the count variables have\r
88  * changed all the tasks are still executing error free, and the check task\r
89  * toggles an LED with a three second period.  Should any task contain an error\r
90  * at any time the LED toggle rate will increase to 500ms.\r
91  *\r
92  * Please read the documentation for the MSP430 port available on\r
93  * http://www.FreeRTOS.org.\r
94  */\r
95 \r
96 /* Standard includes. */\r
97 #include <stdlib.h>\r
98 \r
99 /* Scheduler includes. */\r
100 #include "FreeRTOS.h"\r
101 #include "task.h"\r
102 \r
103 /* Demo application includes. */\r
104 #include "partest.h"\r
105 #include "flash.h"\r
106 #include "integer.h"\r
107 #include "comtest2.h"\r
108 #include "PollQ.h"\r
109 \r
110 /* Constants required for hardware setup. */\r
111 #define mainALL_BITS_OUTPUT             ( ( unsigned char ) 0xff )\r
112 #define mainMAX_FREQUENCY               ( ( unsigned char ) 121 )\r
113 \r
114 /* Constants that define the LED's used by the various tasks. [in this case\r
115 the '*' characters on the LCD represent LED's] */\r
116 #define mainCHECK_LED                   ( 4 )\r
117 #define mainCOM_TEST_LED                ( 10 )\r
118 \r
119 /* Demo task priorities. */\r
120 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
121 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
122 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
123 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
124 \r
125 /* Baud rate used by the COM test tasks. */\r
126 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 19200 )\r
127 \r
128 /* The frequency at which the 'Check' tasks executes.  See the comments at the\r
129 top of the page.  When the system is operating error free the 'Check' task\r
130 toggles an LED every three seconds.  If an error is discovered in any task the\r
131 rate is increased to 500 milliseconds.  [in this case the '*' characters on the\r
132 LCD represent LED's]*/\r
133 #define mainNO_ERROR_CHECK_DELAY                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
134 #define mainERROR_CHECK_DELAY                   ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
135 \r
136 /* The constants used in the calculation. */\r
137 #define intgCONST1                              ( ( long ) 123 )\r
138 #define intgCONST2                              ( ( long ) 234567 )\r
139 #define intgCONST3                              ( ( long ) -3 )\r
140 #define intgCONST4                              ( ( long ) 7 )\r
141 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
142 \r
143 /*\r
144  * The function that implements the Check task.  See the comments at the head\r
145  * of the page for implementation details.\r
146  */\r
147 static void vErrorChecks( void *pvParameters );\r
148 \r
149 /*\r
150  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
151  * to be operating without error - otherwise returns pdFAIL.\r
152  */\r
153 static short prvCheckOtherTasksAreStillRunning( void );\r
154 \r
155 /*\r
156  * Perform the hardware setup required by the ES449 in order to run the demo\r
157  * application.\r
158  */\r
159 static void prvSetupHardware( void );\r
160 \r
161 \r
162 portBASE_TYPE xLocalError = pdFALSE;\r
163 volatile unsigned long ulIdleLoops = 0UL;\r
164 \r
165 /*-----------------------------------------------------------*/\r
166 \r
167 /*\r
168  * Start the demo application tasks - then start the real time scheduler.\r
169  */\r
170 int main( void )\r
171 {\r
172         /* Setup the hardware ready for the demo. */\r
173         prvSetupHardware();\r
174         vParTestInitialise();\r
175 \r
176         /* Start the standard demo application tasks. */\r
177         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
178         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
179         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
180         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
181 \r
182         /* Start the 'Check' task which is defined in this file. */\r
183         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
184 \r
185         /* Start the scheduler. */\r
186         vTaskStartScheduler();\r
187 \r
188         /* As the scheduler has been started the demo applications tasks will be\r
189         executing and we should never get here! */\r
190         return 0;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
195 {\r
196 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY;\r
197 \r
198         /* Cycle for ever, delaying then checking all the other tasks are still\r
199         operating without error. */\r
200         for( ;; )\r
201         {\r
202                 /* Wait until it is time to check again.  The time we wait here depends\r
203                 on whether an error has been detected or not.  When an error is\r
204                 detected the time is shortened resulting in a faster LED flash rate. */\r
205                 vTaskDelay( xDelayPeriod );\r
206 \r
207                 /* See if the other tasks are all ok. */\r
208                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
209                 {\r
210                         /* An error occurred in one of the tasks so shorten the delay\r
211                         period - which has the effect of increasing the frequency of the\r
212                         LED toggle. */\r
213                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
214                 }\r
215 \r
216                 /* Flash! */\r
217                 vParTestToggleLED( mainCHECK_LED );\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 static short prvCheckOtherTasksAreStillRunning( void )\r
223 {\r
224 static short sNoErrorFound = pdTRUE;\r
225 static unsigned long ulLastIdleLoopCount = 0UL;\r
226 \r
227         /* The demo tasks maintain a count that increments every cycle of the task\r
228         provided that the task has never encountered an error.  This function\r
229         checks the counts maintained by the tasks to ensure they are still being\r
230         incremented.  A count remaining at the same value between calls therefore\r
231         indicates that an error has been detected.  Only tasks that do not flash\r
232         an LED are checked. */\r
233 \r
234         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
235         {\r
236                 sNoErrorFound = pdFALSE;\r
237         }\r
238 \r
239         if( xAreComTestTasksStillRunning() != pdTRUE )\r
240         {\r
241                 sNoErrorFound = pdFALSE;\r
242         }\r
243 \r
244         if( xArePollingQueuesStillRunning() != pdTRUE )\r
245         {\r
246                 sNoErrorFound = pdFALSE;\r
247         }\r
248 \r
249         if( xLocalError == pdTRUE )\r
250         {\r
251                 sNoErrorFound = pdFALSE;\r
252         }\r
253 \r
254     if( ulIdleLoops == ulLastIdleLoopCount )\r
255     {\r
256         sNoErrorFound = pdFALSE;\r
257     }\r
258     else\r
259     {\r
260         ulLastIdleLoopCount = ulIdleLoops;\r
261     }\r
262 \r
263         return sNoErrorFound;\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 static void prvSetupHardware( void )\r
268 {\r
269         /* Stop the watchdog. */\r
270         WDTCTL = WDTPW + WDTHOLD;\r
271 \r
272         /* Setup DCO+ for ( xtal * D * (N + 1) ) operation. */\r
273         FLL_CTL0 |= DCOPLUS + XCAP18PF;\r
274 \r
275         /* X2 DCO frequency, 8MHz nominal DCO */\r
276         SCFI0 |= FN_4;\r
277 \r
278         /* (121+1) x 32768 x 2 = 7.99 Mhz */\r
279         SCFQCTL = mainMAX_FREQUENCY;\r
280 \r
281         /* Setup the IO.  This is just copied from the demo supplied by SoftBaugh\r
282          for the ES449 demo board. */\r
283         P1SEL = 0x32;\r
284         P2SEL = 0x00;\r
285         P3SEL = 0x00;\r
286         P4SEL = 0xFC;\r
287         P5SEL = 0xFF;\r
288 }\r
289 /*-----------------------------------------------------------*/\r
290 \r
291 /* The idle hook is just a copy of the standard integer maths tasks.  See\r
292 Demo/Common/integer.c for rationale. */\r
293 \r
294 void vApplicationIdleHook( void ) __toplevel\r
295 {\r
296 /* These variables are all effectively set to constants so they are volatile to\r
297 ensure the compiler does not just get rid of them. */\r
298 volatile long lValue;\r
299 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
300 \r
301         /* Keep performing a calculation and checking the result against a constant. */\r
302         for( ;; )\r
303         {\r
304                 /* Perform the calculation.  This will store partial value in\r
305                 registers, resulting in a good test of the context switch mechanism. */\r
306                 lValue = intgCONST1;\r
307                 lValue += intgCONST2;\r
308 \r
309                 /* Yield in case cooperative scheduling is being used. */\r
310                 #if configUSE_PREEMPTION == 0\r
311                 {\r
312                         taskYIELD();\r
313                 }\r
314                 #endif\r
315 \r
316                 /* Finish off the calculation. */\r
317                 lValue *= intgCONST3;\r
318                 lValue /= intgCONST4;\r
319 \r
320                 /* If the calculation is found to be incorrect we stop setting the\r
321                 TaskHasExecuted variable so the check task can see an error has\r
322                 occurred. */\r
323                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
324                 {\r
325                         /* Don't bother with mutual exclusion - it is only read from the\r
326                         check task and never written. */\r
327                         xLocalError = pdTRUE;\r
328                 }\r
329                 /* Yield in case cooperative scheduling is being used. */\r
330                 #if configUSE_PREEMPTION == 0\r
331                 {\r
332                         taskYIELD();\r
333                 }\r
334                 #endif\r
335 \r
336         ulIdleLoops++;\r
337 \r
338         /* Place the processor into low power mode. */\r
339         LPM3;\r
340         }\r
341 }\r
342 \r
343 \r
344 \r
345 \r
346 \r