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