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