ant_hrm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "sdk_config.h"
  41. #if ANT_HRM_ENABLED
  42. #include "nrf_assert.h"
  43. #include "app_error.h"
  44. #include "ant_interface.h"
  45. #include "app_util.h"
  46. #include "ant_hrm.h"
  47. #include "ant_hrm_utils.h"
  48. #include "app_error.h"
  49. #define NRF_LOG_MODULE_NAME ant_hrm
  50. #if ANT_HRM_LOG_ENABLED
  51. #define NRF_LOG_LEVEL ANT_HRM_LOG_LEVEL
  52. #define NRF_LOG_INFO_COLOR ANT_HRM_INFO_COLOR
  53. #else // ANT_HRM_LOG_ENABLED
  54. #define NRF_LOG_LEVEL 0
  55. #endif // ANT_HRM_LOG_ENABLED
  56. #include "nrf_log.h"
  57. NRF_LOG_MODULE_REGISTER();
  58. #define BACKGROUND_DATA_INTERVAL 64 /**< The number of main data pages sent between background data page.
  59. Background data page is sent every 65th message. */
  60. #define TX_TOGGLE_DIVISOR 4 /**< The number of messages between changing state of toggle bit. */
  61. /**@brief HRM message data layout structure. */
  62. typedef struct
  63. {
  64. ant_hrm_page_t page_number : 7;
  65. uint8_t toggle_bit : 1;
  66. uint8_t page_payload[7];
  67. } ant_hrm_message_layout_t;
  68. /**@brief Function for initializing the ANT HRM profile instance.
  69. *
  70. * @param[in] p_profile Pointer to the profile instance.
  71. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  72. *
  73. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  74. */
  75. static ret_code_t ant_hrm_init(ant_hrm_profile_t * p_profile,
  76. ant_channel_config_t const * p_channel_config)
  77. {
  78. p_profile->channel_number = p_channel_config->channel_number;
  79. p_profile->page_0 = DEFAULT_ANT_HRM_PAGE0();
  80. p_profile->page_1 = DEFAULT_ANT_HRM_PAGE1();
  81. p_profile->page_2 = DEFAULT_ANT_HRM_PAGE2();
  82. p_profile->page_3 = DEFAULT_ANT_HRM_PAGE3();
  83. p_profile->page_4 = DEFAULT_ANT_HRM_PAGE4();
  84. NRF_LOG_INFO("ANT HRM channel %u init", p_profile->channel_number);
  85. return ant_channel_init(p_channel_config);
  86. }
  87. ret_code_t ant_hrm_disp_init(ant_hrm_profile_t * p_profile,
  88. ant_channel_config_t const * p_channel_config,
  89. ant_hrm_evt_handler_t evt_handler)
  90. {
  91. ASSERT(p_profile != NULL);
  92. ASSERT(p_channel_config != NULL);
  93. ASSERT(evt_handler != NULL);
  94. p_profile->evt_handler = evt_handler;
  95. return ant_hrm_init(p_profile, p_channel_config);
  96. }
  97. ret_code_t ant_hrm_sens_init(ant_hrm_profile_t * p_profile,
  98. ant_channel_config_t const * p_channel_config,
  99. ant_hrm_sens_config_t const * p_sens_config)
  100. {
  101. ASSERT(p_profile != NULL);
  102. ASSERT(p_channel_config != NULL);
  103. ASSERT(p_sens_config != NULL);
  104. ASSERT(p_sens_config->p_cb != NULL);
  105. ASSERT(p_sens_config->evt_handler != NULL);
  106. ASSERT((p_sens_config->main_page_number == ANT_HRM_PAGE_0)
  107. || (p_sens_config->main_page_number == ANT_HRM_PAGE_4));
  108. p_profile->evt_handler = p_sens_config->evt_handler;
  109. p_profile->_cb.p_sens_cb = p_sens_config->p_cb;
  110. ant_hrm_sens_cb_t * p_hrm_cb = p_profile->_cb.p_sens_cb;
  111. p_hrm_cb->page_1_present = p_sens_config->page_1_present;
  112. p_hrm_cb->main_page_number = p_sens_config->main_page_number;
  113. p_hrm_cb->ext_page_number = p_hrm_cb->page_1_present ? ANT_HRM_PAGE_1 : ANT_HRM_PAGE_2;
  114. p_hrm_cb->message_counter = 0;
  115. p_hrm_cb->toggle_bit = true;
  116. return ant_hrm_init(p_profile, p_channel_config);
  117. }
  118. /**@brief Function for getting next page number to send.
  119. *
  120. * @param[in] p_profile Pointer to the profile instance.
  121. *
  122. * @return Next page number.
  123. */
  124. static ant_hrm_page_t next_page_number_get(ant_hrm_profile_t * p_profile)
  125. {
  126. ant_hrm_sens_cb_t * p_hrm_cb = p_profile->_cb.p_sens_cb;
  127. ant_hrm_page_t page_number;
  128. if (p_hrm_cb->message_counter == (BACKGROUND_DATA_INTERVAL))
  129. {
  130. page_number = p_hrm_cb->ext_page_number;
  131. p_hrm_cb->message_counter = 0;
  132. p_hrm_cb->ext_page_number++;
  133. if (p_hrm_cb->ext_page_number > ANT_HRM_PAGE_3)
  134. {
  135. p_hrm_cb->ext_page_number = p_hrm_cb->page_1_present ? ANT_HRM_PAGE_1 : ANT_HRM_PAGE_2;
  136. }
  137. }
  138. else
  139. {
  140. page_number = p_hrm_cb->main_page_number;
  141. }
  142. if (p_hrm_cb->message_counter % TX_TOGGLE_DIVISOR == 0)
  143. {
  144. p_hrm_cb->toggle_bit ^= 1;
  145. }
  146. p_hrm_cb->message_counter++;
  147. return page_number;
  148. }
  149. /**@brief Function for encoding HRM message.
  150. *
  151. * @note Assume to be call each time when Tx window will occur.
  152. */
  153. static void sens_message_encode(ant_hrm_profile_t * p_profile, uint8_t * p_message_payload)
  154. {
  155. ant_hrm_message_layout_t * p_hrm_message_payload =
  156. (ant_hrm_message_layout_t *)p_message_payload;
  157. ant_hrm_sens_cb_t * p_hrm_cb = p_profile->_cb.p_sens_cb;
  158. p_hrm_message_payload->page_number = next_page_number_get(p_profile);
  159. p_hrm_message_payload->toggle_bit = p_hrm_cb->toggle_bit;
  160. NRF_LOG_INFO("HRM TX Page number: %u", p_hrm_message_payload->page_number);
  161. ant_hrm_page_0_encode(p_hrm_message_payload->page_payload, &(p_profile->page_0)); // Page 0 is present in each message
  162. switch (p_hrm_message_payload->page_number)
  163. {
  164. case ANT_HRM_PAGE_0:
  165. // No implementation needed
  166. break;
  167. case ANT_HRM_PAGE_1:
  168. ant_hrm_page_1_encode(p_hrm_message_payload->page_payload, &(p_profile->page_1));
  169. break;
  170. case ANT_HRM_PAGE_2:
  171. ant_hrm_page_2_encode(p_hrm_message_payload->page_payload, &(p_profile->page_2));
  172. break;
  173. case ANT_HRM_PAGE_3:
  174. ant_hrm_page_3_encode(p_hrm_message_payload->page_payload, &(p_profile->page_3));
  175. break;
  176. case ANT_HRM_PAGE_4:
  177. ant_hrm_page_4_encode(p_hrm_message_payload->page_payload, &(p_profile->page_4));
  178. break;
  179. default:
  180. return;
  181. }
  182. p_profile->evt_handler(p_profile, (ant_hrm_evt_t)p_hrm_message_payload->page_number);
  183. }
  184. /**@brief Function for setting payload for ANT message and sending it.
  185. *
  186. * @param[in] p_profile Pointer to the profile instance.
  187. */
  188. static void ant_message_send(ant_hrm_profile_t * p_profile)
  189. {
  190. uint32_t err_code;
  191. uint8_t p_message_payload[ANT_STANDARD_DATA_PAYLOAD_SIZE];
  192. sens_message_encode(p_profile, p_message_payload);
  193. err_code =
  194. sd_ant_broadcast_message_tx(p_profile->channel_number,
  195. sizeof (p_message_payload),
  196. p_message_payload);
  197. APP_ERROR_CHECK(err_code);
  198. }
  199. void ant_hrm_sens_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  200. {
  201. ant_hrm_profile_t * p_profile = (ant_hrm_profile_t *)p_context;
  202. if (p_ant_evt->channel == p_profile->channel_number)
  203. {
  204. switch (p_ant_evt->event)
  205. {
  206. case EVENT_TX:
  207. ant_message_send(p_profile);
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. }
  214. ret_code_t ant_hrm_disp_open(ant_hrm_profile_t * p_profile)
  215. {
  216. ASSERT(p_profile != NULL);
  217. NRF_LOG_INFO("ANT HRM channel %u open", p_profile->channel_number);
  218. return sd_ant_channel_open(p_profile->channel_number);
  219. }
  220. ret_code_t ant_hrm_sens_open(ant_hrm_profile_t * p_profile)
  221. {
  222. ASSERT(p_profile != NULL);
  223. // Fill tx buffer for the first frame
  224. ant_message_send(p_profile);
  225. NRF_LOG_INFO("ANT HRM channel %u open", p_profile->channel_number);
  226. return sd_ant_channel_open(p_profile->channel_number);
  227. }
  228. /**@brief Function for decoding HRM message.
  229. *
  230. * @note Assume to be call each time when Rx window will occur.
  231. */
  232. static void disp_message_decode(ant_hrm_profile_t * p_profile, uint8_t * p_message_payload)
  233. {
  234. const ant_hrm_message_layout_t * p_hrm_message_payload =
  235. (ant_hrm_message_layout_t *)p_message_payload;
  236. NRF_LOG_INFO("HRM RX Page Number: %u", p_hrm_message_payload->page_number);
  237. ant_hrm_page_0_decode(p_hrm_message_payload->page_payload, &(p_profile->page_0)); // Page 0 is present in each message
  238. switch (p_hrm_message_payload->page_number)
  239. {
  240. case ANT_HRM_PAGE_0:
  241. // No implementation needed
  242. break;
  243. case ANT_HRM_PAGE_1:
  244. ant_hrm_page_1_decode(p_hrm_message_payload->page_payload, &(p_profile->page_1));
  245. break;
  246. case ANT_HRM_PAGE_2:
  247. ant_hrm_page_2_decode(p_hrm_message_payload->page_payload, &(p_profile->page_2));
  248. break;
  249. case ANT_HRM_PAGE_3:
  250. ant_hrm_page_3_decode(p_hrm_message_payload->page_payload, &(p_profile->page_3));
  251. break;
  252. case ANT_HRM_PAGE_4:
  253. ant_hrm_page_4_decode(p_hrm_message_payload->page_payload, &(p_profile->page_4));
  254. break;
  255. default:
  256. return;
  257. }
  258. p_profile->evt_handler(p_profile, (ant_hrm_evt_t)p_hrm_message_payload->page_number);
  259. }
  260. void ant_hrm_disp_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  261. {
  262. ant_hrm_profile_t * p_profile = ( ant_hrm_profile_t *)p_context;
  263. if (p_ant_evt->channel == p_profile->channel_number)
  264. {
  265. switch (p_ant_evt->event)
  266. {
  267. case EVENT_RX:
  268. if (p_ant_evt->message.ANT_MESSAGE_ucMesgID == MESG_BROADCAST_DATA_ID
  269. || p_ant_evt->message.ANT_MESSAGE_ucMesgID == MESG_ACKNOWLEDGED_DATA_ID
  270. || p_ant_evt->message.ANT_MESSAGE_ucMesgID == MESG_BURST_DATA_ID)
  271. {
  272. disp_message_decode(p_profile, p_ant_evt->message.ANT_MESSAGE_aucPayload);
  273. }
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. }
  280. #endif // ANT_HRM_ENABLED