sleepmgr.h
Go to the documentation of this file.
1 
33 /*
34  * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
35  */
36 #ifndef SLEEPMGR_H
37 #define SLEEPMGR_H
38 
39 #include <compiler.h>
40 #include <parts.h>
41 
42 #if (SAM3S || SAM3U || SAM3N || SAM3XA || SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM || SAMV71 || SAMV70 || SAMS70 || SAME70)
43 # include "sam/sleepmgr.h"
44 #elif XMEGA
45 # include "xmega/sleepmgr.h"
46 #elif UC3
47 # include "uc3/sleepmgr.h"
48 #elif SAM4L
49 # include "sam4l/sleepmgr.h"
50 #elif MEGA
51 # include "mega/sleepmgr.h"
52 #elif (SAMD20 || SAMD21 || SAMR21 || SAMD11 || SAMDA1)
53 # include "samd/sleepmgr.h"
54 #elif (SAML21 || SAML22 || SAMR30 || SAMR34 || SAMR35)
55 # include "saml/sleepmgr.h"
56 #elif (SAMC21)
57 # include "samc/sleepmgr.h"
58 #else
59 # error Unsupported device.
60 #endif
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
99 #if defined(__DOXYGEN__) && !defined(CONFIG_SLEEPMGR_ENABLE)
100 # define CONFIG_SLEEPMGR_ENABLE
101 #endif
102 
117 static inline void sleepmgr_init(void)
118 {
119 #ifdef CONFIG_SLEEPMGR_ENABLE
120  uint8_t i;
121 
122  for (i = 0; i < SLEEPMGR_NR_OF_MODES - 1; i++) {
123  sleepmgr_locks[i] = 0;
124  }
125  sleepmgr_locks[SLEEPMGR_NR_OF_MODES - 1] = 1;
126 #endif /* CONFIG_SLEEPMGR_ENABLE */
127 }
128 
137 static inline void sleepmgr_lock_mode(enum sleepmgr_mode mode)
138 {
139 #ifdef CONFIG_SLEEPMGR_ENABLE
140  irqflags_t flags;
141 
142  if(sleepmgr_locks[mode] >= 0xff) {
143  while (true) {
144  // Warning: maximum value of sleepmgr_locks buffer is no more than 255.
145  // Check APP or change the data type to uint16_t.
146  }
147  }
148 
149  // Enter a critical section
150  flags = cpu_irq_save();
151 
152  ++sleepmgr_locks[mode];
153 
154  // Leave the critical section
155  cpu_irq_restore(flags);
156 #else
157  UNUSED(mode);
158 #endif /* CONFIG_SLEEPMGR_ENABLE */
159 }
160 
169 static inline void sleepmgr_unlock_mode(enum sleepmgr_mode mode)
170 {
171 #ifdef CONFIG_SLEEPMGR_ENABLE
172  irqflags_t flags;
173 
174  if(sleepmgr_locks[mode] == 0) {
175  while (true) {
176  // Warning: minimum value of sleepmgr_locks buffer is no less than 0.
177  // Check APP.
178  }
179  }
180 
181  // Enter a critical section
182  flags = cpu_irq_save();
183 
184  --sleepmgr_locks[mode];
185 
186  // Leave the critical section
187  cpu_irq_restore(flags);
188 #else
189  UNUSED(mode);
190 #endif /* CONFIG_SLEEPMGR_ENABLE */
191 }
192 
200 static inline enum sleepmgr_mode sleepmgr_get_sleep_mode(void)
201 {
202  enum sleepmgr_mode sleep_mode = SLEEPMGR_ACTIVE;
203 
204 #ifdef CONFIG_SLEEPMGR_ENABLE
205  uint8_t *lock_ptr = sleepmgr_locks;
206 
207  // Find first non-zero lock count, starting with the shallowest modes.
208  while (!(*lock_ptr)) {
209  lock_ptr++;
210  sleep_mode = (enum sleepmgr_mode)(sleep_mode + 1);
211  }
212 
213  // Catch the case where one too many sleepmgr_unlock_mode() call has been
214  // performed on the deepest sleep mode.
215  Assert((uintptr_t)(lock_ptr - sleepmgr_locks) < SLEEPMGR_NR_OF_MODES);
216 
217 #endif /* CONFIG_SLEEPMGR_ENABLE */
218 
219  return sleep_mode;
220 }
221 
235 static inline void sleepmgr_enter_sleep(void)
236 {
237 #ifdef CONFIG_SLEEPMGR_ENABLE
238  enum sleepmgr_mode sleep_mode;
239 
240  cpu_irq_disable();
241 
242  // Find the deepest allowable sleep mode
243  sleep_mode = sleepmgr_get_sleep_mode();
244  // Return right away if first mode (ACTIVE) is locked.
245  if (sleep_mode==SLEEPMGR_ACTIVE) {
246  cpu_irq_enable();
247  return;
248  }
249  // Enter the deepest allowable sleep mode with interrupts enabled
250  sleepmgr_sleep(sleep_mode);
251 #else
252  cpu_irq_enable();
253 #endif /* CONFIG_SLEEPMGR_ENABLE */
254 }
255 
256 
258 
259 #ifdef __cplusplus
260 }
261 #endif
262 
263 #endif /* SLEEPMGR_H */
#define cpu_irq_disable()
Disable interrupts globally.
#define UNUSED(v)
Marking v as a unused parameter or value.
Definition: compiler.h:86
static void sleepmgr_init(void)
Initialize the lock counts.
Definition: sleepmgr.h:117
static irqflags_t cpu_irq_save(void)
Get and clear the global interrupt flags.
static void sleepmgr_sleep(const enum sleepmgr_mode sleep_mode)
Definition: sam/sleepmgr.h:90
static void cpu_irq_restore(irqflags_t flags)
Restore global interrupt flags.
Commonly used includes, types and macros.
Atmel part identification macros.
static enum sleepmgr_mode sleepmgr_get_sleep_mode(void)
Retrieves the deepest allowable sleep mode.
Definition: sleepmgr.h:200
static void sleepmgr_enter_sleep(void)
Go to sleep in the deepest allowed mode.
Definition: sleepmgr.h:235
Active mode.
Definition: sam/sleepmgr.h:55
SAM3/SAM4 Sleep manager implementation.
sleepmgr_mode
Sleep mode locks.
Definition: sam/sleepmgr.h:53
uint32_t irqflags_t
Type used for holding state of interrupt flag.
static void sleepmgr_lock_mode(enum sleepmgr_mode mode)
Increase lock count for a sleep mode.
Definition: sleepmgr.h:137
#define Assert(expr)
This macro is used to test fatal errors.
Definition: compiler.h:196
static void sleepmgr_unlock_mode(enum sleepmgr_mode mode)
Decrease lock count for a sleep mode.
Definition: sleepmgr.h:169
#define cpu_irq_enable()
Enable interrupts globally.


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:05