stm32f4_discovery.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4_discovery.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 28-October-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. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f4_discovery.h"
  24. /** @addtogroup Utilities
  25. * @{
  26. */
  27. /** @addtogroup STM32F4_DISCOVERY
  28. * @{
  29. */
  30. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL
  31. * @brief This file provides set of firmware functions to manage Leds and push-button
  32. * available on STM32F4-Discovery Kit from STMicroelectronics.
  33. * @{
  34. */
  35. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions
  36. * @{
  37. */
  38. /**
  39. * @}
  40. */
  41. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Defines
  42. * @{
  43. */
  44. /**
  45. * @}
  46. */
  47. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Macros
  48. * @{
  49. */
  50. /**
  51. * @}
  52. */
  53. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Variables
  54. * @{
  55. */
  56. GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT, LED3_GPIO_PORT, LED5_GPIO_PORT,
  57. LED6_GPIO_PORT};
  58. const uint16_t GPIO_PIN[LEDn] = {LED4_PIN, LED3_PIN, LED5_PIN,
  59. LED6_PIN};
  60. const uint32_t GPIO_CLK[LEDn] = {LED4_GPIO_CLK, LED3_GPIO_CLK, LED5_GPIO_CLK,
  61. LED6_GPIO_CLK};
  62. GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT };
  63. const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN };
  64. const uint32_t BUTTON_CLK[BUTTONn] = {USER_BUTTON_GPIO_CLK };
  65. const uint16_t BUTTON_EXTI_LINE[BUTTONn] = {USER_BUTTON_EXTI_LINE };
  66. const uint8_t BUTTON_PORT_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PORT_SOURCE};
  67. const uint8_t BUTTON_PIN_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PIN_SOURCE };
  68. const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn };
  69. NVIC_InitTypeDef NVIC_InitStructure;
  70. /**
  71. * @}
  72. */
  73. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes
  74. * @{
  75. */
  76. /**
  77. * @}
  78. */
  79. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Functions
  80. * @{
  81. */
  82. /**
  83. * @brief Configures LED GPIO.
  84. * @param Led: Specifies the Led to be configured.
  85. * This parameter can be one of following parameters:
  86. * @arg LED4
  87. * @arg LED3
  88. * @arg LED5
  89. * @arg LED6
  90. * @retval None
  91. */
  92. void STM_EVAL_LEDInit(Led_TypeDef Led)
  93. {
  94. GPIO_InitTypeDef GPIO_InitStructure;
  95. /* Enable the GPIO_LED Clock */
  96. RCC_AHB1PeriphClockCmd(GPIO_CLK[Led], ENABLE);
  97. /* Configure the GPIO_LED pin */
  98. GPIO_InitStructure.GPIO_Pin = GPIO_PIN[Led];
  99. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  100. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  101. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  102. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  103. GPIO_Init(GPIO_PORT[Led], &GPIO_InitStructure);
  104. }
  105. /**
  106. * @brief Turns selected LED On.
  107. * @param Led: Specifies the Led to be set on.
  108. * This parameter can be one of following parameters:
  109. * @arg LED4
  110. * @arg LED3
  111. * @arg LED5
  112. * @arg LED6
  113. * @retval None
  114. */
  115. void STM_EVAL_LEDOn(Led_TypeDef Led)
  116. {
  117. GPIO_PORT[Led]->BSRRL = GPIO_PIN[Led];
  118. }
  119. /**
  120. * @brief Turns selected LED Off.
  121. * @param Led: Specifies the Led to be set off.
  122. * This parameter can be one of following parameters:
  123. * @arg LED4
  124. * @arg LED3
  125. * @arg LED5
  126. * @arg LED6
  127. * @retval None
  128. */
  129. void STM_EVAL_LEDOff(Led_TypeDef Led)
  130. {
  131. GPIO_PORT[Led]->BSRRH = GPIO_PIN[Led];
  132. }
  133. /**
  134. * @brief Toggles the selected LED.
  135. * @param Led: Specifies the Led to be toggled.
  136. * This parameter can be one of following parameters:
  137. * @arg LED4
  138. * @arg LED3
  139. * @arg LED5
  140. * @arg LED6
  141. * @retval None
  142. */
  143. void STM_EVAL_LEDToggle(Led_TypeDef Led)
  144. {
  145. GPIO_PORT[Led]->ODR ^= GPIO_PIN[Led];
  146. }
  147. /**
  148. * @brief Configures Button GPIO and EXTI Line.
  149. * @param Button: Specifies the Button to be configured.
  150. * This parameter should be: BUTTON_USER
  151. * @param Button_Mode: Specifies Button mode.
  152. * This parameter can be one of following parameters:
  153. * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  154. * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  155. * generation capability
  156. * @retval None
  157. */
  158. void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
  159. {
  160. GPIO_InitTypeDef GPIO_InitStructure;
  161. EXTI_InitTypeDef EXTI_InitStructure;
  162. NVIC_InitTypeDef NVIC_InitStructure;
  163. /* Enable the BUTTON Clock */
  164. RCC_AHB1PeriphClockCmd(BUTTON_CLK[Button], ENABLE);
  165. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  166. /* Configure Button pin as input */
  167. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  168. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  169. GPIO_InitStructure.GPIO_Pin = BUTTON_PIN[Button];
  170. GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStructure);
  171. if (Button_Mode == BUTTON_MODE_EXTI)
  172. {
  173. /* Connect Button EXTI Line to Button GPIO Pin */
  174. SYSCFG_EXTILineConfig(BUTTON_PORT_SOURCE[Button], BUTTON_PIN_SOURCE[Button]);
  175. /* Configure Button EXTI line */
  176. EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[Button];
  177. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  178. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  179. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  180. EXTI_Init(&EXTI_InitStructure);
  181. /* Enable and set Button EXTI Interrupt to the lowest priority */
  182. NVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn[Button];
  183. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
  184. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
  185. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  186. NVIC_Init(&NVIC_InitStructure);
  187. }
  188. }
  189. /**
  190. * @brief Returns the selected Button state.
  191. * @param Button: Specifies the Button to be checked.
  192. * This parameter should be: BUTTON_USER
  193. * @retval The Button GPIO pin value.
  194. */
  195. uint32_t STM_EVAL_PBGetState(Button_TypeDef Button)
  196. {
  197. return GPIO_ReadInputDataBit(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  198. }
  199. /**
  200. * @}
  201. */
  202. /**
  203. * @}
  204. */
  205. /**
  206. * @}
  207. */
  208. /**
  209. * @}
  210. */
  211. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/