/** @file pwr.c
 * Module handling the power management's control
 *
 * The module provides functions to enter the different sleep states, control
 * and filter wakeup events (WKUP and RTC) and configure power voltage
 * detection
 */

//--includes--------------------------------------------------------------------

#include "pwr.h"
#include "pwr_regs.h"
#include "scb.h"
#include "rcc.h"


//--local definitions-----------------------------------------------------------

//--local variables-------------------------------------------------------------

static volatile struct PWR* regs = (struct PWR*)PWR_BASE_ADDRESS;


//--public functions------------------------------------------------------------

void pwr_sleep(void)
{
	scb_configure_deepsleep(false);
	__asm("wfe");
}

void pwr_stop(enum PwrWakeupSpeed speed)
{
	rcc_enable(RCC_AHB_NONE, RCC_APB1_PWR, RCC_APB2_NONE);
	scb_configure_deepsleep(true);
	regs->CR.PDDS = 0;
	regs->CR.LPDS = speed;
	__asm("wfe");
	rcc_configure(RCC_PRESET_SPEED);
}

void pwr_standby(void)
{
	rcc_enable(RCC_AHB_NONE, RCC_APB1_PWR, RCC_APB2_NONE);
	scb_configure_deepsleep(true);
	regs->CR.PDDS = 1;
	__asm("wfe");
	rcc_configure(RCC_PRESET_SPEED);
}

void pwr_configure_bkp_write(bool enable)
{
	rcc_enable(RCC_AHB_NONE, RCC_APB1_PWR, RCC_APB2_NONE);
	regs->CR.DBP = enable;
}

//--local functions-------------------------------------------------------------

//--ISRs------------------------------------------------------------------------


