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