]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F103_IAR/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_STM32F103_IAR / 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.\r
48  *\r
49  * "Check" task -  This only executes every five seconds but has the highest\r
50  * priority so is guaranteed to get processor time.  Its main function is to\r
51  * check that all the standard demo tasks are still operational.  Should any\r
52  * unexpected behaviour within a demo task be discovered the 'check' task will\r
53  * write an error to the LCD (via the LCD task).  If all the demo tasks are\r
54  * executing with their expected behaviour then the check task writes PASS\r
55  * along with the max jitter time to the LCD (again via the LCD task), as\r
56  * described above.\r
57  *\r
58  */\r
59 \r
60 /* Standard includes. */\r
61 #include <stdio.h>\r
62 \r
63 /* Scheduler includes. */\r
64 #include "FreeRTOS.h"\r
65 #include "task.h"\r
66 #include "queue.h"\r
67 \r
68 /* Library includes. */\r
69 #include "stm32f10x_it.h"\r
70 \r
71 /* Demo app includes. */\r
72 #include "lcd.h"\r
73 #include "LCD_Message.h"\r
74 #include "BlockQ.h"\r
75 #include "death.h"\r
76 #include "integer.h"\r
77 #include "blocktim.h"\r
78 #include "partest.h"\r
79 #include "semtest.h"\r
80 #include "PollQ.h"\r
81 #include "flash.h"\r
82 #include "comtest2.h"\r
83 \r
84 /* Task priorities. */\r
85 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
86 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
87 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
88 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
89 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
90 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
91 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
92 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
93 \r
94 /* Constants related to the LCD. */\r
95 #define mainMAX_LINE                                            ( 240 )\r
96 #define mainROW_INCREMENT                                       ( 24 )\r
97 #define mainMAX_COLUMN                                          ( 20 )\r
98 #define mainCOLUMN_START                                        ( 319 )\r
99 #define mainCOLUMN_INCREMENT                            ( 16 )\r
100 \r
101 /* The maximum number of message that can be waiting for display at any one\r
102 time. */\r
103 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
104 \r
105 /* The check task uses the sprintf function so requires a little more stack. */\r
106 #define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
107 \r
108 /* Dimensions the buffer into which the jitter time is written. */\r
109 #define mainMAX_MSG_LEN                                         25\r
110 \r
111 /* The time between cycles of the 'check' task. */\r
112 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
113 \r
114 /* The number of nano seconds between each processor clock. */\r
115 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
116 \r
117 /* Baud rate used by the comtest tasks. */\r
118 #define mainCOM_TEST_BAUD_RATE          ( 115200 )\r
119 \r
120 /* The LED used by the comtest tasks. See the comtest.c file for more\r
121 information. */\r
122 #define mainCOM_TEST_LED                        ( 3 )\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * Configure the clocks, GPIO and other peripherals as required by the demo.\r
128  */\r
129 static void prvSetupHardware( void );\r
130 \r
131 /*\r
132  * Configure the LCD as required by the demo.\r
133  */\r
134 static void prvConfigureLCD( void );\r
135 \r
136 /*\r
137  * The LCD is written two by more than one task so is controlled by a\r
138  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
139  * access the LCD directly.  Other tasks wanting to display a message send\r
140  * the message to the gatekeeper.\r
141  */\r
142 static void vLCDTask( void *pvParameters );\r
143 \r
144 /*\r
145  * Retargets the C library printf function to the USART.\r
146  */\r
147 int fputc( int ch, FILE *f );\r
148 \r
149 /*\r
150  * Checks the status of all the demo tasks then prints a message to the\r
151  * display.  The message will be either PASS - and include in brackets the\r
152  * maximum measured jitter time (as described at the to of the file), or a\r
153  * message that describes which of the standard demo tasks an error has been\r
154  * discovered in.\r
155  *\r
156  * Messages are not written directly to the terminal, but passed to vLCDTask\r
157  * via a queue.\r
158  */\r
159 static void vCheckTask( void *pvParameters );\r
160 \r
161 /*\r
162  * Configures the timers and interrupts for the fast interrupt test as\r
163  * described at the top of this file.\r
164  */\r
165 extern void vSetupTimerTest( void );\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /* The queue used to send messages to the LCD task. */\r
170 QueueHandle_t xLCDQueue;\r
171 \r
172 /*-----------------------------------------------------------*/\r
173 \r
174 int main( void )\r
175 {\r
176 #ifdef DEBUG\r
177   debug();\r
178 #endif\r
179 \r
180         prvSetupHardware();\r
181 \r
182         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
183         are received via this queue. */\r
184         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
185 \r
186         /* Start the standard demo tasks. */\r
187         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
188     vCreateBlockTimeTasks();\r
189     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
190     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
191     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
192         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
193         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
194 \r
195         /* Start the tasks defined within this file/specific to this demo. */\r
196     xTaskCreate( vCheckTask, "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
197         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
198 \r
199         /* The suicide tasks must be created last as they need to know how many\r
200         tasks were running prior to their creation in order to ascertain whether\r
201         or not the correct/expected number of tasks are running at any given time. */\r
202     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
203 \r
204         /* Configure the timers used by the fast interrupt timer test. */\r
205         vSetupTimerTest();\r
206 \r
207         /* Start the scheduler. */\r
208         vTaskStartScheduler();\r
209 \r
210         /* Will only get here if there was not enough heap space to create the\r
211         idle task. */\r
212         return 0;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 void vLCDTask( void *pvParameters )\r
217 {\r
218 xLCDMessage xMessage;\r
219 \r
220         /* Initialise the LCD and display a startup message. */\r
221         prvConfigureLCD();\r
222         LCD_DrawMonoPict( ( unsigned long * ) pcBitmap );\r
223 \r
224         for( ;; )\r
225         {\r
226                 /* Wait for a message to arrive that requires displaying. */\r
227                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
228 \r
229                 /* Display the message.  Print each message to a different position. */\r
230                 printf( ( char const * ) xMessage.pcMessage );\r
231         }\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void vCheckTask( void *pvParameters )\r
236 {\r
237 TickType_t xLastExecutionTime;\r
238 xLCDMessage xMessage;\r
239 static signed char cPassMessage[ mainMAX_MSG_LEN ];\r
240 extern unsigned short usMaxJitter;\r
241 \r
242         xLastExecutionTime = xTaskGetTickCount();\r
243         xMessage.pcMessage = cPassMessage;\r
244 \r
245     for( ;; )\r
246         {\r
247                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
248                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
249 \r
250                 /* Has an error been found in any task? */\r
251 \r
252         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
253                 {\r
254                         xMessage.pcMessage = "ERROR IN BLOCK Q\n";\r
255                 }\r
256                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
257                 {\r
258                         xMessage.pcMessage = "ERROR IN BLOCK TIME\n";\r
259                 }\r
260         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
261         {\r
262             xMessage.pcMessage = "ERROR IN SEMAPHORE\n";\r
263         }\r
264         else if( xArePollingQueuesStillRunning() != pdTRUE )\r
265         {\r
266             xMessage.pcMessage = "ERROR IN POLL Q\n";\r
267         }\r
268         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
269         {\r
270             xMessage.pcMessage = "ERROR IN CREATE\n";\r
271         }\r
272         else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
273         {\r
274             xMessage.pcMessage = "ERROR IN MATH\n";\r
275         }\r
276                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
277                 {\r
278                         xMessage.pcMessage = "ERROR IN COM TEST\n";\r
279                 }\r
280                 else\r
281                 {\r
282                         sprintf( ( char * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK );\r
283                 }\r
284 \r
285                 /* Send the message to the LCD gatekeeper for display. */\r
286                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
287         }\r
288 }\r
289 /*-----------------------------------------------------------*/\r
290 \r
291 static void prvSetupHardware( void )\r
292 {\r
293         /* Start with the clocks in their expected state. */\r
294         RCC_DeInit();\r
295 \r
296         /* Enable HSE (high speed external clock). */\r
297         RCC_HSEConfig( RCC_HSE_ON );\r
298 \r
299         /* Wait till HSE is ready. */\r
300         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
301         {\r
302         }\r
303 \r
304         /* 2 wait states required on the flash. */\r
305         *( ( unsigned long * ) 0x40022000 ) = 0x02;\r
306 \r
307         /* HCLK = SYSCLK */\r
308         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
309 \r
310         /* PCLK2 = HCLK */\r
311         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
312 \r
313         /* PCLK1 = HCLK/2 */\r
314         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
315 \r
316         /* PLLCLK = 8MHz * 9 = 72 MHz. */\r
317         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );\r
318 \r
319         /* Enable PLL. */\r
320         RCC_PLLCmd( ENABLE );\r
321 \r
322         /* Wait till PLL is ready. */\r
323         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
324         {\r
325         }\r
326 \r
327         /* Select PLL as system clock source. */\r
328         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
329 \r
330         /* Wait till PLL is used as system clock source. */\r
331         while( RCC_GetSYSCLKSource() != 0x08 )\r
332         {\r
333         }\r
334 \r
335         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
336         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
337                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
338 \r
339         /* SPI2 Periph clock enable */\r
340         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
341 \r
342 \r
343         /* Set the Vector Table base address at 0x08000000 */\r
344         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
345 \r
346         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
347 \r
348         /* Configure HCLK clock as SysTick clock source. */\r
349         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
350 \r
351         vParTestInitialise();\r
352 }\r
353 /*-----------------------------------------------------------*/\r
354 \r
355 static void prvConfigureLCD( void )\r
356 {\r
357 GPIO_InitTypeDef GPIO_InitStructure;\r
358 \r
359         /* Configure LCD Back Light (PA8) as output push-pull */\r
360         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;\r
361         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\r
362         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;\r
363         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
364 \r
365         /* Set the Backlight Pin */\r
366         GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);\r
367 \r
368         /* Initialize the LCD */\r
369         LCD_Init();\r
370 \r
371         /* Set the Back Color */\r
372         LCD_SetBackColor( White );\r
373 \r
374         /* Set the Text Color */\r
375         LCD_SetTextColor( 0x051F );\r
376 \r
377         LCD_Clear();\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r
381 int fputc( int ch, FILE *f )\r
382 {\r
383 static unsigned short usColumn = 0, usRefColumn = mainCOLUMN_START;\r
384 static unsigned char ucLine = 0;\r
385 \r
386         if( ( usColumn == 0 ) && ( ucLine == 0 ) )\r
387         {\r
388                 LCD_Clear();\r
389         }\r
390 \r
391         if( ch != '\n' )\r
392         {\r
393                 /* Display one character on LCD */\r
394                 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );\r
395 \r
396                 /* Decrement the column position by 16 */\r
397                 usRefColumn -= mainCOLUMN_INCREMENT;\r
398 \r
399                 /* Increment the character counter */\r
400                 usColumn++;\r
401                 if( usColumn == mainMAX_COLUMN )\r
402                 {\r
403                         ucLine += mainROW_INCREMENT;\r
404                         usRefColumn = mainCOLUMN_START;\r
405                         usColumn = 0;\r
406                 }\r
407         }\r
408         else\r
409         {\r
410                 /* Move back to the first column of the next line. */\r
411                 ucLine += mainROW_INCREMENT;\r
412                 usRefColumn = mainCOLUMN_START;\r
413                 usColumn = 0;\r
414         }\r
415 \r
416         /* Wrap back to the top of the display. */\r
417         if( ucLine >= mainMAX_LINE )\r
418         {\r
419                 ucLine = 0;\r
420         }\r
421 \r
422         return ch;\r
423 }\r
424 /*-----------------------------------------------------------*/\r
425 \r
426 #ifdef  DEBUG\r
427 /* Keep the linker happy. */\r
428 void assert_failed( unsigned char* pcFile, unsigned long ulLine )\r
429 {\r
430         for( ;; )\r
431         {\r
432         }\r
433 }\r
434 #endif\r