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