text: /**
    ******************************************************************************
    * @file    Examples_MIX/PWR/PWR_STOP/Src/main.c
    * @author  MCD Application Team
    * @brief   This sample code shows how to use STM32L0xx PWR HAL API to enter
    *          and exit the STOP with Low power regulator mode.
    ******************************************************************************
    * @attention
    *
    * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
    *
    * Redistribution and use in source and binary forms, with or without modification,
    * are permitted provided that the following conditions are met:
    *   1. Redistributions of source code must retain the above copyright notice,
    *      this list of conditions and the following disclaimer.
    *   2. Redistributions in binary form must reproduce the above copyright notice,
    *      this list of conditions and the following disclaimer in the documentation
    *      and/or other materials provided with the distribution.
    *   3. Neither the name of STMicroelectronics nor the names of its contributors
    *      may be used to endorse or promote products derived from this software
    *      without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    *
    ******************************************************************************
    */
  
  /* Includes ------------------------------------------------------------------*/
  #include "main.h"
  
  /** @addtogroup STM32L0xx_HAL_Examples
    * @{
    */
  
  /** @addtogroup PWR_STOP
    * @{
    */
  
  /* Private typedef -----------------------------------------------------------*/
  /* Private define ------------------------------------------------------------*/
  #define LED_TOGGLE_DELAY         100
  /* Private macro -------------------------------------------------------------*/
  /* Private variables ---------------------------------------------------------*/
  static __IO uint32_t TimingDelay;
  /* Private function prototypes -----------------------------------------------*/
  __STATIC_INLINE void SystemClock_Config(void);
  __STATIC_INLINE void SYSCLKConfig_FromSTOP(void);
  __STATIC_INLINE void SystemPower_Config(void);
  
  /* Private functions ---------------------------------------------------------*/
  
  /**
    * @brief  Main program
    * @param  None
    * @retval None
    */
  int main(void)
  {
    /* STM32L0xx HAL library initialization:
         - Configure the Flash prefetch, Flash preread and Buffer caches
         - Systick timer is configured by default as source of time base, but user 
               can eventually implement his proper time base source (a general purpose 
               timer for example or other time source), keeping in mind that Time base 
               duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
               handled in milliseconds basis.
         - Low Level Initialization
       */
    HAL_Init();
  
    /* Configure LED2 */
    BSP_LED_Init(LED2); 
  
    /* Configure the system clock to 32 MHz */
    SystemClock_Config();
  
    /* Configure the system Power */
    SystemPower_Config();
  
    while (1)
    {
      /* Insert 5 second delay */
      HAL_Delay(5000);
  
      /* Turn off LED2 */
      BSP_LED_Off(LED2); 
  
      /* Enter STOP with Low power regulator mode */
      HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
      
      /* ... STOP with Low power regulator mode ... */
  
      /* Configure system clock after wake-up from STOP: enable and select PLL as system clock source 
         (PLL is disabled in STOP mode) */
      SYSCLKConfig_FromSTOP();  
    }
  }
  
  /**
    * @brief  System Clock Configuration
    *         The system Clock is configured as follow : 
    *            System Clock source            = PLL (HSI)
    *            SYSCLK(Hz)                     = 32000000
    *            HCLK(Hz)                       = 32000000
    *            AHB Prescaler                  = 1
    *            APB1 Prescaler                 = 1
    *            APB2 Prescaler                 = 1
    *            Flash Latency(WS)              = 1
    *            Main regulator output voltage  = Scale1 mode
    * @retval None
    */
  void SystemClock_Config(void)
  {
    RCC_ClkInitTypeDef RCC_ClkInitStruct ={0};
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    
    /* Enable Power Control clock */
    __HAL_RCC_PWR_CLK_ENABLE();
    
    /* The voltage scaling allows optimizing the power consumption when the device is 
       clocked below the maximum system frequency, to update the voltage scaling value 
       regarding system frequency refer to product datasheet.  */
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    
    /* Disable Power Control clock */
    __HAL_RCC_PWR_CLK_DISABLE();
    
    /* Enable HSE Oscillator */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLSource   = RCC_PLLSOURCE_HSI;
    RCC_OscInitStruct.PLL.PLLState    = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLMUL      = RCC_PLL_MUL4;
    RCC_OscInitStruct.PLL.PLLDIV      = RCC_PLL_DIV2;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK)
    {
      /* Initialization Error */
      while(1); 
    }
    
    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
       clocks dividers */
    RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1)!= HAL_OK)
    {
      /* Initialization Error */
      while(1); 
    }
  }
  
  /**
    * @brief  System Power Configuration
    *         The system Power is configured as follow : 
    *            + Regulator in LP mode
    *            + VREFINT OFF, with fast wakeup enabled
    *            + HSI as SysClk after Wake Up
    *            + No IWDG
    *            + Wakeup using EXTI Line (User push-button PC.13)
    * @param  None
    * @retval None
    */
  static void SystemPower_Config(void)
  {
    GPIO_InitTypeDef gpio_initstruct;
  
    /* Enable Power Control clock */
    __HAL_RCC_PWR_CLK_ENABLE();
  
    /* Enable Ultra low power mode */
    LL_PWR_EnableUltraLowPower();
    
    /* Enable the fast wake up from Ultra low power mode */
    LL_PWR_EnableFastWakeUp();
  
    /* Ensure that HSI is wake-up system clock */ 
    __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
  
    /* Set all GPIO in analog state to reduce power consumption,
    except GPIOC to keep user button interrupt enabled */
    LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA |
                            LL_IOP_GRP1_PERIPH_GPIOB |
                            LL_IOP_GRP1_PERIPH_GPIOD |
                            LL_IOP_GRP1_PERIPH_GPIOE |
                            LL_IOP_GRP1_PERIPH_GPIOH);
    /* In HAL, following codes can be called instead of LL:
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOD_CLK_ENABLE();
    __HAL_RCC_GPIOE_CLK_ENABLE();
    __HAL_RCC_GPIOH_CLK_ENABLE();*/
  
    gpio_initstruct.Mode  = GPIO_MODE_ANALOG;
    gpio_initstruct.Speed = GPIO_SPEED_FREQ_HIGH;
    gpio_initstruct.Pull  = GPIO_NOPULL;
    gpio_initstruct.Pin   = GPIO_PIN_All;
  
    HAL_GPIO_Init(GPIOB, &gpio_initstruct);
    HAL_GPIO_Init(GPIOC, &gpio_initstruct);
    HAL_GPIO_Init(GPIOD, &gpio_initstruct);
    HAL_GPIO_Init(GPIOE, &gpio_initstruct);
    HAL_GPIO_Init(GPIOH, &gpio_initstruct);
  
    /* Set all the GPIOA pin to analog except PA.00 (input pin for wakeup) and PA.05 (LED2) */
    gpio_initstruct.Pin   &= (GPIO_PIN_All & ~GPIO_PIN_0 & ~GPIO_PIN_5);
    HAL_GPIO_Init(GPIOA, &gpio_initstruct);
  
    /* Configure PA.00 pin as input with External interrupt */
    gpio_initstruct.Pin = GPIO_PIN_0;
    gpio_initstruct.Pull = GPIO_PULLDOWN;
    gpio_initstruct.Mode = GPIO_MODE_IT_RISING;
    HAL_GPIO_Init(GPIOA, &gpio_initstruct);
  
    /* Enable and set corresponding EXTI Interrupt to the lowest priority */
    NVIC_SetPriority((IRQn_Type)EXTI0_1_IRQn, 0x03);
    HAL_NVIC_EnableIRQ((IRQn_Type)EXTI0_1_IRQn);
  }
  
  /**
    * @brief  Configures system clock after wake-up from STOP: enable PLL
    *         and select PLL as system clock source.
    * @note   RCC LL API is used in this case to allow MCU to wake-up as quick
    *         as possible from STOP mode.
    * @param  None
    * @retval None
    */
  __STATIC_INLINE void SYSCLKConfig_FromSTOP(void)
  {
    /* Customize process using LL interface to improve the performance 
       (wake-up time from STOP quicker in LL than HAL)*/  
    /* HSI configuration and activation */
    LL_RCC_HSI_Enable();
    while(LL_RCC_HSI_IsReady() != 1) {};
  
    /* Main PLL activation */
    LL_RCC_PLL_Enable();
    while(LL_RCC_PLL_IsReady() != 1) 
    {
    };
    
    /* SYSCLK activation on the main PLL */
    LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
    while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) 
    {
    };
  }
  
  /**
    * @brief  This function is executed in case of error occurrence.
    * @param  None
    * @retval None
    */
  void Error_Handler(void)
  {
    /* Suspend tick */
    HAL_SuspendTick();
    
    /* Turn LED2 on */
    BSP_LED_On(LED2);
    while (1)
    {
    }
  }
  
  /**
    * @brief SYSTICK callback
    * @param None
    * @retval None
    */
  void HAL_SYSTICK_Callback(void)
  {
    HAL_IncTick();
  
    if (TimingDelay != 0)
    {
      TimingDelay--;
    }
    else
    {
      /* Toggle LED2 */
      BSP_LED_Toggle(LED2);
      TimingDelay = LED_TOGGLE_DELAY;
    }
  }
  
  
  /**
    * @brief EXTI line detection callbacks
    * @param GPIO_Pin: Specifies the pins connected EXTI line
    * @retval None
    */
  void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  {
    if (GPIO_Pin == KEY_BUTTON_PIN)
    {
      /* Reconfigure LED2 */
      BSP_LED_Init(LED2); 
      /* Switch on LED2 */
      BSP_LED_On(LED2);
    }
  }
  
  #ifdef  USE_FULL_ASSERT
  
  /**
    * @brief  Reports the name of the source file and the source line number
    *         where the assert_param error has occurred.
    * @param  file: pointer to the source file name
    * @param  line: assert_param error line source number
    * @retval None
    */
  void assert_failed(uint8_t *file, uint32_t line)
  {
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
    /* Infinite loop */
    while (1)
    {
    }
  }
  #endif
  
  /**
    * @}
    */
  
  /**
    * @}
    */
  
  /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  