2017-01-11 21:48:16 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-01-13 10:55:53 +01:00
|
|
|
* @ingroup sys_pm_layered
|
2017-01-11 21:48:16 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @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
|
2020-02-14 12:05:01 +01:00
|
|
|
#define PM_BLOCKER_INITIAL 0x01010101 /* block all by default */
|
2017-01-11 21:48:16 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Global variable for keeping track of blocked modes
|
|
|
|
*/
|
2020-02-14 12:05:01 +01:00
|
|
|
volatile pm_blocker_t pm_blocker = { .val_u32 = PM_BLOCKER_INITIAL };
|
2017-01-11 21:48:16 +01:00
|
|
|
|
|
|
|
void pm_set_lowest(void)
|
|
|
|
{
|
make: fix various compile errors with Wextra
pkg, nordic_softdevice_ble: disable CFLAGS to omit compiler error
sys, pm_layered: fix casting nonscalar to the same type
cpu, stm32_common: fix type-limits, remove always true assert
cpu, stm32f4: fix pointer arithmetic in periph/i2c
drivers, at86rf2xx: fix type-limits where condition always true
saul, gpio: fix if no gpio configured for saul
cpu, saml21: add frequency check to periph/timer
driver, cc110x: fix unused param and type-limts errors
boards, wsn430-common: fix old-style-declaration
make: fix old style definition
drivers, sdcard_spi: fix old style typedef
driver, at30tse: remove unnecessary check
driver, nrf24: fix type-limit
driver, pn532: change buffer from char to uint8_t
tests/driver_sdcard: fix type limits
boards, feather-m0: add missing field inits
driver, tcs37727: fix type limits
pkg, emb6: disable some compiler warnings
tests/emb6: disable some compiler warings
pkg, openthread: fix sign compare and unused params
tests/trickle: fix struct init
tests/pthread_cooperation: fix type limits
board, mips-malta: remove feature periph_uart
shell: fix var size for netif command
gnrc, netif: fix sign-compare
gnrc, nib: fix sign-compare
shell: fix output in netif command
posix: fix type-limits in pthread_cond
2017-10-31 11:52:18 +01:00
|
|
|
pm_blocker_t blocker = pm_blocker;
|
2017-01-11 21:48:16 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-12 16:20:15 +02:00
|
|
|
#ifndef PROVIDES_PM_LAYERED_OFF
|
2017-10-28 16:05:29 +02:00
|
|
|
void pm_off(void)
|
2017-01-11 21:48:16 +01:00
|
|
|
{
|
|
|
|
pm_blocker.val_u32 = 0;
|
|
|
|
pm_set_lowest();
|
2017-10-28 16:05:29 +02:00
|
|
|
while(1) {}
|
2017-01-11 21:48:16 +01:00
|
|
|
}
|
2017-10-12 16:20:15 +02:00
|
|
|
#endif
|