Merge pull request #93 from zyp/master
[fw/stlink] / example / stm32f4 / Utilities / STM32F4-Discovery / stm32f4_discovery.c
1 /**
2   ******************************************************************************
3   * @file    stm32f4_discovery.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    19-September-2011
7   * @brief   This file provides set of firmware functions to manage Leds and
8   *          push-button available on STM32F4-Discovery Kit from STMicroelectronics.
9   ******************************************************************************
10   * @attention
11   *
12   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
15   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18   *
19   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
20   ******************************************************************************
21   */ 
22   
23 /* Includes ------------------------------------------------------------------*/
24 #include "stm32f4_discovery.h"
25
26 //ADDED BY ME!!!!!!!!!!!!!!!!!!!!
27 #include "stm32f4xx_conf.h"
28
29
30 /** @addtogroup Utilities
31   * @{
32   */ 
33
34 /** @addtogroup STM32F4_DISCOVERY
35   * @{
36   */   
37     
38 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL 
39   * @brief This file provides set of firmware functions to manage Leds and push-button
40   *        available on STM32F4-Discovery Kit from STMicroelectronics.
41   * @{
42   */ 
43
44 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions
45   * @{
46   */ 
47 /**
48   * @}
49   */ 
50
51
52 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Defines
53   * @{
54   */ 
55 /**
56   * @}
57   */ 
58
59
60 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Macros
61   * @{
62   */ 
63 /**
64   * @}
65   */ 
66
67
68 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Variables
69   * @{
70   */ 
71 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT, LED3_GPIO_PORT, LED5_GPIO_PORT,
72                                  LED6_GPIO_PORT};
73 const uint16_t GPIO_PIN[LEDn] = {LED4_PIN, LED3_PIN, LED5_PIN,
74                                  LED6_PIN};
75 const uint32_t GPIO_CLK[LEDn] = {LED4_GPIO_CLK, LED3_GPIO_CLK, LED5_GPIO_CLK,
76                                  LED6_GPIO_CLK};
77
78 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT }; 
79
80 const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN }; 
81
82 const uint32_t BUTTON_CLK[BUTTONn] = {USER_BUTTON_GPIO_CLK };
83
84 const uint16_t BUTTON_EXTI_LINE[BUTTONn] = {USER_BUTTON_EXTI_LINE };
85
86 const uint8_t BUTTON_PORT_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PORT_SOURCE};
87                                                                  
88 const uint8_t BUTTON_PIN_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PIN_SOURCE }; 
89 const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn };
90
91 NVIC_InitTypeDef   NVIC_InitStructure;
92
93 /**
94   * @}
95   */ 
96
97
98 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes
99   * @{
100   */ 
101
102 /**
103   * @}
104   */ 
105
106 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Functions
107   * @{
108   */ 
109
110 /**
111   * @brief  Configures LED GPIO.
112   * @param  Led: Specifies the Led to be configured. 
113   *   This parameter can be one of following parameters:
114   *     @arg LED4
115   *     @arg LED3
116   *     @arg LED5
117   *     @arg LED6
118   * @retval None
119   */
120 void STM_EVAL_LEDInit(Led_TypeDef Led)
121 {
122   GPIO_InitTypeDef  GPIO_InitStructure;
123   
124   /* Enable the GPIO_LED Clock */
125   RCC_AHB1PeriphClockCmd(GPIO_CLK[Led], ENABLE);
126
127   /* Configure the GPIO_LED pin */
128   GPIO_InitStructure.GPIO_Pin = GPIO_PIN[Led];
129   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
130   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
131   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
132   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
133   GPIO_Init(GPIO_PORT[Led], &GPIO_InitStructure);
134 }
135
136 /**
137   * @brief  Turns selected LED On.
138   * @param  Led: Specifies the Led to be set on. 
139   *   This parameter can be one of following parameters:
140   *     @arg LED4
141   *     @arg LED3
142   *     @arg LED5
143   *     @arg LED6  
144   * @retval None
145   */
146 void STM_EVAL_LEDOn(Led_TypeDef Led)
147 {
148   GPIO_PORT[Led]->BSRRL = GPIO_PIN[Led];
149 }
150
151 /**
152   * @brief  Turns selected LED Off.
153   * @param  Led: Specifies the Led to be set off. 
154   *   This parameter can be one of following parameters:
155   *     @arg LED4
156   *     @arg LED3
157   *     @arg LED5
158   *     @arg LED6 
159   * @retval None
160   */
161 void STM_EVAL_LEDOff(Led_TypeDef Led)
162 {
163   GPIO_PORT[Led]->BSRRH = GPIO_PIN[Led];  
164 }
165
166 /**
167   * @brief  Toggles the selected LED.
168   * @param  Led: Specifies the Led to be toggled. 
169   *   This parameter can be one of following parameters:
170   *     @arg LED4
171   *     @arg LED3
172   *     @arg LED5
173   *     @arg LED6  
174   * @retval None
175   */
176 void STM_EVAL_LEDToggle(Led_TypeDef Led)
177 {
178   GPIO_PORT[Led]->ODR ^= GPIO_PIN[Led];
179 }
180
181 /**
182   * @brief  Configures Button GPIO and EXTI Line.
183   * @param  Button: Specifies the Button to be configured.
184   *   This parameter should be: BUTTON_USER
185   * @param  Button_Mode: Specifies Button mode.
186   *   This parameter can be one of following parameters:   
187   *     @arg BUTTON_MODE_GPIO: Button will be used as simple IO 
188   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
189   *                            generation capability  
190   * @retval None
191   */
192 void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
193 {
194   GPIO_InitTypeDef GPIO_InitStructure;
195   EXTI_InitTypeDef EXTI_InitStructure;
196   NVIC_InitTypeDef NVIC_InitStructure;
197
198   /* Enable the BUTTON Clock */
199   RCC_AHB1PeriphClockCmd(BUTTON_CLK[Button], ENABLE);
200   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
201
202   /* Configure Button pin as input */
203   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
204   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
205   GPIO_InitStructure.GPIO_Pin = BUTTON_PIN[Button];
206   GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStructure);
207
208   if (Button_Mode == BUTTON_MODE_EXTI)
209   {
210     /* Connect Button EXTI Line to Button GPIO Pin */
211     SYSCFG_EXTILineConfig(BUTTON_PORT_SOURCE[Button], BUTTON_PIN_SOURCE[Button]);
212
213     /* Configure Button EXTI line */
214     EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[Button];
215     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
216     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  
217     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
218     EXTI_Init(&EXTI_InitStructure);
219
220     /* Enable and set Button EXTI Interrupt to the lowest priority */
221     NVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn[Button];
222     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
223     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
224     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
225
226     NVIC_Init(&NVIC_InitStructure); 
227   }
228 }
229
230 /**
231   * @brief  Returns the selected Button state.
232   * @param  Button: Specifies the Button to be checked.
233   *   This parameter should be: BUTTON_USER  
234   * @retval The Button GPIO pin value.
235   */
236 uint32_t STM_EVAL_PBGetState(Button_TypeDef Button)
237 {
238   return GPIO_ReadInputDataBit(BUTTON_PORT[Button], BUTTON_PIN[Button]);
239 }
240
241 /**
242   * @}
243   */ 
244
245 /**
246   * @}
247   */ 
248
249 /**
250   * @}
251   */   
252
253 /**
254   * @}
255   */ 
256     
257 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/