ant_bsc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. /** @file
  41. *
  42. * @defgroup ant_bsc Bicycle Speed and Cadence profile
  43. * @{
  44. * @ingroup ant_sdk_profiles
  45. * @brief This module implements the Bicycle Speed and Cadence profile.
  46. *
  47. */
  48. #ifndef ANT_BSC_H__
  49. #define ANT_BSC_H__
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include "sdk_errors.h"
  53. #include "ant_parameters.h"
  54. #include "nrf_sdh_ant.h"
  55. #include "ant_channel_config.h"
  56. #include "ant_bsc_pages.h"
  57. #define BSC_ANTPLUS_RF_FREQ 0x39u ///< Frequency, decimal 57 (2457 MHz).
  58. #define BSC_SPEED_DEVICE_TYPE 0x7B ///< Device type reserved for ANT+ bike speed sensor.
  59. #define BSC_CADENCE_DEVICE_TYPE 0x7A ///< Device type reserved for ANT+ bike cadence sensor.
  60. #define BSC_COMBINED_DEVICE_TYPE 0x79 ///< Device type reserved for ANT+ bike combined speed and cadence sensor.
  61. #define BSC_MSG_PERIOD_4Hz 1u ///< Message period, 4 Hz (in basic period counts, where basic period time = 0.25 s).
  62. #define BSC_MSG_PERIOD_2Hz 2u ///< Message period, 2 Hz (in basic period counts).
  63. #define BSC_MSG_PERIOD_1Hz 4u ///< Message period, 1 Hz (in basic period counts).
  64. #define BSC_MSG_PERIOD_SPEED 0x1FB6u ///< Message period in ticks, decimal 8118 (4.04 Hz).
  65. #define BSC_MSG_PERIOD_CADENCE 0x1FA6u ///< Message period in ticks, decimal 8102 (4.04 Hz).
  66. #define BSC_MSG_PERIOD_COMBINED 0x1F96u ///< Message period in ticks, decimal 8086 (4.05 Hz).
  67. #define BSC_EXT_ASSIGN 0x00 ///< ANT ext assign (see Ext. Assign Channel Parameters in ant_parameters.h: @ref ant_parameters).
  68. #define BSC_DISP_CHANNEL_TYPE CHANNEL_TYPE_SLAVE_RX_ONLY ///< Display BSC channel type.
  69. #define BSC_SENS_CHANNEL_TYPE CHANNEL_TYPE_MASTER ///< Sensor BSC channel type.
  70. /**@brief Select the basic ANT channel period (in ticks) for the BSC profile depending on the device type.
  71. *
  72. * @param[in] DEVICE_TYPE Type of device. Supported types: @ref BSC_SPEED_DEVICE_TYPE, @ref BSC_CADENCE_DEVICE_TYPE,
  73. * @ref BSC_COMBINED_DEVICE_TYPE.
  74. */
  75. #define BSC_DEVICE_TICKS(DEVICE_TYPE) \
  76. ((DEVICE_TYPE) == (BSC_SPEED_DEVICE_TYPE) ? (BSC_MSG_PERIOD_SPEED) : \
  77. ((DEVICE_TYPE) == (BSC_CADENCE_DEVICE_TYPE) ? (BSC_MSG_PERIOD_CADENCE) : \
  78. ((DEVICE_TYPE) == (BSC_COMBINED_DEVICE_TYPE) ? (BSC_MSG_PERIOD_COMBINED) : 0u)))
  79. /**@brief Calculate the channel period (in ticks) depending on the device type and the chosen message frequency.
  80. *
  81. * @param[in] DEVICE_TYPE Type of device. Supported types: @ref BSC_SPEED_DEVICE_TYPE, @ref BSC_CADENCE_DEVICE_TYPE,
  82. * @ref BSC_COMBINED_DEVICE_TYPE.
  83. * @param[in] BSC_MSG_PERIOD Channel data period. The BSC profile supports only the following periods:
  84. * @ref BSC_MSG_PERIOD_4Hz, @ref BSC_MSG_PERIOD_2Hz, @ref BSC_MSG_PERIOD_1Hz.
  85. */
  86. #define BSC_PERIOD_TICKS(DEVICE_TYPE, BSC_MSG_PERIOD) \
  87. ((BSC_DEVICE_TICKS(DEVICE_TYPE)) * (BSC_MSG_PERIOD))
  88. /**@brief Initialize an ANT channel configuration structure for the BSC profile (Display).
  89. *
  90. * @param[in] NAME Name of related instance.
  91. * @param[in] CHANNEL_NUMBER Number of the channel assigned to the profile instance.
  92. * @param[in] TRANSMISSION_TYPE Type of transmission assigned to the profile instance.
  93. * @param[in] DEVICE_TYPE Type of device. Supported types: @ref BSC_SPEED_DEVICE_TYPE, @ref BSC_CADENCE_DEVICE_TYPE,
  94. * @ref BSC_COMBINED_DEVICE_TYPE.
  95. * @param[in] DEVICE_NUMBER Number of the device assigned to the profile instance.
  96. * @param[in] NETWORK_NUMBER Number of the network assigned to the profile instance.
  97. * @param[in] BSC_MSG_PERIOD Channel data frequency in Hz. The BSC profile supports only the following frequencies:
  98. * @ref BSC_MSG_PERIOD_4Hz, @ref BSC_MSG_PERIOD_2Hz, @ref BSC_MSG_PERIOD_1Hz.
  99. */
  100. #define BSC_DISP_CHANNEL_CONFIG_DEF(NAME, \
  101. CHANNEL_NUMBER, \
  102. TRANSMISSION_TYPE, \
  103. DEVICE_TYPE, \
  104. DEVICE_NUMBER, \
  105. NETWORK_NUMBER, \
  106. BSC_MSG_PERIOD) \
  107. static const ant_channel_config_t CONCAT_2(NAME, _channel_bsc_disp_config) = \
  108. { \
  109. .channel_number = (CHANNEL_NUMBER), \
  110. .channel_type = BSC_DISP_CHANNEL_TYPE, \
  111. .ext_assign = BSC_EXT_ASSIGN, \
  112. .rf_freq = BSC_ANTPLUS_RF_FREQ, \
  113. .transmission_type = (TRANSMISSION_TYPE), \
  114. .device_type = (DEVICE_TYPE), \
  115. .device_number = (DEVICE_NUMBER), \
  116. .channel_period = BSC_PERIOD_TICKS(DEVICE_TYPE, BSC_MSG_PERIOD), \
  117. .network_number = (NETWORK_NUMBER), \
  118. }
  119. #define BSC_DISP_CHANNEL_CONFIG(NAME) &CONCAT_2(NAME, _channel_bsc_disp_config)
  120. /**@brief Initialize an ANT channel configuration structure for the BSC profile (Transmitter).
  121. *
  122. * @param[in] NAME Name of related instance.
  123. * @param[in] CHANNEL_NUMBER Number of the channel assigned to the profile instance.
  124. * @param[in] TRANSMISSION_TYPE Type of transmission assigned to the profile instance.
  125. * @param[in] DEVICE_TYPE Type of device. Supported types: @ref BSC_SPEED_DEVICE_TYPE, @ref BSC_CADENCE_DEVICE_TYPE,
  126. * @ref BSC_COMBINED_DEVICE_TYPE.
  127. * @param[in] DEVICE_NUMBER Number of the device assigned to the profile instance.
  128. * @param[in] NETWORK_NUMBER Number of the network assigned to the profile instance.
  129. */
  130. #define BSC_SENS_CHANNEL_CONFIG_DEF(NAME, \
  131. CHANNEL_NUMBER, \
  132. TRANSMISSION_TYPE, \
  133. DEVICE_TYPE, \
  134. DEVICE_NUMBER, \
  135. NETWORK_NUMBER) \
  136. static const ant_channel_config_t NAME##_channel_bsc_sens_config = \
  137. { \
  138. .channel_number = (CHANNEL_NUMBER), \
  139. .channel_type = BSC_SENS_CHANNEL_TYPE, \
  140. .ext_assign = BSC_EXT_ASSIGN, \
  141. .rf_freq = BSC_ANTPLUS_RF_FREQ, \
  142. .transmission_type = (TRANSMISSION_TYPE), \
  143. .device_type = (DEVICE_TYPE), \
  144. .device_number = (DEVICE_NUMBER), \
  145. .channel_period = BSC_PERIOD_TICKS(DEVICE_TYPE, BSC_MSG_PERIOD_4Hz), \
  146. .network_number = (NETWORK_NUMBER), \
  147. }
  148. #define BSC_SENS_CHANNEL_CONFIG(NAME) &NAME##_channel_bsc_sens_config
  149. /**@brief Initialize an ANT profile configuration structure for the BSC profile (Display).
  150. *
  151. * @param[in] NAME Name of related instance.
  152. * @param[in] EVT_HANDLER Event handler to be called for handling events in the BSC profile.
  153. */
  154. #define BSC_DISP_PROFILE_CONFIG_DEF(NAME, \
  155. EVT_HANDLER) \
  156. static ant_bsc_disp_cb_t CONCAT_2(NAME, _bsc_disp_cb); \
  157. static const ant_bsc_disp_config_t CONCAT_2(NAME, _profile_bsc_disp_config) = \
  158. { \
  159. .p_cb = &CONCAT_2(NAME, _bsc_disp_cb), \
  160. .evt_handler = (EVT_HANDLER), \
  161. }
  162. #define BSC_DISP_PROFILE_CONFIG(NAME) &CONCAT_2(NAME, _profile_bsc_disp_config)
  163. /**@brief Initialize an ANT profile configuration structure for the BSC profile (Sensor).
  164. *
  165. * @param[in] NAME Name of related instance.
  166. * @param[in] PAGE_1_PRESENT Determines whether page 1 is included.
  167. * @param[in] PAGE_4_PRESENT Determines whether page 4 is included.
  168. * @param[in] MAIN_PAGE_NUMBER Determines the main data page (@ref ANT_BSC_PAGE_0 or @ref ANT_BSC_PAGE_5 or @ref ANT_BSC_COMB_PAGE_0).
  169. * @param[in] EVT_HANDLER Event handler to be called for handling events in the BSC profile.
  170. */
  171. #define BSC_SENS_PROFILE_CONFIG_DEF(NAME, \
  172. PAGE_1_PRESENT, \
  173. PAGE_4_PRESENT, \
  174. MAIN_PAGE_NUMBER, \
  175. EVT_HANDLER) \
  176. static ant_bsc_sens_cb_t CONCAT_2(NAME, _bsc_sens_cb); \
  177. static const ant_bsc_sens_config_t CONCAT_2(NAME, _profile_bsc_sens_config) = \
  178. { \
  179. .page_1_present = (PAGE_1_PRESENT), \
  180. .page_4_present = (PAGE_4_PRESENT), \
  181. .main_page_number = (MAIN_PAGE_NUMBER), \
  182. .p_cb = &CONCAT_2(NAME, _bsc_sens_cb), \
  183. .evt_handler = (EVT_HANDLER), \
  184. }
  185. #define BSC_SENS_PROFILE_CONFIG(NAME) &CONCAT_2(NAME, _profile_bsc_sens_config)
  186. /**@brief BSC page number type. */
  187. typedef enum{
  188. ANT_BSC_PAGE_0, ///< Main data page number 0.
  189. ANT_BSC_PAGE_1, ///< Background data page number 1. This page is optional.
  190. ANT_BSC_PAGE_2, ///< Background data page number 2.
  191. ANT_BSC_PAGE_3, ///< Background data page number 3.
  192. ANT_BSC_PAGE_4, ///< Background data page number 4. This page is optional.
  193. ANT_BSC_PAGE_5, ///< Main data page number 5. This page is optional.
  194. ANT_BSC_COMB_PAGE_0 ///< Main data page number 0 for combined speed and cadence sensor.
  195. } ant_bsc_page_t;
  196. /**@brief BSC profile event type. */
  197. typedef enum{
  198. ANT_BSC_PAGE_0_UPDATED = ANT_BSC_PAGE_0, ///< Data page 0 has been updated (Display) or sent (Sensor).
  199. ANT_BSC_PAGE_1_UPDATED = ANT_BSC_PAGE_1, ///< Data page 0 and page 1 have been updated (Display) or sent (Sensor).
  200. ANT_BSC_PAGE_2_UPDATED = ANT_BSC_PAGE_2, ///< Data page 0 and page 2 have been updated (Display) or sent (Sensor).
  201. ANT_BSC_PAGE_3_UPDATED = ANT_BSC_PAGE_3, ///< Data page 0 and page 3 have been updated (Display) or sent (Sensor).
  202. ANT_BSC_PAGE_4_UPDATED = ANT_BSC_PAGE_4, ///< Data page 0 and page 4 have been updated (Display) or sent (Sensor).
  203. ANT_BSC_PAGE_5_UPDATED = ANT_BSC_PAGE_5, ///< Data page 0 and page 5 have been updated (Display) or sent (Sensor).
  204. ANT_BSC_COMB_PAGE_0_UPDATED = ANT_BSC_COMB_PAGE_0, ///< Combined Speed and cadence data page has been updated (Display) or sent (Sensor).
  205. } ant_bsc_evt_t;
  206. // Forward declaration of the ant_bsc_profile_t type.
  207. typedef struct ant_bsc_profile_s ant_bsc_profile_t;
  208. /**@brief BSC event handler type. */
  209. typedef void (* ant_bsc_evt_handler_t) (ant_bsc_profile_t *, ant_bsc_evt_t);
  210. #include "ant_bsc_local.h"
  211. #ifdef __cplusplus
  212. extern "C" {
  213. #endif
  214. /**@brief BSC Display configuration structure. */
  215. typedef struct
  216. {
  217. ant_bsc_disp_cb_t * p_cb; ///< Pointer to the data buffer for internal use.
  218. ant_bsc_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the BSC profile.
  219. } ant_bsc_disp_config_t;
  220. /**@brief BSC Sensor configuration structure. */
  221. typedef struct
  222. {
  223. bool page_1_present; ///< Determines whether page 1 is included.
  224. bool page_4_present; ///< Determines whether page 4 is included.
  225. ant_bsc_page_t main_page_number; ///< Determines the main data page (@ref ANT_BSC_PAGE_0 or @ref ANT_BSC_PAGE_5 or @ref ANT_BSC_COMB_PAGE_0).
  226. ant_bsc_sens_cb_t * p_cb; ///< Pointer to the data buffer for internal use.
  227. ant_bsc_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the BSC profile.
  228. } ant_bsc_sens_config_t;
  229. /**@brief BSC profile structure. */
  230. struct ant_bsc_profile_s
  231. {
  232. uint8_t channel_number; ///< Channel number assigned to the profile.
  233. union {
  234. ant_bsc_disp_cb_t * p_disp_cb;
  235. ant_bsc_sens_cb_t * p_sens_cb;
  236. } _cb; ///< Pointer to internal control block.
  237. ant_bsc_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the BSC profile.
  238. ant_bsc_page0_data_t page_0; ///< Page 0.
  239. ant_bsc_page1_data_t page_1; ///< Page 1.
  240. ant_bsc_page2_data_t page_2; ///< Page 2.
  241. ant_bsc_page3_data_t page_3; ///< Page 3.
  242. ant_bsc_page4_data_t page_4; ///< Page 4.
  243. ant_bsc_page5_data_t page_5; ///< Page 5.
  244. ant_bsc_combined_page0_data_t page_comb_0; ///< Page 0 for combined speed and cadence sensor.
  245. };
  246. /** @name Defines for accessing ant_bsc_profile_t member variables
  247. @{ */
  248. #define BSC_PROFILE_event_time page_0.event_time
  249. #define BSC_PROFILE_rev_count page_0.rev_count
  250. #define BSC_PROFILE_operating_time page_1.operating_time
  251. #define BSC_PROFILE_manuf_id page_2.manuf_id
  252. #define BSC_PROFILE_serial_num page_2.serial_num
  253. #define BSC_PROFILE_hw_version page_3.hw_version
  254. #define BSC_PROFILE_sw_version page_3.sw_version
  255. #define BSC_PROFILE_model_num page_3.model_num
  256. #define BSC_PROFILE_fract_bat_volt page_4.fract_bat_volt
  257. #define BSC_PROFILE_coarse_bat_volt page_4.coarse_bat_volt
  258. #define BSC_PROFILE_bat_status page_4.bat_status
  259. #define BSC_PROFILE_stop_indicator page_5.stop_indicator
  260. #define BSC_PROFILE_cadence_event_time page_comb_0.cadence_event_time
  261. #define BSC_PROFILE_cadence_rev_count page_comb_0.cadence_rev_count
  262. #define BSC_PROFILE_speed_event_time page_comb_0.speed_event_time
  263. #define BSC_PROFILE_speed_rev_count page_comb_0.speed_rev_count
  264. /** @} */
  265. /**@brief Function for initializing the ANT BSC profile instance.
  266. *
  267. * @param[in] p_profile Pointer to the profile instance.
  268. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  269. * @param[in] p_disp_config Pointer to the BSC display configuration structure.
  270. *
  271. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  272. */
  273. ret_code_t ant_bsc_disp_init(ant_bsc_profile_t * p_profile,
  274. ant_channel_config_t const * p_channel_config,
  275. ant_bsc_disp_config_t const * p_disp_config);
  276. /**@brief Function for initializing the ANT BSC profile instance.
  277. *
  278. * @param[in] p_profile Pointer to the profile instance.
  279. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  280. * @param[in] p_sens_config Pointer to the BSC sensor configuration structure.
  281. *
  282. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  283. */
  284. ret_code_t ant_bsc_sens_init(ant_bsc_profile_t * p_profile,
  285. ant_channel_config_t const * p_channel_config,
  286. ant_bsc_sens_config_t const * p_sens_config);
  287. /**@brief Function for opening the profile instance channel for the ANT BSC Display.
  288. *
  289. * Before calling this function, pages should be configured.
  290. *
  291. * @param[in] p_profile Pointer to the profile instance.
  292. *
  293. * @retval NRF_SUCCESS If the channel was successfully opened. Otherwise, an error code is returned.
  294. */
  295. ret_code_t ant_bsc_disp_open(ant_bsc_profile_t * p_profile);
  296. /**@brief Function for opening the profile instance channel for the ANT BSC Sensor.
  297. *
  298. * Before calling this function, pages should be configured.
  299. *
  300. * @param[in] p_profile Pointer to the profile instance.
  301. *
  302. * @retval NRF_SUCCESS If the channel was successfully opened. Otherwise, an error code is returned.
  303. */
  304. ret_code_t ant_bsc_sens_open(ant_bsc_profile_t * p_profile);
  305. /**@brief Function for handling the Sensor ANT events.
  306. *
  307. * @details This function handles all events from the ANT stack that are of interest to the Bicycle Speed and Cadence Sensor profile.
  308. *
  309. * @param[in] p_ant_evt Event received from the ANT stack.
  310. * @param[in] p_context Pointer to the profile instance.
  311. */
  312. void ant_bsc_sens_evt_handler(ant_evt_t * p_ant_evt, void * p_context);
  313. /**@brief Function for handling the Display ANT events.
  314. *
  315. * @details This function handles all events from the ANT stack that are of interest to the Bicycle Speed and Cadence Display profile.
  316. *
  317. * @param[in] p_ant_evt Event received from the ANT stack.
  318. * @param[in] p_context Pointer to the profile instance.
  319. */
  320. void ant_bsc_disp_evt_handler(ant_evt_t * p_ant_evt, void * p_context);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif // ANT_BSC_H__
  325. /** @} */