]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_Keil/main.c
Ensure LPC1768 demos are correct prior to V5.4.0 release.
[freertos] / Demo / CORTEX_STM32F103_Keil / main.c
1 /*\r
2         FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\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         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /*\r
49  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
50  * documentation provides more details of the standard demo application tasks.\r
51  * In addition to the standard demo tasks, the following tasks and tests are\r
52  * defined and/or created within this file:\r
53  *\r
54  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
55  * using a free running timer to demonstrate the use of the\r
56  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
57  * service routine measures the number of processor clocks that occur between\r
58  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
59  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
60  * displayed on the LCD by the 'Check' task as described below.  The\r
61  * fast interrupt is configured and handled in the timertest.c source file.\r
62  *\r
63  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
64  * is permitted to access the display directly.  Other tasks wishing to write a\r
65  * message to the LCD send the message on a queue to the LCD task instead of\r
66  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
67  * for messages - waking and displaying the messages as they arrive.\r
68  *\r
69  * "Check" task -  This only executes every five seconds but has the highest\r
70  * priority so is guaranteed to get processor time.  Its main function is to\r
71  * check that all the standard demo tasks are still operational.  Should any\r
72  * unexpected behaviour within a demo task be discovered the 'check' task will\r
73  * write an error to the LCD (via the LCD task).  If all the demo tasks are\r
74  * executing with their expected behaviour then the check task writes PASS\r
75  * along with the max jitter time to the LCD (again via the LCD task), as\r
76  * described above.\r
77  *\r
78  */\r
79 \r
80 /* Standard includes. */\r
81 #include <stdio.h>\r
82 \r
83 /* Scheduler includes. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 #include "queue.h"\r
87 \r
88 /* Library includes. */\r
89 #include "stm32f10x_it.h"\r
90 \r
91 /* Demo app includes. */\r
92 #include "lcd.h"\r
93 #include "LCD_Message.h"\r
94 #include "BlockQ.h"\r
95 #include "death.h"\r
96 #include "integer.h"\r
97 #include "blocktim.h"\r
98 #include "partest.h"\r
99 #include "semtest.h"\r
100 #include "PollQ.h"\r
101 #include "flash.h"\r
102 #include "comtest2.h"\r
103 \r
104 /* Task priorities. */\r
105 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
106 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
107 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
108 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
109 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
110 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
111 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
112 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
113 \r
114 /* Constants related to the LCD. */\r
115 #define mainMAX_LINE                                            ( 240 )\r
116 #define mainROW_INCREMENT                                       ( 24 )\r
117 #define mainMAX_COLUMN                                          ( 20 )\r
118 #define mainCOLUMN_START                                        ( 319 )\r
119 #define mainCOLUMN_INCREMENT                            ( 16 )\r
120 \r
121 /* The maximum number of message that can be waiting for display at any one\r
122 time. */\r
123 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
124 \r
125 /* The check task uses the sprintf function so requires a little more stack. */\r
126 #define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
127 \r
128 /* Dimensions the buffer into which the jitter time is written. */\r
129 #define mainMAX_MSG_LEN                                         25\r
130 \r
131 /* The time between cycles of the 'check' task. */\r
132 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
133 \r
134 /* The number of nano seconds between each processor clock. */\r
135 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
136 \r
137 /* Baud rate used by the comtest tasks. */\r
138 #define mainCOM_TEST_BAUD_RATE          ( 115200 )\r
139 \r
140 /* The LED used by the comtest tasks. See the comtest.c file for more\r
141 information. */\r
142 #define mainCOM_TEST_LED                        ( 3 )\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 /*\r
147  * Configure the clocks, GPIO and other peripherals as required by the demo.\r
148  */\r
149 static void prvSetupHardware( void );\r
150 \r
151 /*\r
152  * Configure the LCD as required by the demo.\r
153  */\r
154 static void prvConfigureLCD( void );\r
155 \r
156 /*\r
157  * The LCD is written two by more than one task so is controlled by a\r
158  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
159  * access the LCD directly.  Other tasks wanting to display a message send\r
160  * the message to the gatekeeper.\r
161  */\r
162 static void vLCDTask( void *pvParameters );\r
163 \r
164 /*\r
165  * Retargets the C library printf function to the USART.\r
166  */\r
167 int fputc( int ch, FILE *f );\r
168 \r
169 /*\r
170  * Checks the status of all the demo tasks then prints a message to the\r
171  * display.  The message will be either PASS - and include in brackets the\r
172  * maximum measured jitter time (as described at the to of the file), or a\r
173  * message that describes which of the standard demo tasks an error has been\r
174  * discovered in.\r
175  *\r
176  * Messages are not written directly to the terminal, but passed to vLCDTask\r
177  * via a queue.\r
178  */\r
179 static void vCheckTask( void *pvParameters );\r
180 \r
181 /*\r
182  * Configures the timers and interrupts for the fast interrupt test as\r
183  * described at the top of this file.\r
184  */\r
185 extern void vSetupTimerTest( void );\r
186 \r
187 /*-----------------------------------------------------------*/\r
188 \r
189 /* The queue used to send messages to the LCD task. */\r
190 xQueueHandle xLCDQueue;\r
191 \r
192 /*-----------------------------------------------------------*/\r
193 \r
194 int main( void )\r
195 {\r
196 #ifdef DEBUG\r
197   debug();\r
198 #endif\r
199 \r
200         prvSetupHardware();\r
201 \r
202         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
203         are received via this queue. */\r
204         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
205         \r
206         /* Start the standard demo tasks. */\r
207         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
208     vCreateBlockTimeTasks();\r
209     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
210     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
211     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
212         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
213         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
214 \r
215         /* Start the tasks defined within this file/specific to this demo. */\r
216     xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );   \r
217         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
218 \r
219         /* The suicide tasks must be created last as they need to know how many\r
220         tasks were running prior to their creation in order to ascertain whether\r
221         or not the correct/expected number of tasks are running at any given time. */\r
222     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
223         \r
224         /* Configure the timers used by the fast interrupt timer test. */\r
225         vSetupTimerTest();\r
226         \r
227         /* Start the scheduler. */\r
228         vTaskStartScheduler();\r
229         \r
230         /* Will only get here if there was not enough heap space to create the\r
231         idle task. */\r
232         return 0;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vLCDTask( void *pvParameters )\r
237 {\r
238 xLCDMessage xMessage;\r
239 \r
240         /* Initialise the LCD and display a startup message. */\r
241         prvConfigureLCD();\r
242         LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );\r
243 \r
244         for( ;; )\r
245         {\r
246                 /* Wait for a message to arrive that requires displaying. */\r
247                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
248 \r
249                 /* Display the message.  Print each message to a different position. */\r
250                 printf( ( portCHAR const * ) xMessage.pcMessage );\r
251         }\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 static void vCheckTask( void *pvParameters )\r
256 {\r
257 portTickType xLastExecutionTime;\r
258 xLCDMessage xMessage;\r
259 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];\r
260 extern unsigned portSHORT usMaxJitter;\r
261 \r
262         xLastExecutionTime = xTaskGetTickCount();\r
263         xMessage.pcMessage = cPassMessage;\r
264         \r
265     for( ;; )\r
266         {\r
267                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
268                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
269 \r
270                 /* Has an error been found in any task? */\r
271 \r
272         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
273                 {\r
274                         xMessage.pcMessage = "ERROR IN BLOCK Q\n";\r
275                 }\r
276                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
277                 {\r
278                         xMessage.pcMessage = "ERROR IN BLOCK TIME\n";\r
279                 }\r
280         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
281         {\r
282             xMessage.pcMessage = "ERROR IN SEMAPHORE\n";\r
283         }\r
284         else if( xArePollingQueuesStillRunning() != pdTRUE )\r
285         {\r
286             xMessage.pcMessage = "ERROR IN POLL Q\n";\r
287         }\r
288         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
289         {\r
290             xMessage.pcMessage = "ERROR IN CREATE\n";\r
291         }\r
292         else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
293         {\r
294             xMessage.pcMessage = "ERROR IN MATH\n";\r
295         }\r
296                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
297                 {\r
298                         xMessage.pcMessage = "ERROR IN COM TEST\n";\r
299                 }                               \r
300                 else\r
301                 {\r
302                         sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );\r
303                 }\r
304 \r
305                 /* Send the message to the LCD gatekeeper for display. */\r
306                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
307         }\r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r
311 static void prvSetupHardware( void )\r
312 {\r
313         /* Start with the clocks in their expected state. */\r
314         RCC_DeInit();\r
315 \r
316         /* Enable HSE (high speed external clock). */\r
317         RCC_HSEConfig( RCC_HSE_ON );\r
318 \r
319         /* Wait till HSE is ready. */\r
320         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
321         {\r
322         }\r
323 \r
324         /* 2 wait states required on the flash. */\r
325         *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;\r
326 \r
327         /* HCLK = SYSCLK */\r
328         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
329 \r
330         /* PCLK2 = HCLK */\r
331         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
332 \r
333         /* PCLK1 = HCLK/2 */\r
334         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
335 \r
336         /* PLLCLK = 8MHz * 9 = 72 MHz. */\r
337         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );\r
338 \r
339         /* Enable PLL. */\r
340         RCC_PLLCmd( ENABLE );\r
341 \r
342         /* Wait till PLL is ready. */\r
343         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
344         {\r
345         }\r
346 \r
347         /* Select PLL as system clock source. */\r
348         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
349 \r
350         /* Wait till PLL is used as system clock source. */\r
351         while( RCC_GetSYSCLKSource() != 0x08 )\r
352         {\r
353         }\r
354 \r
355         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
356         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
357                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
358 \r
359         /* SPI2 Periph clock enable */\r
360         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
361 \r
362 \r
363         /* Set the Vector Table base address at 0x08000000 */\r
364         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
365 \r
366         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
367         \r
368         /* Configure HCLK clock as SysTick clock source. */\r
369         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
370         \r
371         vParTestInitialise();\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 static void prvConfigureLCD( void )\r
376 {\r
377 GPIO_InitTypeDef GPIO_InitStructure;\r
378 \r
379         /* Configure LCD Back Light (PA8) as output push-pull */\r
380         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;\r
381         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\r
382         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;\r
383         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
384 \r
385         /* Set the Backlight Pin */\r
386         GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);\r
387 \r
388         /* Initialize the LCD */\r
389         LCD_Init();\r
390 \r
391         /* Set the Back Color */\r
392         LCD_SetBackColor( White );\r
393 \r
394         /* Set the Text Color */\r
395         LCD_SetTextColor( 0x051F );\r
396 \r
397         LCD_Clear();\r
398 }\r
399 /*-----------------------------------------------------------*/\r
400 \r
401 int fputc( int ch, FILE *f )\r
402 {\r
403 static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;\r
404 static unsigned portCHAR ucLine = 0;\r
405 \r
406         if( ( usColumn == 0 ) && ( ucLine == 0 ) )\r
407         {\r
408                 LCD_Clear();\r
409         }\r
410 \r
411         if( ch != '\n' )\r
412         {\r
413                 /* Display one character on LCD */\r
414                 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );\r
415                 \r
416                 /* Decrement the column position by 16 */\r
417                 usRefColumn -= mainCOLUMN_INCREMENT;\r
418                 \r
419                 /* Increment the character counter */\r
420                 usColumn++;\r
421                 if( usColumn == mainMAX_COLUMN )\r
422                 {\r
423                         ucLine += mainROW_INCREMENT;\r
424                         usRefColumn = mainCOLUMN_START;\r
425                         usColumn = 0;\r
426                 }\r
427         }\r
428         else\r
429         {\r
430                 /* Move back to the first column of the next line. */\r
431                 ucLine += mainROW_INCREMENT;\r
432                 usRefColumn = mainCOLUMN_START;\r
433                 usColumn = 0;   \r
434         }\r
435 \r
436         /* Wrap back to the top of the display. */\r
437         if( ucLine >= mainMAX_LINE )\r
438         {\r
439                 ucLine = 0;\r
440         }\r
441         \r
442         return ch;\r
443 }\r
444 /*-----------------------------------------------------------*/\r
445 \r
446 #ifdef  DEBUG\r
447 /* Keep the linker happy. */\r
448 void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )\r
449 {\r
450         for( ;; )\r
451         {\r
452         }\r
453 }\r
454 #endif\r