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