]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/msp430_GCC/main.c
516cae928f553de7001d861736dc705e350bc72f
[freertos] / FreeRTOS / Demo / msp430_GCC / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
30  * documentation provides more details of the demo application tasks.\r
31  * \r
32  * This demo is configured to execute on the ES449 prototyping board from\r
33  * SoftBaugh. The ES449 has a built in LCD display and a single built in user\r
34  * LED.  Therefore, in place of flashing an LED, the 'flash' and 'check' tasks\r
35  * toggle '*' characters on the LCD.  The left most '*' represents LED 0, the\r
36  * next LED 1, etc.\r
37  *\r
38  * Main. c also creates a task called 'Check'.  This only executes every three \r
39  * seconds but has the highest priority so is guaranteed to get processor time.  \r
40  * Its main function is to check that all the other tasks are still operational.  \r
41  * Each task that does not flash an LED maintains a unique count that is \r
42  * incremented each time the task successfully completes its function.  Should \r
43  * any error occur within such a task the count is permanently halted.  The \r
44  * 'check' task inspects the count of each task to ensure it has changed since\r
45  * the last time the check task executed.  If all the count variables have \r
46  * changed all the tasks are still executing error free, and the check task\r
47  * toggles an LED with a three second period.  Should any task contain an error \r
48  * at any time the LED toggle rate will increase to 500ms.\r
49  *\r
50  * Please read the documentation for the MSP430 port available on\r
51  * http://www.FreeRTOS.org.\r
52  */\r
53 \r
54 /* Standard includes. */\r
55 #include <stdlib.h>\r
56 #include <signal.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 /* \r
96  * The function that implements the Check task.  See the comments at the head\r
97  * of the page for implementation details.\r
98  */ \r
99 static void vErrorChecks( void *pvParameters );\r
100 \r
101 /*\r
102  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
103  * to be operating without error - otherwise returns pdFAIL.\r
104  */\r
105 static short prvCheckOtherTasksAreStillRunning( void );\r
106 \r
107 /* \r
108  * Perform the hardware setup required by the ES449 in order to run the demo\r
109  * application.\r
110  */\r
111 static void prvSetupHardware( void );\r
112 \r
113 /* Used to detect the idle hook function stalling. */\r
114 static volatile unsigned long ulIdleLoops = 0UL;\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /*\r
119  * Start the demo application tasks - then start the real time scheduler.\r
120  */\r
121 int main( void )\r
122 {\r
123         /* Setup the hardware ready for the demo. */\r
124         prvSetupHardware();\r
125         vParTestInitialise();\r
126 \r
127         /* Start the standard demo application tasks. */\r
128         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
129         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
130         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
131         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
132 \r
133         /* Start the 'Check' task which is defined in this file. */\r
134         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );    \r
135 \r
136         /* Start the scheduler. */\r
137         vTaskStartScheduler();\r
138 \r
139         /* As the scheduler has been started the demo applications tasks will be\r
140         executing and we should never get here! */\r
141         return 0;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void vErrorChecks( void *pvParameters )\r
146 {\r
147 static volatile unsigned long ulDummyVariable = 3UL;\r
148 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY;\r
149 \r
150         /* Cycle for ever, delaying then checking all the other tasks are still\r
151         operating without error. */\r
152         for( ;; )\r
153         {\r
154                 /* Wait until it is time to check again.  The time we wait here depends\r
155                 on whether an error has been detected or not.  When an error is \r
156                 detected the time is shortened resulting in a faster LED flash rate. */\r
157                 vTaskDelay( xDelayPeriod );\r
158 \r
159                 /* Perform a bit of 32bit maths to ensure the registers used by the \r
160                 integer tasks get some exercise outside of the integer tasks \r
161                 themselves. The result here is not important we are just deliberately\r
162                 changing registers used by other tasks to ensure that their context\r
163                 switch is operating as required. - see the demo application \r
164                 documentation for more info. */\r
165                 ulDummyVariable *= 3UL;\r
166                 \r
167                 /* See if the other tasks are all ok. */\r
168                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
169                 {\r
170                         /* An error occurred in one of the tasks so shorten the delay \r
171                         period - which has the effect of increasing the frequency of the\r
172                         LED toggle. */\r
173                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
174                 }\r
175 \r
176                 /* Flash! */\r
177                 vParTestToggleLED( mainCHECK_LED );\r
178         }\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 static short prvCheckOtherTasksAreStillRunning( void )\r
183 {\r
184 static short sNoErrorFound = pdTRUE;\r
185 static unsigned long ulLastIdleLoops = 0UL;\r
186 \r
187         /* The demo tasks maintain a count that increments every cycle of the task\r
188         provided that the task has never encountered an error.  This function \r
189         checks the counts maintained by the tasks to ensure they are still being\r
190         incremented.  A count remaining at the same value between calls therefore\r
191         indicates that an error has been detected.  Only tasks that do not flash\r
192         an LED are checked. */\r
193 \r
194         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
195         {\r
196                 sNoErrorFound = pdFALSE;\r
197         }\r
198 \r
199         if( xAreComTestTasksStillRunning() != pdTRUE )\r
200         {\r
201                 sNoErrorFound = pdFALSE;\r
202         }\r
203         \r
204         if( xArePollingQueuesStillRunning() != pdTRUE )\r
205         {\r
206                 sNoErrorFound = pdFALSE;\r
207         }\r
208 \r
209         if( ulLastIdleLoops == ulIdleLoops )\r
210         {\r
211                 sNoErrorFound = pdFALSE;\r
212         }\r
213 \r
214         ulLastIdleLoops = ulIdleLoops;\r
215         \r
216         return sNoErrorFound;\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static void prvSetupHardware( void )\r
221 {\r
222         /* Stop the watchdog. */\r
223         WDTCTL = WDTPW + WDTHOLD;\r
224 \r
225         /* Setup DCO+ for ( xtal * D * (N + 1) ) operation. */\r
226         FLL_CTL0 |= DCOPLUS + XCAP18PF; \r
227 \r
228         /* X2 DCO frequency, 8MHz nominal DCO */\r
229         SCFI0 |= FN_4;                  \r
230 \r
231         /* (121+1) x 32768 x 2 = 7.99 Mhz */\r
232         SCFQCTL = mainMAX_FREQUENCY;\r
233 \r
234         /* Setup the IO as per the SoftBaugh demo for the same target hardware. */\r
235         P1SEL = 0x32;\r
236         P2SEL = 0x00;\r
237         P3SEL = 0x00;\r
238         P4SEL = 0xFC;\r
239         P5SEL = 0xFF;\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vApplicationIdleHook( void );\r
244 void vApplicationIdleHook( void )\r
245 {\r
246         /* Simple put the CPU into lowpower mode. */\r
247         _BIS_SR( LPM3_bits );\r
248         ulIdleLoops++;\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 \r
253 \r
254 \r
255 \r
256 \r
257 \r