ant_sdm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_sdm Stride Based Speed and Distance Monitor profile
  43. * @{
  44. * @ingroup ant_sdk_profiles
  45. * @brief This module implements the Stride Based Speed and Distance Monitor profile.
  46. *
  47. */
  48. #ifndef ANT_SDM_H__
  49. #define ANT_SDM_H__
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include "ant_parameters.h"
  53. #include "nrf_sdh_ant.h"
  54. #include "ant_channel_config.h"
  55. #include "ant_request_controller.h"
  56. #include "ant_sdm_pages.h"
  57. #include "sdk_errors.h"
  58. #define SDM_DEVICE_TYPE 0x7Cu ///< Device type reserved for ANT+ SDM.
  59. #define SDM_ANTPLUS_RF_FREQ 0x39u ///< Frequency, decimal 57 (2457 MHz).
  60. #define SDM_MSG_PERIOD_4Hz 0x1FC6u ///< Message period, decimal 8134 (4.03 Hz).
  61. #define SDM_MSG_PERIOD_2Hz 0x3F8Cu ///< Message period, decimal 16268 (2.01 Hz).
  62. #define SDM_EXT_ASSIGN 0x00 ///< ANT ext assign (see Ext. Assign Channel Parameters in ant_parameters.h: @ref ant_parameters).
  63. #define SDM_DISP_CHANNEL_TYPE CHANNEL_TYPE_SLAVE ///< RX SDM channel type.
  64. #define SDM_SENS_CHANNEL_TYPE CHANNEL_TYPE_MASTER ///< TX SDM channel type.
  65. /**@brief Initialize an ANT channel configuration structure for the SDM profile (Display).
  66. *
  67. * @param[in] NAME Name of related instance.
  68. * @param[in] CHANNEL_NUMBER Number of the channel assigned to the profile instance.
  69. * @param[in] TRANSMISSION_TYPE Type of transmission assigned to the profile instance.
  70. * @param[in] DEVICE_NUMBER Number of the device assigned to the profile instance.
  71. * @param[in] NETWORK_NUMBER Number of the network assigned to the profile instance.
  72. * @param[in] SDM_MSG_PERIOD Channel period in 32 kHz counts. The SDM profile supports only the following periods:
  73. * @ref SDM_MSG_PERIOD_4Hz, @ref SDM_MSG_PERIOD_2Hz.
  74. */
  75. #define SDM_DISP_CHANNEL_CONFIG_DEF(NAME, \
  76. CHANNEL_NUMBER, \
  77. TRANSMISSION_TYPE, \
  78. DEVICE_NUMBER, \
  79. NETWORK_NUMBER, \
  80. SDM_MSG_PERIOD) \
  81. static const ant_channel_config_t CONCAT_2(NAME,_channel_sdm_disp_config) = \
  82. { \
  83. .channel_number = (CHANNEL_NUMBER), \
  84. .channel_type = SDM_DISP_CHANNEL_TYPE, \
  85. .ext_assign = SDM_EXT_ASSIGN, \
  86. .rf_freq = SDM_ANTPLUS_RF_FREQ, \
  87. .transmission_type = (TRANSMISSION_TYPE), \
  88. .device_type = SDM_DEVICE_TYPE, \
  89. .device_number = (DEVICE_NUMBER), \
  90. .channel_period = (SDM_MSG_PERIOD), \
  91. .network_number = (NETWORK_NUMBER), \
  92. }
  93. #define SDM_DISP_CHANNEL_CONFIG(NAME) &CONCAT_2(NAME,_channel_sdm_disp_config)
  94. /**@brief Initialize an ANT channel configuration structure for the SDM profile (Sensor).
  95. *
  96. * @param[in] NAME Name of related instance.
  97. * @param[in] CHANNEL_NUMBER Number of the channel assigned to the profile instance.
  98. * @param[in] TRANSMISSION_TYPE Type of transmission assigned to the profile instance.
  99. * @param[in] DEVICE_NUMBER Number of the device assigned to the profile instance.
  100. * @param[in] NETWORK_NUMBER Number of the network assigned to the profile instance.
  101. */
  102. #define SDM_SENS_CHANNEL_CONFIG_DEF(NAME, \
  103. CHANNEL_NUMBER, \
  104. TRANSMISSION_TYPE, \
  105. DEVICE_NUMBER, \
  106. NETWORK_NUMBER) \
  107. static const ant_channel_config_t CONCAT_2(NAME,_channel_sdm_sens_config) = \
  108. { \
  109. .channel_number = (CHANNEL_NUMBER), \
  110. .channel_type = SDM_SENS_CHANNEL_TYPE, \
  111. .ext_assign = SDM_EXT_ASSIGN, \
  112. .rf_freq = SDM_ANTPLUS_RF_FREQ, \
  113. .transmission_type = (TRANSMISSION_TYPE), \
  114. .device_type = SDM_DEVICE_TYPE, \
  115. .device_number = (DEVICE_NUMBER), \
  116. .channel_period = SDM_MSG_PERIOD_4Hz, \
  117. .network_number = (NETWORK_NUMBER), \
  118. }
  119. #define SDM_SENS_CHANNEL_CONFIG(NAME) &CONCAT_2(NAME,_channel_sdm_sens_config)
  120. /**@brief Initialize an ANT profile configuration structure for the SDM profile (Display).
  121. *
  122. * @param[in] NAME Name of related instance.
  123. * @param[in] EVT_HANDLER Event handler to be called for handling events in the SDM profile.
  124. */
  125. #define SDM_DISP_PROFILE_CONFIG_DEF(NAME, \
  126. EVT_HANDLER) \
  127. static ant_sdm_disp_cb_t CONCAT_2(NAME,_sdm_disp_cb); \
  128. static const ant_sdm_disp_config_t CONCAT_2(NAME,_profile_sdm_disp_config) = \
  129. { \
  130. .p_cb = &CONCAT_2(NAME,_sdm_disp_cb), \
  131. .evt_handler = (EVT_HANDLER), \
  132. }
  133. #define SDM_DISP_PROFILE_CONFIG(NAME) &CONCAT_2(NAME,_profile_sdm_disp_config)
  134. /**@brief Initialize an ANT profile configuration structure for the SDM profile (Sensor).
  135. *
  136. * @param[in] NAME Name of related instance.
  137. * @param[in] SUPPLEMENTARY_PAGE_NUMBER Supplementary data page (ANT_SDM_PAGE_2 or ANT_SDM_PAGE_3). Use ANT_SDM_PAGE_1 to disable.
  138. * @param[in] EVT_HANDLER Event handler to be called for handling events in the SDM profile.
  139. */
  140. #define SDM_SENS_PROFILE_CONFIG_DEF(NAME, \
  141. SUPPLEMENTARY_PAGE_NUMBER, \
  142. EVT_HANDLER) \
  143. static ant_sdm_sens_cb_t CONCAT_2(NAME,_sdm_sens_cb); \
  144. static const ant_sdm_sens_config_t CONCAT_2(NAME,_profile_sdm_sens_config) = \
  145. { \
  146. .supplementary_page_number = (SUPPLEMENTARY_PAGE_NUMBER), \
  147. .p_cb = &CONCAT_2(NAME,_sdm_sens_cb), \
  148. .evt_handler = (EVT_HANDLER), \
  149. }
  150. #define SDM_SENS_PROFILE_CONFIG(NAME) &CONCAT_2(NAME,_profile_sdm_sens_config)
  151. /**@brief SDM page number type. */
  152. typedef enum{
  153. ANT_SDM_PAGE_1 = 1, ///< Required data page 1.
  154. ANT_SDM_PAGE_2 = 2, ///< Supplementary data page 2.
  155. ANT_SDM_PAGE_3 = 3, ///< Supplementary data page 3.
  156. ANT_SDM_PAGE_16 = 16, ///< Page 16 (sent on request).
  157. ANT_SDM_PAGE_22 = 22, ///< Page 22 (sent on request).
  158. ANT_SDM_PAGE_70 = ANT_COMMON_PAGE_70,
  159. ANT_SDM_PAGE_80 = ANT_COMMON_PAGE_80,
  160. ANT_SDM_PAGE_81 = ANT_COMMON_PAGE_81,
  161. } ant_sdm_page_t;
  162. /**@brief SDM profile event type. */
  163. typedef enum{
  164. ANT_SDM_PAGE_1_UPDATED = ANT_SDM_PAGE_1, ///< Data page 1 and speed have been updated (Display) or sent (Sensor).
  165. ANT_SDM_PAGE_2_UPDATED = ANT_SDM_PAGE_2, ///< Data page 2 and speed have been updated (Display) or sent (Sensor).
  166. ANT_SDM_PAGE_3_UPDATED = ANT_SDM_PAGE_3, ///< Data page 3 and speed have been updated (Display) or sent (Sensor).
  167. ANT_SDM_PAGE_16_UPDATED = ANT_SDM_PAGE_16, ///< Data page 16 has been updated (Display) or sent (Sensor).
  168. ANT_SDM_PAGE_22_UPDATED = ANT_SDM_PAGE_22, ///< Data page 22 has been updated (Display) or sent (Sensor).
  169. ANT_SDM_PAGE_80_UPDATED = ANT_SDM_PAGE_80, ///< Data page 80 has been updated (Display) or sent (Sensor).
  170. ANT_SDM_PAGE_81_UPDATED = ANT_SDM_PAGE_81, ///< Data page 81 has been updated (Display) or sent (Sensor).
  171. ANT_SDM_PAGE_REQUEST_SUCCESS, ///< Data page request reached the destination.
  172. ANT_SDM_PAGE_REQUEST_FAILED, ///< Data page request did not reach the destination.
  173. } ant_sdm_evt_t;
  174. // Forward declaration of the ant_sdm_profile_t type.
  175. typedef struct ant_sdm_profile_s ant_sdm_profile_t;
  176. /**@brief SDM event handler type. */
  177. typedef void (* ant_sdm_evt_handler_t) (ant_sdm_profile_t *, ant_sdm_evt_t);
  178. #include "ant_sdm_local.h"
  179. #ifdef __cplusplus
  180. extern "C" {
  181. #endif
  182. /**@brief SDM Sensor configuration structure. */
  183. typedef struct
  184. {
  185. ant_sdm_page_t supplementary_page_number; ///< Supplementary data page (ANT_SDM_PAGE_2 or ANT_SDM_PAGE_3). Use ANT_SDM_PAGE_1 to disable.
  186. ant_sdm_sens_cb_t * p_cb; ///< Pointer to the data buffer for internal use.
  187. ant_sdm_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the SDM profile.
  188. } ant_sdm_sens_config_t;
  189. /**@brief SDM Display configuration structure. */
  190. typedef struct
  191. {
  192. ant_sdm_disp_cb_t * p_cb; ///< Pointer to the data buffer for internal use.
  193. ant_sdm_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the SDM profile.
  194. } ant_sdm_disp_config_t;
  195. /**@brief SDM profile structure. */
  196. struct ant_sdm_profile_s
  197. {
  198. uint8_t channel_number; ///< Channel number assigned to the profile.
  199. union {
  200. ant_sdm_disp_cb_t * p_disp_cb;
  201. ant_sdm_sens_cb_t * p_sens_cb;
  202. } _cb; ///< Pointer to internal control block.
  203. ant_sdm_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the SDM profile.
  204. ant_sdm_page1_data_t page_1; ///< Page 1.
  205. ant_sdm_page2_data_t page_2; ///< Page 2.
  206. ant_sdm_page3_data_t page_3; ///< Page 3.
  207. ant_sdm_page22_data_t page_22; ///< Page 22.
  208. ant_common_page80_data_t page_80; ///< Page 80.
  209. ant_common_page81_data_t page_81; ///< Page 81.
  210. ant_sdm_common_data_t common; ///< SDM common data.
  211. };
  212. /** @name Defines for accessing ant_sdm_profile_t members variables
  213. @{ */
  214. #define SDM_PROFILE_update_latency page_1.update_latency
  215. #define SDM_PROFILE_time page_1.time
  216. #define SDM_PROFILE_status page_2.status.items
  217. #define SDM_PROFILE_cadence page_2.cadence
  218. #define SDM_PROFILE_calories page_3.calories
  219. #define SDM_PROFILE_capabilities page_22.capabilities.items
  220. #define SDM_PROFILE_speed common.speed
  221. #define SDM_PROFILE_distance common.distance
  222. #define SDM_PROFILE_strides common.strides
  223. #define SDM_PROFILE_hw_revision page_80.hw_revision
  224. #define SDM_PROFILE_manufacturer_id page_80.manufacturer_id
  225. #define SDM_PROFILE_model_number page_80.model_number
  226. #define SDM_PROFILE_sw_revision_minor page_81.sw_revision_minor
  227. #define SDM_PROFILE_sw_revision_major page_81.sw_revision_major
  228. #define SDM_PROFILE_serial_number page_81.serial_number
  229. /** @} */
  230. /**@brief Function for initializing the ANT SDM RX profile instance.
  231. *
  232. * @param[in] p_profile Pointer to the profile instance.
  233. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  234. * @param[in] p_disp_config Pointer to the SDM Display configuration structure.
  235. *
  236. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  237. */
  238. ret_code_t ant_sdm_disp_init(ant_sdm_profile_t * p_profile,
  239. ant_channel_config_t const * p_channel_config,
  240. ant_sdm_disp_config_t const * p_disp_config);
  241. /**@brief Function for initializing the ANT SDM TX profile instance.
  242. *
  243. * @param[in] p_profile Pointer to the profile instance.
  244. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  245. * @param[in] p_sens_config Pointer to the SDM Sensor configuration structure.
  246. *
  247. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  248. */
  249. ret_code_t ant_sdm_sens_init(ant_sdm_profile_t * p_profile,
  250. ant_channel_config_t const * p_channel_config,
  251. ant_sdm_sens_config_t const * p_sens_config);
  252. /**@brief Function for opening the profile instance channel for ANT SDM Display.
  253. *
  254. * Before calling this function, pages should be configured.
  255. *
  256. * @param[in] p_profile Pointer to the profile instance.
  257. *
  258. * @retval NRF_SUCCESS If the channel was successfully opened. Otherwise, an error code is returned.
  259. */
  260. ret_code_t ant_sdm_disp_open(ant_sdm_profile_t * p_profile);
  261. /**@brief Function for opening the profile instance channel for ANT SDM Sensor.
  262. *
  263. * Before calling this function, pages should be configured.
  264. *
  265. * @param[in] p_profile Pointer to the profile instance.
  266. *
  267. * @retval NRF_SUCCESS If the channel was successfully opened. Otherwise, an error code is returned.
  268. */
  269. ret_code_t ant_sdm_sens_open(ant_sdm_profile_t * p_profile);
  270. /**@brief Function for sending a data page request.
  271. *
  272. * This function can be called only on the display side.
  273. *
  274. * @param[in] p_profile Pointer to the profile instance.
  275. * @param[in] p_page_70 Pointer to the prepared page 70.
  276. *
  277. * @retval NRF_SUCCESS If the request has been sent. Otherwise, an error code is returned.
  278. */
  279. ret_code_t ant_sdm_page_request(ant_sdm_profile_t * p_profile, ant_common_page70_data_t * p_page_70);
  280. /**@brief Function for handling the Sensor ANT events.
  281. *
  282. * @details This function handles all events from the ANT stack that are of interest to the SDM Sensor profile.
  283. *
  284. * @param[in] p_ant_evt Event received from the ANT stack.
  285. * @param[in] p_context Pointer to the profile instance.
  286. */
  287. void ant_sdm_sens_evt_handler(ant_evt_t * p_ant_evt, void * p_context);
  288. /**@brief Function for handling the Display ANT events.
  289. *
  290. * @details This function handles all events from the ANT stack that are of interest to the SDM Display profile.
  291. *
  292. * @param[in] p_ant_evt Event received from the ANT stack.
  293. * @param[in] p_context Pointer to the profile instance.
  294. */
  295. void ant_sdm_disp_evt_handler(ant_evt_t * p_ant_evt, void * p_context);
  296. #ifdef __cplusplus
  297. }
  298. #endif
  299. #endif // ANT_SDM_H__
  300. /** @} */