]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_GCC/main.c
24d6dbd08cff4b8565f062c396d360b444b3bb06
[freertos] / FreeRTOS / Demo / msp430_GCC / 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 #include <signal.h>\r
58 \r
59 /* Scheduler includes. */\r
60 #include "FreeRTOS.h"\r
61 #include "task.h"\r
62 \r
63 /* Demo application includes. */\r
64 #include "partest.h"\r
65 #include "flash.h"\r
66 #include "integer.h"\r
67 #include "comtest2.h"\r
68 #include "PollQ.h"\r
69 \r
70 /* Constants required for hardware setup. */\r
71 #define mainALL_BITS_OUTPUT             ( ( unsigned char ) 0xff )\r
72 #define mainMAX_FREQUENCY               ( ( unsigned char ) 121 )\r
73 \r
74 /* Constants that define the LED's used by the various tasks. [in this case\r
75 the '*' characters on the LCD represent LED's] */\r
76 #define mainCHECK_LED                   ( 4 )\r
77 #define mainCOM_TEST_LED                ( 10 )\r
78 \r
79 /* Demo task priorities. */\r
80 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
81 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
82 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
83 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
84 \r
85 /* Baud rate used by the COM test tasks. */\r
86 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 19200 )\r
87 \r
88 /* The frequency at which the 'Check' tasks executes.  See the comments at the \r
89 top of the page.  When the system is operating error free the 'Check' task\r
90 toggles an LED every three seconds.  If an error is discovered in any task the\r
91 rate is increased to 500 milliseconds.  [in this case the '*' characters on the \r
92 LCD represent LED's]*/\r
93 #define mainNO_ERROR_CHECK_DELAY                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
94 #define mainERROR_CHECK_DELAY                   ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
95 \r
96 /* \r
97  * The function that implements the Check task.  See the comments at the head\r
98  * of the page for implementation details.\r
99  */ \r
100 static void vErrorChecks( void *pvParameters );\r
101 \r
102 /*\r
103  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
104  * to be operating without error - otherwise returns pdFAIL.\r
105  */\r
106 static short prvCheckOtherTasksAreStillRunning( void );\r
107 \r
108 /* \r
109  * Perform the hardware setup required by the ES449 in order to run the demo\r
110  * application.\r
111  */\r
112 static void prvSetupHardware( void );\r
113 \r
114 /* Used to detect the idle hook function stalling. */\r
115 static volatile unsigned long ulIdleLoops = 0UL;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /*\r
120  * Start the demo application tasks - then start the real time scheduler.\r
121  */\r
122 int main( void )\r
123 {\r
124         /* Setup the hardware ready for the demo. */\r
125         prvSetupHardware();\r
126         vParTestInitialise();\r
127 \r
128         /* Start the standard demo application tasks. */\r
129         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
130         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
131         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
132         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
133 \r
134         /* Start the 'Check' task which is defined in this file. */\r
135         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );    \r
136 \r
137         /* Start the scheduler. */\r
138         vTaskStartScheduler();\r
139 \r
140         /* As the scheduler has been started the demo applications tasks will be\r
141         executing and we should never get here! */\r
142         return 0;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 static void vErrorChecks( void *pvParameters )\r
147 {\r
148 static volatile unsigned long ulDummyVariable = 3UL;\r
149 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY;\r
150 \r
151         /* Cycle for ever, delaying then checking all the other tasks are still\r
152         operating without error. */\r
153         for( ;; )\r
154         {\r
155                 /* Wait until it is time to check again.  The time we wait here depends\r
156                 on whether an error has been detected or not.  When an error is \r
157                 detected the time is shortened resulting in a faster LED flash rate. */\r
158                 vTaskDelay( xDelayPeriod );\r
159 \r
160                 /* Perform a bit of 32bit maths to ensure the registers used by the \r
161                 integer tasks get some exercise outside of the integer tasks \r
162                 themselves. The result here is not important we are just deliberately\r
163                 changing registers used by other tasks to ensure that their context\r
164                 switch is operating as required. - see the demo application \r
165                 documentation for more info. */\r
166                 ulDummyVariable *= 3UL;\r
167                 \r
168                 /* See if the other tasks are all ok. */\r
169                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
170                 {\r
171                         /* An error occurred in one of the tasks so shorten the delay \r
172                         period - which has the effect of increasing the frequency of the\r
173                         LED toggle. */\r
174                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
175                 }\r
176 \r
177                 /* Flash! */\r
178                 vParTestToggleLED( mainCHECK_LED );\r
179         }\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static short prvCheckOtherTasksAreStillRunning( void )\r
184 {\r
185 static short sNoErrorFound = pdTRUE;\r
186 static unsigned long ulLastIdleLoops = 0UL;\r
187 \r
188         /* The demo tasks maintain a count that increments every cycle of the task\r
189         provided that the task has never encountered an error.  This function \r
190         checks the counts maintained by the tasks to ensure they are still being\r
191         incremented.  A count remaining at the same value between calls therefore\r
192         indicates that an error has been detected.  Only tasks that do not flash\r
193         an LED are checked. */\r
194 \r
195         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
196         {\r
197                 sNoErrorFound = pdFALSE;\r
198         }\r
199 \r
200         if( xAreComTestTasksStillRunning() != pdTRUE )\r
201         {\r
202                 sNoErrorFound = pdFALSE;\r
203         }\r
204         \r
205         if( xArePollingQueuesStillRunning() != pdTRUE )\r
206         {\r
207                 sNoErrorFound = pdFALSE;\r
208         }\r
209 \r
210         if( ulLastIdleLoops == ulIdleLoops )\r
211         {\r
212                 sNoErrorFound = pdFALSE;\r
213         }\r
214 \r
215         ulLastIdleLoops = ulIdleLoops;\r
216         \r
217         return sNoErrorFound;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvSetupHardware( void )\r
222 {\r
223         /* Stop the watchdog. */\r
224         WDTCTL = WDTPW + WDTHOLD;\r
225 \r
226         /* Setup DCO+ for ( xtal * D * (N + 1) ) operation. */\r
227         FLL_CTL0 |= DCOPLUS + XCAP18PF; \r
228 \r
229         /* X2 DCO frequency, 8MHz nominal DCO */\r
230         SCFI0 |= FN_4;                  \r
231 \r
232         /* (121+1) x 32768 x 2 = 7.99 Mhz */\r
233         SCFQCTL = mainMAX_FREQUENCY;\r
234 \r
235         /* Setup the IO as per the SoftBaugh demo for the same target hardware. */\r
236         P1SEL = 0x32;\r
237         P2SEL = 0x00;\r
238         P3SEL = 0x00;\r
239         P4SEL = 0xFC;\r
240         P5SEL = 0xFF;\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 void vApplicationIdleHook( void );\r
245 void vApplicationIdleHook( void )\r
246 {\r
247         /* Simple put the CPU into lowpower mode. */\r
248         _BIS_SR( LPM3_bits );\r
249         ulIdleLoops++;\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 \r
254 \r
255 \r
256 \r
257 \r
258 \r