]> begriffs open source - cmsis-freertos/blob - Demo/AVR_ATMega323_IAR/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / AVR_ATMega323_IAR / 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  * Main. c also creates a task called "Check".  This only executes every three
75  * seconds but has the highest priority so is guaranteed to get processor time.
76  * Its main function is to check that all the other tasks are still operational.
77  * Each task that does not flash an LED maintains a unique count that is
78  * incremented each time the task successfully completes its function.  Should
79  * any error occur within such a task the count is permanently halted.  The
80  * check task inspects the count of each task to ensure it has changed since
81  * the last time the check task executed.  If all the count variables have
82  * changed all the tasks are still executing error free, and the check task
83  * toggles an LED.  Should any task contain an error at any time the LED toggle
84  * will stop.
85  *
86  * The LED flash and communications test tasks do not maintain a count.
87  */
88
89 /*
90 Changes from V1.2.0
91         
92         + Changed the baud rate for the serial test from 19200 to 57600.
93
94 Changes from V1.2.3
95
96         + The integer and comtest tasks are now used when the cooperative scheduler
97           is being used.  Previously they were only used with the preemptive
98           scheduler.
99
100 Changes from V1.2.5
101
102         + Set the baud rate to 38400.  This has a smaller error percentage with an
103           8MHz clock (according to the manual).
104
105 Changes from V2.0.0
106
107         + Delay periods are now specified using variables and constants of
108           TickType_t rather than unsigned long.
109
110 Changes from V2.2.0
111
112         + File can now be built using either the IAR or WinAVR compiler.
113
114 Changes from V2.6.1
115
116         + The IAR and WinAVR AVR ports are now maintained separately.
117
118 Changes from V4.0.5
119
120         + Modified to demonstrate the use of co-routines.
121 */
122
123 #include <stdlib.h>
124 #include <string.h>
125
126 #ifdef GCC_MEGA_AVR
127         /* EEPROM routines used only with the WinAVR compiler. */
128         #include <avr/eeprom.h>
129 #endif
130
131 /* Scheduler include files. */
132 #include "FreeRTOS.h"
133 #include "task.h"
134 #include "croutine.h"
135
136 /* Demo file headers. */
137 #include "PollQ.h"
138 #include "integer.h"
139 #include "serial.h"
140 #include "comtest.h"
141 #include "crflash.h"
142 #include "print.h"
143 #include "partest.h"
144 #include "regtest.h"
145
146 /* Priority definitions for most of the tasks in the demo application.  Some
147 tasks just use the idle priority. */
148 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )
149 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )
150 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )
151 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )
152
153 /* Baud rate used by the serial port tasks. */
154 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 38400 )
155
156 /* LED used by the serial port tasks.  This is toggled on each character Tx,
157 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
158 #define mainCOM_TEST_LED                                ( 4 )
159
160 /* LED that is toggled by the check task.  The check task periodically checks
161 that all the other tasks are operating without error.  If no errors are found
162 the LED is toggled.  If an error is found at any time the LED is never toggles
163 again. */
164 #define mainCHECK_TASK_LED                              ( 7 )
165
166 /* The period between executions of the check task. */
167 #define mainCHECK_PERIOD                                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
168
169 /* An address in the EEPROM used to count resets.  This is used to check that
170 the demo application is not unexpectedly resetting. */
171 #define mainRESET_COUNT_ADDRESS                 ( ( void * ) 0x50 )
172
173 /* The number of coroutines to create. */
174 #define mainNUM_FLASH_COROUTINES                ( 3 )
175
176 /*
177  * The task function for the "Check" task.
178  */
179 static void vErrorChecks( void *pvParameters );
180
181 /*
182  * Checks the unique counts of other tasks to ensure they are still operational.
183  * Flashes an LED if everything is okay.
184  */
185 static void prvCheckOtherTasksAreStillRunning( void );
186
187 /*
188  * Called on boot to increment a count stored in the EEPROM.  This is used to
189  * ensure the CPU does not reset unexpectedly.
190  */
191 static void prvIncrementResetCount( void );
192
193 /*
194  * Idle hook is used to scheduler co-routines.
195  */
196 void vApplicationIdleHook( void );      
197
198 short main( void )
199 {
200         prvIncrementResetCount();
201
202         /* Setup the LED's for output. */
203         vParTestInitialise();
204
205         /* Create the standard demo tasks. */
206         vStartIntegerMathTasks( tskIDLE_PRIORITY );
207         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
208         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
209         vStartRegTestTasks();
210         
211         /* Create the tasks defined within this file. */
212         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
213
214         /* Create the co-routines that flash the LED's. */
215         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );
216         
217         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
218         as 1 in portmacro.h.  To use the cooperative scheduler define
219         configUSE_PREEMPTION as 0. */
220         vTaskStartScheduler();
221
222         return 0;
223 }
224 /*-----------------------------------------------------------*/
225
226 static void vErrorChecks( void *pvParameters )
227 {
228 static volatile unsigned long ulDummyVariable = 3UL;
229
230         /* The parameters are not used. */
231         ( void ) pvParameters;
232
233         /* Cycle for ever, delaying then checking all the other tasks are still
234         operating without error. */
235         for( ;; )
236         {
237                 vTaskDelay( mainCHECK_PERIOD );
238
239                 /* Perform a bit of 32bit maths to ensure the registers used by the
240                 integer tasks get some exercise. The result here is not important -
241                 see the demo application documentation for more info. */
242                 ulDummyVariable *= 3;
243                 
244                 prvCheckOtherTasksAreStillRunning();
245         }
246 }
247 /*-----------------------------------------------------------*/
248
249 static void prvCheckOtherTasksAreStillRunning( void )
250 {
251 static portBASE_TYPE xErrorHasOccurred = pdFALSE;
252
253         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
254         {
255                 xErrorHasOccurred = pdTRUE;
256         }
257
258         if( xAreComTestTasksStillRunning() != pdTRUE )
259         {
260                 xErrorHasOccurred = pdTRUE;
261         }
262
263         if( xArePollingQueuesStillRunning() != pdTRUE )
264         {
265                 xErrorHasOccurred = pdTRUE;
266         }
267
268         if( xAreRegTestTasksStillRunning() != pdTRUE )
269         {
270                 xErrorHasOccurred = pdTRUE;
271         }
272         
273         if( xErrorHasOccurred == pdFALSE )
274         {
275                 /* Toggle the LED if everything is okay so we know if an error occurs even if not
276                 using console IO. */
277                 vParTestToggleLED( mainCHECK_TASK_LED );
278         }
279 }
280 /*-----------------------------------------------------------*/
281
282 static void prvIncrementResetCount( void )
283 {
284 unsigned char ucCount;
285 const unsigned char ucReadBit = ( unsigned char ) 0x01;
286 const unsigned char ucWrite1 = ( unsigned char ) 0x04;
287 const unsigned char ucWrite2 = ( unsigned char ) 0x02;
288
289         /* Increment the EEPROM value at 0x00.
290         
291         Setup the EEPROM address. */
292         EEARH = 0x00;
293         EEARL = 0x00;
294         
295         /* Set the read enable bit. */
296         EECR |= ucReadBit;
297
298         /* Wait for the read. */
299         while( EECR & ucReadBit );
300         
301         /* The byte is ready. */
302         ucCount = EEDR;
303         
304         /* Increment the reset count, then write the byte back. */
305         ucCount++;
306         EEDR = ucCount;
307         EECR = ucWrite1;
308         EECR = ( ucWrite1 | ucWrite2 );
309 }
310 /*-----------------------------------------------------------*/
311
312 void vApplicationIdleHook( void )
313 {
314         vCoRoutineSchedule();
315 }
316