]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_STM32L152_Discovery_IAR/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / CORTEX_STM32L152_Discovery_IAR / 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  * This project provides two demo applications.  A low power project that
72  * demonstrates the FreeRTOS tickless mode, and a more comprehensive test and
73  * demo application.  The configCREATE_LOW_POWER_DEMO setting (defined at the
74  * top of FreeRTOSConfig.h) is used to select between the two.  The low power
75  * demo is implemented and described in main_low_power.c.  The more
76  * comprehensive test and demo application is implemented and described in
77  * main_full.c.
78  *
79  * This file implements the code that is not demo specific, including the
80  * hardware setup and FreeRTOS hook functions.
81  */
82
83 /* Kernel includes. */
84 #include "FreeRTOS.h"
85 #include "task.h"
86
87 /* ST library functions. */
88 #include "stm32l1xx.h"
89 #include "discover_board.h"
90
91 /*-----------------------------------------------------------*/
92
93 /*
94  * Set up the hardware ready to run this demo.
95  */
96 static void prvSetupHardware( void );
97
98 /*
99  * main_low_power() is used when configCREATE_LOW_POWER_DEMO is set to 1.
100  * main_full() is used when configCREATE_LOW_POWER_DEMO is set to 0.
101  * configCREATE_LOW_POWER_DEMO is defined at the top of main.c.
102  */
103 extern void main_low_power( void );
104 extern void main_full( void );
105
106 /* Prototypes for the standard FreeRTOS callback/hook functions implemented
107 within this file. */
108 void vApplicationMallocFailedHook( void );
109 void vApplicationIdleHook( void );
110 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
111 void vApplicationTickHook( void );
112
113 /*-----------------------------------------------------------*/
114
115 int main( void )
116 {
117         /* Prepare the hardware to run this demo. */
118         prvSetupHardware();
119
120         /* The configCREATE_LOW_POWER_DEMO setting is described at the top of
121         this file. */
122         #if configCREATE_LOW_POWER_DEMO == 1
123         {
124                 main_low_power();
125         }
126         #else
127         {
128                 main_full();
129         }
130         #endif
131
132         /* This line will never be reached. */
133         return 0;
134 }
135 /*-----------------------------------------------------------*/
136
137 static void prvSetupHardware( void )
138 {
139 /* GPIO, EXTI and NVIC Init structure declaration */
140 GPIO_InitTypeDef GPIO_InitStructure;
141 EXTI_InitTypeDef EXTI_InitStructure;
142 NVIC_InitTypeDef NVIC_InitStructure;
143 void SystemCoreClockUpdate( void );
144
145         /* System function that updates the SystemCoreClock variable. */
146         SystemCoreClockUpdate();
147
148         /* Essential on STM32 Cortex-M devices. */
149         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
150
151         /* Systick is fed from HCLK/8. */
152         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK_Div8 );
153
154         /* Set MSI clock range to ~4.194MHz. */
155         RCC_MSIRangeConfig( RCC_MSIRange_6 );
156
157         /* Enable the GPIOs clocks. */
158         RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC| RCC_AHBPeriph_GPIOD| RCC_AHBPeriph_GPIOE| RCC_AHBPeriph_GPIOH, ENABLE );
159
160         /* Enable comparator clocks. */
161         RCC_APB1PeriphClockCmd( RCC_APB1Periph_COMP, ENABLE );
162
163         /* Enable SYSCFG clocks. */
164         RCC_APB2PeriphClockCmd( RCC_APB2Periph_SYSCFG , ENABLE );
165
166         /* Set internal voltage regulator to 1.5V. */
167         PWR_VoltageScalingConfig( PWR_VoltageScaling_Range2 );
168
169         /* Wait Until the Voltage Regulator is ready. */
170         while( PWR_GetFlagStatus( PWR_FLAG_VOS ) != RESET );
171
172         /* Configure User Button pin as input */
173         GPIO_InitStructure.GPIO_Pin = USERBUTTON_GPIO_PIN;
174         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
175         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
176         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
177         GPIO_Init( USERBUTTON_GPIO_PORT, &GPIO_InitStructure );
178
179         /* Select User Button pin as input source for EXTI Line */
180         SYSCFG_EXTILineConfig( EXTI_PortSourceGPIOA, EXTI_PinSource0 );
181
182         /* Configure EXT1 Line 0 in interrupt mode trigged on Rising edge */
183         EXTI_InitStructure.EXTI_Line = EXTI_Line0 ;  /* PA0 for User button AND IDD_WakeUP */
184         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
185         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
186         EXTI_InitStructure.EXTI_LineCmd = ENABLE;
187         EXTI_Init( &EXTI_InitStructure );
188
189         /* Enable and set EXTI0 Interrupt to the lowest priority */
190         NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
191         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY;
192         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
193         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
194         NVIC_Init( &NVIC_InitStructure );
195
196         /* Configure the LED_pin as output push-pull for LD3 & LD4 usage */
197         GPIO_InitStructure.GPIO_Pin = LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN;
198         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
199         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
200         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
201         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
202         GPIO_Init( LD_GPIO_PORT, &GPIO_InitStructure );
203
204         /* Force a low level on LEDs */
205         GPIO_LOW( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );
206         GPIO_LOW( LD_GPIO_PORT, LD_BLUE_GPIO_PIN );
207 }
208 /*-----------------------------------------------------------*/
209
210 void vApplicationMallocFailedHook( void )
211 {
212         /* vApplicationMallocFailedHook() will only be called if
213         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
214         function that will get called if a call to pvPortMalloc() fails.
215         pvPortMalloc() is called internally by the kernel whenever a task, queue,
216         timer or semaphore is created.  It is also called by various parts of the
217         demo application.  If heap_1.c or heap_2.c are used, then the size of the
218         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
219         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
220         to query the size of free heap space that remains (although it does not
221         provide information on how the remaining heap might be fragmented). */
222         taskDISABLE_INTERRUPTS();
223         for( ;; );
224 }
225 /*-----------------------------------------------------------*/
226
227 void vApplicationIdleHook( void )
228 {
229         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
230         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
231         task.  It is essential that code added to this hook function never attempts
232         to block in any way (for example, call xQueueReceive() with a block time
233         specified, or call vTaskDelay()).  If the application makes use of the
234         vTaskDelete() API function (as this demo application does) then it is also
235         important that vApplicationIdleHook() is permitted to return to its calling
236         function, because it is the responsibility of the idle task to clean up
237         memory allocated by the kernel to any task that has since been deleted. */
238 }
239 /*-----------------------------------------------------------*/
240
241 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
242 {
243         ( void ) pcTaskName;
244         ( void ) pxTask;
245
246         /* Run time stack overflow checking is performed if
247         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
248         function is called if a stack overflow is detected. */
249         taskDISABLE_INTERRUPTS();
250         for( ;; );
251 }
252 /*-----------------------------------------------------------*/
253
254 void vApplicationTickHook( void )
255 {
256         /* This function will be called by each tick interrupt if
257         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
258         added here, but the tick hook is called from an interrupt context, so
259         code must not attempt to block, and only the interrupt safe FreeRTOS API
260         functions can be used (those that end in FromISR()). */
261 }
262 /*-----------------------------------------------------------*/
263
264 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
265 {
266 volatile unsigned long ulSetToNonZeroInDebuggerToContinue = 0;
267
268         /* Parameters are not used. */
269         ( void ) ulLine;
270         ( void ) pcFileName;
271
272         taskENTER_CRITICAL();
273         {
274                 while( ulSetToNonZeroInDebuggerToContinue == 0 )
275                 {
276                         /* Use the debugger to set ulSetToNonZeroInDebuggerToContinue to a
277                         non zero value to step out of this function to the point that raised
278                         this assert(). */
279                         __asm volatile( "NOP" );
280                         __asm volatile( "NOP" );
281                 }
282         }
283         taskEXIT_CRITICAL();
284 }
285