]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_Primer_GCC/main.c
Update to V5.1.2.
[freertos] / Demo / CORTEX_STM32F103_Primer_GCC / main.c
1 /*\r
2         FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
30         *                                                                         *\r
31         * This is a concise, step by step, 'hands on' guide that describes both   *\r
32         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
33         * explains numerous examples that are written using the FreeRTOS API.     *\r
34         * Full source code for all the examples is provided in an accompanying    *\r
35         * .zip file.                                                              *\r
36     *                                                                         *\r
37     ***************************************************************************\r
38     ***************************************************************************\r
39 \r
40         Please ensure to read the configuration and relevant port sections of the\r
41         online documentation.\r
42 \r
43         http://www.FreeRTOS.org - Documentation, latest information, license and \r
44         contact details.\r
45 \r
46         http://www.SafeRTOS.com - A version that is certified for use in safety \r
47         critical systems.\r
48 \r
49         http://www.OpenRTOS.com - Commercial support, development, porting, \r
50         licensing and training services.\r
51 */\r
52 \r
53 /*\r
54  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
55  * documentation provides more details of the standard demo application tasks.\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  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
60  * using a free running timer to demonstrate the use of the\r
61  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
62  * service routine measures the number of processor clocks that occur between\r
63  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
64  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
65  * displayed on the LCD by the 'Check' task as described below.  The\r
66  * fast interrupt is configured and handled in the timertest.c source file.\r
67  *\r
68  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
69  * is permitted to access the display directly.  Other tasks wishing to write a\r
70  * message to the LCD send the message on a queue to the LCD task instead of\r
71  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
72  * for messages - waking and displaying the messages as they arrive.  Messages\r
73  * can either be a text string to display, or an instruction to update MEMS\r
74  * input.  The MEMS input is used to display a ball that can be moved around\r
75  * LCD by tilting the STM32 Primer.  45% is taken as the neutral position.\r
76  *\r
77  * "Check" task -  This only executes every five seconds but has the highest\r
78  * priority so is guaranteed to get processor time.  Its main function is to\r
79  * check that all the standard demo tasks are still operational.  Should any\r
80  * unexpected behaviour within a demo task be discovered the 'check' task will\r
81  * write an error to the LCD (via the LCD task).  If all the demo tasks are\r
82  * executing with their expected behaviour then the check task writes PASS\r
83  * along with the max jitter time to the LCD (again via the LCD task), as\r
84  * described above.\r
85  *\r
86  * Tick Hook - A tick hook is provided just for demonstration purposes.  In \r
87  * this case it is used to periodically send an instruction to updated the\r
88  * MEMS input to the LCD task.\r
89  *\r
90  */\r
91 \r
92 /* CircleOS includes.  Some of the CircleOS peripheral functionality is \r
93 utilised, although CircleOS itself is not used. */\r
94 #include "circle.h"\r
95 \r
96 /* Standard includes. */\r
97 #include <string.h>\r
98 \r
99 /* Scheduler includes. */\r
100 #include "FreeRTOS.h"\r
101 #include "task.h"\r
102 #include "queue.h"\r
103 \r
104 /* Demo app includes. */\r
105 #include "BlockQ.h"\r
106 #include "blocktim.h"\r
107 #include "GenQTest.h"\r
108 #include "partest.h"\r
109 #include "QPeek.h"\r
110 \r
111 /* The bitmap used to display the FreeRTOS.org logo is stored in 16bit format\r
112 and therefore takes up a large proportion of the Flash space.  Setting this\r
113 parameter to 0 excludes the bitmap from the build, freeing up Flash space for\r
114 extra code. */\r
115 #define mainINCLUDE_BITMAP                                      1\r
116 \r
117 #if mainINCLUDE_BITMAP == 1\r
118         #include "bitmap.h"\r
119 #endif\r
120 \r
121 /* Task priorities. */\r
122 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
123 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
124 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
125 #define mainGEN_Q_PRIORITY                                      ( tskIDLE_PRIORITY + 0 )\r
126 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
127 \r
128 /* Splash screen related constants. */\r
129 #define mainBITMAP_Y                                            ( 38 )\r
130 #define mainBITMAP_X                                            ( 18 )\r
131 #define mainURL_Y                                                       ( 8 )\r
132 #define mainURL_X                                                       ( 78 )\r
133 #define mainSPLASH_SCREEN_DELAY         ( 2000 / portTICK_RATE_MS )\r
134 \r
135 /* Text drawing related constants. */\r
136 #define mainLCD_CHAR_HEIGHT                     ( 13 )\r
137 #define mainLCD_MAX_Y                           ( 110 )\r
138 \r
139 /* The maximum number of message that can be waiting for display at any one\r
140 time. */\r
141 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
142 \r
143 /* The check task uses the sprintf function so requires a little more stack. */\r
144 #define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
145 \r
146 /* The LCD task calls some of the CircleOS functions (for MEMS and LCD access),\r
147 these can require a larger stack. */\r
148 #define configLCD_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
149 \r
150 /* Dimensions the buffer into which the jitter time is written. */\r
151 #define mainMAX_MSG_LEN                                         25\r
152 \r
153 /* The time between cycles of the 'check' task. */\r
154 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
155 \r
156 /* The period at which the MEMS input should be updated. */\r
157 #define mainMEMS_DELAY                                          ( ( portTickType ) 100 / portTICK_RATE_MS )\r
158 \r
159 /* The rate at which the flash task toggles the LED. */\r
160 #define mainFLASH_DELAY                                         ( ( portTickType ) 1000 / portTICK_RATE_MS )\r
161 \r
162 /* The number of nano seconds between each processor clock. */\r
163 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
164 \r
165 /* The two types of message that can be sent to the LCD task. */\r
166 #define mainUPDATE_BALL_MESSAGE                         ( 0 )\r
167 #define mainWRITE_STRING_MESSAGE                        ( 1 )\r
168 \r
169 /* Type of the message sent to the LCD task. */\r
170 typedef struct\r
171 {\r
172         portBASE_TYPE xMessageType;\r
173         signed char *pcMessage;\r
174 } xLCDMessage;\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /*\r
179  * Configure the clocks, GPIO and other peripherals as required by the demo.\r
180  */\r
181 static void prvSetupHardware( void );\r
182 \r
183 /*\r
184  * The LCD is written two by more than one task so is controlled by a\r
185  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
186  * access the LCD directly.  Other tasks wanting to display a message send\r
187  * the message to the gatekeeper.\r
188  */\r
189 static void prvLCDTask( void *pvParameters );\r
190 \r
191 /*\r
192  * Checks the status of all the demo tasks then prints a message to the\r
193  * display.  The message will be either PASS - and include in brackets the\r
194  * maximum measured jitter time (as described at the to of the file), or a\r
195  * message that describes which of the standard demo tasks an error has been\r
196  * discovered in.\r
197  *\r
198  * Messages are not written directly to the terminal, but passed to prvLCDTask\r
199  * via a queue.\r
200  *\r
201  * The check task also receives instructions to update the MEMS input, which\r
202  * in turn can also lead to the LCD being updated.\r
203  */\r
204 static void prvCheckTask( void *pvParameters );\r
205 \r
206 /*\r
207  * Configures the timers and interrupts for the fast interrupt test as\r
208  * described at the top of this file.\r
209  */\r
210 extern void vSetupTimerTest( void );\r
211 \r
212 /*\r
213  * A cut down version of sprintf() used to percent the HUGE GCC library\r
214  * equivalent from being included in the binary image. \r
215  */\r
216 extern int sprintf(char *out, const char *format, ...);\r
217 \r
218 /*\r
219  * Simple toggle the LED periodically for timing verification.\r
220  */\r
221 static void prvFlashTask( void *pvParameters );\r
222 \r
223 /*-----------------------------------------------------------*/\r
224 \r
225 /* The queue used to send messages to the LCD task. */\r
226 xQueueHandle xLCDQueue;\r
227 \r
228 /*-----------------------------------------------------------*/\r
229 \r
230 int main( void )\r
231 {\r
232         #ifdef DEBUG\r
233                 debug();\r
234         #endif\r
235 \r
236         prvSetupHardware();\r
237 \r
238         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
239         are received via this queue. */\r
240         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
241         \r
242         /* Start the standard demo tasks. */\r
243         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
244     vCreateBlockTimeTasks();\r
245         vStartGenericQueueTasks( mainGEN_Q_PRIORITY );\r
246         vStartQueuePeekTasks();\r
247         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
248 \r
249         /* Start the tasks defined within this file/specific to this demo. */\r
250     xTaskCreate( prvCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL ); \r
251         xTaskCreate( prvLCDTask, ( signed portCHAR * ) "LCD", configLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
252         xTaskCreate( prvFlashTask, ( signed portCHAR * ) "Flash", configMINIMAL_STACK_SIZE, NULL, mainFLASH_TASK_PRIORITY, NULL );\r
253 \r
254         /* Configure the timers used by the fast interrupt timer test. */\r
255         vSetupTimerTest();\r
256         \r
257         /* Start the scheduler. */\r
258         vTaskStartScheduler();\r
259         \r
260         /* Will only get here if there was not enough heap space to create the\r
261         idle task. */\r
262         return 0;\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 void prvLCDTask( void *pvParameters )\r
267 {\r
268 xLCDMessage xMessage;\r
269 portCHAR cY = mainLCD_CHAR_HEIGHT;\r
270 const portCHAR * const pcString = "www.FreeRTOS.org";\r
271 const portCHAR * const pcBlankLine = "                  ";\r
272 \r
273         DRAW_Init();\r
274 \r
275         #if mainINCLUDE_BITMAP == 1\r
276                 DRAW_SetImage( pucImage, mainBITMAP_Y, mainBITMAP_X, bmpBITMAP_HEIGHT, bmpBITMAP_WIDTH );\r
277         #endif\r
278 \r
279         LCD_SetScreenOrientation( V9 );\r
280         DRAW_DisplayString( mainURL_Y, mainURL_X, pcString, strlen( pcString ) );\r
281         vTaskDelay( mainSPLASH_SCREEN_DELAY );\r
282         LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, RGB_WHITE );\r
283 \r
284         for( ;; )\r
285         {\r
286                 /* Wait for a message to arrive that requires displaying. */\r
287                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
288 \r
289                 /* Check the message type. */\r
290                 if( xMessage.xMessageType == mainUPDATE_BALL_MESSAGE )\r
291                 {\r
292                         /* Read the MEMS and update the ball display on the LCD if required. */\r
293                         MEMS_Handler();\r
294                         POINTER_Handler();\r
295                 }\r
296                 else\r
297                 {\r
298                         /* A text string was sent.  First blank off the old text string, then\r
299                         draw the new text on the next line down. */\r
300                         DRAW_DisplayString( 0, cY, pcBlankLine, strlen( pcBlankLine ) );\r
301 \r
302                         cY -= mainLCD_CHAR_HEIGHT;\r
303                         if( cY <= ( mainLCD_CHAR_HEIGHT - 1 ) )\r
304                         {                       \r
305                                 /* Wrap the line onto which we are going to write the text. */\r
306                                 cY = mainLCD_MAX_Y;\r
307                         }\r
308                         \r
309                         /* Display the message. */\r
310                         DRAW_DisplayString( 0, cY, xMessage.pcMessage, strlen( xMessage.pcMessage ) );\r
311                 }\r
312         }\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 static void prvCheckTask( void *pvParameters )\r
317 {\r
318 portTickType xLastExecutionTime;\r
319 xLCDMessage xMessage;\r
320 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];\r
321 extern unsigned portSHORT usMaxJitter;\r
322 \r
323         /* Initialise the xLastExecutionTime variable on task entry. */\r
324         xLastExecutionTime = xTaskGetTickCount();\r
325 \r
326         /* Setup the message we are going to send to the LCD task. */\r
327         xMessage.xMessageType = mainWRITE_STRING_MESSAGE;\r
328         xMessage.pcMessage = cPassMessage;\r
329         \r
330     for( ;; )\r
331         {\r
332                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
333                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
334 \r
335                 /* Has an error been found in any task?   If so then point the text\r
336                 we are going to send to the LCD task to an error message instead of\r
337                 the PASS message. */\r
338                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
339                 {\r
340                         xMessage.pcMessage = "ERROR IN GEN Q";\r
341                 }\r
342         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
343                 {\r
344                         xMessage.pcMessage = "ERROR IN BLOCK Q";\r
345                 }\r
346                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
347                 {\r
348                         xMessage.pcMessage = "ERROR IN BLOCK TIME";\r
349                 }\r
350         else if( xArePollingQueuesStillRunning() != pdTRUE )\r
351         {\r
352             xMessage.pcMessage = "ERROR IN POLL Q";\r
353         }\r
354                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
355                 {\r
356                         xMessage.pcMessage = "ERROR IN PEEK Q";\r
357                 }\r
358                 else\r
359                 {\r
360                         /* No errors were found in any task, so send a pass message\r
361                         with the max measured jitter time also included (as per the\r
362                         fast interrupt test described at the top of this file and on\r
363                         the online documentation page for this demo application). */\r
364                         sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );\r
365                 }\r
366 \r
367                 /* Send the message to the LCD gatekeeper for display. */\r
368                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
369         }\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 void vApplicationTickHook( void )\r
374 {\r
375 static unsigned portLONG ulCallCount;\r
376 static const xLCDMessage xMemsMessage = { mainUPDATE_BALL_MESSAGE, NULL };\r
377 static portBASE_TYPE xHigherPriorityTaskWoken;\r
378 \r
379         /* Periodically send a message to the LCD task telling it to update\r
380         the MEMS input, and then if necessary the LCD. */\r
381         ulCallCount++;\r
382         if( ulCallCount >= mainMEMS_DELAY )\r
383         {\r
384                 ulCallCount = 0;\r
385                 xHigherPriorityTaskWoken = pdFALSE;\r
386                 xQueueSendFromISR( xLCDQueue, &xMemsMessage, &xHigherPriorityTaskWoken );\r
387         }\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 static void prvSetupHardware( void )\r
392 {\r
393         /* Start with the clocks in their expected state. */\r
394         RCC_DeInit();\r
395 \r
396         /* Enable HSE (high speed external clock). */\r
397         RCC_HSEConfig( RCC_HSE_ON );\r
398 \r
399         /* Wait till HSE is ready. */\r
400         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
401         {\r
402         }\r
403 \r
404         /* 2 wait states required on the flash. */\r
405         *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;\r
406 \r
407         /* HCLK = SYSCLK */\r
408         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
409 \r
410         /* PCLK2 = HCLK */\r
411         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
412 \r
413         /* PCLK1 = HCLK/2 */\r
414         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
415 \r
416         /* PLLCLK = 12MHz * 6 = 72 MHz. */\r
417         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );\r
418 \r
419         /* Enable PLL. */\r
420         RCC_PLLCmd( ENABLE );\r
421 \r
422         /* Wait till PLL is ready. */\r
423         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
424         {\r
425         }\r
426 \r
427         /* Select PLL as system clock source. */\r
428         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
429 \r
430         /* Wait till PLL is used as system clock source. */\r
431         while( RCC_GetSYSCLKSource() != 0x08 )\r
432         {\r
433         }\r
434 \r
435         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
436         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
437                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
438 \r
439         /* SPI2 Periph clock enable */\r
440         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
441 \r
442 \r
443         /* Set the Vector Table base address at 0x08000000 */\r
444         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
445 \r
446         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
447         \r
448         /* Configure HCLK clock as SysTick clock source. */\r
449         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
450         \r
451         /* Misc initialisation, including some of the CircleOS features.  Note\r
452         that CircleOS itself is not used. */\r
453         vParTestInitialise();\r
454         MEMS_Init();\r
455         POINTER_Init();\r
456         POINTER_SetMode( POINTER_RESTORE_LESS );\r
457 }\r
458 /*-----------------------------------------------------------*/\r
459 \r
460 static void prvFlashTask( void *pvParameters )\r
461 {\r
462 portTickType xLastExecutionTime;\r
463 \r
464         /* Initialise the xLastExecutionTime variable on task entry. */\r
465         xLastExecutionTime = xTaskGetTickCount();\r
466 \r
467     for( ;; )\r
468         {\r
469                 /* Simple toggle the LED periodically.  This just provides some timing\r
470                 verification. */\r
471                 vTaskDelayUntil( &xLastExecutionTime, mainFLASH_DELAY );\r
472                 vParTestToggleLED( 0 );\r
473         }\r
474 }\r
475 /*-----------------------------------------------------------*/\r
476 \r
477 void starting_delay( unsigned long ul )\r
478 {\r
479         vTaskDelay( ( portTickType ) ul );\r
480 }\r
481 \r
482 \r
483 \r