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