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