sys_sleep.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  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. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef SYS_SLEEP_H_INCLUDED
  43. #define SYS_SLEEP_H_INCLUDED
  44. #include <stdint.h>
  45. #include "sys_events.h"
  46. #include "hal_sleep.h"
  47. /** @file
  48. *
  49. * @defgroup sys_sleep Falling Asleep API
  50. * @ingroup sys_15_4
  51. * @{
  52. * @brief Module for declaring the Falling Asleep API.
  53. * @details Because additional preparation may be required to be done by user modules,
  54. * prior to putting hardware into the sleep mode, a notification and approval mechanism
  55. * is provided to the user.
  56. * Each module that wants to be notified about the "falling asleep" event, has to subscribe
  57. * to the HAL_EVENT_FALLING_ASLEEP event, using sys_sleep_approver_register(), and to
  58. * get the unique approver's ID value.
  59. * In the handler of the HAL_EVENT_FALLING_ASLEEP event, the module is able to perform
  60. * the required preparation before falling asleep, and to approve the falling asleep request,
  61. * using the module unique approver ID, after all preparation to sleep is finished.
  62. * The hardware will fall asleep only after all the registered approvers
  63. * approve the fall asleep request.
  64. */
  65. /**@brief Approver ID typedef.
  66. */
  67. typedef uint8_t sys_sleep_approver_id_t;
  68. /* Sanity check for CONFIG_MAX_SLEEP_APPROVERS
  69. */
  70. #if (!defined(CONFIG_MAX_SLEEP_APPROVERS))
  71. # error "CONFIG_MAX_SLEEP_APPROVERS must be defined in config file"
  72. #elif (CONFIG_MAX_SLEEP_APPROVERS >= 256)
  73. # error "CONFIG_MAX_SLEEP_APPROVERS must be less than 256"
  74. #endif
  75. /**@brief Function for initializing the system sleep module.
  76. *
  77. * @details This function must be called before any usage of the System Sleep module.
  78. */
  79. void sys_sleep_init(void);
  80. /**@brief Function for registering the approver of the system sleep request.
  81. *
  82. * @details After the sleep approver is registered with this function, the hardware will
  83. * not fall asleep without its approval.
  84. *
  85. * @param[in] p_event_falling_asleep Event descriptor, which will handle
  86. * the SYS_EVENT_FALLING_ASLEEP event.
  87. * @param[in] p_event_wake_up Event descriptor, which will handle
  88. * the SYS_EVENT_WAKE_UP event.
  89. *
  90. * @retval The unique approver ID, reserved for this newly-registered approver.
  91. * This ID will be required to approve system sleep requests by this approver module.
  92. */
  93. sys_sleep_approver_id_t sys_sleep_approver_register(
  94. sys_event_desc_t * p_event_falling_asleep,
  95. sys_event_desc_t * p_event_wake_up);
  96. /**@brief Function for unregistering the approver of the system sleep request.
  97. *
  98. * @details After the approver is unregistered, its approval will not be
  99. * required to put the system into sleep mode.
  100. *
  101. * @param[in] approver_id The unique approver ID to be unregistered.
  102. * @param[in] p_event_falling_asleep Event descriptor to unsubscribe from
  103. * the SYS_EVENT_FALLING_ASLEEP event.
  104. * @param[in] p_event_wake_up Event descriptor to unsubscribe from
  105. * the SYS_EVENT_WAKE_UP event.
  106. */
  107. void sys_sleep_approver_unregister(
  108. sys_sleep_approver_id_t approver_id,
  109. sys_event_desc_t * p_event_falling_asleep,
  110. sys_event_desc_t * p_event_wake_up);
  111. /**@brief Function for approving the system sleep request.
  112. *
  113. * @details This function is to be called by the registered approver
  114. * in order to approve putting the system into the sleep mode.
  115. *
  116. * @param[in] approver_id The unique approver ID.
  117. */
  118. void sys_sleep_approve(sys_sleep_approver_id_t approver_id);
  119. /**@brief Function for requesting the system to safely enter into sleep mode.
  120. *
  121. * @details This function notifies all the registered sleep approvers with the
  122. * HAL_EVENT_FALLING_ASLEEP event, allowing them to perform all the needed preparation
  123. * before the hardware falls asleep. The hardware will enter sleep mode only after
  124. * all registered approvers approve the fall asleep request.
  125. *
  126. * @param[in] sleep_time_ms Defines sleep time in ms.
  127. */
  128. void sys_sleep_request_ms(uint32_t sleep_time_ms);
  129. /**@brief Function for getting information about the wakeup reason.
  130. *
  131. * @retval hal_wakeup_reason Interrupt source which was the wakeup reason.
  132. */
  133. hal_wakeup_reason_t sys_sleep_wakeup_reason(void);
  134. /** @} */
  135. #endif // SYS_SLEEP_H_INCLUDED