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