367 lines
11 KiB
C
367 lines
11 KiB
C
/**
|
|
******************************************************************************
|
|
* @file ft32f0xx_pwr.c
|
|
* @author FMD AE
|
|
* @brief This file provides firmware functions to manage the following
|
|
* functionalities of the Power Controller (PWR) peripheral:
|
|
* + Backup Domain Access
|
|
* + PVD configuration
|
|
* + WakeUp pins configuration
|
|
* + Low Power modes configuration
|
|
* + Flags management
|
|
* @version V1.0.0
|
|
* @data 2021-07-01
|
|
******************************************************************************
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "ft32f0xx_pwr.h"
|
|
#include "ft32f0xx_rcc.h"
|
|
|
|
|
|
/* ------------------ PWR registers bit mask ------------------------ */
|
|
|
|
/* CR register bit mask */
|
|
#define CR_DS_MASK ((uint32_t)0xFFFFFFFC)
|
|
#define CR_PLS_MASK ((uint32_t)0xFFFFFD1F)
|
|
|
|
|
|
/**
|
|
* @brief Deinitializes the PWR peripheral registers to their default reset values.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void PWR_DeInit(void)
|
|
{
|
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);
|
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);
|
|
}
|
|
|
|
/**
|
|
* @brief Enables or disables access to the Backup domain registers.
|
|
* @note If the HSE divided by 32 is used as the RTC clock, the
|
|
* Backup Domain Access should be kept enabled.
|
|
* @param NewState: new state of the access to the Backup domain registers.
|
|
* This parameter can be: ENABLE or DISABLE.
|
|
* @retval None
|
|
*/
|
|
void PWR_BackupAccessCmd(FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
/* Enable the Backup Domain Access */
|
|
PWR->CR |= PWR_CR_DBP;
|
|
}
|
|
else
|
|
{
|
|
/* Disable the Backup Domain Access */
|
|
PWR->CR &= (uint32_t)~((uint32_t)PWR_CR_DBP);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
|
|
* @param PWR_PVDLevel: specifies the PVD detection level
|
|
* This parameter can be one of the following values:
|
|
* @arg PWR_PVDLevel_0
|
|
* @arg PWR_PVDLevel_1
|
|
* @arg PWR_PVDLevel_2
|
|
* @arg PWR_PVDLevel_3
|
|
* @arg PWR_PVDLevel_4
|
|
* @arg PWR_PVDLevel_5
|
|
* @arg PWR_PVDLevel_6
|
|
* @arg PWR_PVDLevel_7
|
|
* @arg PWR_PVDLevel_8
|
|
* @arg PWR_PVDLevel_9
|
|
* @arg PWR_PVDLevel_10
|
|
* @arg PWR_PVDLevel_11
|
|
* @arg PWR_PVDLevel_12
|
|
* @arg PWR_PVDLevel_13
|
|
* @arg PWR_PVDLevel_14
|
|
* @arg PWR_PVDLevel_15
|
|
* @note Refer to the electrical characteristics of your device datasheet for
|
|
* more details about the voltage threshold corresponding to each
|
|
* detection level.
|
|
* @retval None
|
|
*/
|
|
void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel)
|
|
{
|
|
uint32_t tmpreg = 0;
|
|
|
|
/* Check the parameters */
|
|
assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel));
|
|
|
|
tmpreg = PWR->CR;
|
|
|
|
/* Clear PLS[7:5] bits PLS3*/
|
|
tmpreg &= CR_PLS_MASK;
|
|
|
|
/* Set PLS[7:5] and PLS3 bits according to PWR_PVDLevel value */
|
|
tmpreg |= PWR_PVDLevel;
|
|
|
|
/* Store the new value */
|
|
PWR->CR = tmpreg;
|
|
}
|
|
|
|
/**
|
|
* @brief Enables or disables the Power Voltage Detector(PVD).
|
|
* @param NewState: new state of the PVD.
|
|
* This parameter can be: ENABLE or DISABLE.
|
|
* @retval None
|
|
*/
|
|
void PWR_PVDCmd(FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
/* Enable the PVD */
|
|
PWR->CR |= PWR_CR_PVDE;
|
|
}
|
|
else
|
|
{
|
|
/* Disable the PVD */
|
|
PWR->CR &= (uint32_t)~((uint32_t)PWR_CR_PVDE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
/**
|
|
* @brief Enables or disables the WakeUp Pin functionality.
|
|
* @param PWR_WakeUpPin: specifies the WakeUpPin.
|
|
* This parameter can be one of the following values
|
|
* @arg PWR_WakeUpPin_1
|
|
* @arg PWR_WakeUpPin_2
|
|
* @arg PWR_WakeUpPin_3
|
|
* @arg PWR_WakeUpPin_4
|
|
* @arg PWR_WakeUpPin_5
|
|
* @arg PWR_WakeUpPin_6
|
|
* @arg PWR_WakeUpPin_7
|
|
* @arg PWR_WakeUpPin_8
|
|
* @param NewState: new state of the WakeUp Pin functionality.
|
|
* This parameter can be: ENABLE or DISABLE.
|
|
* @retval None
|
|
*/
|
|
void PWR_WakeUpPinCmd(uint32_t PWR_WakeUpPin, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_PWR_WAKEUP_PIN(PWR_WakeUpPin));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
/* Enable the EWUPx pin */
|
|
PWR->CSR |= PWR_WakeUpPin;
|
|
}
|
|
else
|
|
{
|
|
/* Disable the EWUPx pin */
|
|
PWR->CSR &= ~PWR_WakeUpPin;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @brief Enters Sleep mode.
|
|
* @note In Sleep mode, all I/O pins keep the same state as in Run mode.
|
|
* @param PWR_SLEEPEntry: specifies if SLEEP mode in entered with WFI or WFE instruction.
|
|
* This parameter can be one of the following values:
|
|
* @arg PWR_SLEEPEntry_WFI: enter SLEEP mode with WFI instruction
|
|
* @arg PWR_SLEEPEntry_WFE: enter SLEEP mode with WFE instruction
|
|
* @retval None
|
|
*/
|
|
void PWR_EnterSleepMode(uint8_t PWR_SLEEPEntry)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_PWR_SLEEP_ENTRY(PWR_SLEEPEntry));
|
|
|
|
/* Clear SLEEPDEEP bit of Cortex-M0 System Control Register */
|
|
SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
|
|
|
|
/* Select SLEEP mode entry -------------------------------------------------*/
|
|
if(PWR_SLEEPEntry == PWR_SLEEPEntry_WFI)
|
|
{
|
|
/* Request Wait For Interrupt */
|
|
__WFI();
|
|
}
|
|
else
|
|
{
|
|
/* Request Wait For Event */
|
|
__SEV();
|
|
__WFE();
|
|
__WFE();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Enters STOP mode.
|
|
* @note In Stop mode, all I/O pins keep the same state as in Run mode.
|
|
* @note When exiting Stop mode by issuing an interrupt or a wakeup event,
|
|
* the HSI RC oscillator is selected as system clock.
|
|
* @note When the voltage regulator operates in low power mode, an additional
|
|
* startup delay is incurred when waking up from Stop mode.
|
|
* By keeping the internal regulator ON during Stop mode, the consumption
|
|
* is higher although the startup time is reduced.
|
|
* @param PWR_Regulator: specifies the regulator state in STOP mode.
|
|
* This parameter can be one of the following values:
|
|
* @arg PWR_Regulator_ON: STOP mode with regulator ON
|
|
* @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode
|
|
* @param PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
|
|
* This parameter can be one of the following values:
|
|
* @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
|
|
* @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
|
|
@arg PWR_STOPEntry_SLEEPONEXIT: enter STOP mode with SLEEPONEXIT instruction
|
|
* @retval None
|
|
*/
|
|
void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)
|
|
{
|
|
uint32_t tmpreg = 0;
|
|
|
|
/* Check the parameters */
|
|
assert_param(IS_PWR_REGULATOR(PWR_Regulator));
|
|
assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));
|
|
|
|
/* Select the regulator state in STOP mode ---------------------------------*/
|
|
tmpreg = PWR->CR;
|
|
/* Clear PDDS and LPDSR bits */
|
|
tmpreg &= CR_DS_MASK;
|
|
|
|
/* Set LPDSR bit according to PWR_Regulator value */
|
|
tmpreg |= PWR_Regulator;
|
|
|
|
/* Store the new value */
|
|
PWR->CR = tmpreg;
|
|
|
|
/* Set SLEEPDEEP bit of Cortex-M0 System Control Register */
|
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
|
|
|
/* Select STOP mode entry --------------------------------------------------*/
|
|
if(PWR_STOPEntry == PWR_STOPEntry_WFI)
|
|
{
|
|
/* Request Wait For Interrupt */
|
|
__WFI();
|
|
/* Reset SLEEPDEEP bit of Cortex System Control Register */
|
|
SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
|
|
}
|
|
else if (PWR_STOPEntry == PWR_STOPEntry_WFE)
|
|
{
|
|
/* Request Wait For Event */
|
|
__WFE();
|
|
/* Reset SLEEPDEEP bit of Cortex System Control Register */
|
|
SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
|
|
}
|
|
else
|
|
{
|
|
/* Set SLEEP on exit bit of Cortex-M0 System Control Register */
|
|
SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Enters STANDBY mode.
|
|
* @note In Standby mode, all I/O pins are high impedance except for:
|
|
* - Reset pad (still available)
|
|
* - RTC_AF1 pin (PC13) if configured for Wakeup pin 2 (WKUP2), tamper,
|
|
* time-stamp, RTC Alarm out, or RTC clock calibration out.
|
|
* - WKUP pin 1 (PA0) if enabled.
|
|
* @note The Wakeup flag (WUF) need to be cleared at application level before to call this function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void PWR_EnterSTANDBYMode(void)
|
|
{
|
|
/* Select STANDBY mode */
|
|
PWR->CR |= PWR_CR_PDDS;
|
|
|
|
/* Set SLEEPDEEP bit of Cortex-M0 System Control Register */
|
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
|
|
|
/* Request Wait For Interrupt */
|
|
__WFI();
|
|
}
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @brief Checks whether the specified PWR flag is set or not.
|
|
* @param PWR_FLAG: specifies the flag to check.
|
|
* This parameter can be one of the following values:
|
|
* @arg PWR_FLAG_WU: Wake Up flag. This flag indicates that a wakeup
|
|
* event was received from the WKUP pin or from the RTC alarm
|
|
* (Alarm A or Alarm B), RTC Tamper event or RTC TimeStamp event.
|
|
* @arg PWR_FLAG_SB: StandBy flag. This flag indicates that the
|
|
* system was resumed from StandBy mode.
|
|
* @arg PWR_FLAG_PVDO: PVD Output. This flag is valid only if PVD
|
|
* is enabled by the PWR_PVDCmd() function.
|
|
* @arg PWR_FLAG_VREFINTRDY: Internal Voltage Reference Ready flag.
|
|
* This flag indicates the state of the internal voltage
|
|
* reference, VREFINT.
|
|
* @retval The new state of PWR_FLAG (SET or RESET).
|
|
*/
|
|
FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG)
|
|
{
|
|
FlagStatus bitstatus = RESET;
|
|
/* Check the parameters */
|
|
assert_param(IS_PWR_GET_FLAG(PWR_FLAG));
|
|
|
|
if ((PWR->CSR & PWR_FLAG) != (uint32_t)RESET)
|
|
{
|
|
bitstatus = SET;
|
|
}
|
|
else
|
|
{
|
|
bitstatus = RESET;
|
|
}
|
|
/* Return the flag status */
|
|
return bitstatus;
|
|
}
|
|
|
|
/**
|
|
* @brief Clears the PWR's pending flags.
|
|
* @param PWR_FLAG: specifies the flag to clear.
|
|
* This parameter can be one of the following values:
|
|
* @arg PWR_FLAG_WU: Wake Up flag
|
|
* @arg PWR_FLAG_SB: StandBy flag
|
|
* @retval None
|
|
*/
|
|
void PWR_ClearFlag(uint32_t PWR_FLAG)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_PWR_CLEAR_FLAG(PWR_FLAG));
|
|
|
|
PWR->CR |= PWR_FLAG << 2;
|
|
}
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/************************ (C) COPYRIGHT FMD *****END OF FILE****/
|