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