]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_GCC_Rowley/main.c
Added STM32 Rowley demo.
[freertos] / Demo / CORTEX_STM32F103_GCC_Rowley / main.c
1 /*\r
2     FreeRTOS V6.0.5 - Copyright (C) 2010 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 eBook                                  *\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 7 (PB15) 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  */\r
72 \r
73 /* Standard includes. */\r
74 #include <stdio.h>\r
75 \r
76 /* Scheduler includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "queue.h"\r
80 #include "semphr.h"\r
81 \r
82 /* Library includes. */\r
83 #include "stm32f10x_it.h"\r
84 #include "stm32f10x_tim.h"\r
85 \r
86 /* Demo app includes. */\r
87 #include "BlockQ.h"\r
88 #include "integer.h"\r
89 #include "flash.h"\r
90 #include "partest.h"\r
91 #include "semtest.h"\r
92 #include "GenQTest.h"\r
93 #include "QPeek.h"\r
94 #include "recmutex.h"\r
95 \r
96 \r
97 /* The time between cycles of the 'check' task - which depends on whether the\r
98 check task has detected an error or not. */\r
99 #define mainCHECK_DELAY_NO_ERROR                        ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
100 #define mainCHECK_DELAY_ERROR                           ( ( portTickType ) 500 / portTICK_RATE_MS )\r
101 \r
102 /* The LED controlled by the 'check' task. */\r
103 #define mainCHECK_LED                                           ( 7 )\r
104 \r
105 /* Task priorities. */\r
106 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
107 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
108 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
109 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
110 #define mainLCD_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
111 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
112 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
113 \r
114 /* The WEB server has a larger stack as it utilises stack hungry string\r
115 handling library calls. */\r
116 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )\r
117 \r
118 /* The length of the queue used to send messages to the LCD task. */\r
119 #define mainQUEUE_SIZE                                          ( 3 )\r
120 \r
121 /* The period of the system clock in nano seconds.  This is used to calculate\r
122 the jitter time in nano seconds. */\r
123 #define mainNS_PER_CLOCK                                        ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * Configure the hardware for the demo.\r
129  */\r
130 static void prvSetupHardware( void );\r
131 \r
132 /* The 'check' task as described at the top of this file. */\r
133 static void prvCheckTask( void *pvParameters );\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 int main( void )\r
138 {\r
139 #ifdef DEBUG\r
140   debug();\r
141 #endif\r
142 \r
143         /* Set up the clocks and memory interface. */\r
144         prvSetupHardware();\r
145 \r
146         /* Start the standard demo tasks.  These are just here to exercise the\r
147         kernel port and provide examples of how the FreeRTOS API can be used. */\r
148         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
149     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
150     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
151     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
152         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
153     vStartQueuePeekTasks();\r
154     vStartRecursiveMutexTasks();\r
155 \r
156         /* Create the 'check' task, which is defined within this file. */\r
157         xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
158 \r
159     /* Start the scheduler. */\r
160         vTaskStartScheduler();\r
161 \r
162     /* Will only get here if there was insufficient memory to create the idle\r
163     task.  The idle task is created within vTaskStartScheduler(). */\r
164         for( ;; );\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 static void prvCheckTask( void *pvParameters )\r
169 {\r
170 portTickType xLastExecutionTime;\r
171 unsigned long ulTicksToWait = mainCHECK_DELAY_NO_ERROR;\r
172 \r
173         /* Just to remove the compiler warning about the unused parameter. */\r
174         ( void ) pvParameters;\r
175 \r
176         /* Initialise the variable used to control our iteration rate prior to\r
177         its first use. */\r
178         xLastExecutionTime = xTaskGetTickCount();\r
179 \r
180         for( ;; )\r
181         {\r
182                 /* Wait until it is time to run the tests again. */\r
183                 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );\r
184 \r
185                 /* Has an error been found in any task? */\r
186                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
187                 {\r
188                         /* Reduce the time between cycles of this task - which has the\r
189                         effect of increasing the rate at which the 'check' LED toggles to\r
190                         indicate the existence of an error to an observer. */\r
191                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
192                 }\r
193                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
194                 {\r
195                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
196                 }\r
197                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
198                 {\r
199                         ulTicksToWait = mainCHECK_DELAY_ERROR;\r
200                 }\r
201             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
202             {\r
203                 ulTicksToWait = mainCHECK_DELAY_ERROR;\r
204             }\r
205             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
206             {\r
207                 ulTicksToWait = mainCHECK_DELAY_ERROR;\r
208             }\r
209             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
210             {\r
211                 ulTicksToWait = mainCHECK_DELAY_ERROR;\r
212             }\r
213 \r
214                 vParTestToggleLED( mainCHECK_LED );\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void prvSetupHardware( void )\r
220 {\r
221         /* RCC system reset(for debug purpose). */\r
222         RCC_DeInit ();                        \r
223 \r
224     /* Enable HSE. */\r
225         RCC_HSEConfig (RCC_HSE_ON);           \r
226         \r
227         /* Wait till HSE is ready. */\r
228         while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);\r
229         \r
230     /* HCLK = SYSCLK. */\r
231         RCC_HCLKConfig   (RCC_SYSCLK_Div1);   \r
232 \r
233     /* PCLK2  = HCLK. */\r
234         RCC_PCLK2Config  (RCC_HCLK_Div1);     \r
235 \r
236     /* PCLK1  = HCLK/2. */\r
237         RCC_PCLK1Config  (RCC_HCLK_Div2);     \r
238 \r
239         /* ADCCLK = PCLK2/4. */\r
240         RCC_ADCCLKConfig (RCC_PCLK2_Div4);    \r
241         \r
242     /* Flash 2 wait state. */\r
243         *( volatile unsigned long  * )0x40022000 = 0x01;           \r
244         \r
245         /* PLLCLK = 8MHz * 9 = 72 MHz */\r
246         RCC_PLLConfig (RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);\r
247         \r
248     /* Enable PLL. */\r
249         RCC_PLLCmd (ENABLE);                  \r
250         \r
251         /* Wait till PLL is ready. */\r
252         while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);\r
253         \r
254         /* Select PLL as system clock source. */\r
255         RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);\r
256         \r
257         /* Wait till PLL is used as system clock source. */\r
258         while (RCC_GetSYSCLKSource() != 0x08);\r
259 \r
260         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
261         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
262                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
263 \r
264         /* Set the Vector Table base address at 0x08000000. */\r
265         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
266 \r
267         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
268 \r
269         /* Configure HCLK clock as SysTick clock source. */\r
270         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
271 \r
272         /* Initialise the IO used for the LED outputs. */\r
273         vParTestInitialise();\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
278 {\r
279         /* This function will get called if a task overflows its stack.   If the\r
280         parameters are corrupt then inspect pxCurrentTCB to find which was the\r
281         offending task. */\r
282 \r
283         ( void ) pxTask;\r
284         ( void ) pcTaskName;\r
285 \r
286         for( ;; );\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 void assert_failed( unsigned char *pucFile, unsigned long ulLine )\r
291 {\r
292         ( void ) pucFile;\r
293         ( void ) ulLine;\r
294 \r
295         for( ;; );\r
296 }\r
297 \r