]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_CrossWorks/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / msp430_CrossWorks / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
31  * documentation provides more details of the demo application tasks.\r
32  *\r
33  * This demo is configured to execute on the ES449 prototyping board from\r
34  * SoftBaugh. The ES449 has a built in LCD display and a single built in user\r
35  * LED.  Therefore, in place of flashing an LED, the 'flash' and 'check' tasks\r
36  * toggle '*' characters on the LCD.  The left most '*' represents LED 0, the\r
37  * next LED 1, etc.\r
38  *\r
39  * Main. c also creates a task called 'Check'.  This only executes every three\r
40  * seconds but has the highest priority so is guaranteed to get processor time.\r
41  * Its main function is to check that all the other tasks are still operational.\r
42  * Each task that does not flash an LED maintains a unique count that is\r
43  * incremented each time the task successfully completes its function.  Should\r
44  * any error occur within such a task the count is permanently halted.  The\r
45  * 'check' task inspects the count of each task to ensure it has changed since\r
46  * the last time the check task executed.  If all the count variables have\r
47  * changed all the tasks are still executing error free, and the check task\r
48  * toggles an LED with a three second period.  Should any task contain an error\r
49  * at any time the LED toggle rate will increase to 500ms.\r
50  *\r
51  * Please read the documentation for the MSP430 port available on\r
52  * http://www.FreeRTOS.org.\r
53  */\r
54 \r
55 /* Standard includes. */\r
56 #include <stdlib.h>\r
57 \r
58 /* Scheduler includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 \r
62 /* Demo application includes. */\r
63 #include "partest.h"\r
64 #include "flash.h"\r
65 #include "integer.h"\r
66 #include "comtest2.h"\r
67 #include "PollQ.h"\r
68 \r
69 /* Constants required for hardware setup. */\r
70 #define mainALL_BITS_OUTPUT             ( ( unsigned char ) 0xff )\r
71 #define mainMAX_FREQUENCY               ( ( unsigned char ) 121 )\r
72 \r
73 /* Constants that define the LED's used by the various tasks. [in this case\r
74 the '*' characters on the LCD represent LED's] */\r
75 #define mainCHECK_LED                   ( 4 )\r
76 #define mainCOM_TEST_LED                ( 10 )\r
77 \r
78 /* Demo task priorities. */\r
79 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
80 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
81 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
82 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
83 \r
84 /* Baud rate used by the COM test tasks. */\r
85 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 19200 )\r
86 \r
87 /* The frequency at which the 'Check' tasks executes.  See the comments at the\r
88 top of the page.  When the system is operating error free the 'Check' task\r
89 toggles an LED every three seconds.  If an error is discovered in any task the\r
90 rate is increased to 500 milliseconds.  [in this case the '*' characters on the\r
91 LCD represent LED's]*/\r
92 #define mainNO_ERROR_CHECK_DELAY                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
93 #define mainERROR_CHECK_DELAY                   ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
94 \r
95 /* The constants used in the calculation. */\r
96 #define intgCONST1                              ( ( long ) 123 )\r
97 #define intgCONST2                              ( ( long ) 234567 )\r
98 #define intgCONST3                              ( ( long ) -3 )\r
99 #define intgCONST4                              ( ( long ) 7 )\r
100 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
101 \r
102 /*\r
103  * The function that implements the Check task.  See the comments at the head\r
104  * of the page for implementation details.\r
105  */\r
106 static void vErrorChecks( void *pvParameters );\r
107 \r
108 /*\r
109  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
110  * to be operating without error - otherwise returns pdFAIL.\r
111  */\r
112 static short prvCheckOtherTasksAreStillRunning( void );\r
113 \r
114 /*\r
115  * Perform the hardware setup required by the ES449 in order to run the demo\r
116  * application.\r
117  */\r
118 static void prvSetupHardware( void );\r
119 \r
120 \r
121 portBASE_TYPE xLocalError = pdFALSE;\r
122 volatile unsigned long ulIdleLoops = 0UL;\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * Start the demo application tasks - then start the real time scheduler.\r
128  */\r
129 int main( void )\r
130 {\r
131         /* Setup the hardware ready for the demo. */\r
132         prvSetupHardware();\r
133         vParTestInitialise();\r
134 \r
135         /* Start the standard demo application tasks. */\r
136         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
137         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
138         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
139         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
140 \r
141         /* Start the 'Check' task which is defined in this file. */\r
142         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
143 \r
144         /* Start the scheduler. */\r
145         vTaskStartScheduler();\r
146 \r
147         /* As the scheduler has been started the demo applications tasks will be\r
148         executing and we should never get here! */\r
149         return 0;\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
154 {\r
155 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY;\r
156 \r
157         /* Cycle for ever, delaying then checking all the other tasks are still\r
158         operating without error. */\r
159         for( ;; )\r
160         {\r
161                 /* Wait until it is time to check again.  The time we wait here depends\r
162                 on whether an error has been detected or not.  When an error is\r
163                 detected the time is shortened resulting in a faster LED flash rate. */\r
164                 vTaskDelay( xDelayPeriod );\r
165 \r
166                 /* See if the other tasks are all ok. */\r
167                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
168                 {\r
169                         /* An error occurred in one of the tasks so shorten the delay\r
170                         period - which has the effect of increasing the frequency of the\r
171                         LED toggle. */\r
172                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
173                 }\r
174 \r
175                 /* Flash! */\r
176                 vParTestToggleLED( mainCHECK_LED );\r
177         }\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 static short prvCheckOtherTasksAreStillRunning( void )\r
182 {\r
183 static short sNoErrorFound = pdTRUE;\r
184 static unsigned long ulLastIdleLoopCount = 0UL;\r
185 \r
186         /* The demo tasks maintain a count that increments every cycle of the task\r
187         provided that the task has never encountered an error.  This function\r
188         checks the counts maintained by the tasks to ensure they are still being\r
189         incremented.  A count remaining at the same value between calls therefore\r
190         indicates that an error has been detected.  Only tasks that do not flash\r
191         an LED are checked. */\r
192 \r
193         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
194         {\r
195                 sNoErrorFound = pdFALSE;\r
196         }\r
197 \r
198         if( xAreComTestTasksStillRunning() != pdTRUE )\r
199         {\r
200                 sNoErrorFound = pdFALSE;\r
201         }\r
202 \r
203         if( xArePollingQueuesStillRunning() != pdTRUE )\r
204         {\r
205                 sNoErrorFound = pdFALSE;\r
206         }\r
207 \r
208         if( xLocalError == pdTRUE )\r
209         {\r
210                 sNoErrorFound = pdFALSE;\r
211         }\r
212 \r
213     if( ulIdleLoops == ulLastIdleLoopCount )\r
214     {\r
215         sNoErrorFound = pdFALSE;\r
216     }\r
217     else\r
218     {\r
219         ulLastIdleLoopCount = ulIdleLoops;\r
220     }\r
221 \r
222         return sNoErrorFound;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static void prvSetupHardware( void )\r
227 {\r
228         /* Stop the watchdog. */\r
229         WDTCTL = WDTPW + WDTHOLD;\r
230 \r
231         /* Setup DCO+ for ( xtal * D * (N + 1) ) operation. */\r
232         FLL_CTL0 |= DCOPLUS + XCAP18PF;\r
233 \r
234         /* X2 DCO frequency, 8MHz nominal DCO */\r
235         SCFI0 |= FN_4;\r
236 \r
237         /* (121+1) x 32768 x 2 = 7.99 Mhz */\r
238         SCFQCTL = mainMAX_FREQUENCY;\r
239 \r
240         /* Setup the IO.  This is just copied from the demo supplied by SoftBaugh\r
241          for the ES449 demo board. */\r
242         P1SEL = 0x32;\r
243         P2SEL = 0x00;\r
244         P3SEL = 0x00;\r
245         P4SEL = 0xFC;\r
246         P5SEL = 0xFF;\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 /* The idle hook is just a copy of the standard integer maths tasks.  See\r
251 Demo/Common/integer.c for rationale. */\r
252 \r
253 void vApplicationIdleHook( void ) __toplevel\r
254 {\r
255 /* These variables are all effectively set to constants so they are volatile to\r
256 ensure the compiler does not just get rid of them. */\r
257 volatile long lValue;\r
258 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
259 \r
260         /* Keep performing a calculation and checking the result against a constant. */\r
261         for( ;; )\r
262         {\r
263                 /* Perform the calculation.  This will store partial value in\r
264                 registers, resulting in a good test of the context switch mechanism. */\r
265                 lValue = intgCONST1;\r
266                 lValue += intgCONST2;\r
267 \r
268                 /* Yield in case cooperative scheduling is being used. */\r
269                 #if configUSE_PREEMPTION == 0\r
270                 {\r
271                         taskYIELD();\r
272                 }\r
273                 #endif\r
274 \r
275                 /* Finish off the calculation. */\r
276                 lValue *= intgCONST3;\r
277                 lValue /= intgCONST4;\r
278 \r
279                 /* If the calculation is found to be incorrect we stop setting the\r
280                 TaskHasExecuted variable so the check task can see an error has\r
281                 occurred. */\r
282                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
283                 {\r
284                         /* Don't bother with mutual exclusion - it is only read from the\r
285                         check task and never written. */\r
286                         xLocalError = pdTRUE;\r
287                 }\r
288                 /* Yield in case cooperative scheduling is being used. */\r
289                 #if configUSE_PREEMPTION == 0\r
290                 {\r
291                         taskYIELD();\r
292                 }\r
293                 #endif\r
294 \r
295         ulIdleLoops++;\r
296 \r
297         /* Place the processor into low power mode. */\r
298         LPM3;\r
299         }\r
300 }\r
301 \r
302 \r
303 \r
304 \r
305 \r