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