text: /**
    ******************************************************************************
    * @file    PWR/STOP/main.c 
    * @author  MCD Application Team
    * @version V1.2.2
    * @date    17-November-2021
    * @brief   Main program body
    ******************************************************************************
    * @attention
    *
    * Copyright (c) 2015 STMicroelectronics.
    * All rights reserved.
    *
    * This software is licensed under terms that can be found in the LICENSE file
    * in the root directory of this software component.
    * If no LICENSE file comes with this software, it is provided AS-IS.
    *
    ******************************************************************************
    */
  
  /* Includes ------------------------------------------------------------------*/
  #include "stm32l1xx.h"
  
  #ifdef USE_STM32L152D_EVAL
  #include "stm32l152d_eval.h"
  #include "stm32l152d_eval_glass_lcd.h"
  #else
  #include "stm32l152_eval.h"
  #include "stm32l152_eval_glass_lcd.h"
  #endif
  
  
  /** @addtogroup STM32L1xx_StdPeriph_Examples
    * @{
    */
  
  /** @addtogroup PWR_STOP
    * @{
    */ 
  
  /* Private typedef -----------------------------------------------------------*/
  /* Private define ------------------------------------------------------------*/
  #define SCROLL_SPEED  5000 /* Low value gives higher speed */
  #define SCROLL_NUM    1
  
  /* Private macro -------------------------------------------------------------*/
  /* Private variables ---------------------------------------------------------*/
  #ifdef USE_STM32L152D_EVAL
  uint8_t LCD_String[7]= "STM32L ";
  #else
  uint8_t LCD_String[8]= " STM32L ";
  #endif
  
  /* Private function prototypes -----------------------------------------------*/
  void RTC_Config(void);
  void SysTickConfig(void);
  
  /* Private functions ---------------------------------------------------------*/
  
  /**
    * @brief  Main program
    * @param  None
    * @retval None
    */
  int main(void)
  {
    /*!< At this stage the microcontroller clock setting is already configured, 
         this is done through SystemInit() function which is called from startup
         file (startup_stm32l1xx_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32l1xx.c file
       */     
         
    /* RTC configuration -------------------------------------------------------*/
    RTC_Config();
  
    /* Configure the SysTick to generate an interrupt each 250 ms */
    SysTickConfig();
    
    /* LCD GLASS Initialization */
    LCD_GLASS_Init();
  
    /* Clear the LCD GLASS */
    LCD_GLASS_Clear();
  
    /* Configure STM32L152-EVAL LED1 and LED2 as Output push-pull */
    STM_EVAL_LEDInit(LED1);
    STM_EVAL_LEDInit(LED2);
  
    while (1)
    {
      /* Display " STM32L " string on LCD glass in scrolling mode */
      LCD_GLASS_ScrollString(LCD_String, SCROLL_NUM, SCROLL_SPEED);  
  
      /* Display String on the LCD */
  #ifdef USE_STM32L152D_EVAL
      LCD_GLASS_DisplayString("STOPMOD");
  #else
      LCD_GLASS_DisplayString("STOPMODE");
  #endif
     
  
      /* Enable Wakeup Counter */
      RTC_WakeUpCmd(ENABLE);
  
      /* Enter Stop Mode */
      PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
  
      /* Enable Wakeup Counter */
      RTC_WakeUpCmd(DISABLE);
      
      /* After wake-up from STOP reconfigure the system clock */
      /* Enable HSE */
      RCC_HSEConfig(RCC_HSE_ON);
  
      /* Wait till HSE is ready */
      while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
      {}
      
      /* Enable PLL */
      RCC_PLLCmd(ENABLE);
      
      /* Wait till PLL is ready */
      while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
      {}
      
      /* Select PLL as system clock source */
      RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
      
      /* Wait till PLL is used as system clock source */
      while (RCC_GetSYSCLKSource() != 0x0C)
      {}
    }
  }
  
  /**
    * @brief  Configures the RTC Wakeup.
    * @param  None
    * @retval None
    */
  void RTC_Config(void)
  {
    NVIC_InitTypeDef  NVIC_InitStructure;
    EXTI_InitTypeDef  EXTI_InitStructure;
  
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
    /*!< Allow access to RTC */
    PWR_RTCAccessCmd(ENABLE);
  
    /*!< Reset RTC Domain */
    RCC_RTCResetCmd(ENABLE);
    RCC_RTCResetCmd(DISABLE);
  
    /*!< LSE Enable */
    RCC_LSEConfig(RCC_LSE_ON);
  
    /*!< Wait till LSE is ready */
    while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
    {}
  
    /*!< LCD Clock Source Selection */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  
    /*!< Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);
  
    /*!< Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();
  
    /* EXTI configuration *******************************************************/
    EXTI_ClearITPendingBit(EXTI_Line20);
    EXTI_InitStructure.EXTI_Line = EXTI_Line20;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    
    /* Enable the RTC Wakeup Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
   
    /* RTC Wakeup Interrupt Generation: Clock Source: RTCDiv_16, Wakeup Time Base: 4s */
    RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
    RTC_SetWakeUpCounter(0x1FFF);
  
    /* Enable the Wakeup Interrupt */
    RTC_ITConfig(RTC_IT_WUT, ENABLE);  
  }
  
  /**
    * @brief  Configures the SysTick to generate an interrupt each 250 ms.
    * @param  None
    * @retval None
    */
  void SysTickConfig(void)
  {
    /* SysTick interrupt each 250 ms with SysTick Clock equal to 4MHz */
    if (SysTick_Config((SystemCoreClock/8) / 4))
    { 
      /* Capture error */ 
      while (1);
    }
  
    /* Select AHB clock(HCLK) divided by 8 as SysTick clock source */
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
  
    /* Set SysTick Preemption Priority to 1 */
    NVIC_SetPriority(SysTick_IRQn, 0x04);
  }
  
  #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
  
  /**
    * @}
    */ 
  
  /**
    * @}
    */ 
  
  