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