2 * FreeRTOS Kernel V10.0.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /******************************************************************************
\r
29 * This project provides two demo applications. A low power project that
\r
30 * demonstrates the FreeRTOS tickless mode, and a more comprehensive test and
\r
31 * demo application. The configCREATE_LOW_POWER_DEMO setting (defined at the
\r
32 * top of FreeRTOSConfig.h) is used to select between the two. The low power
\r
33 * demo is implemented and described in main_low_power.c. The more
\r
34 * comprehensive test and demo application is implemented and described in
\r
37 * This file implements the code that is not demo specific, including the
\r
38 * hardware setup and FreeRTOS hook functions.
\r
41 /* Kernel includes. */
\r
42 #include "FreeRTOS.h"
\r
45 /* ST library functions. */
\r
46 #include "stm32l1xx.h"
\r
47 #include "discover_board.h"
\r
49 /*-----------------------------------------------------------*/
\r
52 * Set up the hardware ready to run this demo.
\r
54 static void prvSetupHardware( void );
\r
57 * main_low_power() is used when configCREATE_LOW_POWER_DEMO is set to 1.
\r
58 * main_full() is used when configCREATE_LOW_POWER_DEMO is set to 0.
\r
59 * configCREATE_LOW_POWER_DEMO is defined at the top of main.c.
\r
61 extern void main_low_power( void );
\r
62 extern void main_full( void );
\r
64 /* Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
65 within this file. */
\r
66 void vApplicationMallocFailedHook( void );
\r
67 void vApplicationIdleHook( void );
\r
68 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
69 void vApplicationTickHook( void );
\r
71 /*-----------------------------------------------------------*/
\r
75 /* Prepare the hardware to run this demo. */
\r
78 /* The configCREATE_LOW_POWER_DEMO setting is described at the top of
\r
80 #if configCREATE_LOW_POWER_DEMO == 1
\r
90 /* This line will never be reached. */
\r
93 /*-----------------------------------------------------------*/
\r
95 static void prvSetupHardware( void )
\r
97 /* GPIO, EXTI and NVIC Init structure declaration */
\r
98 GPIO_InitTypeDef GPIO_InitStructure;
\r
99 EXTI_InitTypeDef EXTI_InitStructure;
\r
100 NVIC_InitTypeDef NVIC_InitStructure;
\r
101 void SystemCoreClockUpdate( void );
\r
103 /* System function that updates the SystemCoreClock variable. */
\r
104 SystemCoreClockUpdate();
\r
106 /* Essential on STM32 Cortex-M devices. */
\r
107 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
109 /* Systick is fed from HCLK/8. */
\r
110 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK_Div8 );
\r
112 /* Set MSI clock range to ~4.194MHz. */
\r
113 RCC_MSIRangeConfig( RCC_MSIRange_6 );
\r
115 /* Enable the GPIOs clocks. */
\r
116 RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC| RCC_AHBPeriph_GPIOD| RCC_AHBPeriph_GPIOE| RCC_AHBPeriph_GPIOH, ENABLE );
\r
118 /* Enable comparator clocks. */
\r
119 RCC_APB1PeriphClockCmd( RCC_APB1Periph_COMP, ENABLE );
\r
121 /* Enable SYSCFG clocks. */
\r
122 RCC_APB2PeriphClockCmd( RCC_APB2Periph_SYSCFG , ENABLE );
\r
124 /* Set internal voltage regulator to 1.5V. */
\r
125 PWR_VoltageScalingConfig( PWR_VoltageScaling_Range2 );
\r
127 /* Wait Until the Voltage Regulator is ready. */
\r
128 while( PWR_GetFlagStatus( PWR_FLAG_VOS ) != RESET );
\r
130 /* Configure User Button pin as input */
\r
131 GPIO_InitStructure.GPIO_Pin = USERBUTTON_GPIO_PIN;
\r
132 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
\r
133 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
\r
134 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
\r
135 GPIO_Init( USERBUTTON_GPIO_PORT, &GPIO_InitStructure );
\r
137 /* Select User Button pin as input source for EXTI Line */
\r
138 SYSCFG_EXTILineConfig( EXTI_PortSourceGPIOA, EXTI_PinSource0 );
\r
140 /* Configure EXT1 Line 0 in interrupt mode trigged on Rising edge */
\r
141 EXTI_InitStructure.EXTI_Line = EXTI_Line0 ; /* PA0 for User button AND IDD_WakeUP */
\r
142 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
\r
143 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
\r
144 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
\r
145 EXTI_Init( &EXTI_InitStructure );
\r
147 /* Enable and set EXTI0 Interrupt to the lowest priority */
\r
148 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
\r
149 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY;
\r
150 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
\r
151 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
\r
152 NVIC_Init( &NVIC_InitStructure );
\r
154 /* Configure the LED_pin as output push-pull for LD3 & LD4 usage */
\r
155 GPIO_InitStructure.GPIO_Pin = LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN;
\r
156 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
\r
157 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
\r
158 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
\r
159 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
\r
160 GPIO_Init( LD_GPIO_PORT, &GPIO_InitStructure );
\r
162 /* Force a low level on LEDs */
\r
163 GPIO_LOW( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );
\r
164 GPIO_LOW( LD_GPIO_PORT, LD_BLUE_GPIO_PIN );
\r
166 /*-----------------------------------------------------------*/
\r
168 void vApplicationMallocFailedHook( void )
\r
170 /* vApplicationMallocFailedHook() will only be called if
\r
171 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
172 function that will get called if a call to pvPortMalloc() fails.
\r
173 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
174 timer or semaphore is created. It is also called by various parts of the
\r
175 demo application. If heap_1.c or heap_2.c are used, then the size of the
\r
176 heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
177 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
178 to query the size of free heap space that remains (although it does not
\r
179 provide information on how the remaining heap might be fragmented). */
\r
180 taskDISABLE_INTERRUPTS();
\r
183 /*-----------------------------------------------------------*/
\r
185 void vApplicationIdleHook( void )
\r
187 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
188 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
189 task. It is essential that code added to this hook function never attempts
\r
190 to block in any way (for example, call xQueueReceive() with a block time
\r
191 specified, or call vTaskDelay()). If the application makes use of the
\r
192 vTaskDelete() API function (as this demo application does) then it is also
\r
193 important that vApplicationIdleHook() is permitted to return to its calling
\r
194 function, because it is the responsibility of the idle task to clean up
\r
195 memory allocated by the kernel to any task that has since been deleted. */
\r
197 /*-----------------------------------------------------------*/
\r
199 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
201 ( void ) pcTaskName;
\r
204 /* Run time stack overflow checking is performed if
\r
205 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
206 function is called if a stack overflow is detected. */
\r
207 taskDISABLE_INTERRUPTS();
\r
210 /*-----------------------------------------------------------*/
\r
212 void vApplicationTickHook( void )
\r
214 /* This function will be called by each tick interrupt if
\r
215 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
216 added here, but the tick hook is called from an interrupt context, so
\r
217 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
218 functions can be used (those that end in FromISR()). */
\r
220 /*-----------------------------------------------------------*/
\r
222 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
224 volatile unsigned long ulSetToNonZeroInDebuggerToContinue = 0;
\r
226 /* Parameters are not used. */
\r
228 ( void ) pcFileName;
\r
230 taskENTER_CRITICAL();
\r
232 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
234 /* Use the debugger to set ulSetToNonZeroInDebuggerToContinue to a
\r
235 non zero value to step out of this function to the point that raised
\r
237 __asm volatile( "NOP" );
\r
238 __asm volatile( "NOP" );
\r
241 taskEXIT_CRITICAL();
\r