power save function but not yet working

pull/7/head
Mateusz Lubecki 2021-08-22 20:30:22 +02:00
rodzic 0d8a02e7fb
commit 13de42e7e0
6 zmienionych plików z 136 dodań i 12 usunięć

Wyświetl plik

@ -20,6 +20,7 @@ C_SRCS += \
../src/it_handlers.c \
../src/main.c \
../src/packet_tx_handler.c \
../src/pwr_save.c \
../src/rte_main.c \
../src/rte_pv.c \
../src/rte_rtu.c \
@ -46,6 +47,7 @@ OBJS += \
./src/it_handlers.o \
./src/main.o \
./src/packet_tx_handler.o \
./src/pwr_save.o \
./src/rte_main.o \
./src/rte_pv.o \
./src/rte_rtu.o \
@ -72,6 +74,7 @@ C_DEPS += \
./src/it_handlers.d \
./src/main.d \
./src/packet_tx_handler.d \
./src/pwr_save.d \
./src/rte_main.d \
./src/rte_pv.d \
./src/rte_rtu.d \

Wyświetl plik

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="ilg.gnumcueclipse.debug.gdbjtag.openocd.launchConfigurationType">
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.PERIPHERALS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;peripherals&gt;&#10; &lt;peripheral name=&quot;RTC&quot;/&gt;&#10;&lt;/peripherals&gt;&#10;"/>
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.PERIPHERALS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;peripherals&gt;&#10; &lt;peripheral name=&quot;RTC&quot;/&gt;&#10; &lt;peripheral name=&quot;FLASH&quot;/&gt;&#10; &lt;peripheral name=&quot;SCB&quot;/&gt;&#10; &lt;peripheral name=&quot;DBGMCU&quot;/&gt;&#10; &lt;peripheral name=&quot;EXTI&quot;/&gt;&#10; &lt;peripheral name=&quot;PWR&quot;/&gt;&#10; &lt;peripheral name=&quot;USART2&quot;/&gt;&#10;&lt;/peripherals&gt;&#10;"/>
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doContinue" value="true"/>
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doDebugInRam" value="false"/>
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doFirstReset" value="true"/>

21
include/pwr_save.h 100644
Wyświetl plik

@ -0,0 +1,21 @@
/*
* pwr_save.h
*
* Created on: Aug 22, 2021
* Author: mateusz
*/
#ifndef PWR_SAVE_H_
#define PWR_SAVE_H_
#include "station_config_target_hw.h"
#if defined(STM32L471xx)
void pwr_save_init(void);
void pwr_save_enter_stop2(void);
#endif
#endif /* PWR_SAVE_H_ */

Wyświetl plik

@ -22,11 +22,6 @@ void io_oc_output_hiz(void);
void io_ext_watchdog_config(void);
void io_ext_watchdog_service(void);
//void io_5v_isol_sw_cntrl_vbat_s_enable(void);
//void io_5v_isol_sw_cntrl_vbat_s_disable(void);
//
//void io_12v_sw_cntrl_vbat_g_enable(void);
//void io_12v_sw_cntrl_vbat_g_disable(void);
inline void io_5v_isol_sw_cntrl_vbat_s_enable(void) {
GPIOB->BSRR |= GPIO_BSRR_BS8;

Wyświetl plik

@ -37,6 +37,7 @@
#include "LedConfig.h"
#include "io.h"
#include "float_to_string.h"
#include "pwr_save.h"
#include "it_handlers.h"
@ -82,12 +83,12 @@
// Niebieska dioda -> DCD
// Zielona dioda -> anemometr albo TX
// backup registers
// 2 -> 4bit hard-faults | 4bit boot-counter
// 3 -> hard fault PC LSB
// 4 -> hard fault PC MSB
// 5 -> hard fault LR LSB
// 6 -> hard fault LR MSB
// backup registers (ParaTNC)
// 2 -> boot and hard fault count
// 3 -> controller configuration status
// backup registers (ParaMETEO)
#define CONFIG_FIRST_RESTORED (1)
#define CONFIG_FIRST_FAIL_RESTORING (1 << 1)
@ -430,6 +431,11 @@ int main(int argc, char* argv[]){
#endif
#if defined(STM32L471xx)
// initialize all powersaving functions
pwr_save_init();
#endif
// initalizing separated Open Collector output
io_oc_init();
@ -806,6 +812,8 @@ int main(int argc, char* argv[]){
io_ext_watchdog_service();
pwr_save_enter_stop2();
// Infinite loop
while (1)
{

97
src/pwr_save.c 100644
Wyświetl plik

@ -0,0 +1,97 @@
/*
* pwr_save.c
*
* Created on: Aug 22, 2021
* Author: mateusz
*/
#include "pwr_save.h"
#include "stm32l4xx.h"
#include <stdint.h>
#define IN_STOP2_MODE (1 << 1)
#if defined(STM32L471xx)
/**
* This function initializes everything related to power saving features
* including programming Flash memory option bytes
*/
void pwr_save_init(void) {
// make a pointer to option byte
uint32_t* option_byte = (uint32_t*)0x1FFF7800;
// content of option byte read from the flash memory
uint32_t option_byte_content = *option_byte;
// definition of bitmask
#define IWDG_STBY_STOP (0x3 << 17)
// check if IWDG_STDBY and IWDG_STOP is not set in ''User and read protection option bytes''
// at 0x1FFF7800
if ((option_byte_content & IWDG_STBY_STOP) == IWDG_STBY_STOP) {
// unlock write/erase operations on flash memory
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
// wait for any possible flash operation to finish (rather impossible here, but ST manual recommend doing this)
while((FLASH->SR & FLASH_SR_BSY) != 0);
// unlock operations on option bytes
FLASH->OPTKEYR = 0x08192A3B;
FLASH->OPTKEYR = 0x4C5D6E7F;
// set the flash option register (in RAM!!)
FLASH->OPTR &= (0xFFFFFFFF ^ (FLASH_OPTR_IWDG_STDBY | FLASH_OPTR_IWDG_STOP));
// trigger an update of flash option bytes with values from RAM (from FLASH->OPTR)
FLASH->CR |= FLASH_CR_OPTSTRT;
// wait for option bytes to be updated
while((FLASH->SR & FLASH_SR_BSY) != 0);
// lock flash memory
FLASH-> CR |= FLASH_CR_LOCK;
// forcre reloading option bytes
FLASH->CR |= FLASH_CR_OBL_LAUNCH;
}
}
/**
* Entering STOP2 power save mode. In this mode all clocks except LSI and LSE are disabled. StaticRAM content
* is preserved, optionally GPIO and few other peripherals can be kept power up depending on configuration
*/
void pwr_save_enter_stop2(void) {
// clear previous low power mode selection
PWR->CR1 &= (0xFFFFFFFF ^ PWR_CR1_LPMS_Msk);
// select STOP2
PWR->CR1 |= PWR_CR1_LPMS_STOP2;
// enable write access to RTC registers by writing two magic words
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
// save an information that STOP2 mode has been applied
RTC->BKP4R |= IN_STOP2_MODE;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
DBGMCU->CR &= (0xFFFFFFFF ^ (DBGMCU_CR_DBG_SLEEP_Msk | DBGMCU_CR_DBG_STOP_Msk | DBGMCU_CR_DBG_STANDBY_Msk));
// disabling all IRQs
__disable_irq();
asm ("wfe");
}
#endif