]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR_ATMega323_IAR/main.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / AVR_ATMega323_IAR / main.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
71  * documentation provides more details of the demo application tasks.\r
72  *\r
73  * Main. c also creates a task called "Check".  This only executes every three\r
74  * seconds but has the highest priority so is guaranteed to get processor time.\r
75  * Its main function is to check that all the other tasks are still operational.\r
76  * Each task that does not flash an LED maintains a unique count that is\r
77  * incremented each time the task successfully completes its function.  Should\r
78  * any error occur within such a task the count is permanently halted.  The\r
79  * check task inspects the count of each task to ensure it has changed since\r
80  * the last time the check task executed.  If all the count variables have\r
81  * changed all the tasks are still executing error free, and the check task\r
82  * toggles an LED.  Should any task contain an error at any time the LED toggle\r
83  * will stop.\r
84  *\r
85  * The LED flash and communications test tasks do not maintain a count.\r
86  */\r
87 \r
88 /*\r
89 Changes from V1.2.0\r
90         \r
91         + Changed the baud rate for the serial test from 19200 to 57600.\r
92 \r
93 Changes from V1.2.3\r
94 \r
95         + The integer and comtest tasks are now used when the cooperative scheduler\r
96           is being used.  Previously they were only used with the preemptive\r
97           scheduler.\r
98 \r
99 Changes from V1.2.5\r
100 \r
101         + Set the baud rate to 38400.  This has a smaller error percentage with an\r
102           8MHz clock (according to the manual).\r
103 \r
104 Changes from V2.0.0\r
105 \r
106         + Delay periods are now specified using variables and constants of\r
107           portTickType rather than unsigned long.\r
108 \r
109 Changes from V2.2.0\r
110 \r
111         + File can now be built using either the IAR or WinAVR compiler.\r
112 \r
113 Changes from V2.6.1\r
114 \r
115         + The IAR and WinAVR AVR ports are now maintained separately.\r
116 \r
117 Changes from V4.0.5\r
118 \r
119         + Modified to demonstrate the use of co-routines.\r
120 */\r
121 \r
122 #include <stdlib.h>\r
123 #include <string.h>\r
124 \r
125 #ifdef GCC_MEGA_AVR\r
126         /* EEPROM routines used only with the WinAVR compiler. */\r
127         #include <avr/eeprom.h>\r
128 #endif\r
129 \r
130 /* Scheduler include files. */\r
131 #include "FreeRTOS.h"\r
132 #include "task.h"\r
133 #include "croutine.h"\r
134 \r
135 /* Demo file headers. */\r
136 #include "PollQ.h"\r
137 #include "integer.h"\r
138 #include "serial.h"\r
139 #include "comtest.h"\r
140 #include "crflash.h"\r
141 #include "print.h"\r
142 #include "partest.h"\r
143 #include "regtest.h"\r
144 \r
145 /* Priority definitions for most of the tasks in the demo application.  Some\r
146 tasks just use the idle priority. */\r
147 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
148 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
149 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
150 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
151 \r
152 /* Baud rate used by the serial port tasks. */\r
153 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 38400 )\r
154 \r
155 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
156 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
157 #define mainCOM_TEST_LED                                ( 4 )\r
158 \r
159 /* LED that is toggled by the check task.  The check task periodically checks\r
160 that all the other tasks are operating without error.  If no errors are found\r
161 the LED is toggled.  If an error is found at any time the LED is never toggles\r
162 again. */\r
163 #define mainCHECK_TASK_LED                              ( 7 )\r
164 \r
165 /* The period between executions of the check task. */\r
166 #define mainCHECK_PERIOD                                ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
167 \r
168 /* An address in the EEPROM used to count resets.  This is used to check that\r
169 the demo application is not unexpectedly resetting. */\r
170 #define mainRESET_COUNT_ADDRESS                 ( ( void * ) 0x50 )\r
171 \r
172 /* The number of coroutines to create. */\r
173 #define mainNUM_FLASH_COROUTINES                ( 3 )\r
174 \r
175 /*\r
176  * The task function for the "Check" task.\r
177  */\r
178 static void vErrorChecks( void *pvParameters );\r
179 \r
180 /*\r
181  * Checks the unique counts of other tasks to ensure they are still operational.\r
182  * Flashes an LED if everything is okay.\r
183  */\r
184 static void prvCheckOtherTasksAreStillRunning( void );\r
185 \r
186 /*\r
187  * Called on boot to increment a count stored in the EEPROM.  This is used to\r
188  * ensure the CPU does not reset unexpectedly.\r
189  */\r
190 static void prvIncrementResetCount( void );\r
191 \r
192 /*\r
193  * Idle hook is used to scheduler co-routines.\r
194  */\r
195 void vApplicationIdleHook( void );      \r
196 \r
197 short main( void )\r
198 {\r
199         prvIncrementResetCount();\r
200 \r
201         /* Setup the LED's for output. */\r
202         vParTestInitialise();\r
203 \r
204         /* Create the standard demo tasks. */\r
205         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
206         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
207         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
208         vStartRegTestTasks();\r
209         \r
210         /* Create the tasks defined within this file. */\r
211         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
212 \r
213         /* Create the co-routines that flash the LED's. */\r
214         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
215         \r
216         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION\r
217         as 1 in portmacro.h.  To use the cooperative scheduler define\r
218         configUSE_PREEMPTION as 0. */\r
219         vTaskStartScheduler();\r
220 \r
221         return 0;\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 static void vErrorChecks( void *pvParameters )\r
226 {\r
227 static volatile unsigned long ulDummyVariable = 3UL;\r
228 \r
229         /* The parameters are not used. */\r
230         ( void ) pvParameters;\r
231 \r
232         /* Cycle for ever, delaying then checking all the other tasks are still\r
233         operating without error. */\r
234         for( ;; )\r
235         {\r
236                 vTaskDelay( mainCHECK_PERIOD );\r
237 \r
238                 /* Perform a bit of 32bit maths to ensure the registers used by the\r
239                 integer tasks get some exercise. The result here is not important -\r
240                 see the demo application documentation for more info. */\r
241                 ulDummyVariable *= 3;\r
242                 \r
243                 prvCheckOtherTasksAreStillRunning();\r
244         }\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 static void prvCheckOtherTasksAreStillRunning( void )\r
249 {\r
250 static portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
251 \r
252         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
253         {\r
254                 xErrorHasOccurred = pdTRUE;\r
255         }\r
256 \r
257         if( xAreComTestTasksStillRunning() != pdTRUE )\r
258         {\r
259                 xErrorHasOccurred = pdTRUE;\r
260         }\r
261 \r
262         if( xArePollingQueuesStillRunning() != pdTRUE )\r
263         {\r
264                 xErrorHasOccurred = pdTRUE;\r
265         }\r
266 \r
267         if( xAreRegTestTasksStillRunning() != pdTRUE )\r
268         {\r
269                 xErrorHasOccurred = pdTRUE;\r
270         }\r
271         \r
272         if( xErrorHasOccurred == pdFALSE )\r
273         {\r
274                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
275                 using console IO. */\r
276                 vParTestToggleLED( mainCHECK_TASK_LED );\r
277         }\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 static void prvIncrementResetCount( void )\r
282 {\r
283 unsigned char ucCount;\r
284 const unsigned char ucReadBit = ( unsigned char ) 0x01;\r
285 const unsigned char ucWrite1 = ( unsigned char ) 0x04;\r
286 const unsigned char ucWrite2 = ( unsigned char ) 0x02;\r
287 \r
288         /* Increment the EEPROM value at 0x00.\r
289         \r
290         Setup the EEPROM address. */\r
291         EEARH = 0x00;\r
292         EEARL = 0x00;\r
293         \r
294         /* Set the read enable bit. */\r
295         EECR |= ucReadBit;\r
296 \r
297         /* Wait for the read. */\r
298         while( EECR & ucReadBit );\r
299         \r
300         /* The byte is ready. */\r
301         ucCount = EEDR;\r
302         \r
303         /* Increment the reset count, then write the byte back. */\r
304         ucCount++;\r
305         EEDR = ucCount;\r
306         EECR = ucWrite1;\r
307         EECR = ( ucWrite1 | ucWrite2 );\r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r
311 void vApplicationIdleHook( void )\r
312 {\r
313         vCoRoutineSchedule();\r
314 }\r
315 \r