]> git.sur5r.net Git - freertos/blob - Demo/AVR_ATMega323_IAR/main.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Demo / AVR_ATMega323_IAR / main.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
56  * documentation provides more details of the demo application tasks.\r
57  *\r
58  * Main. c also creates a task called "Check".  This only executes every three\r
59  * seconds but has the highest priority so is guaranteed to get processor time.\r
60  * Its main function is to check that all the other tasks are still operational.\r
61  * Each task that does not flash an LED maintains a unique count that is\r
62  * incremented each time the task successfully completes its function.  Should\r
63  * any error occur within such a task the count is permanently halted.  The\r
64  * check task inspects the count of each task to ensure it has changed since\r
65  * the last time the check task executed.  If all the count variables have\r
66  * changed all the tasks are still executing error free, and the check task\r
67  * toggles an LED.  Should any task contain an error at any time the LED toggle\r
68  * will stop.\r
69  *\r
70  * The LED flash and communications test tasks do not maintain a count.\r
71  */\r
72 \r
73 /*\r
74 Changes from V1.2.0\r
75         \r
76         + Changed the baud rate for the serial test from 19200 to 57600.\r
77 \r
78 Changes from V1.2.3\r
79 \r
80         + The integer and comtest tasks are now used when the cooperative scheduler\r
81           is being used.  Previously they were only used with the preemptive\r
82           scheduler.\r
83 \r
84 Changes from V1.2.5\r
85 \r
86         + Set the baud rate to 38400.  This has a smaller error percentage with an\r
87           8MHz clock (according to the manual).\r
88 \r
89 Changes from V2.0.0\r
90 \r
91         + Delay periods are now specified using variables and constants of\r
92           portTickType rather than unsigned long.\r
93 \r
94 Changes from V2.2.0\r
95 \r
96         + File can now be built using either the IAR or WinAVR compiler.\r
97 \r
98 Changes from V2.6.1\r
99 \r
100         + The IAR and WinAVR AVR ports are now maintained separately.\r
101 \r
102 Changes from V4.0.5\r
103 \r
104         + Modified to demonstrate the use of co-routines.\r
105 */\r
106 \r
107 #include <stdlib.h>\r
108 #include <string.h>\r
109 \r
110 #ifdef GCC_MEGA_AVR\r
111         /* EEPROM routines used only with the WinAVR compiler. */\r
112         #include <avr/eeprom.h>\r
113 #endif\r
114 \r
115 /* Scheduler include files. */\r
116 #include "FreeRTOS.h"\r
117 #include "task.h"\r
118 #include "croutine.h"\r
119 \r
120 /* Demo file headers. */\r
121 #include "PollQ.h"\r
122 #include "integer.h"\r
123 #include "serial.h"\r
124 #include "comtest.h"\r
125 #include "crflash.h"\r
126 #include "print.h"\r
127 #include "partest.h"\r
128 #include "regtest.h"\r
129 \r
130 /* Priority definitions for most of the tasks in the demo application.  Some\r
131 tasks just use the idle priority. */\r
132 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
133 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
134 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
135 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
136 \r
137 /* Baud rate used by the serial port tasks. */\r
138 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 38400 )\r
139 \r
140 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
141 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
142 #define mainCOM_TEST_LED                                ( 4 )\r
143 \r
144 /* LED that is toggled by the check task.  The check task periodically checks\r
145 that all the other tasks are operating without error.  If no errors are found\r
146 the LED is toggled.  If an error is found at any time the LED is never toggles\r
147 again. */\r
148 #define mainCHECK_TASK_LED                              ( 7 )\r
149 \r
150 /* The period between executions of the check task. */\r
151 #define mainCHECK_PERIOD                                ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
152 \r
153 /* An address in the EEPROM used to count resets.  This is used to check that\r
154 the demo application is not unexpectedly resetting. */\r
155 #define mainRESET_COUNT_ADDRESS                 ( ( void * ) 0x50 )\r
156 \r
157 /* The number of coroutines to create. */\r
158 #define mainNUM_FLASH_COROUTINES                ( 3 )\r
159 \r
160 /*\r
161  * The task function for the "Check" task.\r
162  */\r
163 static void vErrorChecks( void *pvParameters );\r
164 \r
165 /*\r
166  * Checks the unique counts of other tasks to ensure they are still operational.\r
167  * Flashes an LED if everything is okay.\r
168  */\r
169 static void prvCheckOtherTasksAreStillRunning( void );\r
170 \r
171 /*\r
172  * Called on boot to increment a count stored in the EEPROM.  This is used to\r
173  * ensure the CPU does not reset unexpectedly.\r
174  */\r
175 static void prvIncrementResetCount( void );\r
176 \r
177 /*\r
178  * Idle hook is used to scheduler co-routines.\r
179  */\r
180 void vApplicationIdleHook( void );      \r
181 \r
182 short main( void )\r
183 {\r
184         prvIncrementResetCount();\r
185 \r
186         /* Setup the LED's for output. */\r
187         vParTestInitialise();\r
188 \r
189         /* Create the standard demo tasks. */\r
190         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
191         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
192         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
193         vStartRegTestTasks();\r
194         \r
195         /* Create the tasks defined within this file. */\r
196         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
197 \r
198         /* Create the co-routines that flash the LED's. */\r
199         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
200         \r
201         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION\r
202         as 1 in portmacro.h.  To use the cooperative scheduler define\r
203         configUSE_PREEMPTION as 0. */\r
204         vTaskStartScheduler();\r
205 \r
206         return 0;\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 static void vErrorChecks( void *pvParameters )\r
211 {\r
212 static volatile unsigned long ulDummyVariable = 3UL;\r
213 \r
214         /* The parameters are not used. */\r
215         ( void ) pvParameters;\r
216 \r
217         /* Cycle for ever, delaying then checking all the other tasks are still\r
218         operating without error. */\r
219         for( ;; )\r
220         {\r
221                 vTaskDelay( mainCHECK_PERIOD );\r
222 \r
223                 /* Perform a bit of 32bit maths to ensure the registers used by the\r
224                 integer tasks get some exercise. The result here is not important -\r
225                 see the demo application documentation for more info. */\r
226                 ulDummyVariable *= 3;\r
227                 \r
228                 prvCheckOtherTasksAreStillRunning();\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvCheckOtherTasksAreStillRunning( void )\r
234 {\r
235 static portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
236 \r
237         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
238         {\r
239                 xErrorHasOccurred = pdTRUE;\r
240         }\r
241 \r
242         if( xAreComTestTasksStillRunning() != pdTRUE )\r
243         {\r
244                 xErrorHasOccurred = pdTRUE;\r
245         }\r
246 \r
247         if( xArePollingQueuesStillRunning() != pdTRUE )\r
248         {\r
249                 xErrorHasOccurred = pdTRUE;\r
250         }\r
251 \r
252         if( xAreRegTestTasksStillRunning() != pdTRUE )\r
253         {\r
254                 xErrorHasOccurred = pdTRUE;\r
255         }\r
256         \r
257         if( xErrorHasOccurred == pdFALSE )\r
258         {\r
259                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
260                 using console IO. */\r
261                 vParTestToggleLED( mainCHECK_TASK_LED );\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 static void prvIncrementResetCount( void )\r
267 {\r
268 unsigned char ucCount;\r
269 const unsigned char ucReadBit = ( unsigned char ) 0x01;\r
270 const unsigned char ucWrite1 = ( unsigned char ) 0x04;\r
271 const unsigned char ucWrite2 = ( unsigned char ) 0x02;\r
272 \r
273         /* Increment the EEPROM value at 0x00.\r
274         \r
275         Setup the EEPROM address. */\r
276         EEARH = 0x00;\r
277         EEARL = 0x00;\r
278         \r
279         /* Set the read enable bit. */\r
280         EECR |= ucReadBit;\r
281 \r
282         /* Wait for the read. */\r
283         while( EECR & ucReadBit );\r
284         \r
285         /* The byte is ready. */\r
286         ucCount = EEDR;\r
287         \r
288         /* Increment the reset count, then write the byte back. */\r
289         ucCount++;\r
290         EEDR = ucCount;\r
291         EECR = ucWrite1;\r
292         EECR = ( ucWrite1 | ucWrite2 );\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 void vApplicationIdleHook( void )\r
297 {\r
298         vCoRoutineSchedule();\r
299 }\r
300 \r