nrf_drv_wdt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "nrf_drv_wdt.h"
  13. #include "nrf_drv_common.h"
  14. #include "nrf_error.h"
  15. #include "nrf_assert.h"
  16. #include "nrf_wdt.h"
  17. #include "app_util_platform.h"
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. /**@brief WDT event handler. */
  21. static nrf_wdt_event_handler_t m_wdt_event_handler;
  22. /**@brief WDT state. */
  23. static nrf_drv_state_t m_state;
  24. /**@brief WDT alloc table. */
  25. static uint32_t m_alloc_index;
  26. static const nrf_drv_wdt_config_t m_default_config = NRF_DRV_WDT_DEAFULT_CONFIG;
  27. /**@brief WDT interrupt handler. */
  28. void WDT_IRQHandler(void)
  29. {
  30. if (nrf_wdt_int_enable_check(NRF_WDT_INT_TIMEOUT_MASK) == true)
  31. {
  32. nrf_wdt_event_clear(NRF_WDT_EVENT_TIMEOUT);
  33. m_wdt_event_handler();
  34. }
  35. }
  36. ret_code_t nrf_drv_wdt_init(nrf_drv_wdt_config_t const * p_config,
  37. nrf_wdt_event_handler_t wdt_event_handler)
  38. {
  39. ASSERT(wdt_event_handler != NULL);
  40. m_wdt_event_handler = wdt_event_handler;
  41. if (m_state == NRF_DRV_STATE_UNINITIALIZED)
  42. {
  43. m_state = NRF_DRV_STATE_INITIALIZED;
  44. }
  45. else
  46. {
  47. return NRF_ERROR_INVALID_STATE; // WDT already initialized
  48. }
  49. if (p_config == NULL)
  50. {
  51. p_config = &m_default_config;
  52. }
  53. nrf_wdt_behaviour_set(p_config->behaviour);
  54. nrf_wdt_reload_value_set((p_config->reload_value * 32768) / 1000);
  55. nrf_drv_common_irq_enable(WDT_IRQn, p_config->interrupt_priority);
  56. return NRF_SUCCESS;
  57. }
  58. void nrf_drv_wdt_enable(void)
  59. {
  60. ASSERT(m_alloc_index != 0);
  61. ASSERT(m_state == NRF_DRV_STATE_INITIALIZED);
  62. nrf_wdt_int_enable(NRF_WDT_INT_TIMEOUT_MASK);
  63. nrf_wdt_task_trigger(NRF_WDT_TASK_START);
  64. m_state = NRF_DRV_STATE_POWERED_ON;
  65. }
  66. void nrf_drv_wdt_feed(void)
  67. {
  68. ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
  69. for(uint32_t i = 0; i < m_alloc_index; i++)
  70. {
  71. nrf_wdt_reload_request_set((nrf_wdt_rr_register_t)(NRF_WDT_RR0 + i));
  72. }
  73. }
  74. ret_code_t nrf_drv_wdt_channel_alloc(nrf_drv_wdt_channel_id * p_channel_id)
  75. {
  76. ret_code_t result;
  77. ASSERT(p_channel_id);
  78. ASSERT(m_state == NRF_DRV_STATE_INITIALIZED);
  79. CRITICAL_REGION_ENTER();
  80. if (m_alloc_index < NRF_WDT_CHANNEL_NUMBER)
  81. {
  82. *p_channel_id = (nrf_drv_wdt_channel_id)(NRF_WDT_RR0 + m_alloc_index);
  83. m_alloc_index++;
  84. nrf_wdt_reload_request_enable(*p_channel_id);
  85. result = NRF_SUCCESS;
  86. }
  87. else
  88. {
  89. result = NRF_ERROR_NO_MEM;
  90. }
  91. CRITICAL_REGION_EXIT();
  92. return result;
  93. }
  94. void nrf_drv_wdt_channel_feed(nrf_drv_wdt_channel_id channel_id)
  95. {
  96. ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
  97. nrf_wdt_reload_request_set(channel_id);
  98. }