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