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