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