Skip to content

Commit

Permalink
cpu/stm32f0: add periph_pm support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Dupont committed Mar 13, 2019
1 parent 49807f1 commit 110c16c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
7 changes: 4 additions & 3 deletions cpu/stm32_common/include/periph_cpu_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ extern "C" {
/**
* @brief Number of usable low power modes
*/
#if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L0) || \
defined(CPU_FAM_STM32L1) || defined(DOXYGEN)
#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F1) || \
defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) || \
defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1) || \
defined(DOXYGEN)
#define PM_NUM_MODES (2U)

/**
Expand Down
75 changes: 55 additions & 20 deletions cpu/stm32_common/periph/pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
* 2015 Freie Universität Berlin
* 2015 Engineering-Spirit
* 2017-2018 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand All @@ -19,15 +20,16 @@
* @author Nick v. IJzendoorn <nijzndoorn@engineering-spirit.nl>
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Fabian Nack <nack@inf.fu-berlin.de>
* @author Vincent Dupont <vincent@otakeys.com>
*
* @}
*/

#include "irq.h"
#include "periph/pm.h"
#if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L0) || \
defined(CPU_FAM_STM32L1)
#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F1) || \
defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) || \
defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1)
#include "stmclk.h"
#endif

Expand All @@ -40,18 +42,58 @@
*
* Available values can be found in reference manual, PWR section, register CR.
*/
#if defined(CPU_FAM_STM32F0)
#define PM_STOP_CONFIG (PWR_CR_LPDS)
#else
#define PM_STOP_CONFIG (PWR_CR_LPDS | PWR_CR_FPDS)
#endif
#endif

static inline uint32_t _ewup_config(void)
{
uint32_t tmp = 0;
#ifdef PM_EWUP_CONFIG
tmp |= PM_EWUP_CONFIG;
#elif defined(PWR_CSR_EWUP)
tmp |= PWR_CSR_EWUP;
#else
#if defined(PWR_CSR_EWUP8)
tmp |= PWR_CSR_EWUP8;
#endif
#if defined(PWR_CSR_EWUP7)
tmp |= PWR_CSR_EWUP7;
#endif
#if defined(PWR_CSR_EWUP6)
tmp |= PWR_CSR_EWUP6;
#endif
#if defined(PWR_CSR_EWUP5)
tmp |= PWR_CSR_EWUP5;
#endif
#if defined(PWR_CSR_EWUP4)
tmp |= PWR_CSR_EWUP4;
#endif
#if defined(PWR_CSR_EWUP3)
tmp |= PWR_CSR_EWUP3;
#endif
#if defined(PWR_CSR_EWUP2)
tmp |= PWR_CSR_EWUP2;
#endif
#if defined(PWR_CSR_EWUP1)
tmp |= PWR_CSR_EWUP1;
#endif
#endif
return tmp;
}

void pm_set(unsigned mode)
{
int deep = 0;

/* I just copied it from stm32f1/2/4, but I suppose it would work for the
* others... /KS */
#if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L0) || \
defined(CPU_FAM_STM32L1)
#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F1) || \
defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) || \
defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1)
switch (mode) {
case STM32_PM_STANDBY:
/* Set PDDS to enter standby mode on deepsleep and clear flags */
Expand All @@ -60,15 +102,8 @@ void pm_set(unsigned mode)
#if defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1)
/* Enable Ultra Low Power mode */
PWR->CR |= PWR_CR_ULP;

PWR->CSR |= PWR_CSR_EWUP1;
#if !defined(CPU_LINE_STM32L053xx)
/* STM32L053 only have 2 wake pins */
PWR->CSR |= PWR_CSR_EWUP3;
#endif
#else
PWR->CSR |= PWR_CSR_EWUP;
#endif
PWR->CSR |= _ewup_config();
/* Set SLEEPDEEP bit of system control block */
deep = 1;
break;
Expand Down Expand Up @@ -99,19 +134,19 @@ void pm_set(unsigned mode)

cortexm_sleep(deep);

#if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L0) || \
defined(CPU_FAM_STM32L1)
#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F1) || \
defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) || \
defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1)
if (deep) {
/* Re-init clock after STOP */
stmclk_init_sysclk();
}
#endif
}

#if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L0) || \
defined(CPU_FAM_STM32L1)
#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F1) || \
defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) || \
defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1)
void pm_off(void)
{
irq_disable();
Expand Down
2 changes: 2 additions & 0 deletions cpu/stm32f0/Makefile.include
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export CPU_ARCH = cortex-m0
export CPU_FAM = stm32f0

USEMODULE += pm_layered

include $(RIOTCPU)/stm32_common/Makefile.include
include $(RIOTMAKE)/arch/cortexm.inc.mk

0 comments on commit 110c16c

Please sign in to comment.