nrfx_power.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * Copyright (c) 2017 - 2018, 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 point of view 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 entering/exiting sleep
  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 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 regulator can be used.
  140. * The regulator would 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 regulator can be used.
  150. * The regulator would 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 power failure comparator
  160. */
  161. typedef struct
  162. {
  163. nrfx_power_pofwarn_event_handler_t handler; //!< Event handler
  164. nrf_power_pof_thr_t thr; //!< Threshold for power failure detection
  165. #if NRF_POWER_HAS_VDDH || defined(__NRFX_DOXYGEN__)
  166. nrf_power_pof_thrvddh_t thrvddh; //!< Threshold for power failure detection on VDDH pin
  167. #endif
  168. }nrfx_power_pofwarn_config_t;
  169. #if NRF_POWER_HAS_SLEEPEVT || defined(__NRFX_DOXYGEN__)
  170. /**
  171. * @brief The configuration of sleep event processing
  172. *
  173. * Configuration used to enable and configure sleep event handling
  174. */
  175. typedef struct
  176. {
  177. nrfx_power_sleep_event_handler_t handler; //!< Event handler
  178. bool en_enter:1; //!< Enable event on sleep entering
  179. bool en_exit :1; //!< Enable event on sleep exiting
  180. }nrfx_power_sleepevt_config_t;
  181. #endif
  182. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  183. /**
  184. * @brief The configuration of USB related power events
  185. *
  186. * Configuration used to enable and configure USB power event handling
  187. */
  188. typedef struct
  189. {
  190. nrfx_power_usb_event_handler_t handler; //!< Event processing
  191. }nrfx_power_usbevt_config_t;
  192. #endif /* NRF_POWER_HAS_USBREG */
  193. /**
  194. * @brief Function for getting the handler of the power failure comparator.
  195. * @return Handler of the power failure comparator.
  196. */
  197. nrfx_power_pofwarn_event_handler_t nrfx_power_pof_handler_get(void);
  198. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  199. /**
  200. * @brief Function for getting the handler of the USB power.
  201. * @return Handler of the USB power.
  202. */
  203. nrfx_power_usb_event_handler_t nrfx_power_usb_handler_get(void);
  204. #endif
  205. /**
  206. * @brief Initialize power module driver
  207. *
  208. * Enabled power module driver would process all the interrupts from power system.
  209. *
  210. * @param[in] p_config Pointer to the structure with initial configuration.
  211. *
  212. * @retval NRFX_SUCCESS Successfully initialized.
  213. * @retval NRFX_ERROR_ALREADY_INITIALIZED Module was already initialized.
  214. */
  215. nrfx_err_t nrfx_power_init(nrfx_power_config_t const * p_config);
  216. /**
  217. * @brief Unintialize power module driver
  218. *
  219. * Disables all the interrupt handling in the module.
  220. *
  221. * @sa nrfx_power_init
  222. */
  223. void nrfx_power_uninit(void);
  224. /**
  225. * @brief Initialize power failure comparator
  226. *
  227. * Configures the power failure comparator. This function does not setup and enable it.
  228. * Those steps can be done with functions @ref nrfx_power_pof_enable and @ref nrfx_power_pof_disable
  229. * or with Softdevice API (when Softdevice is using).
  230. *
  231. * @param[in] p_config Configuration with values and event handler.
  232. * If event handler is set to NULL, interrupt would be disabled.
  233. */
  234. void nrfx_power_pof_init(nrfx_power_pofwarn_config_t const * p_config);
  235. /**
  236. * @brief Enable power failure comparator
  237. * Sets and enables interrupt of the power failure comparator. This functions cannot be using
  238. * when Softdevice is enabled. If event handler set in init function is set to NULL, interrupt
  239. * would be disabled.
  240. *
  241. * @param[in] p_config Configuration with values and event handler.
  242. */
  243. void nrfx_power_pof_enable(nrfx_power_pofwarn_config_t const * p_config);
  244. /**
  245. * @brief Disable the power failure comparator
  246. *
  247. * Disables the power failure comparator interrupt.
  248. */
  249. void nrfx_power_pof_disable(void);
  250. /**
  251. * @brief Clear the power failure comparator settings
  252. *
  253. * Clears the settings of the power failure comparator.
  254. */
  255. void nrfx_power_pof_uninit(void);
  256. #if NRF_POWER_HAS_SLEEPEVT || defined(__NRFX_DOXYGEN__)
  257. /**
  258. * @brief Initialize sleep entering and exiting events processing
  259. *
  260. * Configures and setups the sleep event processing.
  261. *
  262. * @param[in] p_config Configuration with values and event handler.
  263. *
  264. * @sa nrfx_power_sleepevt_uninit
  265. *
  266. */
  267. void nrfx_power_sleepevt_init(nrfx_power_sleepevt_config_t const * p_config);
  268. /**
  269. * @brief Enable sleep entering and exiting events processing
  270. *
  271. * @param[in] p_config Configuration with values and event handler.
  272. */
  273. void nrfx_power_sleepevt_enable(nrfx_power_sleepevt_config_t const * p_config);
  274. /**
  275. * @brief Disable sleep entering and exiting events processing
  276. */
  277. void nrfx_power_sleepevt_disable(void);
  278. /**
  279. * @brief Uninitialize sleep entering and exiting events processing
  280. *
  281. * @sa nrfx_power_sleepevt_init
  282. */
  283. void nrfx_power_sleepevt_uninit(void);
  284. #endif /* NRF_POWER_HAS_SLEEPEVT */
  285. #if NRF_POWER_HAS_USBREG || defined(__NRFX_DOXYGEN__)
  286. /**
  287. * @brief Initialize USB power event processing
  288. *
  289. * Configures and setups the USB power event processing.
  290. *
  291. * @param[in] p_config Configuration with values and event handler.
  292. *
  293. * @sa nrfx_power_usbevt_uninit
  294. */
  295. void nrfx_power_usbevt_init(nrfx_power_usbevt_config_t const * p_config);
  296. /**
  297. * @brief Enable USB power event processing
  298. */
  299. void nrfx_power_usbevt_enable(void);
  300. /**
  301. * @brief Disable USB power event processing
  302. */
  303. void nrfx_power_usbevt_disable(void);
  304. /**
  305. * @brief Uninitalize USB power event processing
  306. *
  307. * @sa nrfx_power_usbevt_init
  308. */
  309. void nrfx_power_usbevt_uninit(void);
  310. /**
  311. * @brief Get the status of USB power
  312. *
  313. * @return Current USB power status
  314. */
  315. __STATIC_INLINE nrfx_power_usb_state_t nrfx_power_usbstatus_get(void);
  316. #endif /* NRF_POWER_HAS_USBREG */
  317. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  318. #if NRF_POWER_HAS_USBREG
  319. __STATIC_INLINE nrfx_power_usb_state_t nrfx_power_usbstatus_get(void)
  320. {
  321. uint32_t status = nrf_power_usbregstatus_get();
  322. if(0 == (status & NRF_POWER_USBREGSTATUS_VBUSDETECT_MASK))
  323. {
  324. return NRFX_POWER_USB_STATE_DISCONNECTED;
  325. }
  326. if(0 == (status & NRF_POWER_USBREGSTATUS_OUTPUTRDY_MASK))
  327. {
  328. return NRFX_POWER_USB_STATE_CONNECTED;
  329. }
  330. return NRFX_POWER_USB_STATE_READY;
  331. }
  332. #endif /* NRF_POWER_HAS_USBREG */
  333. #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
  334. void nrfx_power_irq_handler(void);
  335. /** @} */
  336. #ifdef __cplusplus
  337. }
  338. #endif
  339. #endif /* NRFX_POWER_H__ */