2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
31 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
32 called. The demo applications included in the FreeRTOS.org download switch
\r
33 to supervisor mode prior to main being called. If you are not using one of
\r
34 these demo application projects then ensure Supervisor mode is used.
\r
39 * Program entry point.
\r
41 * main() is responsible for setting up the microcontroller peripherals, then
\r
42 * starting the demo application tasks. Once the tasks have been created the
\r
43 * scheduler is started and main() should never complete.
\r
45 * The demo creates the three standard 'flash' tasks to provide some visual
\r
46 * feedback that the system and scheduler are still operating correctly.
\r
48 * The HTTP server task operates at the highest priority so will always preempt
\r
49 * the flash or idle task on TCP/IP events.
\r
52 /* Standard includes. */
\r
55 /* Scheduler include files. */
\r
56 #include "FreeRTOS.h"
\r
60 /* Application includes. */
\r
62 #include "HTTP_Serv.h"
\r
64 #include "partest.h"
\r
65 #include "dynamic.h"
\r
66 #include "semtest.h"
\r
69 #include "integer.h"
\r
71 /*-----------------------------------------------------------*/
\r
73 /* Constants to setup the PLL. */
\r
74 #define mainPLL_MUL_4 ( ( unsigned char ) 0x0003 )
\r
75 #define mainPLL_DIV_1 ( ( unsigned char ) 0x0000 )
\r
76 #define mainPLL_ENABLE ( ( unsigned char ) 0x0001 )
\r
77 #define mainPLL_CONNECT ( ( unsigned char ) 0x0003 )
\r
78 #define mainPLL_FEED_BYTE1 ( ( unsigned char ) 0xaa )
\r
79 #define mainPLL_FEED_BYTE2 ( ( unsigned char ) 0x55 )
\r
80 #define mainPLL_LOCK ( ( unsigned long ) 0x0400 )
\r
82 /* Constants to setup the MAM. */
\r
83 #define mainMAM_TIM_3 ( ( unsigned char ) 0x03 )
\r
84 #define mainMAM_MODE_FULL ( ( unsigned char ) 0x02 )
\r
86 /* Constants to setup the peripheral bus. */
\r
87 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
89 /* Constants to setup I/O and processor. */
\r
90 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
91 #define mainLED_TO_OUTPUT ( ( unsigned long ) 0xff0000 )
\r
92 #define mainJTAG_PORT ( ( unsigned long ) 0x3E0000UL )
\r
94 /* Priorities for the demo application tasks. */
\r
95 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
96 #define mainHTTP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
97 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
98 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
99 #define mainERROR_CHECK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
101 /* Flash rates of the on board LED to indicate the health of the system. */
\r
102 #define mainNO_ERROR_DELAY ( 3000 )
\r
103 #define mainERROR_DELAY ( 500 )
\r
104 #define mainON_BOARD_LED_BIT ( ( unsigned long ) 0x80 )
\r
106 /*-----------------------------------------------------------*/
\r
109 * The Olimex demo board has a single built in LED. This function simply
\r
110 * toggles its state.
\r
112 void prvToggleOnBoardLED( void );
\r
115 * Configure the processor for use with the Olimex demo board.
\r
117 static void prvSetupHardware( void );
\r
120 * Simply check for errors and toggle the onboard LED.
\r
122 static void prvErrorChecks( void *pvParameters );
\r
125 * Return true if the demo tasks are executing without error - otherwise
\r
128 static void prvMainCheckOtherTasksAreStillRunning( void );
\r
129 /*-----------------------------------------------------------*/
\r
131 /* Flag set by prvMainCheckOtherTasksAreStillExecuting(). */
\r
132 long lErrorInTask = pdFALSE;
\r
135 * Application entry point:
\r
136 * Starts all the other tasks, then starts the scheduler.
\r
140 /* Setup the hardware for use with the Olimex demo board. */
\r
141 prvSetupHardware();
\r
143 /* Start the standard flash tasks so the WEB server is not the only thing
\r
145 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
146 vStartSemaphoreTasks( tskIDLE_PRIORITY );
\r
147 vStartDynamicPriorityTasks();
\r
148 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
149 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
150 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
152 /* Start the WEB server task and the error check task. */
\r
153 xTaskCreate( vHTTPServerTask, "HTTP", configMINIMAL_STACK_SIZE, NULL, mainHTTP_TASK_PRIORITY, NULL );
\r
154 xTaskCreate( prvErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainERROR_CHECK_PRIORITY, NULL );
\r
156 /* Now all the tasks have been started - start the scheduler.
\r
158 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
159 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
160 called. The demo applications included in the FreeRTOS.org download switch
\r
161 to supervisor mode prior to main being called. If you are not using one of
\r
162 these demo application projects then ensure Supervisor mode is used. */
\r
163 vTaskStartScheduler();
\r
165 /* Should never reach here! */
\r
168 /*-----------------------------------------------------------*/
\r
170 static void prvSetupHardware( void )
\r
172 #ifdef RUN_FROM_RAM
\r
173 /* Remap the interrupt vectors to RAM if we are are running from RAM. */
\r
177 /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.
\r
178 The JTAG pins are left as input as I'm not sure what will happen if the
\r
179 Wiggler is connected after powerup - not that it would be a good idea to
\r
181 GPIO_IODIR = ~( mainJTAG_PORT );
\r
183 /* Setup the PLL to multiply the XTAL input by 4. */
\r
184 SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );
\r
186 /* Activate the PLL by turning it on then feeding the correct sequence of
\r
188 SCB_PLLCON = mainPLL_ENABLE;
\r
189 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
190 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
192 /* Wait for the PLL to lock... */
\r
193 while( !( SCB_PLLSTAT & mainPLL_LOCK ) );
\r
195 /* ...before connecting it using the feed sequence again. */
\r
196 SCB_PLLCON = mainPLL_CONNECT;
\r
197 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
198 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
200 /* Setup and turn on the MAM. Three cycle access is used due to the fast
\r
201 PLL used. It is possible faster overall performance could be obtained by
\r
202 tuning the MAM and PLL settings. */
\r
203 MAM_TIM = mainMAM_TIM_3;
\r
204 MAM_CR = mainMAM_MODE_FULL;
\r
206 /* Setup the peripheral bus to be the same as the PLL output. */
\r
207 SCB_VPBDIV = mainBUS_CLK_FULL;
\r
209 /* Initialise the i2c peripheral. */
\r
212 /* Initialise the LED's used by the flash tasks. */
\r
213 vParTestInitialise();
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvMainCheckOtherTasksAreStillRunning( void )
\r
219 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
220 that they are all still running, and that none of them have detected
\r
223 /* This function is called from more than one task. */
\r
224 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
226 lErrorInTask = pdTRUE;
\r
229 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
231 lErrorInTask = pdTRUE;
\r
234 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
236 lErrorInTask = pdTRUE;
\r
239 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
241 lErrorInTask = pdTRUE;
\r
244 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
246 lErrorInTask = pdTRUE;
\r
249 /*-----------------------------------------------------------*/
\r
251 void prvToggleOnBoardLED( void )
\r
253 unsigned long ulState;
\r
255 ulState = GPIO0_IOPIN;
\r
256 if( ulState & mainON_BOARD_LED_BIT )
\r
258 GPIO_IOCLR = mainON_BOARD_LED_BIT;
\r
262 GPIO_IOSET = mainON_BOARD_LED_BIT;
\r
265 /*-----------------------------------------------------------*/
\r
267 static void prvErrorChecks( void *pvParameters )
\r
269 TickType_t xDelay = mainNO_ERROR_DELAY;
\r
271 /* The parameters are not used. */
\r
272 ( void ) pvParameters;
\r
276 /* How long we delay depends on whether an error has been detected
\r
277 or not. Therefore the flash rate of the on board LED indicates
\r
278 whether or not an error has occurred. */
\r
279 vTaskDelay( xDelay );
\r
281 /* Update the lErrorInTask flag. */
\r
282 prvMainCheckOtherTasksAreStillRunning();
\r
286 /* An error has been found so reduce the delay period and in so
\r
287 doing speed up the flash rate of the on board LED. */
\r
288 xDelay = mainERROR_DELAY;
\r
291 prvToggleOnBoardLED();
\r