nrfx_power.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**
  2. * Copyright (c) 2017 - 2020, 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. #ifndef NRFX_POWER_H__
  41. #define NRFX_POWER_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_power.h>
  44. #include <nrfx_power_clock.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup nrfx_power POWER driver
  50. * @{
  51. * @ingroup nrf_power
  52. * @brief POWER peripheral driver.
  53. */
  54. /**
  55. * @brief Power mode possible configurations
  56. */
  57. typedef enum
  58. {
  59. NRFX_POWER_MODE_CONSTLAT, /**< Constant latency mode */
  60. NRFX_POWER_MODE_LOWPWR /**< Low power mode */
  61. }nrfx_power_mode_t;
  62. #if NRF_POWER_HAS_SLEEPEVT || defined(__NRFX_DOXYGEN__)
  63. /**
  64. * @brief Events from power system
  65. */
  66. typedef enum
  67. {
  68. NRFX_POWER_SLEEP_EVT_ENTER, /**< CPU entered WFI/WFE sleep
  69. *
  70. * Keep in mind that if this interrupt is enabled,
  71. * it means that CPU was waken up just after WFI by this interrupt.
  72. */
  73. NRFX_POWER_SLEEP_EVT_EXIT /**< CPU exited WFI/WFE sleep */
  74. }nrfx_power_sleep_evt_t;
  75. #endif /* NRF_POWER_HAS_SLEEPEVT */
  76. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  77. /**
  78. * @brief Events from USB power system
  79. */
  80. typedef enum
  81. {
  82. NRFX_POWER_USB_EVT_DETECTED, /**< USB power detected on the connector (plugged in). */
  83. NRFX_POWER_USB_EVT_REMOVED, /**< USB power removed from the connector. */
  84. NRFX_POWER_USB_EVT_READY /**< USB power regulator ready. */
  85. }nrfx_power_usb_evt_t;
  86. /**
  87. * @brief USB power state
  88. *
  89. * The single enumerator that holds all data about current state of USB
  90. * related POWER.
  91. *
  92. * Organized this way that higher power state has higher numeric value
  93. */
  94. typedef enum
  95. {
  96. NRFX_POWER_USB_STATE_DISCONNECTED, /**< No power on USB lines detected. */
  97. NRFX_POWER_USB_STATE_CONNECTED, /**< The USB power is detected, but USB power regulator is not ready. */
  98. NRFX_POWER_USB_STATE_READY /**< From the power viewpoint, USB is ready for working. */
  99. }nrfx_power_usb_state_t;
  100. #endif /* NRF_POWER_HAS_USBREG */
  101. /**
  102. * @name Callback types
  103. *
  104. * Defined types of callback functions.
  105. * @{
  106. */
  107. /**
  108. * @brief Event handler for power failure warning.
  109. */
  110. typedef void (*nrfx_power_pofwarn_event_handler_t)(void);
  111. #if NRF_POWER_HAS_SLEEPEVT || defined(__NRFX_DOXYGEN__)
  112. /**
  113. * @brief Event handler for the sleep events.
  114. *
  115. * @param event Event type
  116. */
  117. typedef void (*nrfx_power_sleep_event_handler_t)(nrfx_power_sleep_evt_t event);
  118. #endif
  119. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  120. /**
  121. * @brief Event handler for the USB-related power events.
  122. *
  123. * @param event Event type
  124. */
  125. typedef void (*nrfx_power_usb_event_handler_t)(nrfx_power_usb_evt_t event);
  126. #endif
  127. /** @} */
  128. /**
  129. * @brief General power configuration
  130. *
  131. * Parameters required to initialize power driver.
  132. */
  133. typedef struct
  134. {
  135. /**
  136. * @brief Enable main DCDC regulator.
  137. *
  138. * This bit only informs the driver that elements for DCDC regulator
  139. * are installed and the regulator can be used.
  140. * The regulator will be enabled or disabled automatically
  141. * by the hardware, basing on current power requirement.
  142. */
  143. bool dcdcen:1;
  144. #if NRF_POWER_HAS_VDDH || defined(__NRFX_DOXYGEN__)
  145. /**
  146. * @brief Enable HV DCDC regulator.
  147. *
  148. * This bit only informs the driver that elements for DCDC regulator
  149. * are installed and the regulator can be used.
  150. * The regulator will be enabled or disabled automatically
  151. * by the hardware, basing on current power requirement.
  152. */
  153. bool dcdcenhv: 1;
  154. #endif
  155. }nrfx_power_config_t;
  156. /**
  157. * @brief The configuration for power failure comparator.
  158. *
  159. * Configuration used to enable and configure the power failure comparator.
  160. */
  161. typedef struct
  162. {
  163. nrfx_power_pofwarn_event_handler_t handler; //!< Event handler.
  164. #if NRF_POWER_HAS_POFCON || defined(__NRFX_DOXYGEN__)
  165. nrf_power_pof_thr_t thr; //!< Threshold for power failure detection
  166. #endif
  167. #if NRF_POWER_HAS_VDDH || defined(__NRFX_DOXYGEN__)
  168. nrf_power_pof_thrvddh_t thrvddh; //!< Threshold for power failure detection on the VDDH pin.
  169. #endif
  170. }nrfx_power_pofwarn_config_t;
  171. #if NRF_POWER_HAS_SLEEPEVT || defined(__NRFX_DOXYGEN__)
  172. /**
  173. * @brief The configuration of sleep event processing.
  174. *
  175. * Configuration used to enable and configure sleep event handling.
  176. */
  177. typedef struct
  178. {
  179. nrfx_power_sleep_event_handler_t handler; //!< Event handler.
  180. bool en_enter:1; //!< Enable event on sleep entering.
  181. bool en_exit :1; //!< Enable event on sleep exiting.
  182. }nrfx_power_sleepevt_config_t;
  183. #endif
  184. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  185. /**
  186. * @brief The configuration of the USB-related power events.
  187. *
  188. * Configuration used to enable and configure USB power event handling.
  189. */
  190. typedef struct
  191. {
  192. nrfx_power_usb_event_handler_t handler; //!< Event processing.
  193. }nrfx_power_usbevt_config_t;
  194. #endif /* NRF_POWER_HAS_USBREG */
  195. /**
  196. * @brief Function for getting the handler of the power failure comparator.
  197. * @return Handler of the power failure comparator.
  198. */
  199. nrfx_power_pofwarn_event_handler_t nrfx_power_pof_handler_get(void);
  200. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  201. /**
  202. * @brief Function for getting the handler of the USB power.
  203. * @return Handler of the USB power.
  204. */
  205. nrfx_power_usb_event_handler_t nrfx_power_usb_handler_get(void);
  206. #endif
  207. /**
  208. * @brief Function for initializing the power module driver.
  209. *
  210. * Enabled power module driver processes all the interrupts from the power system.
  211. *
  212. * @param[in] p_config Pointer to the structure with the initial configuration.
  213. *
  214. * @retval NRFX_SUCCESS Successfully initialized.
  215. * @retval NRFX_ERROR_ALREADY_INITIALIZED Module was already initialized.
  216. */
  217. nrfx_err_t nrfx_power_init(nrfx_power_config_t const * p_config);
  218. /**
  219. * @brief Function for unintializing the power module driver.
  220. *
  221. * Disables all the interrupt handling in the module.
  222. *
  223. * @sa nrfx_power_init
  224. */
  225. void nrfx_power_uninit(void);
  226. #if NRF_POWER_HAS_POFCON || defined(__NRFX_DOXYGEN__)
  227. /**
  228. * @brief Function for initializing the power failure comparator.
  229. *
  230. * Configures the power failure comparator. This function does not set it up and enable it.
  231. * These steps can be done with functions @ref nrfx_power_pof_enable and @ref nrfx_power_pof_disable
  232. * or with the SoftDevice API (when in use).
  233. *
  234. * @param[in] p_config Configuration with values and event handler.
  235. * If event handler is set to NULL, the interrupt will be disabled.
  236. */
  237. void nrfx_power_pof_init(nrfx_power_pofwarn_config_t const * p_config);
  238. /**
  239. * @brief Function for enabling the power failure comparator.
  240. * Sets and enables the interrupt of the power failure comparator. This function cannot be in use
  241. * when SoftDevice is enabled. If the event handler set in the init function is set to NULL, the interrupt
  242. * will be disabled.
  243. *
  244. * @param[in] p_config Configuration with values and event handler.
  245. */
  246. void nrfx_power_pof_enable(nrfx_power_pofwarn_config_t const * p_config);
  247. /**
  248. * @brief Function for disabling the power failure comparator.
  249. *
  250. * Disables the power failure comparator interrupt.
  251. */
  252. void nrfx_power_pof_disable(void);
  253. /**
  254. * @brief Function for clearing the power failure comparator settings.
  255. *
  256. * Clears the settings of the power failure comparator.
  257. */
  258. void nrfx_power_pof_uninit(void);
  259. #endif // NRF_POWER_HAS_POFCON || defined(__NRFX_DOXYGEN__)
  260. #if NRF_POWER_HAS_SLEEPEVT || defined(__NRFX_DOXYGEN__)
  261. /**
  262. * @brief Function for initializing the processing of the sleep events.
  263. *
  264. * Configures and sets up the sleep event processing.
  265. *
  266. * @param[in] p_config Configuration with values and event handler.
  267. *
  268. * @sa nrfx_power_sleepevt_uninit
  269. *
  270. */
  271. void nrfx_power_sleepevt_init(nrfx_power_sleepevt_config_t const * p_config);
  272. /**
  273. * @brief Function for enabling the processing of the sleep events.
  274. *
  275. * @param[in] p_config Configuration with values and event handler.
  276. */
  277. void nrfx_power_sleepevt_enable(nrfx_power_sleepevt_config_t const * p_config);
  278. /** @brief Function for disabling the processing of the sleep events. */
  279. void nrfx_power_sleepevt_disable(void);
  280. /**
  281. * @brief Function for uninitializing the processing of the sleep events.
  282. *
  283. * @sa nrfx_power_sleepevt_init
  284. */
  285. void nrfx_power_sleepevt_uninit(void);
  286. #endif /* NRF_POWER_HAS_SLEEPEVT */
  287. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  288. /**
  289. * @brief Function for initializing the processing of USB power event.
  290. *
  291. * Configures and sets up the USB power event processing.
  292. *
  293. * @param[in] p_config Configuration with values and event handler.
  294. *
  295. * @sa nrfx_power_usbevt_uninit
  296. */
  297. void nrfx_power_usbevt_init(nrfx_power_usbevt_config_t const * p_config);
  298. /** @brief Function for enabling the processing of USB power event. */
  299. void nrfx_power_usbevt_enable(void);
  300. /** @brief Function for disabling the processing of USB power event. */
  301. void nrfx_power_usbevt_disable(void);
  302. /**
  303. * @brief Function for uninitalizing the processing of USB power event.
  304. *
  305. * @sa nrfx_power_usbevt_init
  306. */
  307. void nrfx_power_usbevt_uninit(void);
  308. /**
  309. * @brief Function for getting the status of USB power.
  310. *
  311. * @return Current USB power status.
  312. */
  313. __STATIC_INLINE nrfx_power_usb_state_t nrfx_power_usbstatus_get(void);
  314. #endif /* NRF_POWER_HAS_USBREG */
  315. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  316. #if NRF_POWER_HAS_USBREG
  317. __STATIC_INLINE nrfx_power_usb_state_t nrfx_power_usbstatus_get(void)
  318. {
  319. uint32_t status = nrf_power_usbregstatus_get();
  320. if(0 == (status & NRF_POWER_USBREGSTATUS_VBUSDETECT_MASK))
  321. {
  322. return NRFX_POWER_USB_STATE_DISCONNECTED;
  323. }
  324. if(0 == (status & NRF_POWER_USBREGSTATUS_OUTPUTRDY_MASK))
  325. {
  326. return NRFX_POWER_USB_STATE_CONNECTED;
  327. }
  328. return NRFX_POWER_USB_STATE_READY;
  329. }
  330. #endif /* NRF_POWER_HAS_USBREG */
  331. #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
  332. /** @} */
  333. void nrfx_power_irq_handler(void);
  334. #ifdef __cplusplus
  335. }
  336. #endif
  337. #endif /* NRFX_POWER_H__ */