1 |
582 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/*
|
56 |
|
|
* Creates all the demo application tasks, then starts the scheduler. The WEB
|
57 |
|
|
* documentation provides more details of the standard demo application tasks
|
58 |
|
|
* (which just exist to test the kernel port and provide an example of how to use
|
59 |
|
|
* each FreeRTOS API function).
|
60 |
|
|
*
|
61 |
|
|
* In addition to the standard demo tasks, the following tasks and tests are
|
62 |
|
|
* defined and/or created within this file:
|
63 |
|
|
*
|
64 |
|
|
* "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
|
65 |
|
|
* is permitted to access the display directly. Other tasks wishing to write a
|
66 |
|
|
* message to the LCD send the message on a queue to the LCD task instead of
|
67 |
|
|
* accessing the LCD themselves. The LCD task just blocks on the queue waiting
|
68 |
|
|
* for messages - waking and displaying the messages as they arrive. The use
|
69 |
|
|
* of a gatekeeper in this manner permits both tasks and interrupts to write to
|
70 |
|
|
* the LCD without worrying about mutual exclusion. This is demonstrated by the
|
71 |
|
|
* check hook (see below) which sends messages to the display even though it
|
72 |
|
|
* executes from an interrupt context.
|
73 |
|
|
*
|
74 |
|
|
* "Check" hook - This only executes fully every five seconds from the tick
|
75 |
|
|
* hook. Its main function is to check that all the standard demo tasks are
|
76 |
|
|
* still operational. Should any unexpected behaviour be discovered within a
|
77 |
|
|
* demo task then the tick hook will write an error to the LCD (via the LCD task).
|
78 |
|
|
* If all the demo tasks are executing with their expected behaviour then the
|
79 |
|
|
* check task writes PASS to the LCD (again via the LCD task), as described above.
|
80 |
|
|
*
|
81 |
|
|
* LED tasks - These just demonstrate how multiple instances of a single task
|
82 |
|
|
* definition can be created. Each LED task simply toggles an LED. The task
|
83 |
|
|
* parameter is used to pass the number of the LED to be toggled into the task.
|
84 |
|
|
*
|
85 |
|
|
* "uIP" task - This is the task that handles the uIP stack. All TCP/IP
|
86 |
|
|
* processing is performed in this task.
|
87 |
|
|
*
|
88 |
|
|
* "Fast Interrupt Test" - A high frequency periodic interrupt is generated
|
89 |
|
|
* using a free running timer to demonstrate the use of the
|
90 |
|
|
* configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
|
91 |
|
|
* service routine measures the number of processor clocks that occur between
|
92 |
|
|
* each interrupt - and in so doing measures the jitter in the interrupt timing.
|
93 |
|
|
* The maximum measured jitter time is latched in the ulMaxJitter variable, and
|
94 |
|
|
* displayed on the OLED display by the 'OLED' task as described below. The
|
95 |
|
|
* fast interrupt is configured and handled in the timertest.c source file.
|
96 |
|
|
*
|
97 |
|
|
*/
|
98 |
|
|
|
99 |
|
|
/* Standard includes. */
|
100 |
|
|
#include <stdio.h>
|
101 |
|
|
|
102 |
|
|
/* Scheduler includes. */
|
103 |
|
|
#include "FreeRTOS.h"
|
104 |
|
|
#include "task.h"
|
105 |
|
|
#include "queue.h"
|
106 |
|
|
#include "semphr.h"
|
107 |
|
|
|
108 |
|
|
/* Library includes. */
|
109 |
|
|
#include "stm32f10x_it.h"
|
110 |
|
|
#include "stm32f10x_tim.h"
|
111 |
|
|
#include "STM3210D_lcd.h"
|
112 |
|
|
|
113 |
|
|
/* Demo app includes. */
|
114 |
|
|
#include "BlockQ.h"
|
115 |
|
|
#include "integer.h"
|
116 |
|
|
#include "flash.h"
|
117 |
|
|
#include "partest.h"
|
118 |
|
|
#include "semtest.h"
|
119 |
|
|
#include "PollQ.h"
|
120 |
|
|
#include "GenQTest.h"
|
121 |
|
|
#include "QPeek.h"
|
122 |
|
|
#include "recmutex.h"
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
/* The time between cycles of the 'check' functionality (defined within the
|
126 |
|
|
tick hook. */
|
127 |
|
|
#define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
|
128 |
|
|
|
129 |
|
|
/* Task priorities. */
|
130 |
|
|
#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
131 |
|
|
#define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
132 |
|
|
#define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
133 |
|
|
#define mainUIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
|
134 |
|
|
#define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
135 |
|
|
#define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
|
136 |
|
|
#define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
|
137 |
|
|
#define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
|
138 |
|
|
|
139 |
|
|
/* The WEB server has a larger stack as it utilises stack hungry string
|
140 |
|
|
handling library calls. */
|
141 |
|
|
#define mainBASIC_WEB_STACK_SIZE ( configMINIMAL_STACK_SIZE * 4 )
|
142 |
|
|
|
143 |
|
|
/* The length of the queue used to send messages to the LCD task. */
|
144 |
|
|
#define mainQUEUE_SIZE ( 3 )
|
145 |
|
|
|
146 |
|
|
/* The period of the system clock in nano seconds. This is used to calculate
|
147 |
|
|
the jitter time in nano seconds. */
|
148 |
|
|
#define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
|
149 |
|
|
|
150 |
|
|
/*-----------------------------------------------------------*/
|
151 |
|
|
|
152 |
|
|
/*
|
153 |
|
|
* Configure the hardware for the demo.
|
154 |
|
|
*/
|
155 |
|
|
static void prvSetupHardware( void );
|
156 |
|
|
|
157 |
|
|
/*
|
158 |
|
|
* Very simple task that toggles an LED.
|
159 |
|
|
*/
|
160 |
|
|
static void prvLCDTask( void *pvparameters );
|
161 |
|
|
|
162 |
|
|
/*
|
163 |
|
|
* The task that handles the uIP stack. All TCP/IP processing is performed in
|
164 |
|
|
* this task.
|
165 |
|
|
*/
|
166 |
|
|
extern void vuIP_Task( void *pvParameters );
|
167 |
|
|
|
168 |
|
|
/*
|
169 |
|
|
* The LCD gatekeeper task as described in the comments at the top of this file.
|
170 |
|
|
* */
|
171 |
|
|
static void prvLCDTask( void *pvParameters );
|
172 |
|
|
|
173 |
|
|
/*
|
174 |
|
|
* Configures the high frequency timers - those used to measure the timing
|
175 |
|
|
* jitter while the real time kernel is executing.
|
176 |
|
|
*/
|
177 |
|
|
extern void vSetupHighFrequencyTimer( void );
|
178 |
|
|
|
179 |
|
|
/*-----------------------------------------------------------*/
|
180 |
|
|
|
181 |
|
|
/* The queue used to send messages to the LCD task. */
|
182 |
|
|
xQueueHandle xLCDQueue;
|
183 |
|
|
|
184 |
|
|
/*-----------------------------------------------------------*/
|
185 |
|
|
|
186 |
|
|
int main( void )
|
187 |
|
|
{
|
188 |
|
|
#ifdef DEBUG
|
189 |
|
|
debug();
|
190 |
|
|
#endif
|
191 |
|
|
|
192 |
|
|
prvSetupHardware();
|
193 |
|
|
|
194 |
|
|
/* Start the standard demo tasks. These are just here to exercise the
|
195 |
|
|
kernel port and provide examples of how the FreeRTOS API can be used. */
|
196 |
|
|
vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
|
197 |
|
|
vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
|
198 |
|
|
vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
|
199 |
|
|
vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
|
200 |
|
|
vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
|
201 |
|
|
vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
|
202 |
|
|
vStartQueuePeekTasks();
|
203 |
|
|
vStartRecursiveMutexTasks();
|
204 |
|
|
|
205 |
|
|
/* Create the uIP task. The WEB server runs in this task. */
|
206 |
|
|
xTaskCreate( vuIP_Task, ( signed char * ) "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
|
207 |
|
|
|
208 |
|
|
/* Create the queue used by the LCD task. Messages for display on the LCD
|
209 |
|
|
are received via this queue. */
|
210 |
|
|
xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
|
211 |
|
|
|
212 |
|
|
/* Start the LCD gatekeeper task - as described in the comments at the top
|
213 |
|
|
of this file. */
|
214 |
|
|
xTaskCreate( prvLCDTask, ( signed char * ) "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );
|
215 |
|
|
|
216 |
|
|
/* Configure the high frequency interrupt used to measure the interrupt
|
217 |
|
|
jitter time. When debugging it can be helpful to comment this line out
|
218 |
|
|
to prevent the debugger repeatedly going into the interrupt service
|
219 |
|
|
routine. */
|
220 |
|
|
vSetupHighFrequencyTimer();
|
221 |
|
|
|
222 |
|
|
/* Start the scheduler. */
|
223 |
|
|
vTaskStartScheduler();
|
224 |
|
|
|
225 |
|
|
/* Will only get here if there was insufficient memory to create the idle
|
226 |
|
|
task. The idle task is created within vTaskStartScheduler(). */
|
227 |
|
|
for( ;; );
|
228 |
|
|
}
|
229 |
|
|
/*-----------------------------------------------------------*/
|
230 |
|
|
|
231 |
|
|
static void prvLCDTask( void *pvParameters )
|
232 |
|
|
{
|
233 |
|
|
unsigned char *pucMessage;
|
234 |
|
|
unsigned long ulLine = Line3;
|
235 |
|
|
const unsigned long ulLineHeight = 24;
|
236 |
|
|
static char cMsgBuf[ 30 ];
|
237 |
|
|
extern unsigned short usMaxJitter;
|
238 |
|
|
|
239 |
|
|
( void ) pvParameters;
|
240 |
|
|
|
241 |
|
|
/* The LCD gatekeeper task as described in the comments at the top of this
|
242 |
|
|
file. */
|
243 |
|
|
|
244 |
|
|
/* Initialise the LCD and display a startup message that includes the
|
245 |
|
|
configured IP address. */
|
246 |
|
|
STM3210D_LCD_Init();
|
247 |
|
|
LCD_Clear(White);
|
248 |
|
|
LCD_SetTextColor(Green);
|
249 |
|
|
LCD_DisplayStringLine( Line0, ( unsigned char * ) " www.FreeRTOS.org" );
|
250 |
|
|
LCD_SetTextColor(Blue);
|
251 |
|
|
sprintf( cMsgBuf, " %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
|
252 |
|
|
LCD_DisplayStringLine( Line1, ( unsigned char * ) cMsgBuf );
|
253 |
|
|
LCD_SetTextColor(Black);
|
254 |
|
|
|
255 |
|
|
for( ;; )
|
256 |
|
|
{
|
257 |
|
|
/* Wait for a message to arrive to be displayed. */
|
258 |
|
|
xQueueReceive( xLCDQueue, &pucMessage, portMAX_DELAY );
|
259 |
|
|
|
260 |
|
|
/* Clear the current line of text. */
|
261 |
|
|
LCD_ClearLine( ulLine );
|
262 |
|
|
|
263 |
|
|
/* Move on to the next line. */
|
264 |
|
|
ulLine += ulLineHeight;
|
265 |
|
|
if( ulLine > Line9 )
|
266 |
|
|
{
|
267 |
|
|
ulLine = Line3;
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
/* Display the received text, and the max jitter value. */
|
271 |
|
|
sprintf( cMsgBuf, "%s [%uns]", pucMessage, usMaxJitter * mainNS_PER_CLOCK );
|
272 |
|
|
LCD_DisplayStringLine( ulLine, ( unsigned char * ) cMsgBuf );
|
273 |
|
|
}
|
274 |
|
|
}
|
275 |
|
|
/*-----------------------------------------------------------*/
|
276 |
|
|
|
277 |
|
|
static void prvSetupHardware( void )
|
278 |
|
|
{
|
279 |
|
|
/* Start with the clocks in their expected state. */
|
280 |
|
|
RCC_DeInit();
|
281 |
|
|
|
282 |
|
|
/* Enable HSE (high speed external clock). */
|
283 |
|
|
RCC_HSEConfig( RCC_HSE_ON );
|
284 |
|
|
|
285 |
|
|
/* Wait till HSE is ready. */
|
286 |
|
|
while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
|
287 |
|
|
{
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
/* 2 wait states required on the flash. */
|
291 |
|
|
*( ( unsigned long * ) 0x40022000 ) = 0x02;
|
292 |
|
|
|
293 |
|
|
/* HCLK = SYSCLK */
|
294 |
|
|
RCC_HCLKConfig( RCC_SYSCLK_Div1 );
|
295 |
|
|
|
296 |
|
|
/* PCLK2 = HCLK */
|
297 |
|
|
RCC_PCLK2Config( RCC_HCLK_Div1 );
|
298 |
|
|
|
299 |
|
|
/* PCLK1 = HCLK/2 */
|
300 |
|
|
RCC_PCLK1Config( RCC_HCLK_Div2 );
|
301 |
|
|
|
302 |
|
|
/* PLLCLK = (25MHz / 2 ) * 5 = 62.5 MHz. */
|
303 |
|
|
RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_5 );
|
304 |
|
|
|
305 |
|
|
/* Enable PLL. */
|
306 |
|
|
RCC_PLLCmd( ENABLE );
|
307 |
|
|
|
308 |
|
|
/* Wait till PLL is ready. */
|
309 |
|
|
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
|
310 |
|
|
{
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
/* Select PLL as system clock source. */
|
314 |
|
|
RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
|
315 |
|
|
|
316 |
|
|
/* Wait till PLL is used as system clock source. */
|
317 |
|
|
while( RCC_GetSYSCLKSource() != 0x08 )
|
318 |
|
|
{
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
|
322 |
|
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
|
323 |
|
|
| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
|
324 |
|
|
|
325 |
|
|
/* Set the Vector Table base address at 0x08000000 */
|
326 |
|
|
NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
|
327 |
|
|
|
328 |
|
|
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
|
329 |
|
|
|
330 |
|
|
/* Configure HCLK clock as SysTick clock source. */
|
331 |
|
|
SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
|
332 |
|
|
|
333 |
|
|
/* Initialise the IO used for the LED outputs. */
|
334 |
|
|
vParTestInitialise();
|
335 |
|
|
}
|
336 |
|
|
/*-----------------------------------------------------------*/
|
337 |
|
|
|
338 |
|
|
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
|
339 |
|
|
{
|
340 |
|
|
/* This function will get called if a task overflows its stack. If the
|
341 |
|
|
parameters are corrupt then inspect pxCurrentTCB to find which was the
|
342 |
|
|
offending task. */
|
343 |
|
|
|
344 |
|
|
( void ) pxTask;
|
345 |
|
|
( void ) pcTaskName;
|
346 |
|
|
|
347 |
|
|
for( ;; );
|
348 |
|
|
}
|
349 |
|
|
/*-----------------------------------------------------------*/
|
350 |
|
|
|
351 |
|
|
void vApplicationTickHook( void )
|
352 |
|
|
{
|
353 |
|
|
char *pcMessage = "Status: PASS";
|
354 |
|
|
static unsigned long ulTicksSinceLastDisplay = 0;
|
355 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
356 |
|
|
|
357 |
|
|
/* Called from every tick interrupt as described in the comments at the top
|
358 |
|
|
of this file.
|
359 |
|
|
|
360 |
|
|
Have enough ticks passed to make it time to perform our health status
|
361 |
|
|
check again? */
|
362 |
|
|
ulTicksSinceLastDisplay++;
|
363 |
|
|
if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
|
364 |
|
|
{
|
365 |
|
|
/* Reset the counter so these checks run again in mainCHECK_DELAY
|
366 |
|
|
ticks time. */
|
367 |
|
|
ulTicksSinceLastDisplay = 0;
|
368 |
|
|
|
369 |
|
|
/* Has an error been found in any task? */
|
370 |
|
|
if( xAreGenericQueueTasksStillRunning() != pdTRUE )
|
371 |
|
|
{
|
372 |
|
|
pcMessage = "ERROR: GEN Q";
|
373 |
|
|
}
|
374 |
|
|
else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
|
375 |
|
|
{
|
376 |
|
|
pcMessage = "ERROR: PEEK Q";
|
377 |
|
|
}
|
378 |
|
|
else if( xAreBlockingQueuesStillRunning() != pdTRUE )
|
379 |
|
|
{
|
380 |
|
|
pcMessage = "ERROR: BLOCK Q";
|
381 |
|
|
}
|
382 |
|
|
else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
|
383 |
|
|
{
|
384 |
|
|
pcMessage = "ERROR: SEMAPHR";
|
385 |
|
|
}
|
386 |
|
|
else if( xArePollingQueuesStillRunning() != pdTRUE )
|
387 |
|
|
{
|
388 |
|
|
pcMessage = "ERROR: POLL Q";
|
389 |
|
|
}
|
390 |
|
|
else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
|
391 |
|
|
{
|
392 |
|
|
pcMessage = "ERROR: INT MATH";
|
393 |
|
|
}
|
394 |
|
|
else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
|
395 |
|
|
{
|
396 |
|
|
pcMessage = "ERROR: REC MUTEX";
|
397 |
|
|
}
|
398 |
|
|
|
399 |
|
|
/* Send the message to the OLED gatekeeper for display. The
|
400 |
|
|
xHigherPriorityTaskWoken parameter is not actually used here
|
401 |
|
|
as this function is running in the tick interrupt anyway - but
|
402 |
|
|
it must still be supplied. */
|
403 |
|
|
xHigherPriorityTaskWoken = pdFALSE;
|
404 |
|
|
xQueueSendFromISR( xLCDQueue, &pcMessage, &xHigherPriorityTaskWoken );
|
405 |
|
|
}
|
406 |
|
|
}
|
407 |
|
|
/*-----------------------------------------------------------*/
|