nrfx_power.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <nrfx.h>
  41. #if NRFX_CHECK(NRFX_POWER_ENABLED)
  42. #include <nrfx_power.h>
  43. #if defined(REGULATORS_PRESENT)
  44. #include <hal/nrf_regulators.h>
  45. #endif
  46. #if NRFX_CHECK(NRFX_CLOCK_ENABLED)
  47. extern bool nrfx_clock_irq_enabled;
  48. extern void nrfx_clock_irq_handler(void);
  49. #endif
  50. /**
  51. * @internal
  52. * @defgroup nrfx_power_internals POWER driver internals
  53. * @ingroup nrfx_power
  54. *
  55. * Internal variables, auxiliary macros and functions of POWER driver.
  56. * @{
  57. */
  58. /**
  59. * This variable is used to check whether common POWER_CLOCK common interrupt
  60. * should be disabled or not if @ref nrfx_clock tries to disable the interrupt.
  61. */
  62. bool nrfx_power_irq_enabled;
  63. /**
  64. * @brief The initialization flag
  65. */
  66. #define m_initialized nrfx_power_irq_enabled
  67. /**
  68. * @brief The handler of power fail comparator warning event
  69. */
  70. static nrfx_power_pofwarn_event_handler_t m_pofwarn_handler;
  71. #if NRF_POWER_HAS_SLEEPEVT
  72. /**
  73. * @brief The handler of sleep event handler
  74. */
  75. static nrfx_power_sleep_event_handler_t m_sleepevt_handler;
  76. #endif
  77. #if NRF_POWER_HAS_USBREG
  78. /**
  79. * @brief The handler of USB power events
  80. */
  81. static nrfx_power_usb_event_handler_t m_usbevt_handler;
  82. #endif
  83. /** @} */
  84. nrfx_power_pofwarn_event_handler_t nrfx_power_pof_handler_get(void)
  85. {
  86. return m_pofwarn_handler;
  87. }
  88. #if NRF_POWER_HAS_USBREG
  89. nrfx_power_usb_event_handler_t nrfx_power_usb_handler_get(void)
  90. {
  91. return m_usbevt_handler;
  92. }
  93. #endif
  94. nrfx_err_t nrfx_power_init(nrfx_power_config_t const * p_config)
  95. {
  96. NRFX_ASSERT(p_config);
  97. if (m_initialized)
  98. {
  99. return NRFX_ERROR_ALREADY_INITIALIZED;
  100. }
  101. #if NRF_POWER_HAS_VDDH
  102. nrf_power_dcdcen_vddh_set(p_config->dcdcenhv);
  103. #endif
  104. #if NRF_POWER_HAS_DCDCEN
  105. nrf_power_dcdcen_set(p_config->dcdcen);
  106. #else
  107. nrf_regulators_dcdcen_set(NRF_REGULATORS, p_config->dcdcen);
  108. #endif
  109. nrfx_power_clock_irq_init();
  110. m_initialized = true;
  111. return NRFX_SUCCESS;
  112. }
  113. void nrfx_power_uninit(void)
  114. {
  115. NRFX_ASSERT(m_initialized);
  116. #if NRFX_CHECK(NRFX_CLOCK_ENABLED)
  117. if (!nrfx_clock_irq_enabled)
  118. #endif
  119. {
  120. NRFX_IRQ_DISABLE(nrfx_get_irq_number(NRF_POWER));
  121. }
  122. #if NRF_POWER_HAS_POFCON
  123. nrfx_power_pof_uninit();
  124. #endif
  125. #if NRF_POWER_HAS_SLEEPEVT
  126. nrfx_power_sleepevt_uninit();
  127. #endif
  128. #if NRF_POWER_HAS_USBREG
  129. nrfx_power_usbevt_uninit();
  130. #endif
  131. m_initialized = false;
  132. }
  133. #if NRF_POWER_HAS_POFCON
  134. void nrfx_power_pof_init(nrfx_power_pofwarn_config_t const * p_config)
  135. {
  136. NRFX_ASSERT(p_config != NULL);
  137. nrfx_power_pof_uninit();
  138. if (p_config->handler != NULL)
  139. {
  140. m_pofwarn_handler = p_config->handler;
  141. }
  142. }
  143. void nrfx_power_pof_enable(nrfx_power_pofwarn_config_t const * p_config)
  144. {
  145. nrf_power_pofcon_set(true, p_config->thr);
  146. #if NRF_POWER_HAS_VDDH
  147. nrf_power_pofcon_vddh_set(p_config->thrvddh);
  148. #endif
  149. if (m_pofwarn_handler != NULL)
  150. {
  151. nrf_power_int_enable(NRF_POWER_INT_POFWARN_MASK);
  152. }
  153. }
  154. void nrfx_power_pof_disable(void)
  155. {
  156. nrf_power_pofcon_set(false, NRF_POWER_POFTHR_V27);
  157. nrf_power_int_disable(NRF_POWER_INT_POFWARN_MASK);
  158. }
  159. void nrfx_power_pof_uninit(void)
  160. {
  161. m_pofwarn_handler = NULL;
  162. }
  163. #endif // NRF_POWER_HAS_POFCON
  164. #if NRF_POWER_HAS_SLEEPEVT
  165. void nrfx_power_sleepevt_init(nrfx_power_sleepevt_config_t const * p_config)
  166. {
  167. NRFX_ASSERT(p_config != NULL);
  168. nrfx_power_sleepevt_uninit();
  169. if (p_config->handler != NULL)
  170. {
  171. m_sleepevt_handler = p_config->handler;
  172. }
  173. }
  174. void nrfx_power_sleepevt_enable(nrfx_power_sleepevt_config_t const * p_config)
  175. {
  176. uint32_t enmask = 0;
  177. if (p_config->en_enter)
  178. {
  179. enmask |= NRF_POWER_INT_SLEEPENTER_MASK;
  180. nrf_power_event_clear(NRF_POWER_EVENT_SLEEPENTER);
  181. }
  182. if (p_config->en_exit)
  183. {
  184. enmask |= NRF_POWER_INT_SLEEPEXIT_MASK;
  185. nrf_power_event_clear(NRF_POWER_EVENT_SLEEPEXIT);
  186. }
  187. nrf_power_int_enable(enmask);
  188. }
  189. void nrfx_power_sleepevt_disable(void)
  190. {
  191. nrf_power_int_disable(
  192. NRF_POWER_INT_SLEEPENTER_MASK |
  193. NRF_POWER_INT_SLEEPEXIT_MASK);
  194. }
  195. void nrfx_power_sleepevt_uninit(void)
  196. {
  197. m_sleepevt_handler = NULL;
  198. }
  199. #endif /* NRF_POWER_HAS_SLEEPEVT */
  200. #if NRF_POWER_HAS_USBREG
  201. void nrfx_power_usbevt_init(nrfx_power_usbevt_config_t const * p_config)
  202. {
  203. nrfx_power_usbevt_uninit();
  204. if (p_config->handler != NULL)
  205. {
  206. m_usbevt_handler = p_config->handler;
  207. }
  208. }
  209. void nrfx_power_usbevt_enable(void)
  210. {
  211. nrf_power_int_enable(
  212. NRF_POWER_INT_USBDETECTED_MASK |
  213. NRF_POWER_INT_USBREMOVED_MASK |
  214. NRF_POWER_INT_USBPWRRDY_MASK);
  215. }
  216. void nrfx_power_usbevt_disable(void)
  217. {
  218. nrf_power_int_disable(
  219. NRF_POWER_INT_USBDETECTED_MASK |
  220. NRF_POWER_INT_USBREMOVED_MASK |
  221. NRF_POWER_INT_USBPWRRDY_MASK);
  222. }
  223. void nrfx_power_usbevt_uninit(void)
  224. {
  225. m_usbevt_handler = NULL;
  226. }
  227. #endif /* NRF_POWER_HAS_USBREG */
  228. void nrfx_power_irq_handler(void)
  229. {
  230. uint32_t enabled = nrf_power_int_enable_get();
  231. #if NRF_POWER_HAS_POFCON
  232. if ((0 != (enabled & NRF_POWER_INT_POFWARN_MASK)) &&
  233. nrf_power_event_get_and_clear(NRF_POWER_EVENT_POFWARN))
  234. {
  235. /* Cannot be null if event is enabled */
  236. NRFX_ASSERT(m_pofwarn_handler != NULL);
  237. m_pofwarn_handler();
  238. }
  239. #endif
  240. #if NRF_POWER_HAS_SLEEPEVT
  241. if ((0 != (enabled & NRF_POWER_INT_SLEEPENTER_MASK)) &&
  242. nrf_power_event_get_and_clear(NRF_POWER_EVENT_SLEEPENTER))
  243. {
  244. /* Cannot be null if event is enabled */
  245. NRFX_ASSERT(m_sleepevt_handler != NULL);
  246. m_sleepevt_handler(NRFX_POWER_SLEEP_EVT_ENTER);
  247. }
  248. if ((0 != (enabled & NRF_POWER_INT_SLEEPEXIT_MASK)) &&
  249. nrf_power_event_get_and_clear(NRF_POWER_EVENT_SLEEPEXIT))
  250. {
  251. /* Cannot be null if event is enabled */
  252. NRFX_ASSERT(m_sleepevt_handler != NULL);
  253. m_sleepevt_handler(NRFX_POWER_SLEEP_EVT_EXIT);
  254. }
  255. #endif
  256. #if NRF_POWER_HAS_USBREG
  257. if ((0 != (enabled & NRF_POWER_INT_USBDETECTED_MASK)) &&
  258. nrf_power_event_get_and_clear(NRF_POWER_EVENT_USBDETECTED))
  259. {
  260. /* Cannot be null if event is enabled */
  261. NRFX_ASSERT(m_usbevt_handler != NULL);
  262. m_usbevt_handler(NRFX_POWER_USB_EVT_DETECTED);
  263. }
  264. if ((0 != (enabled & NRF_POWER_INT_USBREMOVED_MASK)) &&
  265. nrf_power_event_get_and_clear(NRF_POWER_EVENT_USBREMOVED))
  266. {
  267. /* Cannot be null if event is enabled */
  268. NRFX_ASSERT(m_usbevt_handler != NULL);
  269. m_usbevt_handler(NRFX_POWER_USB_EVT_REMOVED);
  270. }
  271. if ((0 != (enabled & NRF_POWER_INT_USBPWRRDY_MASK)) &&
  272. nrf_power_event_get_and_clear(NRF_POWER_EVENT_USBPWRRDY))
  273. {
  274. /* Cannot be null if event is enabled */
  275. NRFX_ASSERT(m_usbevt_handler != NULL);
  276. m_usbevt_handler(NRFX_POWER_USB_EVT_READY);
  277. }
  278. #endif
  279. }
  280. #if NRFX_CHECK(NRFX_CLOCK_ENABLED)
  281. /*
  282. * If both POWER and CLOCK drivers are used, a common IRQ handler function must
  283. * be used that calls the handlers in these two drivers. This is because these
  284. * two peripherals share one interrupt.
  285. * This function is located here, not in a separate nrfx_power_clock.c file,
  286. * so that it does not end up as the only symbol in a separate object when
  287. * a library with nrfx is created. In such case, forcing a linker to use this
  288. * function instead of another one defined as weak will require additional
  289. * actions, and might be even impossible.
  290. */
  291. void nrfx_power_clock_irq_handler(void)
  292. {
  293. nrfx_power_irq_handler();
  294. nrfx_clock_irq_handler();
  295. }
  296. #endif
  297. #endif // NRFX_CHECK(NRFX_POWER_ENABLED)