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