bsp_btn_ant.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Copyright (c) 2015 - 2018, 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(BSP_BTN_ANT)
  42. #include "bsp_btn_ant.h"
  43. #include "nrf_sdh_ant.h"
  44. #include "bsp.h"
  45. static bool m_connected = false; /**< Notify if channel is connected. */
  46. static uint8_t m_channel = UINT8_MAX; /**< ANT channel number linked to indication. */
  47. static uint8_t m_channel_type; /**< Type of linked ANT channel. */
  48. /**@brief Function for configuring the buttons for connection.
  49. *
  50. * @retval NRF_SUCCESS Configured successfully.
  51. * @return A propagated error code.
  52. */
  53. static ret_code_t connection_buttons_configure(void)
  54. {
  55. ret_code_t err_code = bsp_event_to_button_action_assign(BSP_BTN_ANT_CONFIG_SLEEP_BTN_ID,
  56. BSP_BUTTON_ACTION_RELEASE,
  57. BSP_EVENT_DEFAULT);
  58. if (err_code == NRF_ERROR_INVALID_PARAM)
  59. {
  60. return NRF_SUCCESS;
  61. }
  62. return err_code;
  63. }
  64. /**@brief Function for configuring the buttons for searching.
  65. *
  66. * @retval NRF_SUCCESS Configured successfully.
  67. * @return A propagated error code.
  68. */
  69. static ret_code_t searching_buttons_configure(void)
  70. {
  71. ret_code_t err_code = bsp_event_to_button_action_assign(BSP_BTN_ANT_CONFIG_SLEEP_BTN_ID,
  72. BSP_BUTTON_ACTION_RELEASE,
  73. BSP_EVENT_SLEEP);
  74. if (err_code == NRF_ERROR_INVALID_PARAM)
  75. {
  76. return NRF_SUCCESS;
  77. }
  78. return err_code;
  79. }
  80. ret_code_t bsp_btn_ant_sleep_mode_prepare(void)
  81. {
  82. ret_code_t err_code = bsp_buttons_disable();
  83. if (err_code != NRF_SUCCESS)
  84. {
  85. return err_code;
  86. }
  87. err_code = bsp_wakeup_button_enable(BSP_BTN_ANT_CONFIG_WAKEUP_BTN_ID);
  88. if (err_code == NRF_ERROR_NOT_SUPPORTED)
  89. {
  90. return NRF_SUCCESS;
  91. }
  92. return err_code;
  93. }
  94. /**
  95. * @brief Function for handling ANT events.
  96. *
  97. * @param[in] p_ant_evt Event received from the ANT stack.
  98. * @param[in] p_context Context.
  99. */
  100. static void ant_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  101. {
  102. ret_code_t err_code = NRF_SUCCESS;
  103. if (m_channel != p_ant_evt->channel)
  104. {
  105. return;
  106. }
  107. switch (m_channel_type)
  108. {
  109. case CHANNEL_TYPE_SLAVE:
  110. /* fall through */
  111. case CHANNEL_TYPE_SLAVE_RX_ONLY:
  112. switch (p_ant_evt->event)
  113. {
  114. case EVENT_RX:
  115. if (!m_connected)
  116. {
  117. err_code = connection_buttons_configure();
  118. }
  119. m_connected = true;
  120. break;
  121. case EVENT_RX_FAIL_GO_TO_SEARCH:
  122. m_connected = false;
  123. err_code = searching_buttons_configure();
  124. break;
  125. default:
  126. break;
  127. }
  128. break;
  129. }
  130. APP_ERROR_CHECK(err_code);
  131. }
  132. NRF_SDH_ANT_OBSERVER(m_ant_observer, BSP_BTN_ANT_OBSERVER_PRIO, ant_evt_handler, NULL);
  133. ret_code_t bsp_btn_ant_init(uint8_t channel, uint8_t channel_type)
  134. {
  135. ret_code_t err_code = NRF_SUCCESS;
  136. m_channel = channel;
  137. m_channel_type = channel_type;
  138. if (!m_connected)
  139. {
  140. err_code = searching_buttons_configure();
  141. }
  142. else
  143. {
  144. err_code = connection_buttons_configure();
  145. }
  146. return err_code;
  147. }
  148. #endif // NRF_MODULE_ENABLED(BSP_BTN_ANT)