]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR_ATMega323_WinAVR/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / AVR_ATMega323_WinAVR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
30  * documentation provides more details of the demo application tasks.\r
31  *\r
32  * Main. c also creates a task called "Check".  This only executes every three\r
33  * seconds but has the highest priority so is guaranteed to get processor time.\r
34  * Its main function is to check that all the other tasks are still operational.\r
35  * Each task that does not flash an LED maintains a unique count that is\r
36  * incremented each time the task successfully completes its function.  Should\r
37  * any error occur within such a task the count is permanently halted.  The\r
38  * check task inspects the count of each task to ensure it has changed since\r
39  * the last time the check task executed.  If all the count variables have\r
40  * changed all the tasks are still executing error free, and the check task\r
41  * toggles an LED.  Should any task contain an error at any time the LED toggle\r
42  * will stop.\r
43  *\r
44  * The LED flash and communications test tasks do not maintain a count.\r
45  */\r
46 \r
47 /*\r
48 Changes from V1.2.0\r
49 \r
50         + Changed the baud rate for the serial test from 19200 to 57600.\r
51 \r
52 Changes from V1.2.3\r
53 \r
54         + The integer and comtest tasks are now used when the cooperative scheduler\r
55           is being used.  Previously they were only used with the preemptive\r
56           scheduler.\r
57 \r
58 Changes from V1.2.5\r
59 \r
60         + Set the baud rate to 38400.  This has a smaller error percentage with an\r
61           8MHz clock (according to the manual).\r
62 \r
63 Changes from V2.0.0\r
64 \r
65         + Delay periods are now specified using variables and constants of\r
66           TickType_t rather than unsigned long.\r
67 \r
68 Changes from V2.6.1\r
69 \r
70         + The IAR and WinAVR AVR ports are now maintained separately.\r
71 \r
72 Changes from V4.0.5\r
73 \r
74         + Modified to demonstrate the use of co-routines.\r
75 \r
76 */\r
77 \r
78 #include <stdlib.h>\r
79 #include <string.h>\r
80 \r
81 #ifdef GCC_MEGA_AVR\r
82         /* EEPROM routines used only with the WinAVR compiler. */\r
83         #include <avr/eeprom.h>\r
84 #endif\r
85 \r
86 /* Scheduler include files. */\r
87 #include "FreeRTOS.h"\r
88 #include "task.h"\r
89 #include "croutine.h"\r
90 \r
91 /* Demo file headers. */\r
92 #include "PollQ.h"\r
93 #include "integer.h"\r
94 #include "serial.h"\r
95 #include "comtest.h"\r
96 #include "crflash.h"\r
97 #include "print.h"\r
98 #include "partest.h"\r
99 #include "regtest.h"\r
100 \r
101 /* Priority definitions for most of the tasks in the demo application.  Some\r
102 tasks just use the idle priority. */\r
103 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
104 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
105 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
106 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
107 \r
108 /* Baud rate used by the serial port tasks. */\r
109 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 38400 )\r
110 \r
111 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
112 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
113 #define mainCOM_TEST_LED                                ( 4 )\r
114 \r
115 /* LED that is toggled by the check task.  The check task periodically checks\r
116 that all the other tasks are operating without error.  If no errors are found\r
117 the LED is toggled.  If an error is found at any time the LED is never toggles\r
118 again. */\r
119 #define mainCHECK_TASK_LED                              ( 7 )\r
120 \r
121 /* The period between executions of the check task. */\r
122 #define mainCHECK_PERIOD                                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
123 \r
124 /* An address in the EEPROM used to count resets.  This is used to check that\r
125 the demo application is not unexpectedly resetting. */\r
126 #define mainRESET_COUNT_ADDRESS                 ( ( void * ) 0x50 )\r
127 \r
128 /* The number of coroutines to create. */\r
129 #define mainNUM_FLASH_COROUTINES                ( 3 )\r
130 \r
131 /*\r
132  * The task function for the "Check" task.\r
133  */\r
134 static void vErrorChecks( void *pvParameters );\r
135 \r
136 /*\r
137  * Checks the unique counts of other tasks to ensure they are still operational.\r
138  * Flashes an LED if everything is okay.\r
139  */\r
140 static void prvCheckOtherTasksAreStillRunning( void );\r
141 \r
142 /*\r
143  * Called on boot to increment a count stored in the EEPROM.  This is used to\r
144  * ensure the CPU does not reset unexpectedly.\r
145  */\r
146 static void prvIncrementResetCount( void );\r
147 \r
148 /*\r
149  * The idle hook is used to scheduler co-routines.\r
150  */\r
151 void vApplicationIdleHook( void );\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 short main( void )\r
156 {\r
157         prvIncrementResetCount();\r
158 \r
159         /* Setup the LED's for output. */\r
160         vParTestInitialise();\r
161 \r
162         /* Create the standard demo tasks. */\r
163         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
164         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
165         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
166         vStartRegTestTasks();\r
167 \r
168         /* Create the tasks defined within this file. */\r
169         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
170 \r
171         /* Create the co-routines that flash the LED's. */\r
172         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
173 \r
174         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION\r
175         as 1 in portmacro.h.  To use the cooperative scheduler define\r
176         configUSE_PREEMPTION as 0. */\r
177         vTaskStartScheduler();\r
178 \r
179         return 0;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void vErrorChecks( void *pvParameters )\r
184 {\r
185 static volatile unsigned long ulDummyVariable = 3UL;\r
186 \r
187         /* The parameters are not used. */\r
188         ( void ) pvParameters;\r
189 \r
190         /* Cycle for ever, delaying then checking all the other tasks are still\r
191         operating without error. */\r
192         for( ;; )\r
193         {\r
194                 vTaskDelay( mainCHECK_PERIOD );\r
195 \r
196                 /* Perform a bit of 32bit maths to ensure the registers used by the\r
197                 integer tasks get some exercise. The result here is not important -\r
198                 see the demo application documentation for more info. */\r
199                 ulDummyVariable *= 3;\r
200 \r
201                 prvCheckOtherTasksAreStillRunning();\r
202         }\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvCheckOtherTasksAreStillRunning( void )\r
207 {\r
208 static portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
209 \r
210         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
211         {\r
212                 xErrorHasOccurred = pdTRUE;\r
213         }\r
214 \r
215         if( xAreComTestTasksStillRunning() != pdTRUE )\r
216         {\r
217                 xErrorHasOccurred = pdTRUE;\r
218         }\r
219 \r
220         if( xArePollingQueuesStillRunning() != pdTRUE )\r
221         {\r
222                 xErrorHasOccurred = pdTRUE;\r
223         }\r
224 \r
225         if( xAreRegTestTasksStillRunning() != pdTRUE )\r
226         {\r
227                 xErrorHasOccurred = pdTRUE;\r
228         }\r
229 \r
230         if( xErrorHasOccurred == pdFALSE )\r
231         {\r
232                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
233                 using console IO. */\r
234                 vParTestToggleLED( mainCHECK_TASK_LED );\r
235         }\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 static void prvIncrementResetCount( void )\r
240 {\r
241 unsigned char ucCount;\r
242 \r
243         eeprom_read_block( &ucCount, mainRESET_COUNT_ADDRESS, sizeof( ucCount ) );\r
244         ucCount++;\r
245         eeprom_write_byte( mainRESET_COUNT_ADDRESS, ucCount );\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 void vApplicationIdleHook( void )\r
250 {\r
251         vCoRoutineSchedule();\r
252 }\r
253 \r