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