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