nrf_pwr_mgmt.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Copyright (c) 2016 - 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. /**
  41. * @defgroup nrf_pwr_mgmt Power management
  42. * @ingroup app_common
  43. * @{
  44. * @brief This module handles power management features.
  45. *
  46. */
  47. #ifndef NRF_PWR_MGMT_H__
  48. #define NRF_PWR_MGMT_H__
  49. #include <stdbool.h>
  50. #include <stdint.h>
  51. #include <sdk_errors.h>
  52. #include "nrf_section_iter.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /**@brief Power management shutdown types. */
  57. typedef enum
  58. {
  59. NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF,
  60. //!< Go to System OFF.
  61. NRF_PWR_MGMT_SHUTDOWN_STAY_IN_SYSOFF,
  62. //!< Go to System OFF and stay there.
  63. /**<
  64. * Useful when battery level is dangerously low, for example.
  65. */
  66. NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU,
  67. //!< Go to DFU mode.
  68. NRF_PWR_MGMT_SHUTDOWN_RESET,
  69. //!< Reset chip.
  70. NRF_PWR_MGMT_SHUTDOWN_CONTINUE
  71. //!< Continue shutdown.
  72. /**<
  73. * This should be used by modules that block the shutdown process, when they become ready for
  74. * shutdown.
  75. */
  76. } nrf_pwr_mgmt_shutdown_t;
  77. /**@brief Shutdown event types. */
  78. typedef enum
  79. {
  80. NRF_PWR_MGMT_EVT_PREPARE_WAKEUP = NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF,
  81. //!< Application will prepare the wakeup mechanism.
  82. NRF_PWR_MGMT_EVT_PREPARE_SYSOFF = NRF_PWR_MGMT_SHUTDOWN_STAY_IN_SYSOFF,
  83. //!< Application will prepare to stay in System OFF state.
  84. NRF_PWR_MGMT_EVT_PREPARE_DFU = NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU,
  85. //!< Application will prepare to enter DFU mode.
  86. NRF_PWR_MGMT_EVT_PREPARE_RESET = NRF_PWR_MGMT_SHUTDOWN_RESET,
  87. //!< Application will prepare to chip reset.
  88. } nrf_pwr_mgmt_evt_t;
  89. /**@brief Shutdown callback.
  90. * @param[in] event Type of shutdown process.
  91. *
  92. * @retval true System OFF / Enter DFU preparation successful. Process will be continued.
  93. * @retval false System OFF / Enter DFU preparation failed. @ref NRF_PWR_MGMT_SHUTDOWN_CONTINUE
  94. * should be used to continue the shutdown process.
  95. */
  96. typedef bool (*nrf_pwr_mgmt_shutdown_handler_t)(nrf_pwr_mgmt_evt_t event);
  97. /**@brief Macro for registering a shutdown handler. Modules that want to get events
  98. * from this module must register the handler using this macro.
  99. *
  100. * @details This macro places the handler in a section named "pwr_mgmt_data".
  101. *
  102. * @param[in] _handler Event handler (@ref nrf_pwr_mgmt_shutdown_handler_t).
  103. * @param[in] _priority Priority of the given handler.
  104. */
  105. #define NRF_PWR_MGMT_HANDLER_REGISTER(_handler, _priority) \
  106. STATIC_ASSERT(_priority < NRF_PWR_MGMT_CONFIG_HANDLER_PRIORITY_COUNT); \
  107. /*lint -esym(528,*_handler_function) -esym(529,*_handler_function) : Symbol not referenced. */ \
  108. NRF_SECTION_SET_ITEM_REGISTER(pwr_mgmt_data, _priority, \
  109. static nrf_pwr_mgmt_shutdown_handler_t const CONCAT_2(_handler, _handler_function)) = (_handler)
  110. /**@brief Function for initializing power management.
  111. *
  112. * @warning Depending on configuration, this function sets SEVONPEND in System Control Block (SCB).
  113. * This operation is unsafe with the SoftDevice from interrupt priority higher than SVC.
  114. *
  115. * @retval NRF_SUCCESS
  116. */
  117. ret_code_t nrf_pwr_mgmt_init(void);
  118. /**@brief Function for running power management. Should run in the main loop.
  119. */
  120. void nrf_pwr_mgmt_run(void);
  121. /**@brief Function for indicating activity.
  122. *
  123. * @details Call this function whenever doing something that constitutes "activity".
  124. * For example, whenever sending data, call this function to indicate that the application
  125. * is active and should not disconnect any ongoing communication links.
  126. */
  127. void nrf_pwr_mgmt_feed(void);
  128. /**@brief Function for shutting down the system.
  129. *
  130. * @param[in] shutdown_type Type of operation.
  131. *
  132. * @details All callbacks will be executed prior to shutdown.
  133. */
  134. void nrf_pwr_mgmt_shutdown(nrf_pwr_mgmt_shutdown_t shutdown_type);
  135. #ifdef __cplusplus
  136. }
  137. #endif
  138. #endif // NRF_PWR_MGMT_H__
  139. /** @} */