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