]> begriffs open source - cmsis-freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / lwIP_Demo_Rowley_ARM7 / main.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /*
71         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
72         The processor MUST be in supervisor mode when vTaskStartScheduler is
73         called.  The demo applications included in the FreeRTOS.org download switch
74         to supervisor mode prior to main being called.  If you are not using one of
75         these demo application projects then ensure Supervisor mode is used.
76 */
77
78
79 /*
80  * Creates all the application tasks, then starts the scheduler.
81  *
82  * A task defined by the function vBasicWEBServer is created.  This executes
83  * the lwIP stack and basic WEB server sample.  A task defined by the function
84  * vUSBCDCTask.  This executes the USB to serial CDC example.  All the other
85  * tasks are from the set of standard demo tasks.  The WEB documentation
86  * provides more details of the standard demo application tasks.
87  *
88  * Main.c also creates a task called "Check".  This only executes every three
89  * seconds but has the highest priority so is guaranteed to get processor time.
90  * Its main function is to check the status of all the other demo application
91  * tasks.  LED mainCHECK_LED is toggled every three seconds by the check task
92  * should no error conditions be detected in any of the standard demo tasks.
93  * The toggle rate increasing to 500ms indicates that at least one error has
94  * been detected.
95  *
96  * Main.c includes an idle hook function that simply periodically sends data
97  * to the USB task for transmission.
98  */
99
100 /*
101         Changes from V3.2.2
102
103         + Modified the stack sizes used by some tasks to permit use of the
104           command line GCC tools.
105 */
106
107 /* Library includes. */
108 #include <string.h>
109 #include <stdio.h>
110
111 /* Scheduler includes. */
112 #include "FreeRTOS.h"
113 #include "task.h"
114
115 /* Demo application includes. */
116 #include "partest.h"
117 #include "PollQ.h"
118 #include "semtest.h"
119 #include "flash.h"
120 #include "integer.h"
121 #include "BlockQ.h"
122 #include "BasicWEB.h"
123 #include "USB-CDC.h"
124
125 /* lwIP includes. */
126 #include "lwip/api.h"
127
128 /* Hardware specific headers. */
129 #include "Board.h"
130 #include "AT91SAM7X256.h"
131
132 /* Priorities/stacks for the various tasks within the demo application. */
133 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )
134 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
135 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
136 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
137 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 1 )
138 #define mainWEBSERVER_PRIORITY      ( tskIDLE_PRIORITY + 2 )
139 #define mainUSB_PRIORITY                        ( tskIDLE_PRIORITY + 1 )
140 #define mainUSB_TASK_STACK                      ( 200 )
141
142 /* The rate at which the on board LED will toggle when there is/is not an
143 error. */
144 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
145 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )
146
147 /* The rate at which the idle hook sends data to the USB port. */
148 #define mainUSB_TX_FREQUENCY            ( 100 / portTICK_PERIOD_MS )
149
150 /* The string that is transmitted down the USB port. */
151 #define mainFIRST_TX_CHAR                       'a'
152 #define mainLAST_TX_CHAR                        'z'
153
154 /* The LED used by the check task to indicate the system status. */
155 #define mainCHECK_LED                           ( 3 )
156 /*-----------------------------------------------------------*/
157
158 /*
159  * Checks that all the demo application tasks are still executing without error
160  * - as described at the top of the file.
161  */
162 static long prvCheckOtherTasksAreStillRunning( void );
163
164 /*
165  * The task that executes at the highest priority and calls
166  * prvCheckOtherTasksAreStillRunning().  See the description at the top
167  * of the file.
168  */
169 static void vErrorChecks( void *pvParameters );
170
171 /*
172  * Configure the processor for use with the Atmel demo board.  This is very
173  * minimal as most of the setup is performed in the startup code.
174  */
175 static void prvSetupHardware( void );
176
177 /*
178  * The idle hook is just used to stream data to the USB port.
179  */
180 void vApplicationIdleHook( void );
181 /*-----------------------------------------------------------*/
182
183 /*
184  * Setup hardware then start all the demo application tasks.
185  */
186 int main( void )
187 {
188         /* Setup the ports. */
189         prvSetupHardware();
190
191         /* Setup the IO required for the LED's. */
192         vParTestInitialise();
193
194         /* Setup lwIP. */
195     vlwIPInit();
196
197         /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/
198     sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );
199
200         /* Create the demo USB CDC task. */
201         xTaskCreate( vUSBCDCTask, "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );
202
203         /* Create the standard demo application tasks. */
204         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
205         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
206         vStartLEDFlashTasks( mainFLASH_PRIORITY );
207         vStartIntegerMathTasks( tskIDLE_PRIORITY );
208         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
209
210         /* Start the check task - which is defined in this file. */
211     xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
212
213         /* Finally, start the scheduler.
214
215         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
216         The processor MUST be in supervisor mode when vTaskStartScheduler is
217         called.  The demo applications included in the FreeRTOS.org download switch
218         to supervisor mode prior to main being called.  If you are not using one of
219         these demo application projects then ensure Supervisor mode is used here. */
220         vTaskStartScheduler();
221
222         /* Should never get here! */
223         return 0;
224 }
225 /*-----------------------------------------------------------*/
226
227
228 static void prvSetupHardware( void )
229 {
230         /* When using the JTAG debugger the hardware is not always initialised to
231         the correct default state.  This line just ensures that this does not
232         cause all interrupts to be masked at the start. */
233         AT91C_BASE_AIC->AIC_EOICR = 0;
234
235         /* Most setup is performed by the low level init function called from the
236         startup asm file.
237
238         Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as
239         well as the UART Tx line. */
240         AT91C_BASE_PIOB->PIO_PER = LED_MASK; // Set in PIO mode
241         AT91C_BASE_PIOB->PIO_OER = LED_MASK; // Configure in Output
242
243
244         /* Enable the peripheral clock. */
245     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
246     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
247         AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;
248 }
249 /*-----------------------------------------------------------*/
250
251 static void vErrorChecks( void *pvParameters )
252 {
253 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
254 TickType_t xLastWakeTime;
255
256         /* The parameters are not used. */
257         ( void ) pvParameters;
258
259         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
260         functions correctly. */
261         xLastWakeTime = xTaskGetTickCount();
262
263         /* Cycle for ever, delaying then checking all the other tasks are still
264         operating without error.  If an error is detected then the delay period
265         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
266         the Check LED flash rate will increase. */
267         for( ;; )
268         {
269                 /* Delay until it is time to execute again.  The delay period is
270                 shorter following an error. */
271                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
272
273                 /* Check all the standard demo application tasks are executing without
274                 error.  */
275                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
276                 {
277                         /* An error has been detected in one of the tasks - flash faster. */
278                         xDelayPeriod = mainERROR_FLASH_PERIOD;
279                 }
280
281                 vParTestToggleLED( mainCHECK_LED );
282         }
283 }
284 /*-----------------------------------------------------------*/
285
286 static long prvCheckOtherTasksAreStillRunning( void )
287 {
288 long lReturn = ( long ) pdPASS;
289
290         /* Check all the demo tasks (other than the flash tasks) to ensure
291         that they are all still running, and that none of them have detected
292         an error. */
293
294         if( xArePollingQueuesStillRunning() != pdTRUE )
295         {
296                 lReturn = ( long ) pdFAIL;
297         }
298
299         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
300         {
301                 lReturn = ( long ) pdFAIL;
302         }
303
304         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
305         {
306                 lReturn = ( long ) pdFAIL;
307         }
308
309         if( xAreBlockingQueuesStillRunning() != pdTRUE )
310         {
311                 lReturn = ( long ) pdFAIL;
312         }
313
314         return lReturn;
315 }
316 /*-----------------------------------------------------------*/
317
318 void vApplicationIdleHook( void )
319 {
320 static TickType_t xLastTx = 0;
321 char cTxByte;
322
323         /* The idle hook simply sends a string of characters to the USB port.
324         The characters will be buffered and sent once the port is connected. */
325         if( ( xTaskGetTickCount() - xLastTx ) > mainUSB_TX_FREQUENCY )
326         {
327                 xLastTx = xTaskGetTickCount();
328                 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )
329                 {
330                         vUSBSendByte( cTxByte );
331                 }
332         }
333 }
334
335