ant_sdm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_common.h"
  41. #if NRF_MODULE_ENABLED(ANT_SDM)
  42. #include "nrf_assert.h"
  43. #include "app_error.h"
  44. #include "ant_interface.h"
  45. #include "ant_sdm.h"
  46. #include "app_error.h"
  47. #include "ant_sdm_utils.h"
  48. #define NRF_LOG_MODULE_NAME ant_sdm
  49. #if ANT_SDM_LOG_ENABLED
  50. #define NRF_LOG_LEVEL ANT_SDM_LOG_LEVEL
  51. #define NRF_LOG_INFO_COLOR ANT_SDM_INFO_COLOR
  52. #else // ANT_SDM_LOG_ENABLED
  53. #define NRF_LOG_LEVEL 0
  54. #endif // ANT_SDM_LOG_ENABLED
  55. #include "nrf_log.h"
  56. NRF_LOG_MODULE_REGISTER();
  57. #define COMMON_DATA_INTERVAL 64 /**< Common data page is sent every 65th message. */
  58. /**@brief SDM message data layout structure. */
  59. typedef struct
  60. {
  61. ant_sdm_page_t page_number;
  62. uint8_t page_payload[7];
  63. }ant_sdm_message_layout_t;
  64. /**@brief Function for initializing the ANT SDM profile instance.
  65. *
  66. * @param[in] p_profile Pointer to the profile instance.
  67. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  68. *
  69. * @retval NRF_SUCCESS Successful initialization.
  70. * Error code when initialization failed.
  71. */
  72. static ret_code_t ant_sdm_init(ant_sdm_profile_t * p_profile,
  73. ant_channel_config_t const * p_channel_config)
  74. {
  75. p_profile->channel_number = p_channel_config->channel_number;
  76. p_profile->page_1 = DEFAULT_ANT_SDM_PAGE1();
  77. p_profile->page_2 = DEFAULT_ANT_SDM_PAGE2();
  78. p_profile->page_3 = DEFAULT_ANT_SDM_PAGE3();
  79. p_profile->page_22 = DEFAULT_ANT_SDM_PAGE22();
  80. p_profile->common = DEFAULT_ANT_SDM_COMMON_DATA();
  81. p_profile->page_80 = DEFAULT_ANT_COMMON_page80();
  82. p_profile->page_81 = DEFAULT_ANT_COMMON_page81();
  83. NRF_LOG_INFO("ANT SDM channel %u init", p_profile->channel_number);
  84. return ant_channel_init(p_channel_config);
  85. }
  86. ret_code_t ant_sdm_disp_init(ant_sdm_profile_t * p_profile,
  87. ant_channel_config_t const * p_channel_config,
  88. ant_sdm_disp_config_t const * p_disp_config)
  89. {
  90. ASSERT(p_profile != NULL);
  91. ASSERT(p_channel_config != NULL);
  92. ASSERT(p_disp_config != NULL);
  93. ASSERT(p_disp_config->p_cb != NULL);
  94. ASSERT(p_disp_config->evt_handler != NULL);
  95. p_profile->evt_handler = p_disp_config->evt_handler;
  96. p_profile->_cb.p_disp_cb = p_disp_config->p_cb;
  97. ant_request_controller_init(&(p_profile->_cb.p_disp_cb->req_controller));
  98. return ant_sdm_init(p_profile, p_channel_config);
  99. }
  100. ret_code_t ant_sdm_sens_init(ant_sdm_profile_t * p_profile,
  101. ant_channel_config_t const * p_channel_config,
  102. ant_sdm_sens_config_t const * p_sens_config)
  103. {
  104. ASSERT(p_profile != NULL);
  105. ASSERT(p_channel_config != NULL);
  106. ASSERT(p_sens_config != NULL);
  107. ASSERT(p_sens_config->p_cb != NULL);
  108. ASSERT(p_sens_config->evt_handler != NULL);
  109. ASSERT(p_sens_config->supplementary_page_number == ANT_SDM_PAGE_1
  110. || p_sens_config->supplementary_page_number == ANT_SDM_PAGE_2
  111. || p_sens_config->supplementary_page_number == ANT_SDM_PAGE_3);
  112. p_profile->evt_handler = p_sens_config->evt_handler;
  113. p_profile->_cb.p_sens_cb = p_sens_config->p_cb;
  114. ant_request_controller_init(&(p_profile->_cb.p_sens_cb->req_controller));
  115. p_profile->_cb.p_sens_cb->message_counter = 0;
  116. p_profile->_cb.p_sens_cb->supp_page_control = 0;
  117. p_profile->_cb.p_sens_cb->supp_page_number = p_sens_config->supplementary_page_number;
  118. p_profile->_cb.p_sens_cb->common_page_number = ANT_SDM_PAGE_80;
  119. return ant_sdm_init(p_profile, p_channel_config);
  120. }
  121. ret_code_t ant_sdm_page_request(ant_sdm_profile_t * p_profile, ant_common_page70_data_t * p_page_70)
  122. {
  123. ASSERT(p_profile != NULL);
  124. ASSERT(p_page_70 != NULL);
  125. uint32_t err_code = ant_request_controller_request(&(p_profile->_cb.p_disp_cb->req_controller),
  126. p_profile->channel_number, p_page_70);
  127. NRF_LOG_INFO("");
  128. return err_code;
  129. }
  130. /**@brief Function for getting next page number to send.
  131. *
  132. * @param[in] p_profile Pointer to the profile instance.
  133. *
  134. * @return Next page number.
  135. */
  136. static ant_sdm_page_t next_page_number_get(ant_sdm_profile_t * p_profile)
  137. {
  138. ant_sdm_sens_cb_t * p_sdm_cb = p_profile->_cb.p_sens_cb;
  139. ant_sdm_page_t page_number;
  140. if (ant_request_controller_pending_get(&(p_sdm_cb->req_controller), (uint8_t *)&page_number))
  141. {
  142. // No implementation needed
  143. }
  144. else if (p_sdm_cb->message_counter == (COMMON_DATA_INTERVAL))
  145. {
  146. page_number = p_sdm_cb->common_page_number;
  147. p_sdm_cb->message_counter++;
  148. }
  149. else if (p_sdm_cb->message_counter == (COMMON_DATA_INTERVAL + 1))
  150. {
  151. page_number = p_sdm_cb->common_page_number;
  152. p_sdm_cb->common_page_number = (p_sdm_cb->common_page_number == ANT_SDM_PAGE_80)
  153. ? ANT_SDM_PAGE_81 : ANT_SDM_PAGE_80;
  154. p_sdm_cb->message_counter = 0;
  155. }
  156. else
  157. {
  158. if (p_sdm_cb->supp_page_control)
  159. {
  160. page_number = p_sdm_cb->supp_page_number;
  161. }
  162. else
  163. {
  164. page_number = ANT_SDM_PAGE_1;
  165. }
  166. if ((p_sdm_cb->message_counter % 2) == 1)
  167. {
  168. p_sdm_cb->supp_page_control = !p_sdm_cb->supp_page_control;
  169. }
  170. p_sdm_cb->message_counter++;
  171. }
  172. return page_number;
  173. }
  174. /**@brief Function for encoding SDM message.
  175. *
  176. * @param[in] p_profile Pointer to the profile instance.
  177. * @param[in] p_message_payload Pointer to the message payload.
  178. *
  179. * @note Assume to be call each time when Tx window will occur.
  180. */
  181. static void sens_message_encode(ant_sdm_profile_t * p_profile, uint8_t * p_message_payload)
  182. {
  183. ant_sdm_message_layout_t * p_sdm_message_payload =
  184. (ant_sdm_message_layout_t *)p_message_payload;
  185. p_sdm_message_payload->page_number = next_page_number_get(p_profile);
  186. NRF_LOG_INFO("SDM Page number: %u", p_sdm_message_payload->page_number);
  187. switch (p_sdm_message_payload->page_number)
  188. {
  189. case ANT_SDM_PAGE_1:
  190. ant_sdm_page_1_encode(p_sdm_message_payload->page_payload, &(p_profile->page_1),
  191. &(p_profile->common));
  192. ant_sdm_speed_encode(p_sdm_message_payload->page_payload, &(p_profile->common));
  193. break;
  194. case ANT_SDM_PAGE_2:
  195. ant_sdm_page_2_encode(p_sdm_message_payload->page_payload, &(p_profile->page_2));
  196. ant_sdm_speed_encode(p_sdm_message_payload->page_payload, &(p_profile->common));
  197. break;
  198. case ANT_SDM_PAGE_3:
  199. ant_sdm_page_2_encode(p_sdm_message_payload->page_payload, &(p_profile->page_2));
  200. ant_sdm_page_3_encode(p_sdm_message_payload->page_payload, &(p_profile->page_3));
  201. ant_sdm_speed_encode(p_sdm_message_payload->page_payload, &(p_profile->common));
  202. break;
  203. case ANT_SDM_PAGE_16:
  204. ant_sdm_page_16_encode(p_sdm_message_payload->page_payload, &(p_profile->common));
  205. break;
  206. case ANT_SDM_PAGE_22:
  207. ant_sdm_page_22_encode(p_sdm_message_payload->page_payload, &(p_profile->page_22));
  208. break;
  209. case ANT_SDM_PAGE_80:
  210. ant_common_page_80_encode(p_sdm_message_payload->page_payload, &(p_profile->page_80));
  211. break;
  212. case ANT_SDM_PAGE_81:
  213. ant_common_page_81_encode(p_sdm_message_payload->page_payload, &(p_profile->page_81));
  214. break;
  215. default:
  216. return;
  217. }
  218. p_profile->evt_handler(p_profile, (ant_sdm_evt_t)p_sdm_message_payload->page_number);
  219. }
  220. void ant_sdm_sens_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  221. {
  222. ASSERT(p_context != NULL);
  223. ASSERT(p_ant_evt != NULL);
  224. ant_sdm_profile_t * p_profile = (ant_sdm_profile_t *)p_context;
  225. if (p_ant_evt->channel == p_profile->channel_number)
  226. {
  227. uint32_t err_code;
  228. uint8_t p_message_payload[ANT_STANDARD_DATA_PAYLOAD_SIZE];
  229. ant_sdm_sens_cb_t * p_sdm_cb = p_profile->_cb.p_sens_cb;
  230. ant_request_controller_sens_evt_handler(&(p_sdm_cb->req_controller), p_ant_evt);
  231. switch (p_ant_evt->event)
  232. {
  233. case EVENT_TX:
  234. case EVENT_TRANSFER_TX_FAILED:
  235. case EVENT_TRANSFER_TX_COMPLETED:
  236. sens_message_encode(p_profile, p_message_payload);
  237. if (ant_request_controller_ack_needed(&(p_sdm_cb->req_controller)))
  238. {
  239. err_code = sd_ant_acknowledge_message_tx(p_profile->channel_number,
  240. sizeof(p_message_payload),
  241. p_message_payload);
  242. }
  243. else
  244. {
  245. err_code = sd_ant_broadcast_message_tx(p_profile->channel_number,
  246. sizeof(p_message_payload),
  247. p_message_payload);
  248. }
  249. APP_ERROR_CHECK(err_code);
  250. break;
  251. default:
  252. break;
  253. }
  254. }
  255. }
  256. ret_code_t ant_sdm_disp_open(ant_sdm_profile_t * p_profile)
  257. {
  258. ASSERT(p_profile != NULL);
  259. NRF_LOG_INFO("ANT SDM channel %u open", p_profile->channel_number);
  260. return sd_ant_channel_open(p_profile->channel_number);
  261. }
  262. ret_code_t ant_sdm_sens_open(ant_sdm_profile_t * p_profile)
  263. {
  264. ASSERT(p_profile != NULL);
  265. // Fill tx buffer for the first frame
  266. uint32_t err_code;
  267. uint8_t p_message_payload[ANT_STANDARD_DATA_PAYLOAD_SIZE];
  268. sens_message_encode(p_profile, p_message_payload);
  269. err_code =
  270. sd_ant_broadcast_message_tx(p_profile->channel_number,
  271. sizeof(p_message_payload),
  272. p_message_payload);
  273. APP_ERROR_CHECK(err_code);
  274. NRF_LOG_INFO("ANT SDM channel %u open", p_profile->channel_number);
  275. return sd_ant_channel_open(p_profile->channel_number);
  276. }
  277. /**@brief Function for decoding SDM message.
  278. *
  279. * @note Assume to be call each time when Rx window will occur.
  280. */
  281. static void disp_message_decode(ant_sdm_profile_t * p_profile, uint8_t * p_message_payload)
  282. {
  283. const ant_sdm_message_layout_t * p_sdm_message_payload =
  284. (ant_sdm_message_layout_t *)p_message_payload;
  285. NRF_LOG_INFO("SDM Page number: %u", p_sdm_message_payload->page_number);
  286. switch (p_sdm_message_payload->page_number)
  287. {
  288. case ANT_SDM_PAGE_1:
  289. ant_sdm_page_1_decode(p_sdm_message_payload->page_payload,
  290. &(p_profile->page_1),
  291. &(p_profile->common));
  292. ant_sdm_speed_decode(p_sdm_message_payload->page_payload, &(p_profile->common));
  293. break;
  294. case ANT_SDM_PAGE_3:
  295. ant_sdm_page_3_decode(p_sdm_message_payload->page_payload, &(p_profile->page_3));
  296. /* fall through */
  297. case ANT_SDM_PAGE_2:
  298. ant_sdm_page_2_decode(p_sdm_message_payload->page_payload, &(p_profile->page_2));
  299. ant_sdm_speed_decode(p_sdm_message_payload->page_payload, &(p_profile->common));
  300. break;
  301. case ANT_SDM_PAGE_16:
  302. ant_sdm_page_16_decode(p_sdm_message_payload->page_payload, &(p_profile->common));
  303. break;
  304. case ANT_SDM_PAGE_22:
  305. ant_sdm_page_22_decode(p_sdm_message_payload->page_payload, &(p_profile->page_22));
  306. break;
  307. case ANT_SDM_PAGE_80:
  308. ant_common_page_80_decode(p_sdm_message_payload->page_payload, &(p_profile->page_80));
  309. break;
  310. case ANT_SDM_PAGE_81:
  311. ant_common_page_81_decode(p_sdm_message_payload->page_payload, &(p_profile->page_81));
  312. break;
  313. default:
  314. return;
  315. }
  316. p_profile->evt_handler(p_profile, (ant_sdm_evt_t)p_sdm_message_payload->page_number);
  317. }
  318. void ant_sdm_disp_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  319. {
  320. ASSERT(p_context != NULL);
  321. ASSERT(p_ant_evt != NULL);
  322. ant_sdm_profile_t * p_profile = ( ant_sdm_profile_t *)p_context;
  323. if (p_ant_evt->channel == p_profile->channel_number)
  324. {
  325. ant_sdm_disp_cb_t * p_sdm_cb = p_profile->_cb.p_disp_cb;
  326. switch (ant_request_controller_disp_evt_handler(&(p_sdm_cb->req_controller), p_ant_evt))
  327. {
  328. case ANT_REQUEST_CONTROLLER_SUCCESS:
  329. p_profile->evt_handler(p_profile, ANT_SDM_PAGE_REQUEST_SUCCESS);
  330. break;
  331. case ANT_REQUEST_CONTROLLER_FAILED:
  332. p_profile->evt_handler(p_profile, ANT_SDM_PAGE_REQUEST_FAILED);
  333. break;
  334. default:
  335. break;
  336. }
  337. switch (p_ant_evt->event)
  338. {
  339. case EVENT_RX:
  340. if (p_ant_evt->message.ANT_MESSAGE_ucMesgID == MESG_BROADCAST_DATA_ID
  341. || p_ant_evt->message.ANT_MESSAGE_ucMesgID == MESG_ACKNOWLEDGED_DATA_ID
  342. || p_ant_evt->message.ANT_MESSAGE_ucMesgID == MESG_BURST_DATA_ID)
  343. {
  344. disp_message_decode(p_profile, p_ant_evt->message.ANT_MESSAGE_aucPayload);
  345. }
  346. break;
  347. default:
  348. break;
  349. }
  350. }
  351. }
  352. #endif // NRF_MODULE_ENABLED(ANT_SDM)