nrf_sdh_ant.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Copyright (c) 2017 - 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_SDH_ANT)
  42. #include "nrf_sdh_ant.h"
  43. #include "nrf_sdh.h"
  44. #include "app_error.h"
  45. #include "nrf_strerror.h"
  46. #include "ant_interface.h"
  47. #define NRF_LOG_MODULE_NAME nrf_sdh_ant
  48. #if NRF_SDH_ANT_LOG_ENABLED
  49. #define NRF_LOG_LEVEL NRF_SDH_ANT_LOG_LEVEL
  50. #define NRF_LOG_INFO_COLOR NRF_SDH_ANT_INFO_COLOR
  51. #define NRF_LOG_DEBUG_COLOR NRF_SDH_ANT_DEBUG_COLOR
  52. #else
  53. #define NRF_LOG_LEVEL 0
  54. #endif // NRF_SDH_ANT_LOG_ENABLED
  55. #include "nrf_log.h"
  56. NRF_LOG_MODULE_REGISTER();
  57. STATIC_ASSERT(NRF_SDH_ANT_TOTAL_CHANNELS_ALLOCATED <= MAX_ANT_CHANNELS);
  58. STATIC_ASSERT(NRF_SDH_ANT_ENCRYPTED_CHANNELS <= NRF_SDH_ANT_TOTAL_CHANNELS_ALLOCATED);
  59. // Create section set "sdh_ant_observers".
  60. NRF_SECTION_SET_DEF(sdh_ant_observers, nrf_sdh_ant_evt_observer_t, NRF_SDH_ANT_OBSERVER_PRIO_LEVELS);
  61. // Memory buffer provided in order to support channel configuration.
  62. __ALIGN(4) static uint8_t m_ant_stack_buffer[NRF_SDH_ANT_BUF_SIZE];
  63. static bool m_stack_is_enabled;
  64. ret_code_t nrf_sdh_ant_enable(void)
  65. {
  66. ANT_ENABLE ant_enable_cfg =
  67. {
  68. .ucTotalNumberOfChannels = NRF_SDH_ANT_TOTAL_CHANNELS_ALLOCATED,
  69. .ucNumberOfEncryptedChannels = NRF_SDH_ANT_ENCRYPTED_CHANNELS,
  70. .usNumberOfEvents = NRF_SDH_ANT_EVENT_QUEUE_SIZE,
  71. .pucMemoryBlockStartLocation = m_ant_stack_buffer,
  72. .usMemoryBlockByteSize = sizeof(m_ant_stack_buffer),
  73. };
  74. ret_code_t ret_code = sd_ant_enable(&ant_enable_cfg);
  75. if (ret_code == NRF_SUCCESS)
  76. {
  77. m_stack_is_enabled = true;
  78. }
  79. else
  80. {
  81. NRF_LOG_ERROR("sd_ant_enable() returned %s.", nrf_strerror_get(ret_code));
  82. }
  83. return ret_code;
  84. }
  85. /**@brief Function for polling ANT events.
  86. *
  87. * @param[in] p_context Context of the observer.
  88. */
  89. static void nrf_sdh_ant_evts_poll(void * p_context)
  90. {
  91. UNUSED_VARIABLE(p_context);
  92. ret_code_t ret_code;
  93. #ifndef SER_CONNECTIVITY
  94. if (!m_stack_is_enabled)
  95. {
  96. return;
  97. }
  98. #else
  99. UNUSED_VARIABLE(m_stack_is_enabled);
  100. #endif // SER_CONNECTIVITY
  101. while (true)
  102. {
  103. ant_evt_t ant_evt;
  104. ret_code = sd_ant_event_get(&ant_evt.channel, &ant_evt.event, ant_evt.message.aucMessage);
  105. if (ret_code != NRF_SUCCESS)
  106. {
  107. break;
  108. }
  109. NRF_LOG_DEBUG("ANT Event 0x%02X Channel 0x%02X", ant_evt.event, ant_evt.channel);
  110. // Forward the event to ANT observers.
  111. nrf_section_iter_t iter;
  112. for (nrf_section_iter_init(&iter, &sdh_ant_observers);
  113. nrf_section_iter_get(&iter) != NULL;
  114. nrf_section_iter_next(&iter))
  115. {
  116. nrf_sdh_ant_evt_observer_t * p_observer;
  117. nrf_sdh_ant_evt_handler_t handler;
  118. p_observer = (nrf_sdh_ant_evt_observer_t *) nrf_section_iter_get(&iter);
  119. handler = p_observer->handler;
  120. handler(&ant_evt, p_observer->p_context);
  121. }
  122. }
  123. if (ret_code != NRF_ERROR_NOT_FOUND)
  124. {
  125. APP_ERROR_HANDLER(ret_code);
  126. }
  127. }
  128. NRF_SDH_STACK_OBSERVER(m_nrf_sdh_ant_evts_poll, NRF_SDH_ANT_STACK_OBSERVER_PRIO) =
  129. {
  130. .handler = nrf_sdh_ant_evts_poll,
  131. .p_context = NULL,
  132. };
  133. #endif // NRF_MODULE_ENABLED(NRF_SDH_ANT)