sleepmgr.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * \file
  3. *
  4. * \brief Sleep manager
  5. *
  6. * Copyright (c) 2010-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef SLEEPMGR_H
  47. #define SLEEPMGR_H
  48. #include <compiler.h>
  49. #include <parts.h>
  50. #if (SAM3S || SAM3U || SAM3N || SAM3XA || SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM || SAMV71 || SAMV70 || SAMS70 || SAME70)
  51. # include "sam/sleepmgr.h"
  52. #elif XMEGA
  53. # include "xmega/sleepmgr.h"
  54. #elif UC3
  55. # include "uc3/sleepmgr.h"
  56. #elif SAM4L
  57. # include "sam4l/sleepmgr.h"
  58. #elif MEGA
  59. # include "mega/sleepmgr.h"
  60. #elif (SAMD20 || SAMD21 || SAMR21 || SAMD11 || SAMDA1)
  61. # include "samd/sleepmgr.h"
  62. #elif (SAML21 || SAML22 || SAMR30)
  63. # include "saml/sleepmgr.h"
  64. #elif (SAMC21)
  65. # include "samc/sleepmgr.h"
  66. #else
  67. # error Unsupported device.
  68. #endif
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. /**
  73. * \defgroup sleepmgr_group Sleep manager
  74. *
  75. * The sleep manager is a service for ensuring that the device is not put to
  76. * sleep in deeper sleep modes than the system (e.g., peripheral drivers,
  77. * services or the application) allows at any given time.
  78. *
  79. * It is based on the use of lock counting for the individual sleep modes, and
  80. * will put the device to sleep in the shallowest sleep mode that has a non-zero
  81. * lock count. The drivers/services/application can change these counts by use
  82. * of \ref sleepmgr_lock_mode and \ref sleepmgr_unlock_mode.
  83. * Refer to \ref sleepmgr_mode for a list of the sleep modes available for
  84. * locking, and the device datasheet for information on their effect.
  85. *
  86. * The application must supply the file \ref conf_sleepmgr.h.
  87. *
  88. * For the sleep manager to be enabled, the symbol \ref CONFIG_SLEEPMGR_ENABLE
  89. * must be defined, e.g., in \ref conf_sleepmgr.h. If this symbol is not
  90. * defined, the functions are replaced with dummy functions and no RAM is used.
  91. *
  92. * @{
  93. */
  94. /**
  95. * \def CONFIG_SLEEPMGR_ENABLE
  96. * \brief Configuration symbol for enabling the sleep manager
  97. *
  98. * If this symbol is not defined, the functions of this service are replaced
  99. * with dummy functions. This is useful for reducing code size and execution
  100. * time if the sleep manager is not needed in the application.
  101. *
  102. * This symbol may be defined in \ref conf_sleepmgr.h.
  103. */
  104. #if defined(__DOXYGEN__) && !defined(CONFIG_SLEEPMGR_ENABLE)
  105. # define CONFIG_SLEEPMGR_ENABLE
  106. #endif
  107. /**
  108. * \enum sleepmgr_mode
  109. * \brief Sleep mode locks
  110. *
  111. * Identifiers for the different sleep mode locks.
  112. */
  113. /**
  114. * \brief Initialize the lock counts
  115. *
  116. * Sets all lock counts to 0, except the very last one, which is set to 1. This
  117. * is done to simplify the algorithm for finding the deepest allowable sleep
  118. * mode in \ref sleepmgr_enter_sleep.
  119. */
  120. static inline void sleepmgr_init(void)
  121. {
  122. #ifdef CONFIG_SLEEPMGR_ENABLE
  123. uint8_t i;
  124. for (i = 0; i < SLEEPMGR_NR_OF_MODES - 1; i++) {
  125. sleepmgr_locks[i] = 0;
  126. }
  127. sleepmgr_locks[SLEEPMGR_NR_OF_MODES - 1] = 1;
  128. #endif /* CONFIG_SLEEPMGR_ENABLE */
  129. }
  130. /**
  131. * \brief Increase lock count for a sleep mode
  132. *
  133. * Increases the lock count for \a mode to ensure that the sleep manager does
  134. * not put the device to sleep in the deeper sleep modes.
  135. *
  136. * \param mode Sleep mode to lock.
  137. */
  138. static inline void sleepmgr_lock_mode(enum sleepmgr_mode mode)
  139. {
  140. #ifdef CONFIG_SLEEPMGR_ENABLE
  141. irqflags_t flags;
  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. // Enter a critical section
  149. flags = cpu_irq_save();
  150. ++sleepmgr_locks[mode];
  151. // Leave the critical section
  152. cpu_irq_restore(flags);
  153. #else
  154. UNUSED(mode);
  155. #endif /* CONFIG_SLEEPMGR_ENABLE */
  156. }
  157. /**
  158. * \brief Decrease lock count for a sleep mode
  159. *
  160. * Decreases the lock count for \a mode. If the lock count reaches 0, the sleep
  161. * manager can put the device to sleep in the deeper sleep modes.
  162. *
  163. * \param mode Sleep mode to unlock.
  164. */
  165. static inline void sleepmgr_unlock_mode(enum sleepmgr_mode mode)
  166. {
  167. #ifdef CONFIG_SLEEPMGR_ENABLE
  168. irqflags_t flags;
  169. if(sleepmgr_locks[mode] == 0) {
  170. while (true) {
  171. // Warning: minimum value of sleepmgr_locks buffer is no less than 0.
  172. // Check APP.
  173. }
  174. }
  175. // Enter a critical section
  176. flags = cpu_irq_save();
  177. --sleepmgr_locks[mode];
  178. // Leave the critical section
  179. cpu_irq_restore(flags);
  180. #else
  181. UNUSED(mode);
  182. #endif /* CONFIG_SLEEPMGR_ENABLE */
  183. }
  184. /**
  185. * \brief Retrieves the deepest allowable sleep mode
  186. *
  187. * Searches through the sleep mode lock counts, starting at the shallowest sleep
  188. * mode, until the first non-zero lock count is found. The deepest allowable
  189. * sleep mode is then returned.
  190. */
  191. static inline enum sleepmgr_mode sleepmgr_get_sleep_mode(void)
  192. {
  193. enum sleepmgr_mode sleep_mode = SLEEPMGR_ACTIVE;
  194. #ifdef CONFIG_SLEEPMGR_ENABLE
  195. uint8_t *lock_ptr = sleepmgr_locks;
  196. // Find first non-zero lock count, starting with the shallowest modes.
  197. while (!(*lock_ptr)) {
  198. lock_ptr++;
  199. sleep_mode = (enum sleepmgr_mode)(sleep_mode + 1);
  200. }
  201. // Catch the case where one too many sleepmgr_unlock_mode() call has been
  202. // performed on the deepest sleep mode.
  203. Assert((uintptr_t)(lock_ptr - sleepmgr_locks) < SLEEPMGR_NR_OF_MODES);
  204. #endif /* CONFIG_SLEEPMGR_ENABLE */
  205. return sleep_mode;
  206. }
  207. /**
  208. * \fn sleepmgr_enter_sleep
  209. * \brief Go to sleep in the deepest allowed mode
  210. *
  211. * Searches through the sleep mode lock counts, starting at the shallowest sleep
  212. * mode, until the first non-zero lock count is found. The device is then put to
  213. * sleep in the sleep mode that corresponds to the lock.
  214. *
  215. * \note This function enables interrupts before going to sleep, and will leave
  216. * them enabled upon return. This also applies if sleep is skipped due to ACTIVE
  217. * mode being locked.
  218. */
  219. static inline void sleepmgr_enter_sleep(void)
  220. {
  221. #ifdef CONFIG_SLEEPMGR_ENABLE
  222. enum sleepmgr_mode sleep_mode;
  223. cpu_irq_disable();
  224. // Find the deepest allowable sleep mode
  225. sleep_mode = sleepmgr_get_sleep_mode();
  226. // Return right away if first mode (ACTIVE) is locked.
  227. if (sleep_mode==SLEEPMGR_ACTIVE) {
  228. cpu_irq_enable();
  229. return;
  230. }
  231. // Enter the deepest allowable sleep mode with interrupts enabled
  232. sleepmgr_sleep(sleep_mode);
  233. #else
  234. cpu_irq_enable();
  235. #endif /* CONFIG_SLEEPMGR_ENABLE */
  236. }
  237. //! @}
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif /* SLEEPMGR_H */