]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F103_GCC_Rowley/main.c
bb258349034366030e6695f522228e62470763e1
[freertos] / FreeRTOS / Demo / CORTEX_STM32F103_GCC_Rowley / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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 /*\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  * (which just exist to test the kernel port and provide an example of how to use\r
33  * each FreeRTOS API function).\r
34  *\r
35  * In addition to the standard demo tasks, the following tasks and tests are\r
36  * defined and/or created within this file:\r
37  *\r
38  * "Check" task - This only executes every five seconds but has the highest\r
39  * priority so is guaranteed to get processor time.  Its main function is to\r
40  * check that all the standard demo tasks are still operational. The check task\r
41  * will toggle LED 3 (PB11) every five seconds so long as no errors have been\r
42  * detected.  The toggle rate will increase to half a second if an error has\r
43  * been found in any task.\r
44  *\r
45  * "Echo" task - This is a very basic task that simply echoes any characters\r
46  * received on COM0 (USART1).  This can be tested by transmitting a text file\r
47  * from a dumb terminal to the STM32 USART then observing or capturing the text\r
48  * that is echoed back.  Missing characters will be all the more obvious if the\r
49  * file contains a simple repeating string of fixed width.\r
50  *\r
51  * Currently this demo does not include interrupt nesting examples.  High\r
52  * frequency timer and simpler nesting examples can be found in most Cortex-M3\r
53  * demo applications.\r
54  *\r
55  * The functions used to initialise, set and clear LED outputs are normally\r
56  * defined in partest.c.  This demo includes two partest files, one that is\r
57  * configured for use with the Keil MCBSTM32 evaluation board (called\r
58  * ParTest_MCBSTM32.c) and one that is configured for use with the official\r
59  * ST Eval board (called ParTest_ST_Eval.c).  One one of these files should be\r
60  * included in the build at any one time, as appropriate for the hardware\r
61  * actually being used.\r
62  */\r
63 \r
64 /* Standard includes. */\r
65 #include <string.h>\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "queue.h"\r
71 \r
72 /* Library includes. */\r
73 #include "stm32f10x_it.h"\r
74 \r
75 /* Demo app includes. */\r
76 #include "BlockQ.h"\r
77 #include "integer.h"\r
78 #include "flash.h"\r
79 #include "partest.h"\r
80 #include "semtest.h"\r
81 #include "GenQTest.h"\r
82 #include "QPeek.h"\r
83 #include "recmutex.h"\r
84 \r
85 /* Driver includes. */\r
86 #include "STM32_USART.h"\r
87 \r
88 \r
89 /* The time between cycles of the 'check' task - which depends on whether the\r
90 check task has detected an error or not. */\r
91 #define mainCHECK_DELAY_NO_ERROR                        ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
92 #define mainCHECK_DELAY_ERROR                           ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
93 \r
94 /* The LED controlled by the 'check' task. */\r
95 #define mainCHECK_LED                                           ( 3 )\r
96 \r
97 /* Task priorities. */\r
98 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
99 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
100 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
101 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
102 #define mainECHO_TASK_PRIORITY                          ( tskIDLE_PRIORITY + 1 )\r
103 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
104 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
105 \r
106 /* COM port and baud rate used by the echo task. */\r
107 #define mainCOM0                                                        ( 0 )\r
108 #define mainBAUD_RATE                                           ( 115200 )\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * Configure the hardware for the demo.\r
114  */\r
115 static void prvSetupHardware( void );\r
116 \r
117 /* The 'check' task as described at the top of this file. */\r
118 static void prvCheckTask( void *pvParameters );\r
119 \r
120 /* A simple task that echoes all the characters that are received on COM0\r
121 (USART1). */\r
122 static void prvUSARTEchoTask( void *pvParameters );\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 int main( void )\r
127 {\r
128 #ifdef DEBUG\r
129   debug();\r
130 #endif\r
131 \r
132         /* Set up the clocks and memory interface. */\r
133         prvSetupHardware();\r
134 \r
135         /* Start the standard demo tasks.  These are just here to exercise the\r
136         kernel port and provide examples of how the FreeRTOS API can be used. */\r
137         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
138     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
139     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
140     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
141         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
142     vStartQueuePeekTasks();\r
143     vStartRecursiveMutexTasks();\r
144 \r
145         /* Create the 'echo' task, which is also defined within this file. */\r
146         xTaskCreate( prvUSARTEchoTask, "Echo", configMINIMAL_STACK_SIZE, NULL, mainECHO_TASK_PRIORITY, NULL );\r
147 \r
148         /* Create the 'check' task, which is also defined within this file. */\r
149         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
150 \r
151     /* Start the scheduler. */\r
152         vTaskStartScheduler();\r
153 \r
154     /* Will only get here if there was insufficient memory to create the idle\r
155     task.  The idle task is created within vTaskStartScheduler(). */\r
156         for( ;; );\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /* Described at the top of this file. */\r
161 static void prvCheckTask( void *pvParameters )\r
162 {\r
163 TickType_t xLastExecutionTime;\r
164 unsigned long ulTicksToWait = mainCHECK_DELAY_NO_ERROR;\r
165 \r
166         /* Just to remove the compiler warning about the unused parameter. */\r
167         ( void ) pvParameters;\r
168 \r
169         /* Initialise the variable used to control our iteration rate prior to\r
170         its first use. */\r
171         xLastExecutionTime = xTaskGetTickCount();\r
172 \r
173         for( ;; )\r
174         {\r
175                 /* Wait until it is time to run the tests again. */\r
176                 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );\r
177 \r
178                 /* Has an error been found in any task? */\r
179                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
180                 {\r
181                         /* Reduce the time between cycles of this task - which has the\r
182                         effect of increasing the rate at which the 'check' LED toggles to\r
183                         indicate the existence of an error to an observer. */\r
184                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
185                 }\r
186                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
187                 {\r
188                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
189                 }\r
190                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
191                 {\r
192                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
193                 }\r
194                 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
195                 {\r
196                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
197                 }\r
198                 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
199                 {\r
200                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
201                 }\r
202                 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
203                 {\r
204                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
205                 }\r
206 \r
207                 vParTestToggleLED( mainCHECK_LED );\r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 /* Described at the top of this file. */\r
213 static void prvUSARTEchoTask( void *pvParameters )\r
214 {\r
215 signed char cChar;\r
216 \r
217 /* String declared static to ensure it does not end up on the stack, no matter\r
218 what the optimisation level. */\r
219 static const char *pcLongishString =\r
220 "ABBA was a Swedish pop music group formed in Stockholm in 1972, consisting of Anni-Frid Frida Lyngstad, "\r
221 "Björn Ulvaeus, Benny Andersson and Agnetha Fältskog. Throughout the band's existence, Fältskog and Ulvaeus "\r
222 "were a married couple, as were Lyngstad and Andersson - although both couples later divorced. They became one "\r
223 "of the most commercially successful acts in the history of popular music, and they topped the charts worldwide "\r
224 "from 1972 to 1983.  ABBA gained international popularity employing catchy song hooks, simple lyrics, sound "\r
225 "effects (reverb, phasing) and a Wall of Sound achieved by overdubbing the female singers' voices in multiple "\r
226 "harmonies. As their popularity grew, they were sought after to tour Europe, Australia, and North America, drawing "\r
227 "crowds of ardent fans, notably in Australia. Touring became a contentious issue, being particularly cumbersome for "\r
228 "Fältskog, but they continued to release studio albums to widespread commercial success. At the height of their "\r
229 "popularity, however, both relationships began suffering strain that led ultimately to the collapse of first the "\r
230 "Ulvaeus-Fältskog marriage (in 1979) and then of the Andersson-Lyngstad marriage in 1981. In the late 1970s and early "\r
231 "1980s these relationship changes began manifesting in the group's music, as they produced more thoughtful, "\r
232 "introspective lyrics with different compositions.";\r
233 \r
234         /* Just to avoid compiler warnings. */\r
235         ( void ) pvParameters;\r
236 \r
237         /* Initialise COM0, which is USART1 according to the STM32 libraries. */\r
238         lCOMPortInit( mainCOM0, mainBAUD_RATE );\r
239 \r
240         /* Try sending out a string all in one go, as a very basic test of the\r
241     lSerialPutString() function. */\r
242     lSerialPutString( mainCOM0, pcLongishString, strlen( pcLongishString ) );\r
243 \r
244         for( ;; )\r
245         {\r
246                 /* Block to wait for a character to be received on COM0. */\r
247                 xSerialGetChar( mainCOM0, &cChar, portMAX_DELAY );\r
248 \r
249                 /* Write the received character back to COM0. */\r
250                 xSerialPutChar( mainCOM0, cChar, 0 );\r
251         }\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 static void prvSetupHardware( void )\r
256 {\r
257         /* RCC system reset(for debug purpose). */\r
258         RCC_DeInit ();\r
259 \r
260     /* Enable HSE. */\r
261         RCC_HSEConfig( RCC_HSE_ON );\r
262 \r
263         /* Wait till HSE is ready. */\r
264         while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);\r
265 \r
266     /* HCLK = SYSCLK. */\r
267         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
268 \r
269     /* PCLK2  = HCLK. */\r
270         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
271 \r
272     /* PCLK1  = HCLK/2. */\r
273         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
274 \r
275         /* ADCCLK = PCLK2/4. */\r
276         RCC_ADCCLKConfig( RCC_PCLK2_Div4 );\r
277 \r
278     /* Flash 2 wait state. */\r
279         *( volatile unsigned long  * )0x40022000 = 0x01;\r
280 \r
281         /* PLLCLK = 8MHz * 9 = 72 MHz */\r
282         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );\r
283 \r
284     /* Enable PLL. */\r
285         RCC_PLLCmd( ENABLE );\r
286 \r
287         /* Wait till PLL is ready. */\r
288         while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);\r
289 \r
290         /* Select PLL as system clock source. */\r
291         RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);\r
292 \r
293         /* Wait till PLL is used as system clock source. */\r
294         while (RCC_GetSYSCLKSource() != 0x08);\r
295 \r
296         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
297         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
298                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
299 \r
300         /* Set the Vector Table base address at 0x08000000. */\r
301         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
302 \r
303         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
304 \r
305         /* Configure HCLK clock as SysTick clock source. */\r
306         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
307 \r
308         /* Initialise the IO used for the LED outputs. */\r
309         vParTestInitialise();\r
310 \r
311         /* SPI2 Periph clock enable */\r
312         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
317 {\r
318         /* This function will get called if a task overflows its stack.   If the\r
319         parameters are corrupt then inspect pxCurrentTCB to find which was the\r
320         offending task. */\r
321 \r
322         ( void ) pxTask;\r
323         ( void ) pcTaskName;\r
324 \r
325         for( ;; );\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 void assert_failed( unsigned char *pucFile, unsigned long ulLine )\r
330 {\r
331         ( void ) pucFile;\r
332         ( void ) ulLine;\r
333 \r
334         for( ;; );\r
335 }\r
336 \r