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