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