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