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