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