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