Advertisement
Guest User

STM32 official example GPIO_EXTI for Nucleo-F767ZI

a guest
Jan 19th, 2017
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.81 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    GPIO/GPIO_EXTI/Src/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.1
  6.   * @date    23-September-2016
  7.   * @brief   This example describes how to configure and use GPIOs through
  8.   *          the STM32F7xx HAL API.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38.  
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41.  
  42. /** @addtogroup STM32F7xx_HAL_Examples
  43.   * @{
  44.   */
  45.  
  46. /** @addtogroup GPIO_EXTI
  47.   * @{
  48.   */
  49.  
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. /* Private function prototypes -----------------------------------------------*/
  55. static void SystemClock_Config(void);
  56. static void EXTI15_10_IRQHandler_Config(void);
  57. /* Private functions ---------------------------------------------------------*/
  58.  
  59. /**
  60.   * @brief  Main program
  61.   * @param  None
  62.   * @retval None
  63.   */
  64. int main(void)
  65. {
  66.   /* STM32F7xx HAL library initialization:
  67.        - Configure the Flash prefetch
  68.        - Systick timer is configured by default as source of time base, but user
  69.          can eventually implement his proper time base source (a general purpose
  70.          timer for example or other time source), keeping in mind that Time base
  71.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  72.          handled in milliseconds basis.
  73.        - Set NVIC Group Priority to 4
  74.        - Low Level Initialization
  75.      */
  76.   HAL_Init();
  77.  
  78.   /* Configure the system clock to 216 MHz */
  79.   SystemClock_Config();
  80.  
  81.   /* -1- Initialize LEDs mounted on STM32F767ZI-Nucleo_144 board */
  82.   BSP_LED_Init(LED1);
  83.  
  84.   /* -2- Configure EXTI_Line15_10 (connected to PC.13 pin) in interrupt mode */
  85.   EXTI15_10_IRQHandler_Config();
  86.  
  87.   /* Infinite loop */
  88.   int num = 0;
  89.   while (1)
  90.   {
  91.       num ++;
  92.   }
  93. }
  94.  
  95. /**
  96.   * @brief  System Clock Configuration
  97.   *         The system Clock is configured as follow :
  98.   *            System Clock source            = PLL (HSE)
  99.   *            SYSCLK(Hz)                     = 216000000
  100.   *            HCLK(Hz)                       = 216000000
  101.   *            AHB Prescaler                  = 1
  102.   *            APB1 Prescaler                 = 4
  103.   *            APB2 Prescaler                 = 2
  104.   *            HSE Frequency(Hz)              = 8000000
  105.   *            PLL_M                          = 8
  106.   *            PLL_N                          = 432
  107.   *            PLL_P                          = 2
  108.   *            PLL_Q                          = 9
  109.   *            PLL_R                          = 7
  110.   *            VDD(V)                         = 3.3
  111.   *            Main regulator output voltage  = Scale1 mode
  112.   *            Flash Latency(WS)              = 7
  113.   * @param  None
  114.   * @retval None
  115.   */
  116. static void SystemClock_Config(void)
  117. {
  118.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  119.   RCC_OscInitTypeDef RCC_OscInitStruct;
  120.  
  121.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  122.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  123.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  124.   RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  125.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  126.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  127.   RCC_OscInitStruct.PLL.PLLM = 8;
  128.   RCC_OscInitStruct.PLL.PLLN = 432;  
  129.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  130.   RCC_OscInitStruct.PLL.PLLQ = 9;
  131.   RCC_OscInitStruct.PLL.PLLR = 7;
  132.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  133.   {
  134.     while(1) {};
  135.   }
  136.  
  137.   /* Activate the OverDrive to reach the 216 Mhz Frequency */
  138.   if(HAL_PWREx_EnableOverDrive() != HAL_OK)
  139.   {
  140.     while(1) {};
  141.   }
  142.  
  143.  
  144.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  145.      clocks dividers */
  146.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  147.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  148.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  149.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  150.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  151.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  152.   {
  153.     while(1) {};
  154.   }
  155. }
  156. /**
  157.   * @brief  Configures EXTI line 15_10 (connected to PC.13 pin) in interrupt mode
  158.   * @param  None
  159.   * @retval None
  160.   */
  161. static void EXTI15_10_IRQHandler_Config(void)
  162. {
  163.   GPIO_InitTypeDef   GPIO_InitStructure;
  164.  
  165.   /* Enable GPIOC clock */
  166.   __HAL_RCC_GPIOC_CLK_ENABLE();
  167.  
  168.   /* Configure PC.13 pin as input floating */
  169.   GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
  170.   GPIO_InitStructure.Pull = GPIO_NOPULL;
  171.   GPIO_InitStructure.Pin = GPIO_PIN_13;
  172.   HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
  173.  
  174.   /* Enable and set EXTI line 15_10 Interrupt to the lowest priority */
  175.   HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0);
  176.   HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  177. }
  178.  
  179. /**
  180.   * @brief EXTI line detection callbacks
  181.   * @param GPIO_Pin: Specifies the pins connected EXTI line
  182.   * @retval None
  183.   */
  184. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  185. {
  186.   if (GPIO_Pin == GPIO_PIN_13)
  187.   {
  188.     /* Toggle LED1 */
  189.     BSP_LED_Toggle(LED1);
  190.   }
  191. }
  192.  
  193. #ifdef  USE_FULL_ASSERT
  194.  
  195. /**
  196.   * @brief  Reports the name of the source file and the source line number
  197.   *         where the assert_param error has occurred.
  198.   * @param  file: pointer to the source file name
  199.   * @param  line: assert_param error line source number
  200.   * @retval None
  201.   */
  202. void assert_failed(uint8_t *file, uint32_t line)
  203. {
  204.   /* User can add his own implementation to report the file name and line number,
  205.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  206.  
  207.   /* Infinite loop */
  208.   while (1)
  209.   {
  210.   }
  211. }
  212. #endif
  213.  
  214. /**
  215.   * @}
  216.   */
  217.  
  218. /**
  219.   * @}
  220.   */
  221.  
  222. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement