ant_bpwr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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_BPWR)
  42. #include "nrf_assert.h"
  43. #include "app_error.h"
  44. #include "ant_interface.h"
  45. #include "ant_bpwr.h"
  46. #define NRF_LOG_MODULE_NAME ant_bpwr
  47. #if ANT_BPWR_LOG_ENABLED
  48. #define NRF_LOG_LEVEL ANT_BPWR_LOG_LEVEL
  49. #define NRF_LOG_INFO_COLOR ANT_BPWR_INFO_COLOR
  50. #else // ANT_BPWR_LOG_ENABLED
  51. #define NRF_LOG_LEVEL 0
  52. #endif // ANT_BPWR_LOG_ENABLED
  53. #include "nrf_log.h"
  54. NRF_LOG_MODULE_REGISTER();
  55. #define BPWR_CALIB_INT_TIMEOUT ((ANT_CLOCK_FREQUENCY * BPWR_CALIBRATION_TIMOUT_S) / BPWR_MSG_PERIOD) // calibration timeout in ant message period's unit
  56. // for torque sensor Minimum: Interleave every 9th message
  57. #define BPWR_PAGE_16_INTERVAL 5 // Preferred: Interleave every 5th message
  58. #define BPWR_PAGE_16_INTERVAL_OFS 2 // Permissible offset
  59. #define COMMON_PAGE_80_INTERVAL 119 // Minimum: Interleave every 121 messages
  60. #define COMMON_PAGE_81_INTERVAL 120 // Minimum: Interleave every 121 messages
  61. #define AUTO_ZERO_SUPPORT_INTERVAL 120 // Minimum: Interleave every 121 messages
  62. /**@brief Bicycle power message data layout structure. */
  63. typedef struct
  64. {
  65. uint8_t page_number;
  66. uint8_t page_payload[7];
  67. } ant_bpwr_message_layout_t;
  68. /**@brief Function for initializing the ANT Bicycle Power 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_bpwr_init(ant_bpwr_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_1 = DEFAULT_ANT_BPWR_PAGE1();
  80. p_profile->page_16 = DEFAULT_ANT_BPWR_PAGE16();
  81. p_profile->page_17 = DEFAULT_ANT_BPWR_PAGE17();
  82. p_profile->page_18 = DEFAULT_ANT_BPWR_PAGE18();
  83. p_profile->page_80 = DEFAULT_ANT_COMMON_page80();
  84. p_profile->page_81 = DEFAULT_ANT_COMMON_page81();
  85. NRF_LOG_INFO("ANT B-PWR channel %u init", p_profile->channel_number);
  86. return ant_channel_init(p_channel_config);
  87. }
  88. ret_code_t ant_bpwr_disp_init(ant_bpwr_profile_t * p_profile,
  89. ant_channel_config_t const * p_channel_config,
  90. ant_bpwr_disp_config_t const * p_disp_config)
  91. {
  92. ASSERT(p_profile != NULL);
  93. ASSERT(p_channel_config != NULL);
  94. ASSERT(p_disp_config != NULL);
  95. ASSERT(p_disp_config->evt_handler != NULL);
  96. ASSERT(p_disp_config->p_cb != NULL);
  97. p_profile->evt_handler = p_disp_config->evt_handler;
  98. p_profile->_cb.p_disp_cb = p_disp_config->p_cb;
  99. p_profile->_cb.p_disp_cb ->calib_timeout = 0;
  100. p_profile->_cb.p_disp_cb ->calib_stat = BPWR_DISP_CALIB_NONE;
  101. return ant_bpwr_init(p_profile, p_channel_config);
  102. }
  103. ret_code_t ant_bpwr_sens_init(ant_bpwr_profile_t * p_profile,
  104. ant_channel_config_t const * p_channel_config,
  105. ant_bpwr_sens_config_t const * p_sens_config)
  106. {
  107. ASSERT(p_profile != NULL);
  108. ASSERT(p_channel_config != NULL);
  109. ASSERT(p_sens_config != NULL);
  110. ASSERT(p_sens_config->p_cb != NULL);
  111. ASSERT(p_sens_config->evt_handler != NULL);
  112. ASSERT(p_sens_config->calib_handler != NULL);
  113. p_profile->evt_handler = p_sens_config->evt_handler;
  114. p_profile->_cb.p_sens_cb = p_sens_config->p_cb;
  115. p_profile->_cb.p_sens_cb->torque_use = p_sens_config->torque_use;
  116. p_profile->_cb.p_sens_cb->calib_handler = p_sens_config->calib_handler;
  117. p_profile->_cb.p_sens_cb->calib_stat = BPWR_SENS_CALIB_NONE;
  118. p_profile->_cb.p_sens_cb->message_counter = 0;
  119. return ant_bpwr_init(p_profile, p_channel_config);
  120. }
  121. /**@brief Function for getting next page number to send.
  122. *
  123. * @param[in] p_profile Pointer to the profile instance.
  124. *
  125. * @return Next page number.
  126. */
  127. static ant_bpwr_page_t next_page_number_get(ant_bpwr_profile_t * p_profile)
  128. {
  129. ant_bpwr_sens_cb_t * p_bpwr_cb = p_profile->_cb.p_sens_cb;
  130. ant_bpwr_page_t page_number;
  131. if (p_bpwr_cb->calib_stat == BPWR_SENS_CALIB_READY)
  132. {
  133. page_number = ANT_BPWR_PAGE_1;
  134. p_bpwr_cb->calib_stat = BPWR_SENS_CALIB_NONE; // mark event as processed
  135. }
  136. else if ((p_profile->BPWR_PROFILE_auto_zero_status != ANT_BPWR_AUTO_ZERO_NOT_SUPPORTED)
  137. && (p_bpwr_cb->message_counter == AUTO_ZERO_SUPPORT_INTERVAL))
  138. {
  139. page_number = ANT_BPWR_PAGE_1;
  140. p_profile->BPWR_PROFILE_calibration_id = ANT_BPWR_CALIB_ID_AUTO_SUPPORT;
  141. p_bpwr_cb->message_counter++;
  142. }
  143. else if (p_bpwr_cb->message_counter >= COMMON_PAGE_81_INTERVAL)
  144. {
  145. page_number = ANT_BPWR_PAGE_81;
  146. p_bpwr_cb->message_counter = 0;
  147. }
  148. else
  149. {
  150. if (p_bpwr_cb->message_counter == COMMON_PAGE_80_INTERVAL)
  151. {
  152. page_number = ANT_BPWR_PAGE_80;
  153. }
  154. else
  155. {
  156. if ( p_bpwr_cb->torque_use == TORQUE_NONE)
  157. {
  158. page_number = ANT_BPWR_PAGE_16;
  159. }
  160. else if ((p_bpwr_cb->message_counter % BPWR_PAGE_16_INTERVAL)
  161. == BPWR_PAGE_16_INTERVAL_OFS)
  162. {
  163. page_number = ANT_BPWR_PAGE_16;
  164. }
  165. else if ( p_bpwr_cb->torque_use == TORQUE_WHEEL)
  166. {
  167. page_number = ANT_BPWR_PAGE_17;
  168. }
  169. else // assumed TORQUE_CRANK
  170. {
  171. page_number = ANT_BPWR_PAGE_18;
  172. }
  173. }
  174. p_bpwr_cb->message_counter++;
  175. }
  176. return page_number;
  177. }
  178. /**@brief Function for encoding Bicycle Power Sensor message.
  179. *
  180. * @note Assume to be call each time when Tx window will occur.
  181. */
  182. static void sens_message_encode(ant_bpwr_profile_t * p_profile, uint8_t * p_message_payload)
  183. {
  184. ant_bpwr_message_layout_t * p_bpwr_message_payload =
  185. (ant_bpwr_message_layout_t *)p_message_payload;
  186. p_bpwr_message_payload->page_number = next_page_number_get(p_profile);
  187. NRF_LOG_INFO("B-PWR tx page: %u", p_bpwr_message_payload->page_number);
  188. switch (p_bpwr_message_payload->page_number)
  189. {
  190. case ANT_BPWR_PAGE_1:
  191. ant_bpwr_page_1_encode(p_bpwr_message_payload->page_payload, &(p_profile->page_1));
  192. break;
  193. case ANT_BPWR_PAGE_16:
  194. ant_bpwr_page_16_encode(p_bpwr_message_payload->page_payload, &(p_profile->page_16));
  195. ant_bpwr_cadence_encode(p_bpwr_message_payload->page_payload, &(p_profile->common));
  196. break;
  197. case ANT_BPWR_PAGE_17:
  198. ant_bpwr_page_17_encode(p_bpwr_message_payload->page_payload, &(p_profile->page_17));
  199. ant_bpwr_cadence_encode(p_bpwr_message_payload->page_payload, &(p_profile->common));
  200. break;
  201. case ANT_BPWR_PAGE_18:
  202. ant_bpwr_page_18_encode(p_bpwr_message_payload->page_payload, &(p_profile->page_18));
  203. ant_bpwr_cadence_encode(p_bpwr_message_payload->page_payload, &(p_profile->common));
  204. break;
  205. case ANT_COMMON_PAGE_80:
  206. ant_common_page_80_encode(p_bpwr_message_payload->page_payload, &(p_profile->page_80));
  207. break;
  208. case ANT_COMMON_PAGE_81:
  209. ant_common_page_81_encode(p_bpwr_message_payload->page_payload, &(p_profile->page_81));
  210. break;
  211. default:
  212. return;
  213. }
  214. p_profile->evt_handler(p_profile, (ant_bpwr_evt_t)p_bpwr_message_payload->page_number);
  215. }
  216. /**@brief Function for decoding messages received by Bicycle Power sensor message.
  217. *
  218. * @note Assume to be call each time when Rx window will occur.
  219. */
  220. static void sens_message_decode(ant_bpwr_profile_t * p_profile, uint8_t * p_message_payload)
  221. {
  222. const ant_bpwr_message_layout_t * p_bpwr_message_payload =
  223. (ant_bpwr_message_layout_t *)p_message_payload;
  224. ant_bpwr_page1_data_t page1;
  225. switch (p_bpwr_message_payload->page_number)
  226. {
  227. case ANT_BPWR_PAGE_1:
  228. ant_bpwr_page_1_decode(p_bpwr_message_payload->page_payload, &page1);
  229. p_profile->_cb.p_sens_cb->calib_stat = BPWR_SENS_CALIB_REQUESTED;
  230. p_profile->_cb.p_sens_cb->calib_handler(p_profile, &page1);
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. /**@brief Function for decoding messages received by Bicycle Power display message.
  237. *
  238. * @note Assume to be call each time when Rx window will occur.
  239. */
  240. static void disp_message_decode(ant_bpwr_profile_t * p_profile, uint8_t * p_message_payload)
  241. {
  242. const ant_bpwr_message_layout_t * p_bpwr_message_payload =
  243. (ant_bpwr_message_layout_t *)p_message_payload;
  244. NRF_LOG_INFO("B-PWR rx page: %u", p_bpwr_message_payload->page_number);
  245. switch (p_bpwr_message_payload->page_number)
  246. {
  247. case ANT_BPWR_PAGE_1:
  248. ant_bpwr_page_1_decode(p_bpwr_message_payload->page_payload, &(p_profile->page_1));
  249. p_profile->_cb.p_disp_cb->calib_stat = BPWR_DISP_CALIB_NONE;
  250. break;
  251. case ANT_BPWR_PAGE_16:
  252. ant_bpwr_page_16_decode(p_bpwr_message_payload->page_payload, &(p_profile->page_16));
  253. ant_bpwr_cadence_decode(p_bpwr_message_payload->page_payload, &(p_profile->common));
  254. break;
  255. case ANT_BPWR_PAGE_17:
  256. ant_bpwr_page_17_decode(p_bpwr_message_payload->page_payload, &(p_profile->page_17));
  257. ant_bpwr_cadence_decode(p_bpwr_message_payload->page_payload, &(p_profile->common));
  258. break;
  259. case ANT_BPWR_PAGE_18:
  260. ant_bpwr_page_18_decode(p_bpwr_message_payload->page_payload, &(p_profile->page_18));
  261. ant_bpwr_cadence_decode(p_bpwr_message_payload->page_payload, &(p_profile->common));
  262. break;
  263. case ANT_COMMON_PAGE_80:
  264. ant_common_page_80_decode(p_bpwr_message_payload->page_payload, &(p_profile->page_80));
  265. break;
  266. case ANT_COMMON_PAGE_81:
  267. ant_common_page_81_decode(p_bpwr_message_payload->page_payload, &(p_profile->page_81));
  268. break;
  269. default:
  270. return;
  271. }
  272. p_profile->evt_handler(p_profile, (ant_bpwr_evt_t)p_bpwr_message_payload->page_number);
  273. }
  274. ret_code_t ant_bpwr_calib_request(ant_bpwr_profile_t * p_profile, ant_bpwr_page1_data_t * p_page_1)
  275. {
  276. ant_bpwr_message_layout_t bpwr_message_payload;
  277. if (p_profile->_cb.p_disp_cb->calib_stat == BPWR_DISP_CALIB_REQUESTED)
  278. {
  279. return NRF_SUCCESS; // calibration in progress, so omit this request
  280. }
  281. bpwr_message_payload.page_number = ANT_BPWR_PAGE_1;
  282. ant_bpwr_page_1_encode(bpwr_message_payload.page_payload, p_page_1);
  283. uint32_t err_code = sd_ant_acknowledge_message_tx(p_profile->channel_number,
  284. sizeof (bpwr_message_payload),
  285. (uint8_t *) &bpwr_message_payload);
  286. if (err_code == NRF_SUCCESS)
  287. {
  288. p_profile->_cb.p_disp_cb->calib_timeout = BPWR_CALIB_INT_TIMEOUT; // initialize watch on calibration's time-out
  289. p_profile->_cb.p_disp_cb->calib_stat = BPWR_DISP_CALIB_REQUESTED;
  290. NRF_LOG_INFO("Start calibration process");
  291. }
  292. return err_code;
  293. }
  294. void ant_bpwr_calib_response(ant_bpwr_profile_t * p_profile)
  295. {
  296. if (p_profile->_cb.p_sens_cb->calib_stat != BPWR_SENS_CALIB_READY) // abort if callback request is in progress
  297. {
  298. p_profile->_cb.p_sens_cb->calib_stat = BPWR_SENS_CALIB_READY; // calibration respond
  299. }
  300. }
  301. /**@brief Function for hangling calibration events.
  302. */
  303. static void service_calib(ant_bpwr_profile_t * p_profile, uint8_t event)
  304. {
  305. ant_bpwr_evt_t bpwr_event;
  306. if (p_profile->_cb.p_disp_cb->calib_stat == BPWR_DISP_CALIB_REQUESTED)
  307. {
  308. switch (event)
  309. {
  310. case EVENT_RX:
  311. /* fall through */
  312. case EVENT_RX_FAIL:
  313. if (p_profile->_cb.p_disp_cb->calib_timeout-- == 0)
  314. {
  315. bpwr_event = ANT_BPWR_CALIB_TIMEOUT;
  316. break;
  317. }
  318. else
  319. {
  320. return;
  321. }
  322. case EVENT_TRANSFER_TX_FAILED:
  323. bpwr_event = ANT_BPWR_CALIB_REQUEST_TX_FAILED;
  324. break;
  325. case EVENT_RX_SEARCH_TIMEOUT:
  326. bpwr_event = ANT_BPWR_CALIB_TIMEOUT;
  327. break;
  328. default:
  329. return;
  330. }
  331. NRF_LOG_INFO("End calibration process");
  332. p_profile->_cb.p_disp_cb->calib_stat = BPWR_DISP_CALIB_NONE;
  333. p_profile->evt_handler(p_profile, bpwr_event);
  334. }
  335. }
  336. static void ant_message_send(ant_bpwr_profile_t * p_profile)
  337. {
  338. uint32_t err_code;
  339. uint8_t p_message_payload[ANT_STANDARD_DATA_PAYLOAD_SIZE];
  340. sens_message_encode(p_profile, p_message_payload);
  341. err_code =
  342. sd_ant_broadcast_message_tx(p_profile->channel_number,
  343. sizeof (p_message_payload),
  344. p_message_payload);
  345. APP_ERROR_CHECK(err_code);
  346. }
  347. ret_code_t ant_bpwr_disp_open(ant_bpwr_profile_t * p_profile)
  348. {
  349. NRF_LOG_INFO("ANT B-PWR %u open", p_profile->channel_number);
  350. return sd_ant_channel_open(p_profile->channel_number);
  351. }
  352. ret_code_t ant_bpwr_sens_open(ant_bpwr_profile_t * p_profile)
  353. {
  354. // Fill tx buffer for the first frame
  355. ant_message_send(p_profile);
  356. NRF_LOG_INFO("ANT B-PWR %u open", p_profile->channel_number);
  357. return sd_ant_channel_open(p_profile->channel_number);
  358. }
  359. void ant_bpwr_sens_evt_handler(ant_evt_t * p_ant_event, void * p_context)
  360. {
  361. ant_bpwr_profile_t * p_profile = ( ant_bpwr_profile_t *)p_context;
  362. if (p_ant_event->channel == p_profile->channel_number)
  363. {
  364. switch (p_ant_event->event)
  365. {
  366. case EVENT_TX:
  367. ant_message_send(p_profile);
  368. break;
  369. case EVENT_RX:
  370. if (p_ant_event->message.ANT_MESSAGE_ucMesgID == MESG_ACKNOWLEDGED_DATA_ID)
  371. {
  372. sens_message_decode(p_profile, p_ant_event->message.ANT_MESSAGE_aucPayload);
  373. }
  374. break;
  375. default:
  376. // No implementation needed
  377. break;
  378. }
  379. }
  380. }
  381. void ant_bpwr_disp_evt_handler(ant_evt_t * p_ant_event, void * p_context)
  382. {
  383. ant_bpwr_profile_t * p_profile = ( ant_bpwr_profile_t *)p_context;
  384. if (p_ant_event->channel == p_profile->channel_number)
  385. {
  386. switch (p_ant_event->event)
  387. {
  388. case EVENT_RX:
  389. if (p_ant_event->message.ANT_MESSAGE_ucMesgID == MESG_BROADCAST_DATA_ID
  390. || p_ant_event->message.ANT_MESSAGE_ucMesgID == MESG_ACKNOWLEDGED_DATA_ID
  391. || p_ant_event->message.ANT_MESSAGE_ucMesgID == MESG_BURST_DATA_ID)
  392. {
  393. disp_message_decode(p_profile, p_ant_event->message.ANT_MESSAGE_aucPayload);
  394. }
  395. break;
  396. default:
  397. break;
  398. }
  399. service_calib(p_profile, p_ant_event->event);
  400. }
  401. }
  402. #endif // NRF_MODULE_ENABLED(ANT_BPWR)