]> begriffs open source - cmsis-freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/main.c
Set error state if no delay or already expired
[cmsis-freertos] / Demo / lwIP_Demo_Rowley_ARM7 / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 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         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
30         The processor MUST be in supervisor mode when vTaskStartScheduler is
31         called.  The demo applications included in the FreeRTOS.org download switch
32         to supervisor mode prior to main being called.  If you are not using one of
33         these demo application projects then ensure Supervisor mode is used.
34 */
35
36
37 /*
38  * Creates all the application tasks, then starts the scheduler.
39  *
40  * A task defined by the function vBasicWEBServer is created.  This executes
41  * the lwIP stack and basic WEB server sample.  A task defined by the function
42  * vUSBCDCTask.  This executes the USB to serial CDC example.  All the other
43  * tasks are from the set of standard demo tasks.  The WEB documentation
44  * provides more details of the standard demo application tasks.
45  *
46  * Main.c also creates a task called "Check".  This only executes every three
47  * seconds but has the highest priority so is guaranteed to get processor time.
48  * Its main function is to check the status of all the other demo application
49  * tasks.  LED mainCHECK_LED is toggled every three seconds by the check task
50  * should no error conditions be detected in any of the standard demo tasks.
51  * The toggle rate increasing to 500ms indicates that at least one error has
52  * been detected.
53  *
54  * Main.c includes an idle hook function that simply periodically sends data
55  * to the USB task for transmission.
56  */
57
58 /*
59         Changes from V3.2.2
60
61         + Modified the stack sizes used by some tasks to permit use of the
62           command line GCC tools.
63 */
64
65 /* Library includes. */
66 #include <string.h>
67 #include <stdio.h>
68
69 /* Scheduler includes. */
70 #include "FreeRTOS.h"
71 #include "task.h"
72
73 /* Demo application includes. */
74 #include "partest.h"
75 #include "PollQ.h"
76 #include "semtest.h"
77 #include "flash.h"
78 #include "integer.h"
79 #include "BlockQ.h"
80 #include "BasicWEB.h"
81 #include "USB-CDC.h"
82
83 /* lwIP includes. */
84 #include "lwip/api.h"
85
86 /* Hardware specific headers. */
87 #include "Board.h"
88 #include "AT91SAM7X256.h"
89
90 /* Priorities/stacks for the various tasks within the demo application. */
91 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )
92 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
93 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
94 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
95 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 1 )
96 #define mainWEBSERVER_PRIORITY      ( tskIDLE_PRIORITY + 2 )
97 #define mainUSB_PRIORITY                        ( tskIDLE_PRIORITY + 1 )
98 #define mainUSB_TASK_STACK                      ( 200 )
99
100 /* The rate at which the on board LED will toggle when there is/is not an
101 error. */
102 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
103 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )
104
105 /* The rate at which the idle hook sends data to the USB port. */
106 #define mainUSB_TX_FREQUENCY            ( 100 / portTICK_PERIOD_MS )
107
108 /* The string that is transmitted down the USB port. */
109 #define mainFIRST_TX_CHAR                       'a'
110 #define mainLAST_TX_CHAR                        'z'
111
112 /* The LED used by the check task to indicate the system status. */
113 #define mainCHECK_LED                           ( 3 )
114 /*-----------------------------------------------------------*/
115
116 /*
117  * Checks that all the demo application tasks are still executing without error
118  * - as described at the top of the file.
119  */
120 static long prvCheckOtherTasksAreStillRunning( void );
121
122 /*
123  * The task that executes at the highest priority and calls
124  * prvCheckOtherTasksAreStillRunning().  See the description at the top
125  * of the file.
126  */
127 static void vErrorChecks( void *pvParameters );
128
129 /*
130  * Configure the processor for use with the Atmel demo board.  This is very
131  * minimal as most of the setup is performed in the startup code.
132  */
133 static void prvSetupHardware( void );
134
135 /*
136  * The idle hook is just used to stream data to the USB port.
137  */
138 void vApplicationIdleHook( void );
139 /*-----------------------------------------------------------*/
140
141 /*
142  * Setup hardware then start all the demo application tasks.
143  */
144 int main( void )
145 {
146         /* Setup the ports. */
147         prvSetupHardware();
148
149         /* Setup the IO required for the LED's. */
150         vParTestInitialise();
151
152         /* Setup lwIP. */
153     vlwIPInit();
154
155         /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/
156     sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );
157
158         /* Create the demo USB CDC task. */
159         xTaskCreate( vUSBCDCTask, "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );
160
161         /* Create the standard demo application tasks. */
162         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
163         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
164         vStartLEDFlashTasks( mainFLASH_PRIORITY );
165         vStartIntegerMathTasks( tskIDLE_PRIORITY );
166         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
167
168         /* Start the check task - which is defined in this file. */
169     xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
170
171         /* Finally, start the scheduler.
172
173         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
174         The processor MUST be in supervisor mode when vTaskStartScheduler is
175         called.  The demo applications included in the FreeRTOS.org download switch
176         to supervisor mode prior to main being called.  If you are not using one of
177         these demo application projects then ensure Supervisor mode is used here. */
178         vTaskStartScheduler();
179
180         /* Should never get here! */
181         return 0;
182 }
183 /*-----------------------------------------------------------*/
184
185
186 static void prvSetupHardware( void )
187 {
188         /* When using the JTAG debugger the hardware is not always initialised to
189         the correct default state.  This line just ensures that this does not
190         cause all interrupts to be masked at the start. */
191         AT91C_BASE_AIC->AIC_EOICR = 0;
192
193         /* Most setup is performed by the low level init function called from the
194         startup asm file.
195
196         Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as
197         well as the UART Tx line. */
198         AT91C_BASE_PIOB->PIO_PER = LED_MASK; // Set in PIO mode
199         AT91C_BASE_PIOB->PIO_OER = LED_MASK; // Configure in Output
200
201
202         /* Enable the peripheral clock. */
203     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
204     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
205         AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;
206 }
207 /*-----------------------------------------------------------*/
208
209 static void vErrorChecks( void *pvParameters )
210 {
211 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
212 TickType_t xLastWakeTime;
213
214         /* The parameters are not used. */
215         ( void ) pvParameters;
216
217         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
218         functions correctly. */
219         xLastWakeTime = xTaskGetTickCount();
220
221         /* Cycle for ever, delaying then checking all the other tasks are still
222         operating without error.  If an error is detected then the delay period
223         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
224         the Check LED flash rate will increase. */
225         for( ;; )
226         {
227                 /* Delay until it is time to execute again.  The delay period is
228                 shorter following an error. */
229                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
230
231                 /* Check all the standard demo application tasks are executing without
232                 error.  */
233                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
234                 {
235                         /* An error has been detected in one of the tasks - flash faster. */
236                         xDelayPeriod = mainERROR_FLASH_PERIOD;
237                 }
238
239                 vParTestToggleLED( mainCHECK_LED );
240         }
241 }
242 /*-----------------------------------------------------------*/
243
244 static long prvCheckOtherTasksAreStillRunning( void )
245 {
246 long lReturn = ( long ) pdPASS;
247
248         /* Check all the demo tasks (other than the flash tasks) to ensure
249         that they are all still running, and that none of them have detected
250         an error. */
251
252         if( xArePollingQueuesStillRunning() != pdTRUE )
253         {
254                 lReturn = ( long ) pdFAIL;
255         }
256
257         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
258         {
259                 lReturn = ( long ) pdFAIL;
260         }
261
262         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
263         {
264                 lReturn = ( long ) pdFAIL;
265         }
266
267         if( xAreBlockingQueuesStillRunning() != pdTRUE )
268         {
269                 lReturn = ( long ) pdFAIL;
270         }
271
272         return lReturn;
273 }
274 /*-----------------------------------------------------------*/
275
276 void vApplicationIdleHook( void )
277 {
278 static TickType_t xLastTx = 0;
279 char cTxByte;
280
281         /* The idle hook simply sends a string of characters to the USB port.
282         The characters will be buffered and sent once the port is connected. */
283         if( ( xTaskGetTickCount() - xLastTx ) > mainUSB_TX_FREQUENCY )
284         {
285                 xLastTx = xTaskGetTickCount();
286                 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )
287                 {
288                         vUSBSendByte( cTxByte );
289                 }
290         }
291 }
292
293