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