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