nrfx_wdt(5384).c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Copyright (c) 2015 - 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. #include <nrfx.h>
  41. #if NRFX_CHECK(NRFX_WDT_ENABLED)
  42. #include <nrfx_wdt.h>
  43. #define NRFX_LOG_MODULE WDT
  44. #include <nrfx_log.h>
  45. /**@brief WDT state. */
  46. static nrfx_drv_state_t m_state;
  47. /**@brief WDT alloc table. */
  48. static uint8_t m_alloc_index;
  49. #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
  50. /**@brief WDT event handler. */
  51. static nrfx_wdt_event_handler_t m_wdt_event_handler;
  52. /**@brief WDT interrupt handler. */
  53. void nrfx_wdt_irq_handler(void)
  54. {
  55. if (nrf_wdt_event_check(NRF_WDT_EVENT_TIMEOUT))
  56. {
  57. m_wdt_event_handler();
  58. nrf_wdt_event_clear(NRF_WDT_EVENT_TIMEOUT);
  59. }
  60. }
  61. #endif
  62. nrfx_err_t nrfx_wdt_init(nrfx_wdt_config_t const * p_config,
  63. nrfx_wdt_event_handler_t wdt_event_handler)
  64. {
  65. NRFX_ASSERT(p_config);
  66. nrfx_err_t err_code;
  67. #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
  68. NRFX_ASSERT(wdt_event_handler != NULL);
  69. m_wdt_event_handler = wdt_event_handler;
  70. #else
  71. NRFX_ASSERT(wdt_event_handler == NULL);
  72. (void)wdt_event_handler;
  73. #endif
  74. if (m_state == NRFX_DRV_STATE_UNINITIALIZED)
  75. {
  76. m_state = NRFX_DRV_STATE_INITIALIZED;
  77. }
  78. else
  79. {
  80. err_code = NRFX_ERROR_INVALID_STATE;
  81. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  82. __func__,
  83. NRFX_LOG_ERROR_STRING_GET(err_code));
  84. return err_code;
  85. }
  86. nrf_wdt_behaviour_set(p_config->behaviour);
  87. uint64_t ticks = (p_config->reload_value * 32768ULL) / 1000;
  88. NRFX_ASSERT(ticks <= UINT32_MAX);
  89. nrf_wdt_reload_value_set((uint32_t) ticks);
  90. #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
  91. NRFX_IRQ_PRIORITY_SET(WDT_IRQn, p_config->interrupt_priority);
  92. NRFX_IRQ_ENABLE(WDT_IRQn);
  93. #endif
  94. err_code = NRFX_SUCCESS;
  95. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
  96. return err_code;
  97. }
  98. void nrfx_wdt_enable(void)
  99. {
  100. NRFX_ASSERT(m_alloc_index != 0);
  101. NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
  102. #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
  103. nrf_wdt_int_enable(NRF_WDT_INT_TIMEOUT_MASK);
  104. #endif
  105. nrf_wdt_task_trigger(NRF_WDT_TASK_START);
  106. m_state = NRFX_DRV_STATE_POWERED_ON;
  107. NRFX_LOG_INFO("Enabled.");
  108. }
  109. void nrfx_wdt_feed(void)
  110. {
  111. NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
  112. for (uint8_t i = 0; i < m_alloc_index; i++)
  113. {
  114. nrf_wdt_reload_request_set((nrf_wdt_rr_register_t)(NRF_WDT_RR0 + i));
  115. }
  116. }
  117. nrfx_err_t nrfx_wdt_channel_alloc(nrfx_wdt_channel_id * p_channel_id)
  118. {
  119. nrfx_err_t result;
  120. NRFX_ASSERT(p_channel_id);
  121. NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
  122. NRFX_CRITICAL_SECTION_ENTER();
  123. if (m_alloc_index < NRF_WDT_CHANNEL_NUMBER)
  124. {
  125. *p_channel_id = (nrfx_wdt_channel_id)(NRF_WDT_RR0 + m_alloc_index);
  126. m_alloc_index++;
  127. nrf_wdt_reload_request_enable(*p_channel_id);
  128. result = NRFX_SUCCESS;
  129. }
  130. else
  131. {
  132. result = NRFX_ERROR_NO_MEM;
  133. }
  134. NRFX_CRITICAL_SECTION_EXIT();
  135. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(result));
  136. return result;
  137. }
  138. void nrfx_wdt_channel_feed(nrfx_wdt_channel_id channel_id)
  139. {
  140. NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
  141. nrf_wdt_reload_request_set(channel_id);
  142. }
  143. #endif // NRFX_CHECK(NRFX_WDT_ENABLED)