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