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