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