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