Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.54 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f072b_discovery.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.0
  6.   * @date    27-December-2013
  7.   * @brief   This file provides set of firmware functions to manage Leds,
  8.   *          push-button for STM32F072B DISCO.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  13.   *
  14.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15.   * You may not use this file except in compliance with the License.
  16.   * You may obtain a copy of the License at:
  17.   *
  18.   *        http://www.st.com/software_license_agreement_liberty_v2
  19.   *
  20.   * Unless required by applicable law or agreed to in writing, software
  21.   * distributed under the License is distributed on an "AS IS" BASIS,
  22.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23.   * See the License for the specific language governing permissions and
  24.   * limitations under the License.
  25.   *
  26.   ******************************************************************************
  27.   */
  28.  
  29. /* Includes ------------------------------------------------------------------*/
  30. #include "stm32f072b_discovery.h"
  31. #include "stm32f0xx.h"
  32. #include "stm32f0xx_hal.h"
  33. #include "stm32f0xx_hal_gpio.h"
  34. #include "stm32f0xx_gpio.h"
  35. #include "main.h"
  36. #include "stm32f0xx_exti.h"
  37. #include "stm32f0xx_misc.h"
  38.  
  39.  
  40. /** @addtogroup Utilities
  41.   * @{
  42.   */
  43.  
  44. /** @addtogroup STM32F072B_DISCOVERY
  45.   * @{
  46.   */  
  47.    
  48. /** @defgroup STM32F072B_DISCOVERY_LOW_LEVEL
  49.   * @brief This file provides firmware functions to manage Leds and push-button
  50.   *        available on STM32F072B-DISCO board from STMicroelectronics.
  51.   * @{
  52.   */
  53.  
  54. /** @defgroup STM32F072B_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions
  55.   * @{
  56.   */
  57. /**
  58.   * @}
  59.   */
  60.  
  61.  
  62. /** @defgroup STM32F072B_DISCOVERY_LOW_LEVEL_Private_Defines
  63.   * @{
  64.   */
  65. /**
  66.   * @}
  67.   */
  68.  
  69.  
  70. /** @defgroup STM32F072B_DISCOVERY_LOW_LEVEL_Private_Macros
  71.   * @{
  72.   */
  73. /**
  74.   * @}
  75.   */
  76.  
  77.  
  78. /** @defgroup STM32F072B_DISCOVERY_LOW_LEVEL_Private_Variables
  79.   * @{
  80.   */
  81. GPIO_TypeDef* GPIO_PORT[LEDn] = {LED3_GPIO_PORT, LED4_GPIO_PORT,
  82.                                  LED5_GPIO_PORT, LED6_GPIO_PORT};
  83.                                  
  84. const uint16_t GPIO_PIN[LEDn] = {LED3_PIN, LED4_PIN,
  85.                                  LED5_PIN, LED6_PIN};
  86.                                  
  87. const uint32_t GPIO_CLK[LEDn] = {LED3_GPIO_CLK, LED4_GPIO_CLK,
  88.                                  LED5_GPIO_CLK, LED6_GPIO_CLK};
  89.  
  90. GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT};
  91.  
  92. const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN};
  93.  
  94. const uint32_t BUTTON_CLK[BUTTONn] = {USER_BUTTON_GPIO_CLK};
  95.  
  96. const uint16_t BUTTON_EXTI_LINE[BUTTONn] = {USER_BUTTON_EXTI_LINE};
  97.  
  98. const uint8_t BUTTON_PORT_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PORT_SOURCE};
  99.                                  
  100. const uint8_t BUTTON_PIN_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PIN_SOURCE};
  101.  
  102. const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
  103.  
  104. /**
  105.   * @}
  106.   */
  107.  
  108.  
  109. /** @defgroup STM32F072B_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes
  110.   * @{
  111.   */
  112.  
  113. /**
  114.   * @}
  115.   */
  116.  
  117. /** @defgroup STM32F072B_DISCOVERY_LOW_LEVEL_Private_Functions
  118.   * @{
  119.   */
  120.  
  121. /**
  122.   * @brief  Configures LED GPIO.
  123.   * @param  Led: Specifies the Led to be configured.
  124.   *          This parameter can be one of following parameters:
  125.   *            @arg LED3
  126.   *            @arg LED4
  127.   *            @arg LED5
  128.   *            @arg LED6
  129.   * @retval None
  130.   */
  131. void STM_EVAL_LEDInit(Led_TypeDef Led)
  132. {
  133.   GPIO_InitTypeDef  GPIO_InitStructure;
  134.  
  135.   /* Enable the GPIO_LED Clock */
  136.   RCC_AHBPeriphClockCmd(GPIO_CLK[Led], ENABLE);
  137.  
  138.   /* Configure the GPIO_LED pin */
  139.   GPIO_InitStructure.GPIO_Pin = GPIO_PIN[Led];
  140.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  141.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  142.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  143.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  144.   GPIO_Init(GPIO_PORT[Led], &GPIO_InitStructure);
  145. }
  146.  
  147. /**
  148.   * @brief  Turns selected LED On.
  149.   * @param  Led: Specifies the Led to be set on.
  150.   *          This parameter can be one of following parameters:
  151.   *            @arg LED6
  152.   *            @arg LED3
  153.   *            @arg LED4
  154.   *            @arg LED5  
  155.   * @retval None
  156.   */
  157. void STM_EVAL_LEDOn(Led_TypeDef Led)
  158. {
  159.   GPIO_PORT[Led]->BSRR = GPIO_PIN[Led];
  160. }
  161.  
  162. /**
  163.   * @brief  Turns selected LED Off.
  164.   * @param  Led: Specifies the Led to be set off.
  165.   *          This parameter can be one of following parameters:
  166.   *            @arg LED6
  167.   *            @arg LED3
  168.   *            @arg LED4
  169.   *            @arg LED5
  170.   * @retval None
  171.   */
  172. void STM_EVAL_LEDOff(Led_TypeDef Led)
  173. {
  174.   GPIO_PORT[Led]->BRR = GPIO_PIN[Led];
  175. }
  176.  
  177. /**
  178.   * @brief  Toggles the selected LED.
  179.   * @param  Led: Specifies the Led to be toggled.
  180.   *          This parameter can be one of following parameters:
  181.   *            @arg LED6
  182.   *            @arg LED3
  183.   *            @arg LED4
  184.   *            @arg LED5  
  185.   * @retval None
  186.   */
  187. void STM_EVAL_LEDToggle(Led_TypeDef Led)
  188. {
  189.   GPIO_PORT[Led]->ODR ^= GPIO_PIN[Led];
  190. }
  191.  
  192. /**
  193.   * @brief  Configures Button GPIO and EXTI Line.
  194.   * @param  Button: Specifies the Button to be configured.
  195.   *   This parameter should be: BUTTON_USER
  196.   * @param  Button_Mode: Specifies Button mode.
  197.   *   This parameter can be one of following parameters:  
  198.   *     @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  199.   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  200.   *                            generation capability  
  201.   * @retval None
  202.   */
  203. void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
  204. {
  205.   GPIO_InitTypeDef GPIO_InitStructure;
  206.   EXTI_InitTypeDef EXTI_InitStructure;
  207.   NVIC_InitTypeDef NVIC_InitStructure;
  208.  
  209.   /* Enable the BUTTON Clock */
  210.   RCC_AHBPeriphClockCmd(BUTTON_CLK[Button], ENABLE);
  211.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  212.  
  213.   /* Configure Button pin as input */
  214.   GPIO_InitStructure.GPIO_Pin = BUTTON_PIN[Button];  
  215.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  216.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;  
  217.  
  218.   GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStructure);
  219.  
  220.   if (Button_Mode == BUTTON_MODE_EXTI)
  221.   {
  222.     /* Connect Button EXTI Line to Button GPIO Pin */
  223.     SYSCFG_EXTILineConfig(BUTTON_PORT_SOURCE[Button], BUTTON_PIN_SOURCE[Button]);
  224.  
  225.     /* Configure Button EXTI line */
  226.     EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[Button];
  227.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  228.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  
  229.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  230.     EXTI_Init(&EXTI_InitStructure);
  231.  
  232.     /* Enable and set Button EXTI Interrupt to the lowest priority */
  233.     NVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn[Button];
  234.     NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03;
  235.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  236.  
  237.     NVIC_Init(&NVIC_InitStructure);
  238.  
  239.   }
  240. }
  241.  
  242. /**
  243.   * @brief  Returns the selected Button state.
  244.   * @param  Button: Specifies the Button to be checked.
  245.   *   This parameter should be: BUTTON_USER  
  246.   * @retval The Button GPIO pin value.
  247.   */
  248. uint32_t STM_EVAL_PBGetState(Button_TypeDef Button)
  249. {
  250.   return GPIO_ReadInputDataBit(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  251. }
  252.  
  253. /**
  254.   * @}
  255.   */
  256.  
  257. /**
  258.   * @}
  259.   */
  260.  
  261. /**
  262.   * @}
  263.   */
  264.  
  265. /**
  266.   * @}
  267.   */
  268.  
  269. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement