From 3e97d4fe7e13aeebae0ada1d323023028c338e25 Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sun, 14 Jul 2024 19:17:20 +0200 Subject: [PATCH] Implement sleep modes --- drv/pwr.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ drv/pwr.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 drv/pwr.c create mode 100644 drv/pwr.h diff --git a/drv/pwr.c b/drv/pwr.c new file mode 100644 index 0000000..223a660 --- /dev/null +++ b/drv/pwr.c @@ -0,0 +1,51 @@ +/** @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" + + +//--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("wfi"); +} + +void pwr_stop(enum PwrWakeupSpeed speed) +{ + scb_configure_deepsleep(true); + regs->CR.PDDS = 0; + regs->CR.LPDS = speed; + __asm("wfi"); +} + +void pwr_standby(void) +{ + scb_configure_deepsleep(true); + regs->CR.PDDS = 1; + __asm("wfi"); +} + + +//--local functions------------------------------------------------------------- + +//--ISRs------------------------------------------------------------------------ + + diff --git a/drv/pwr.h b/drv/pwr.h new file mode 100644 index 0000000..f20ad9a --- /dev/null +++ b/drv/pwr.h @@ -0,0 +1,51 @@ +/** @file pwr.h + * 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 + */ + +#ifndef _PWR_H_ +#define _PWR_H_ + +//--includes-------------------------------------------------------------------- + +//--type definitions------------------------------------------------------------ + +enum PwrWakeupSpeed { + PWR_WAKEUP_SPEED_FAST, //faster wakeup, higher consumption in stop mode + PWR_WAKEUP_SPEED_SLOW, //slower wakeup, lower consumption in stop mode +}; + +enum PwrPvdThreshold { + PWR_PVD_THRESHOLD_2_2V, + PWR_PVD_THRESHOLD_2_3V, + PWR_PVD_THRESHOLD_2_4V, + PWR_PVD_THRESHOLD_2_5V, + PWR_PVD_THRESHOLD_2_6V, + PWR_PVD_THRESHOLD_2_7V, + PWR_PVD_THRESHOLD_2_8V, + PWR_PVD_THRESHOLD_2_9V, + +}; + +typedef void (*PwrPvdCallback)(void); + + +//--functions------------------------------------------------------------------- + +void pwr_sleep(void); +void pwr_stop(enum PwrWakeupSpeed speed); +void pwr_standby(void); + +bool pwr_wakeup_event(void); +bool pwr_standby_exit(void); + +void pwr_configure_bkp_write(bool enable); +void pwr_configure_wakeup_pin(bool enable); +void pwr_configure_pvd(enum PwrPvdThreshold treshold); + + +#endif //_PWR_H_ +