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