]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F103_GCC_Rowley/main.c
aeecddafcb28a3504ce97fc828c61c371fcd5922
[freertos] / FreeRTOS / Demo / CORTEX_STM32F103_GCC_Rowley / main.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\r
72  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
73  * documentation provides more details of the standard demo application tasks\r
74  * (which just exist to test the kernel port and provide an example of how to use\r
75  * each FreeRTOS API function).\r
76  *\r
77  * In addition to the standard demo tasks, the following tasks and tests are\r
78  * defined and/or created within this file:\r
79  *\r
80  * "Check" task - This only executes every five seconds but has the highest\r
81  * priority so is guaranteed to get processor time.  Its main function is to\r
82  * check that all the standard demo tasks are still operational. The check task\r
83  * will toggle LED 3 (PB11) every five seconds so long as no errors have been\r
84  * detected.  The toggle rate will increase to half a second if an error has\r
85  * been found in any task.\r
86  *\r
87  * "Echo" task - This is a very basic task that simply echoes any characters\r
88  * received on COM0 (USART1).  This can be tested by transmitting a text file\r
89  * from a dumb terminal to the STM32 USART then observing or capturing the text\r
90  * that is echoed back.  Missing characters will be all the more obvious if the\r
91  * file contains a simple repeating string of fixed width.\r
92  *\r
93  * Currently this demo does not include interrupt nesting examples.  High\r
94  * frequency timer and simpler nesting examples can be found in most Cortex-M3\r
95  * demo applications.\r
96  *\r
97  * The functions used to initialise, set and clear LED outputs are normally\r
98  * defined in partest.c.  This demo includes two partest files, one that is\r
99  * configured for use with the Keil MCBSTM32 evaluation board (called\r
100  * ParTest_MCBSTM32.c) and one that is configured for use with the official\r
101  * ST Eval board (called ParTest_ST_Eval.c).  One one of these files should be\r
102  * included in the build at any one time, as appropriate for the hardware\r
103  * actually being used.\r
104  */\r
105 \r
106 /* Standard includes. */\r
107 #include <string.h>\r
108 \r
109 /* Scheduler includes. */\r
110 #include "FreeRTOS.h"\r
111 #include "task.h"\r
112 #include "queue.h"\r
113 \r
114 /* Library includes. */\r
115 #include "stm32f10x_it.h"\r
116 \r
117 /* Demo app includes. */\r
118 #include "BlockQ.h"\r
119 #include "integer.h"\r
120 #include "flash.h"\r
121 #include "partest.h"\r
122 #include "semtest.h"\r
123 #include "GenQTest.h"\r
124 #include "QPeek.h"\r
125 #include "recmutex.h"\r
126 \r
127 /* Driver includes. */\r
128 #include "STM32_USART.h"\r
129 \r
130 \r
131 /* The time between cycles of the 'check' task - which depends on whether the\r
132 check task has detected an error or not. */\r
133 #define mainCHECK_DELAY_NO_ERROR                        ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
134 #define mainCHECK_DELAY_ERROR                           ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
135 \r
136 /* The LED controlled by the 'check' task. */\r
137 #define mainCHECK_LED                                           ( 3 )\r
138 \r
139 /* Task priorities. */\r
140 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
141 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
142 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
143 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
144 #define mainECHO_TASK_PRIORITY                          ( tskIDLE_PRIORITY + 1 )\r
145 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
146 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
147 \r
148 /* COM port and baud rate used by the echo task. */\r
149 #define mainCOM0                                                        ( 0 )\r
150 #define mainBAUD_RATE                                           ( 115200 )\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /*\r
155  * Configure the hardware for the demo.\r
156  */\r
157 static void prvSetupHardware( void );\r
158 \r
159 /* The 'check' task as described at the top of this file. */\r
160 static void prvCheckTask( void *pvParameters );\r
161 \r
162 /* A simple task that echoes all the characters that are received on COM0\r
163 (USART1). */\r
164 static void prvUSARTEchoTask( void *pvParameters );\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 int main( void )\r
169 {\r
170 #ifdef DEBUG\r
171   debug();\r
172 #endif\r
173 \r
174         /* Set up the clocks and memory interface. */\r
175         prvSetupHardware();\r
176 \r
177         /* Start the standard demo tasks.  These are just here to exercise the\r
178         kernel port and provide examples of how the FreeRTOS API can be used. */\r
179         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
180     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
181     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
182     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
183         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
184     vStartQueuePeekTasks();\r
185     vStartRecursiveMutexTasks();\r
186 \r
187         /* Create the 'echo' task, which is also defined within this file. */\r
188         xTaskCreate( prvUSARTEchoTask, "Echo", configMINIMAL_STACK_SIZE, NULL, mainECHO_TASK_PRIORITY, NULL );\r
189 \r
190         /* Create the 'check' task, which is also defined within this file. */\r
191         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
192 \r
193     /* Start the scheduler. */\r
194         vTaskStartScheduler();\r
195 \r
196     /* Will only get here if there was insufficient memory to create the idle\r
197     task.  The idle task is created within vTaskStartScheduler(). */\r
198         for( ;; );\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /* Described at the top of this file. */\r
203 static void prvCheckTask( void *pvParameters )\r
204 {\r
205 TickType_t xLastExecutionTime;\r
206 unsigned long ulTicksToWait = mainCHECK_DELAY_NO_ERROR;\r
207 \r
208         /* Just to remove the compiler warning about the unused parameter. */\r
209         ( void ) pvParameters;\r
210 \r
211         /* Initialise the variable used to control our iteration rate prior to\r
212         its first use. */\r
213         xLastExecutionTime = xTaskGetTickCount();\r
214 \r
215         for( ;; )\r
216         {\r
217                 /* Wait until it is time to run the tests again. */\r
218                 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );\r
219 \r
220                 /* Has an error been found in any task? */\r
221                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
222                 {\r
223                         /* Reduce the time between cycles of this task - which has the\r
224                         effect of increasing the rate at which the 'check' LED toggles to\r
225                         indicate the existence of an error to an observer. */\r
226                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
227                 }\r
228                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
229                 {\r
230                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
231                 }\r
232                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
233                 {\r
234                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
235                 }\r
236             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
237             {\r
238                 ulTicksToWait = mainCHECK_DELAY_ERROR;\r
239             }\r
240             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
241             {\r
242                 ulTicksToWait = mainCHECK_DELAY_ERROR;\r
243             }\r
244             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
245             {\r
246                 ulTicksToWait = mainCHECK_DELAY_ERROR;\r
247             }\r
248 \r
249                 vParTestToggleLED( mainCHECK_LED );\r
250         }\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 /* Described at the top of this file. */\r
255 static void prvUSARTEchoTask( void *pvParameters )\r
256 {\r
257 signed char cChar;\r
258 \r
259 /* String declared static to ensure it does not end up on the stack, no matter\r
260 what the optimisation level. */\r
261 static const char *pcLongishString =\r
262 "ABBA was a Swedish pop music group formed in Stockholm in 1972, consisting of Anni-Frid Frida Lyngstad, "\r
263 "Björn Ulvaeus, Benny Andersson and Agnetha Fältskog. Throughout the band's existence, Fältskog and Ulvaeus "\r
264 "were a married couple, as were Lyngstad and Andersson - although both couples later divorced. They became one "\r
265 "of the most commercially successful acts in the history of popular music, and they topped the charts worldwide "\r
266 "from 1972 to 1983.  ABBA gained international popularity employing catchy song hooks, simple lyrics, sound "\r
267 "effects (reverb, phasing) and a Wall of Sound achieved by overdubbing the female singers' voices in multiple "\r
268 "harmonies. As their popularity grew, they were sought after to tour Europe, Australia, and North America, drawing "\r
269 "crowds of ardent fans, notably in Australia. Touring became a contentious issue, being particularly cumbersome for "\r
270 "Fältskog, but they continued to release studio albums to widespread commercial success. At the height of their "\r
271 "popularity, however, both relationships began suffering strain that led ultimately to the collapse of first the "\r
272 "Ulvaeus-Fältskog marriage (in 1979) and then of the Andersson-Lyngstad marriage in 1981. In the late 1970s and early "\r
273 "1980s these relationship changes began manifesting in the group's music, as they produced more thoughtful, "\r
274 "introspective lyrics with different compositions.";\r
275 \r
276         /* Just to avoid compiler warnings. */\r
277         ( void ) pvParameters;\r
278 \r
279         /* Initialise COM0, which is USART1 according to the STM32 libraries. */\r
280         lCOMPortInit( mainCOM0, mainBAUD_RATE );\r
281 \r
282         /* Try sending out a string all in one go, as a very basic test of the\r
283     lSerialPutString() function. */\r
284     lSerialPutString( mainCOM0, pcLongishString, strlen( pcLongishString ) );\r
285 \r
286         for( ;; )\r
287         {\r
288                 /* Block to wait for a character to be received on COM0. */\r
289                 xSerialGetChar( mainCOM0, &cChar, portMAX_DELAY );\r
290 \r
291                 /* Write the received character back to COM0. */\r
292                 xSerialPutChar( mainCOM0, cChar, 0 );\r
293         }\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 static void prvSetupHardware( void )\r
298 {\r
299         /* RCC system reset(for debug purpose). */\r
300         RCC_DeInit ();\r
301 \r
302     /* Enable HSE. */\r
303         RCC_HSEConfig( RCC_HSE_ON );\r
304 \r
305         /* Wait till HSE is ready. */\r
306         while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);\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         /* ADCCLK = PCLK2/4. */\r
318         RCC_ADCCLKConfig( RCC_PCLK2_Div4 );\r
319 \r
320     /* Flash 2 wait state. */\r
321         *( volatile unsigned long  * )0x40022000 = 0x01;\r
322 \r
323         /* PLLCLK = 8MHz * 9 = 72 MHz */\r
324         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );\r
325 \r
326     /* Enable PLL. */\r
327         RCC_PLLCmd( ENABLE );\r
328 \r
329         /* Wait till PLL is ready. */\r
330         while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);\r
331 \r
332         /* Select PLL as system clock source. */\r
333         RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);\r
334 \r
335         /* Wait till PLL is used as system clock source. */\r
336         while (RCC_GetSYSCLKSource() != 0x08);\r
337 \r
338         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
339         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
340                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
341 \r
342         /* Set the Vector Table base address at 0x08000000. */\r
343         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
344 \r
345         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
346 \r
347         /* Configure HCLK clock as SysTick clock source. */\r
348         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
349 \r
350         /* Initialise the IO used for the LED outputs. */\r
351         vParTestInitialise();\r
352 \r
353         /* SPI2 Periph clock enable */\r
354         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
359 {\r
360         /* This function will get called if a task overflows its stack.   If the\r
361         parameters are corrupt then inspect pxCurrentTCB to find which was the\r
362         offending task. */\r
363 \r
364         ( void ) pxTask;\r
365         ( void ) pcTaskName;\r
366 \r
367         for( ;; );\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 void assert_failed( unsigned char *pucFile, unsigned long ulLine )\r
372 {\r
373         ( void ) pucFile;\r
374         ( void ) ulLine;\r
375 \r
376         for( ;; );\r
377 }\r
378 \r