nrf_pwr_mgmt.h 5.8 KB

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