Marian Buschsieweke a3e7abc2e2
sys/pm: Fix behavior of fallback pm_off() implementation
- pm_off() should prevent other threads from getting executed after it is
  called, as it should turn off the MCU completely according to its doc
    - Previously, the fallback implementation would still allow other threads
      to continue working
    - Simply disabling IRQs and never enabling them again should make sure
      the MCU behaves like it would be completely off
- pm_off() should reduce the power consumption as much as possible
    - Previously, when IRQs came after the call to pm_set_lowest() in the
      fallback implementation of pm_off(), `while(1) {}` got executed at full
      power consumption
    - Just calling `pm_set(0);` in a loop while make sure that lowest power mode
      is restored if the MCU wakes up again.
    - The check if the lowest power mode is available is skipped, as no code
      gets executed afterwards anyway
2020-04-29 12:08:12 +02:00

91 lines
1.8 KiB
C

/*
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
*
* 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
* directory for more details.
*/
/**
* @ingroup sys_pm_layered
* @{
*
* @file
* @brief Platform-independent power management code
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include "irq.h"
#include "periph/pm.h"
#include "pm_layered.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#ifndef PM_NUM_MODES
#error PM_NUM_MODES must be defined in periph_cpu.h!
#endif
#ifndef PM_BLOCKER_INITIAL
#define PM_BLOCKER_INITIAL 0x01010101 /* block all by default */
#endif
/**
* @brief Global variable for keeping track of blocked modes
*/
volatile pm_blocker_t pm_blocker = { .val_u32 = PM_BLOCKER_INITIAL };
void pm_set_lowest(void)
{
pm_blocker_t blocker = pm_blocker;
unsigned mode = PM_NUM_MODES;
while (mode) {
if (blocker.val_u8[mode-1]) {
break;
}
mode--;
}
/* set lowest mode if blocker is still the same */
unsigned state = irq_disable();
if (blocker.val_u32 == pm_blocker.val_u32) {
DEBUG("pm: setting mode %u\n", mode);
pm_set(mode);
}
else {
DEBUG("pm: mode block changed\n");
}
irq_restore(state);
}
void pm_block(unsigned mode)
{
assert(pm_blocker.val_u8[mode] != 255);
unsigned state = irq_disable();
pm_blocker.val_u8[mode]++;
irq_restore(state);
}
void pm_unblock(unsigned mode)
{
assert(pm_blocker.val_u8[mode] > 0);
unsigned state = irq_disable();
pm_blocker.val_u8[mode]--;
irq_restore(state);
}
#ifndef PROVIDES_PM_LAYERED_OFF
void pm_off(void)
{
irq_disable();
while(1) {
pm_set(0);
}
}
#endif