]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/main.c
ed5cf6a383cdd80f0334a518bc6c8c1b2ac34282
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \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
35  * main_full.c.\r
36  *\r
37  * This file implements the code that is not demo specific, including the\r
38  * hardware setup and FreeRTOS hook functions.\r
39  */\r
40 \r
41 /* Kernel includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 \r
45 /* ST library functions. */\r
46 #include "stm32l1xx.h"\r
47 #include "discover_board.h"\r
48 \r
49 /*-----------------------------------------------------------*/\r
50 \r
51 /*\r
52  * Set up the hardware ready to run this demo.\r
53  */\r
54 static void prvSetupHardware( void );\r
55 \r
56 /*\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
60  */\r
61 extern void main_low_power( void );\r
62 extern void main_full( void );\r
63 \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
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 int main( void )\r
74 {\r
75         /* Prepare the hardware to run this demo. */\r
76         prvSetupHardware();\r
77 \r
78         /* The configCREATE_LOW_POWER_DEMO setting is described at the top of\r
79         this file. */\r
80         #if configCREATE_LOW_POWER_DEMO == 1\r
81         {\r
82                 main_low_power();\r
83         }\r
84         #else\r
85         {\r
86                 main_full();\r
87         }\r
88         #endif\r
89 \r
90         /* This line will never be reached. */\r
91         return 0;\r
92 }\r
93 /*-----------------------------------------------------------*/\r
94 \r
95 static void prvSetupHardware( void )\r
96 {\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
102 \r
103         /* System function that updates the SystemCoreClock variable. */\r
104         SystemCoreClockUpdate();\r
105 \r
106         /* Essential on STM32 Cortex-M devices. */\r
107         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
108 \r
109         /* Systick is fed from HCLK/8. */\r
110         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK_Div8 );\r
111 \r
112         /* Set MSI clock range to ~4.194MHz. */\r
113         RCC_MSIRangeConfig( RCC_MSIRange_6 );\r
114 \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
117 \r
118         /* Enable comparator clocks. */\r
119         RCC_APB1PeriphClockCmd( RCC_APB1Periph_COMP, ENABLE );\r
120 \r
121         /* Enable SYSCFG clocks. */\r
122         RCC_APB2PeriphClockCmd( RCC_APB2Periph_SYSCFG , ENABLE );\r
123 \r
124         /* Set internal voltage regulator to 1.5V. */\r
125         PWR_VoltageScalingConfig( PWR_VoltageScaling_Range2 );\r
126 \r
127         /* Wait Until the Voltage Regulator is ready. */\r
128         while( PWR_GetFlagStatus( PWR_FLAG_VOS ) != RESET );\r
129 \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
136 \r
137         /* Select User Button pin as input source for EXTI Line */\r
138         SYSCFG_EXTILineConfig( EXTI_PortSourceGPIOA, EXTI_PinSource0 );\r
139 \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
146 \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
153 \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
161 \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
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 void vApplicationMallocFailedHook( void )\r
169 {\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
181         for( ;; );\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vApplicationIdleHook( void )\r
186 {\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
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
200 {\r
201         ( void ) pcTaskName;\r
202         ( void ) pxTask;\r
203 \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
208         for( ;; );\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vApplicationTickHook( void )\r
213 {\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
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
223 {\r
224 volatile unsigned long ulSetToNonZeroInDebuggerToContinue = 0;\r
225 \r
226         /* Parameters are not used. */\r
227         ( void ) ulLine;\r
228         ( void ) pcFileName;\r
229 \r
230         taskENTER_CRITICAL();\r
231         {\r
232                 while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
233                 {\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
236                         this assert(). */\r
237                         __asm volatile( "NOP" );\r
238                         __asm volatile( "NOP" );\r
239                 }\r
240         }\r
241         taskEXIT_CRITICAL();\r
242 }\r
243 \r