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