-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathpm.c
More file actions
116 lines (98 loc) · 2.21 KB
/
pm.c
File metadata and controls
116 lines (98 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (C) 2016 Kaspar Schleiser <[email protected]>
*
* 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 <[email protected]>
*
* @}
*/
#include <assert.h>
#include "board.h"
#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
#if PM_NUM_MODES == 1
#define PM_BLOCKER_INITIAL { 0 }
#endif
#if PM_NUM_MODES == 2
#define PM_BLOCKER_INITIAL { 1, 0 }
#endif
#if PM_NUM_MODES == 3
#define PM_BLOCKER_INITIAL { 1, 1, 0 }
#endif
#if PM_NUM_MODES == 4
#define PM_BLOCKER_INITIAL { 1, 1, 1, 0 }
#endif
#if PM_NUM_MODES == 5
#define PM_BLOCKER_INITIAL { 1, 1, 1, 1, 0 }
#endif
#endif
/**
* @brief Global variable for keeping track of blocked modes
*/
static pm_blocker_t pm_blocker = { .blockers = PM_BLOCKER_INITIAL };
void pm_set_lowest(void)
{
unsigned mode = PM_NUM_MODES;
/* set lowest mode if blocker is still the same */
unsigned state = irq_disable();
while (mode) {
if (pm_blocker.blockers[mode - 1]) {
break;
}
mode--;
}
if (mode != PM_NUM_MODES) {
pm_set(mode);
}
irq_restore(state);
}
void pm_block(unsigned mode)
{
DEBUG("[pm_layered] pm_block(%d)\n", mode);
unsigned state = irq_disable();
assert(pm_blocker.blockers[mode] != 255);
pm_blocker.blockers[mode]++;
irq_restore(state);
}
void pm_unblock(unsigned mode)
{
DEBUG("[pm_layered] pm_unblock(%d)\n", mode);
unsigned state = irq_disable();
assert(pm_blocker.blockers[mode] > 0);
pm_blocker.blockers[mode]--;
irq_restore(state);
}
pm_blocker_t pm_get_blocker(void)
{
pm_blocker_t result;
unsigned state = irq_disable();
result = pm_blocker;
irq_restore(state);
return result;
}
#ifndef PROVIDES_PM_LAYERED_OFF
void pm_off(void)
{
irq_disable();
while(1) {
pm_set(0);
}
}
#endif