]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR_ATMega323_IAR/main.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / AVR_ATMega323_IAR / main.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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           TickType_t rather than unsigned long.\r
105 \r
106 Changes from V2.2.0\r
107 \r
108         + File can now be built using either the IAR or WinAVR compiler.\r
109 \r
110 Changes from V2.6.1\r
111 \r
112         + The IAR and WinAVR AVR ports are now maintained separately.\r
113 \r
114 Changes from V4.0.5\r
115 \r
116         + Modified to demonstrate the use of co-routines.\r
117 */\r
118 \r
119 #include <stdlib.h>\r
120 #include <string.h>\r
121 \r
122 #ifdef GCC_MEGA_AVR\r
123         /* EEPROM routines used only with the WinAVR compiler. */\r
124         #include <avr/eeprom.h>\r
125 #endif\r
126 \r
127 /* Scheduler include files. */\r
128 #include "FreeRTOS.h"\r
129 #include "task.h"\r
130 #include "croutine.h"\r
131 \r
132 /* Demo file headers. */\r
133 #include "PollQ.h"\r
134 #include "integer.h"\r
135 #include "serial.h"\r
136 #include "comtest.h"\r
137 #include "crflash.h"\r
138 #include "print.h"\r
139 #include "partest.h"\r
140 #include "regtest.h"\r
141 \r
142 /* Priority definitions for most of the tasks in the demo application.  Some\r
143 tasks just use the idle priority. */\r
144 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
145 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
146 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
147 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
148 \r
149 /* Baud rate used by the serial port tasks. */\r
150 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 38400 )\r
151 \r
152 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
153 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
154 #define mainCOM_TEST_LED                                ( 4 )\r
155 \r
156 /* LED that is toggled by the check task.  The check task periodically checks\r
157 that all the other tasks are operating without error.  If no errors are found\r
158 the LED is toggled.  If an error is found at any time the LED is never toggles\r
159 again. */\r
160 #define mainCHECK_TASK_LED                              ( 7 )\r
161 \r
162 /* The period between executions of the check task. */\r
163 #define mainCHECK_PERIOD                                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
164 \r
165 /* An address in the EEPROM used to count resets.  This is used to check that\r
166 the demo application is not unexpectedly resetting. */\r
167 #define mainRESET_COUNT_ADDRESS                 ( ( void * ) 0x50 )\r
168 \r
169 /* The number of coroutines to create. */\r
170 #define mainNUM_FLASH_COROUTINES                ( 3 )\r
171 \r
172 /*\r
173  * The task function for the "Check" task.\r
174  */\r
175 static void vErrorChecks( void *pvParameters );\r
176 \r
177 /*\r
178  * Checks the unique counts of other tasks to ensure they are still operational.\r
179  * Flashes an LED if everything is okay.\r
180  */\r
181 static void prvCheckOtherTasksAreStillRunning( void );\r
182 \r
183 /*\r
184  * Called on boot to increment a count stored in the EEPROM.  This is used to\r
185  * ensure the CPU does not reset unexpectedly.\r
186  */\r
187 static void prvIncrementResetCount( void );\r
188 \r
189 /*\r
190  * Idle hook is used to scheduler co-routines.\r
191  */\r
192 void vApplicationIdleHook( void );      \r
193 \r
194 short main( void )\r
195 {\r
196         prvIncrementResetCount();\r
197 \r
198         /* Setup the LED's for output. */\r
199         vParTestInitialise();\r
200 \r
201         /* Create the standard demo tasks. */\r
202         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
203         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
204         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
205         vStartRegTestTasks();\r
206         \r
207         /* Create the tasks defined within this file. */\r
208         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
209 \r
210         /* Create the co-routines that flash the LED's. */\r
211         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
212         \r
213         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION\r
214         as 1 in portmacro.h.  To use the cooperative scheduler define\r
215         configUSE_PREEMPTION as 0. */\r
216         vTaskStartScheduler();\r
217 \r
218         return 0;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 static void vErrorChecks( void *pvParameters )\r
223 {\r
224 static volatile unsigned long ulDummyVariable = 3UL;\r
225 \r
226         /* The parameters are not used. */\r
227         ( void ) pvParameters;\r
228 \r
229         /* Cycle for ever, delaying then checking all the other tasks are still\r
230         operating without error. */\r
231         for( ;; )\r
232         {\r
233                 vTaskDelay( mainCHECK_PERIOD );\r
234 \r
235                 /* Perform a bit of 32bit maths to ensure the registers used by the\r
236                 integer tasks get some exercise. The result here is not important -\r
237                 see the demo application documentation for more info. */\r
238                 ulDummyVariable *= 3;\r
239                 \r
240                 prvCheckOtherTasksAreStillRunning();\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvCheckOtherTasksAreStillRunning( void )\r
246 {\r
247 static portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
248 \r
249         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
250         {\r
251                 xErrorHasOccurred = pdTRUE;\r
252         }\r
253 \r
254         if( xAreComTestTasksStillRunning() != pdTRUE )\r
255         {\r
256                 xErrorHasOccurred = pdTRUE;\r
257         }\r
258 \r
259         if( xArePollingQueuesStillRunning() != pdTRUE )\r
260         {\r
261                 xErrorHasOccurred = pdTRUE;\r
262         }\r
263 \r
264         if( xAreRegTestTasksStillRunning() != pdTRUE )\r
265         {\r
266                 xErrorHasOccurred = pdTRUE;\r
267         }\r
268         \r
269         if( xErrorHasOccurred == pdFALSE )\r
270         {\r
271                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
272                 using console IO. */\r
273                 vParTestToggleLED( mainCHECK_TASK_LED );\r
274         }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 static void prvIncrementResetCount( void )\r
279 {\r
280 unsigned char ucCount;\r
281 const unsigned char ucReadBit = ( unsigned char ) 0x01;\r
282 const unsigned char ucWrite1 = ( unsigned char ) 0x04;\r
283 const unsigned char ucWrite2 = ( unsigned char ) 0x02;\r
284 \r
285         /* Increment the EEPROM value at 0x00.\r
286         \r
287         Setup the EEPROM address. */\r
288         EEARH = 0x00;\r
289         EEARL = 0x00;\r
290         \r
291         /* Set the read enable bit. */\r
292         EECR |= ucReadBit;\r
293 \r
294         /* Wait for the read. */\r
295         while( EECR & ucReadBit );\r
296         \r
297         /* The byte is ready. */\r
298         ucCount = EEDR;\r
299         \r
300         /* Increment the reset count, then write the byte back. */\r
301         ucCount++;\r
302         EEDR = ucCount;\r
303         EECR = ucWrite1;\r
304         EECR = ( ucWrite1 | ucWrite2 );\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 void vApplicationIdleHook( void )\r
309 {\r
310         vCoRoutineSchedule();\r
311 }\r
312 \r