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