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