基于STM32F030的电子开关系统

在嵌入式系统中经常需要开发长按开关机的功能,并且能够应对恶劣环境导致的电容充放电

异常情况,并且能够应对主控芯片跑飞死机的情况。所以可以使用一个小单片机独立成模块,

专门负责长按开关机,休眠低功耗,充放电管理和安全检查等工作,从而决定是否能够正常开机。


芯片手册上提到的几种退出待机模式的事件,

1.NRST引脚复位,

2.看门狗复位,

3.WKUP(PA0)引脚产生上升沿电平,

4.RTC时钟产生alarm,


STM32的低功耗模式也有,
1.睡眠模式(CM3内核停止,外设仍然运行,
2.停止模式(所有时钟都停止,
3.待机(standby)模式 (1.8v内核电源关闭),
从待机模式唤醒后的代码执行等同于复位后的执行进入standby模式后,只能有Wake-Up脚和RTC唤醒,特别是唤醒后,程序将从最开始运行,也相当于软件复位

void PWR_EnterSTANDBYMode(void)
{
     // Clear Wake-up flag 
     PWR->CR |= CR_CWUF_Set;
     // Select STANDBY mode 
     PWR->CR |= CR_PDDS_Set;
     // Set SLEEPDEEP bit of Cortex System Control Register 
     *(vu32 *) SCB_SysCtrl |= SysCtrl_SLEEPDEEP_Set;
    // Request Wait For Interrupt 
    __WFI();
}


void PWR_WakeUpPinCmd(FunctionalState NewState)
{
    // Check the parameters 
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    *(vu32 *) CSR_EWUP_BB = (u32)NewState;
}


void LowPower_Init(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    // Enable WakeUp pin 
    PWR_WakeUpPinCmd(ENABLE);
    // Enable Clock Security System(CSS) 
    RCC_ClockSecuritySystemCmd(ENABLE);
}

main.c从唤醒到启动,

#include "main.h"


//中断标志位
static __IO BOOLEAN is_key_trigger                    = FALSE;
static __IO BOOLEAN is_usb_pullout                    = FALSE;

static __IO uint8_t key_pressed_debonce_cnt           = 0;
static __IO uint8_t usb_pullout_debonce_cnt           = 0;

static __IO BOOLEAN is_mainmcu_crash                  = FALSE;


/* Private functions ---------------------------------------------------------*/
void SystemClock_Config(void);



/**
返回TRUE表示正常工作模式
返回FALSE表示待机模式
*/
static BOOLEAN Check_System_StartUp(void)
{
		
		__HAL_RCC_PWR_CLK_ENABLE();
		
	  /*wakeup from StandBy mode*/
		if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
		{
				__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
				
				return TRUE;
		}		  
		
		return FALSE;
}


//flash只读保护
static void Flash_Read_Protect(void)
{
	
	  FLASH_OBProgramInitTypeDef OptionsBytesStruct;
	
	  /* Unlock the Flash to enable the flash control register access *************/ 
	  HAL_FLASH_Unlock();

	  /* Unlock the Options Bytes *************************************************/
	  HAL_FLASH_OB_Unlock();

	  /* Get pages write protection status ****************************************/
	  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);	 
	
	
	  if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
	  {
			 OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
			
			 if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
			 {
				 /* Error occurred while options bytes programming. **********************/
				 while (1);
			 }

			 /* Generate System Reset to load the new option byte values ***************/
			 HAL_FLASH_OB_Launch();			  
	   }
		
		
	   /* Lock the Options Bytes *************************************************/
      HAL_FLASH_OB_Lock();
}





/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
	  #define MAX_CHECK_CNT         (3)
	  #define DELAY_TIME            (20)
	  #define ON_LONG_PRESS_TIME    (10)
	  #define OFF_LONG_PRESS_TIME   (20)
	  #define EQ_MAX_REFRESH_CNT    (150)
	
	  BOOLEAN is_wakeup           = FALSE;
	  uint8_t check_cnt           = 0;
	  BOOLEAN is_usb_wakeup       = FALSE;
	  uint8_t eq                  = 0;
	  BOOLEAN is_usb_insert       = FALSE;
	  BOOLEAN is_usb_power_down   = FALSE;
	  uint8_t eq_led_refresh_cnt  = 0;
	  BOOLEAN is_led_all_off      = FALSE;

	  HAL_Init();

	  /* Configure the system clock to 48 MHz */
	  SystemClock_Config();


#if 1	
	   /**flash protect*/
	   Flash_Read_Protect();
#endif

	  /**check wakeup flag*/
    is_wakeup = Check_System_StartUp();
		
		
		/**
		增加纯软件延时防止误触发 
		*/
		if(is_wakeup)
		{
			 Key_Init();
			
			 USB_FeedBack_Init();
			
			 //希望保持高电平1S 
			 check_cnt = 0;
			 while(check_cnt++ < ON_LONG_PRESS_TIME)
			 {
				  HAL_Delay(100);
				 
				  if(!Key_PressKey_Read()){
						 is_wakeup = FALSE;
						 break;
				  }
					
					if(USB_FeedBack_Check()){
						 //关闭按键中断,防止干扰到USB中断
						 Key_IRQ_Disable();
						 break;
					}
			 }

			 //清除可能产生的中断标志
       is_key_trigger = FALSE;			 
		}
		
		
		
		/**正常工作模式初始化*/
		if(is_wakeup)
		{
			  //LED灯初始化
			  LED_Init();

			  //电量采集初始化
			  MX_ADC_Init();

			  //充电反馈信号
			  Charge_FeedBack_Init();
        
			
			  //是否是USB插入
			  check_cnt = 0;
			  while(check_cnt++ < MAX_CHECK_CNT)
				{
					 if(USB_FeedBack_Check()){
												 
						   is_usb_wakeup = TRUE;
						 
						   break;
					 }
					 
					 HAL_Delay(DELAY_TIME << 1);
				}
		
				
	
			  //按键按下
        if(!is_usb_wakeup)
				{						
					 //按键初始化
			     //Key_Init();
					
			     //主MCU反馈初始化
			     MainMCU_FeedBack_Init();
					
           //电源使能初始化					
			     PWR_Init();
					 
					 //电源使能
					 PWR_ENABLE();
					
					 //启动灯效
					 LED_Start_Action();
					
					 //初始获取电量
					 eq = Get_Battery_EQ();
					
					 //检测主MCU反馈
					 if(!MainMCU_FeedBack_Check()){
						 
						   if(eq >= LSUC_LOW_POWER)
							 {
								   is_mainmcu_crash = TRUE;
							 }
							 else
							 {  
								  //电量可能不够,大概率是不足矣开机
								  //如果不足以启动主MCU则进入休眠状态
								  PWR_DISABLE();
								 
								  is_wakeup = FALSE;
							 }
					 }
					 else{
						   LED_Show_EQ_Percent(eq);
					 } 
				}
				else{
					 //按键切换初始化
					 /*Key_Switch_Init();*/
				}
		}
		/**低功耗模式初始化**/
		else{

		}
		
		
		while(TRUE)
		{
			  if(!is_wakeup)
				{			  

					 /**进入待机模式*/
					 StandbyMode_Measure();
				}
				else
				{
				   /**正常工作模式**/
           if(is_key_trigger && 
						  key_pressed_debonce_cnt++ >= 1 &&
						  (Key_PressKey_Read() /*|| Key_Switch_PressKey_Read()*/)){
						 
						    is_key_trigger = FALSE;
						    is_usb_insert  = FALSE;
	
								
						    //检测USB使能
						    check_cnt      = 0;
								while(check_cnt++ < MAX_CHECK_CNT)
								{
									 if(USB_FeedBack_Check()){
											 is_usb_insert = TRUE;
											 break;
									 }
									 
									 HAL_Delay(DELAY_TIME << 1);
								}               
                
								
								
								//USB插入
								if(is_usb_insert)
								{
									 Key_IRQ_Disable();
									
									 //此时使能USB拔出使能
									 USB_IRQ_Enable();
									
									 //按键启动后插入USB
									 if(!is_usb_wakeup){
										 										 
										   is_usb_wakeup = TRUE;
									 }
									 //USB启动后按键按下
									 else{
											 //电源打开
										   if(!MainMCU_FeedBack_Check())
											 {
												 
											    PWR_ENABLE();
											    LED_Start_Action();												  
											 }
											 //电源关闭
											 else{
												 
													 PWR_DISABLE();										   
											 }
									 }
								}
								//按键按下
								else{
																		
									  //希望保持高电平2S
										check_cnt = 0;
										while(check_cnt++ < OFF_LONG_PRESS_TIME)
										{
												if(!Key_PressKey_Read()){
													 break;
												}
												
												HAL_Delay(100);
										}
										
										if(check_cnt < OFF_LONG_PRESS_TIME)
										{
											 continue;
										}
	
																				
										//特殊情况直接进入standby模式
									  if(is_mainmcu_crash)
										{
											  LED_ALL_OFF();
												PWR_DISABLE();
											  is_wakeup = FALSE;
												continue;
										}
										else
										{
									     //电源关闭并进入standby模式
										   if(MainMCU_FeedBack_Check()){
																									 
													 LED_ALL_OFF();
													 PWR_DISABLE();
												 
												   is_led_all_off = TRUE;
										   }	
										}								 
								}		
					 }
					 
					 
					 
					 
					 
					 //按键抬起来时进入standby模式 
           if(is_key_trigger && 
						  key_pressed_debonce_cnt++ >= 1 &&
						  !Key_PressKey_Read()){
								
								is_key_trigger = FALSE;
																
                //不能同时发生
								if(USB_FeedBack_Check())
								{									
									 continue;
								}

								
								//只针对按键电源关闭并进入standby模式的情况进入休眠模式 
								if(!is_mainmcu_crash &&
									  is_led_all_off){
									 	is_wakeup = FALSE;
								    continue;
								}
					 }								
					 
					 
					 
					 
					 if(is_usb_pullout &&
						  usb_pullout_debonce_cnt++ >= 1){
						 
						    is_usb_pullout           = FALSE;
								usb_pullout_debonce_cnt  = 0;
						    is_usb_power_down        = FALSE;
								
							  //检测USB使能
						    check_cnt      = 0;
								while(check_cnt++ < MAX_CHECK_CNT)
								{
									 if(!USB_FeedBack_Check()){
											 is_usb_power_down = TRUE;
											 break;
									 }
									 
									 HAL_Delay(DELAY_TIME << 1);
								} 

							
                if(is_usb_power_down)
                {
									 Key_IRQ_Enable();
									
									 //此时关闭USB拔出使能
									 USB_IRQ_Disable();
									
									 //先执行这里
									 //按键启动后插上USB再拔出USB
									 if(is_usb_wakeup && is_usb_insert){
										 										 
										   is_usb_wakeup = FALSE;
									 }
									 //USB启动后拔出USB
									 else{
										 
										  LED_ALL_OFF();
											is_wakeup = FALSE;
											continue;										   
									 }
								} 									
					 }

					 
					 					 
					 //获取当前电量
					 eq                  = Get_Battery_EQ();
					 

					 //低电量时应该自动关机,防止主MCU比自己先断电
					 if(eq < LSUC_LOW_POWER){
						 
								LED_ALL_OFF();
						    PWR_DISABLE();
						 
								is_wakeup = FALSE;
								continue;						    
					 }
					 
					 
					 //usb唤醒就显示充电
           if(is_usb_wakeup){
						 
						    if(!is_led_all_off)
						       LED_Show_USB_Charge(eq,DELAY_TIME); 
					 }
					 //按键唤醒就显示电量
           else{
						 
						 
						    if(eq_led_refresh_cnt++ >= EQ_MAX_REFRESH_CNT)
								{
									  eq_led_refresh_cnt = 0;
									
									  if(!is_led_all_off)
									    LED_Show_EQ_Percent(eq);
								}
					 }						 
				}
				
				//周期轮询
				HAL_Delay(DELAY_TIME);
		}
}




/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow : 
  *            System Clock source            = PLL (HSI/2)
  *            SYSCLK(Hz)                     = 48000000
  *            HCLK(Hz)                       = 48000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 1
  *            HSI Frequency(Hz)              = 8000000
  *            PREDIV                         = 1
  *            PLLMUL                         = 12
  *            Flash Latency(WS)              = 1
  * @param  None
  * @retval None
  */
void SystemClock_Config(void)
{
		RCC_ClkInitTypeDef RCC_ClkInitStruct;
		RCC_OscInitTypeDef RCC_OscInitStruct;
		
	  /*STM32F030??PF0 PF1时钟关闭*/
	  //RCC->CR &= ~((uint32_t)RCC_CR_HSEON);
	
	  //已经使用内部时钟 
		/* No HSE Oscillator on Nucleo, Activate PLL with HSI/2 as source */
		RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_NONE;
		RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
		RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
		RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
		RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
		if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK)
		{
			// Initialization Error 
			Error_Handler();
		}

		
		// Select PLL as system clock source and configure the HCLK, PCLK1 clocks dividers 
		RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1);
		RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
		RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
		RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
		if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1)!= HAL_OK)
		{
			// Initialization Error 
			Error_Handler();
		}
}




/**
  * @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_PIN /*|| GPIO_Pin == KEY_SWITCH_PIN*/)
	  {  
         is_key_trigger          = TRUE;
			   key_pressed_debonce_cnt = 0;
	  }
		else if(GPIO_Pin == USB_PIN)
		{
			   is_usb_pullout          = TRUE;
			   usb_pullout_debonce_cnt = 0;
		}
}





#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(char* 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

stm32f0xx_lp_modes.c待机模式控制,

/**
  ******************************************************************************
  * @file    PWR/PWR_CurrentConsumption/stm32f0xx_lp_modes.c
  * @author  MCD Application Team
  * @brief   This file provides firmware functions to manage the following
  *          functionalities of the STM32F0xx Low Power Modes:
  *           - Sleep Mode
  *           - STOP mode with RTC
  *           - STANDBY mode without RTC
  *           - STANDBY mode with RTC 
  ******************************************************************************
  * @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 "stm32f0xx_lp_modes.h"

/** @addtogroup STM32F0xx_HAL_Examples
  * @{
  */

/** @addtogroup PWR_CurrentConsumption
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* RTC handler declaration */
RTC_HandleTypeDef RTCHandle;

/* Private function prototypes -----------------------------------------------*/
static void SYSCLKConfig_STOP(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  This function configures the system to enter Sleep mode for
  *         current consumption measurement purpose.
  *         Sleep Mode
  *         ==========
  *            - System Running at PLL (48 MHz)
  *            - Flash 1 wait state
  *            - Prefetch ON
  *            - Code running from Internal FLASH
  *            - All peripherals disabled.
  *            - Wakeup using EXTI Line (User push-button PC.13 pin)
  * @param  None
  * @retval None
  */
void SleepMode_Measure(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* Configure all GPIO as analog to reduce current consumption on non used IOs */
  /* Enable GPIOs clock */
  /* Warning : Reconfiguring all GPIO will close the connexion with the debugger */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOF_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(GPIOA, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

  /* Disable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_DISABLE();
  __HAL_RCC_GPIOB_CLK_DISABLE();
  __HAL_RCC_GPIOC_CLK_DISABLE();
  __HAL_RCC_GPIOD_CLK_DISABLE();
  __HAL_RCC_GPIOF_CLK_DISABLE();

  /* Configure User push-button as external interrupt generator */
  //BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

  /*Suspend Tick increment to prevent wakeup by Systick interrupt. 
    Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
  HAL_SuspendTick();

  /* Request to enter SLEEP mode */
  HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

  /* Resume Tick interrupt if disabled prior to sleep mode entry*/
  HAL_ResumeTick();

  /* Initialize LED2 on the board */
  //BSP_LED_Init(LED2);
  
  /* Turn LED2 On */
  //BSP_LED_On(LED2);
  
  /* Inserted Delay */
  HAL_Delay(200);
  
}

/**
  * @brief  This function configures the system to enter Stop mode with RTC 
  *         clocked by LSE or LSI for current consumption measurement purpose.
  *         STOP Mode with RTC clocked by LSE/LSI
  *         =====================================   
  *           - RTC Clocked by LSE or LSI
  *           - Regulator in LP mode
  *           - HSI, HSE OFF and LSI OFF if not used as RTC Clock source
  *           - No IWDG
  *           - Wakeup using EXTI Line (User push-button PC.13)
  * @param  None
  * @retval None
  */
void StopMode_Measure(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  
  /* Configure all GPIO as analog to reduce current consumption on non used IOs */
  /* Warning : Reconfiguring all GPIO will close the connexion with the debugger */
  /* Enable GPIOs clock */

  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOF_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(GPIOA, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);  
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);


  /* Disable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_DISABLE();
  __HAL_RCC_GPIOB_CLK_DISABLE();
  __HAL_RCC_GPIOC_CLK_DISABLE();
  __HAL_RCC_GPIOD_CLK_DISABLE();
  __HAL_RCC_GPIOF_CLK_DISABLE();



    /* Configure User Button */
  //BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

  /* Enter Stop Mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* Configures system clock after wake-up from STOP: enable HSI and PLL with HSI as source*/
  SYSCLKConfig_STOP();
  
  /* Initialize LED2 on the board */
  //BSP_LED_Init(LED2);
  
  /* Turn LED2 On */
  //BSP_LED_On(LED2);
  
  /* Inserted Delay */
  HAL_Delay(200);

  
}



/**
  * @brief  This function configures the system to enter Standby mode for
  *         current consumption measurement purpose.
  *         STANDBY Mode
  *         ============
  *           - RTC OFF
  *           - IWDG and LSI OFF
  *           - Wakeup using WakeUp Pin PWR_WAKEUP_PIN2 connected to PC.13
  * @param  None
  * @retval None
  */
void StandbyMode_Measure(void)
{
  /* Enable Power Clock*/
  __HAL_RCC_PWR_CLK_ENABLE();

  /* Allow access to Backup */
  HAL_PWR_EnableBkUpAccess();

  /* Reset RTC Domain */
  __HAL_RCC_BACKUPRESET_FORCE();
  __HAL_RCC_BACKUPRESET_RELEASE();
  
  /* The Following Wakeup sequence is highly recommended prior to each Standby mode entry
     mainly  when using more than one wakeup source this is to not miss any wakeup event.
       - Disable all used wakeup sources,
       - Clear all related wakeup flags, 
       - Re-enable all used wakeup sources,
       - Enter the Standby mode.
  */
  
  /*#### Disable all used wakeup sources: WKUP pin ###########################*/
  HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);//PA0
  
  /*#### Clear all related wakeup flags ######################################*/
  /* Clear PWR wake up Flag */
  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  
  /* Enable WKUP pin */
  HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);

  /* Request to enter STANDBY mode */
  HAL_PWR_EnterSTANDBYMode();
}



/**
  * @brief  Configures system clock after wake-up from STOP: enable HSE, PLL
  *         and select PLL as system clock source.
  * @param  None
  * @retval None
  */
static void SYSCLKConfig_STOP(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
  uint32_t pFLatency = 0;

  /* Get the Oscillators configuration according to the internal RCC registers */
  HAL_RCC_GetOscConfig(&RCC_OscInitStruct);

  /* Activate PLL with HSI as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_NONE;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /* Get the Clocks configuration according to the internal RCC registers */
  HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);

  /* Select PLL as system clock source and configure the HCLK and PCLK1 clocks dividers */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值