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