ant_hrm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_hrm Heart Rate Monitor profile
  43. * @{
  44. * @ingroup ant_sdk_profiles
  45. * @brief This module implements the Heart Rate Monitor profile.
  46. *
  47. */
  48. #ifndef ANT_HRM_H__
  49. #define ANT_HRM_H__
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include "app_util.h"
  53. #include "ant_parameters.h"
  54. #include "nrf_sdh_ant.h"
  55. #include "ant_channel_config.h"
  56. #include "ant_hrm_pages.h"
  57. #include "sdk_errors.h"
  58. #define HRM_DEVICE_TYPE 0x78u ///< Device type reserved for ANT+ heart rate monitor.
  59. #define HRM_ANTPLUS_RF_FREQ 0x39u ///< Frequency, decimal 57 (2457 MHz).
  60. #define HRM_MSG_PERIOD_4Hz 0x1F86u ///< Message period, decimal 8070 (4.06 Hz).
  61. #define HRM_MSG_PERIOD_2Hz 0x3F0Cu ///< Message period, decimal 16140 (2.03 Hz).
  62. #define HRM_MSG_PERIOD_1Hz 0x7E18u ///< Message period, decimal 32280 (1.02 Hz).
  63. #define HRM_EXT_ASSIGN 0x00 ///< ANT ext assign.
  64. #define HRM_DISP_CHANNEL_TYPE CHANNEL_TYPE_SLAVE ///< Display HRM channel type.
  65. #define HRM_SENS_CHANNEL_TYPE CHANNEL_TYPE_MASTER ///< Sensor HRM channel type.
  66. /**@brief Initialize an ANT channel configuration structure for the HRM profile (Display).
  67. *
  68. * @param[in] NAME Name of related instance.
  69. * @param[in] CHANNEL_NUMBER Number of the channel assigned to the profile instance.
  70. * @param[in] TRANSMISSION_TYPE Type of transmission assigned to the profile instance.
  71. * @param[in] DEVICE_NUMBER Number of the device assigned to the profile instance.
  72. * @param[in] NETWORK_NUMBER Number of the network assigned to the profile instance.
  73. * @param[in] HRM_MSG_PERIOD Channel period in 32 kHz counts. The HRM profile supports only the following periods:
  74. * @ref HRM_MSG_PERIOD_4Hz, @ref HRM_MSG_PERIOD_2Hz, @ref HRM_MSG_PERIOD_1Hz.
  75. */
  76. #define HRM_DISP_CHANNEL_CONFIG_DEF(NAME, \
  77. CHANNEL_NUMBER, \
  78. TRANSMISSION_TYPE, \
  79. DEVICE_NUMBER, \
  80. NETWORK_NUMBER, \
  81. HRM_MSG_PERIOD) \
  82. static const ant_channel_config_t CONCAT_2(NAME,_channel_hrm_disp_config) = \
  83. { \
  84. .channel_number = (CHANNEL_NUMBER), \
  85. .channel_type = HRM_DISP_CHANNEL_TYPE, \
  86. .ext_assign = HRM_EXT_ASSIGN, \
  87. .rf_freq = HRM_ANTPLUS_RF_FREQ, \
  88. .transmission_type = (TRANSMISSION_TYPE), \
  89. .device_type = HRM_DEVICE_TYPE, \
  90. .device_number = (DEVICE_NUMBER), \
  91. .channel_period = (HRM_MSG_PERIOD), \
  92. .network_number = (NETWORK_NUMBER), \
  93. }
  94. #define HRM_DISP_CHANNEL_CONFIG(NAME) &CONCAT_2(NAME,_channel_hrm_disp_config)
  95. /**@brief Initialize an ANT channel configuration structure for the HRM profile (Sensor).
  96. *
  97. * @param[in] NAME Name of related instance.
  98. * @param[in] CHANNEL_NUMBER Number of the channel assigned to the profile instance.
  99. * @param[in] TRANSMISSION_TYPE Type of transmission assigned to the profile instance.
  100. * @param[in] DEVICE_NUMBER Number of the device assigned to the profile instance.
  101. * @param[in] NETWORK_NUMBER Number of the network assigned to the profile instance.
  102. */
  103. #define HRM_SENS_CHANNEL_CONFIG_DEF(NAME, \
  104. CHANNEL_NUMBER, \
  105. TRANSMISSION_TYPE, \
  106. DEVICE_NUMBER, \
  107. NETWORK_NUMBER) \
  108. static const ant_channel_config_t CONCAT_2(NAME,_channel_hrm_sens_config) = \
  109. { \
  110. .channel_number = (CHANNEL_NUMBER), \
  111. .channel_type = HRM_SENS_CHANNEL_TYPE, \
  112. .ext_assign = HRM_EXT_ASSIGN, \
  113. .rf_freq = HRM_ANTPLUS_RF_FREQ, \
  114. .transmission_type = (TRANSMISSION_TYPE), \
  115. .device_type = HRM_DEVICE_TYPE, \
  116. .device_number = (DEVICE_NUMBER), \
  117. .channel_period = HRM_MSG_PERIOD_4Hz, \
  118. .network_number = (NETWORK_NUMBER), \
  119. }
  120. #define HRM_SENS_CHANNEL_CONFIG(NAME) &CONCAT_2(NAME,_channel_hrm_sens_config)
  121. /**@brief Initialize an ANT profile configuration structure for the HRM profile (Sensor).
  122. *
  123. * @param[in] NAME Name of related instance.
  124. * @param[in] PAGE_1_PRESENT Determines whether page 1 is included.
  125. * @param[in] MAIN_PAGE_NUMBER Determines the main data page (@ref ANT_HRM_PAGE_0 or @ref ANT_HRM_PAGE_4).
  126. * @param[in] EVT_HANDLER Event handler to be called for handling events in the HRM profile.
  127. */
  128. #define HRM_SENS_PROFILE_CONFIG_DEF(NAME, \
  129. PAGE_1_PRESENT, \
  130. MAIN_PAGE_NUMBER, \
  131. EVT_HANDLER) \
  132. static ant_hrm_sens_cb_t CONCAT_2(NAME,_hrm_sens_cb); \
  133. static const ant_hrm_sens_config_t CONCAT_2(NAME,_profile_hrm_sens_config) = \
  134. { \
  135. .page_1_present = (PAGE_1_PRESENT), \
  136. .main_page_number = (MAIN_PAGE_NUMBER), \
  137. .p_cb = &CONCAT_2(NAME,_hrm_sens_cb), \
  138. .evt_handler = (EVT_HANDLER), \
  139. }
  140. #define HRM_SENS_PROFILE_CONFIG(NAME) &CONCAT_2(NAME,_profile_hrm_sens_config)
  141. /**@brief HRM page number type. */
  142. typedef enum
  143. {
  144. ANT_HRM_PAGE_0, ///< Main data page number 0.
  145. ANT_HRM_PAGE_1, ///< Background data page number 1. This page is optional.
  146. ANT_HRM_PAGE_2, ///< Background data page number 2.
  147. ANT_HRM_PAGE_3, ///< Background data page number 3.
  148. ANT_HRM_PAGE_4 ///< Main data page number 4.
  149. } ant_hrm_page_t;
  150. /**@brief HRM profile event type. */
  151. typedef enum
  152. {
  153. ANT_HRM_PAGE_0_UPDATED = ANT_HRM_PAGE_0, ///< Data page 0 has been updated (Display) or sent (Sensor).
  154. ANT_HRM_PAGE_1_UPDATED = ANT_HRM_PAGE_1, ///< Data page 0 and page 1 have been updated (Display) or sent (Sensor).
  155. ANT_HRM_PAGE_2_UPDATED = ANT_HRM_PAGE_2, ///< Data page 0 and page 2 have been updated (Display) or sent (Sensor).
  156. ANT_HRM_PAGE_3_UPDATED = ANT_HRM_PAGE_3, ///< Data page 0 and page 3 have been updated (Display) or sent (Sensor).
  157. ANT_HRM_PAGE_4_UPDATED = ANT_HRM_PAGE_4, ///< Data page 0 and page 4 have been updated (Display) or sent (Sensor).
  158. } ant_hrm_evt_t;
  159. // Forward declaration of the ant_hrm_profile_t type.
  160. typedef struct ant_hrm_profile_s ant_hrm_profile_t;
  161. /**@brief HRM event handler type. */
  162. typedef void (* ant_hrm_evt_handler_t) (ant_hrm_profile_t *, ant_hrm_evt_t);
  163. #include "ant_hrm_local.h"
  164. #ifdef __cplusplus
  165. extern "C" {
  166. #endif
  167. /**@brief HRM sensor configuration structure. */
  168. typedef struct
  169. {
  170. bool page_1_present; ///< Determines whether page 1 is included.
  171. ant_hrm_page_t main_page_number; ///< Determines the main data page (@ref ANT_HRM_PAGE_0 or @ref ANT_HRM_PAGE_4).
  172. ant_hrm_sens_cb_t * p_cb; ///< Pointer to the data buffer for internal use.
  173. ant_hrm_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the HRM profile.
  174. } ant_hrm_sens_config_t;
  175. /**@brief HRM profile structure. */
  176. struct ant_hrm_profile_s
  177. {
  178. uint8_t channel_number; ///< Channel number assigned to the profile.
  179. union {
  180. void * p_none;
  181. ant_hrm_sens_cb_t * p_sens_cb;
  182. } _cb; ///< Pointer to internal control block.
  183. ant_hrm_evt_handler_t evt_handler; ///< Event handler to be called for handling events in the HRM profile.
  184. ant_hrm_page0_data_t page_0; ///< Page 0.
  185. ant_hrm_page1_data_t page_1; ///< Page 1.
  186. ant_hrm_page2_data_t page_2; ///< Page 2.
  187. ant_hrm_page3_data_t page_3; ///< Page 3.
  188. ant_hrm_page4_data_t page_4; ///< Page 4.
  189. };
  190. /** @name Defines for accessing ant_hrm_profile_t member variables
  191. @{ */
  192. #define HRM_PROFILE_beat_count page_0.beat_count
  193. #define HRM_PROFILE_computed_heart_rate page_0.computed_heart_rate
  194. #define HRM_PROFILE_beat_time page_0.beat_time
  195. #define HRM_PROFILE_operating_time page_1.operating_time
  196. #define HRM_PROFILE_manuf_id page_2.manuf_id
  197. #define HRM_PROFILE_serial_num page_2.serial_num
  198. #define HRM_PROFILE_hw_version page_3.hw_version
  199. #define HRM_PROFILE_sw_version page_3.sw_version
  200. #define HRM_PROFILE_model_num page_3.model_num
  201. #define HRM_PROFILE_manuf_spec page_4.manuf_spec
  202. #define HRM_PROFILE_prev_beat page_4.prev_beat
  203. /** @} */
  204. /**@brief Function for initializing the ANT HRM Display profile instance.
  205. *
  206. * @param[in] p_profile Pointer to the profile instance.
  207. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  208. * @param[in] evt_handler Event handler to be called for handling events in the HRM profile.
  209. *
  210. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  211. */
  212. ret_code_t ant_hrm_disp_init(ant_hrm_profile_t * p_profile,
  213. ant_channel_config_t const * p_channel_config,
  214. ant_hrm_evt_handler_t evt_handler);
  215. /**@brief Function for initializing the ANT HRM Sensor profile instance.
  216. *
  217. * @param[in] p_profile Pointer to the profile instance.
  218. * @param[in] p_channel_config Pointer to the ANT channel configuration structure.
  219. * @param[in] p_sens_config Pointer to the HRM sensor configuration structure.
  220. *
  221. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  222. */
  223. ret_code_t ant_hrm_sens_init(ant_hrm_profile_t * p_profile,
  224. ant_channel_config_t const * p_channel_config,
  225. ant_hrm_sens_config_t const * p_sens_config);
  226. /**@brief Function for opening the profile instance channel for ANT HRM Display.
  227. *
  228. * Before calling this function, pages should be configured.
  229. *
  230. * @param[in] p_profile Pointer to the profile instance.
  231. *
  232. * @retval NRF_SUCCESS If the channel was successfully opened. Otherwise, an error code is returned.
  233. */
  234. ret_code_t ant_hrm_disp_open(ant_hrm_profile_t * p_profile);
  235. /**@brief Function for opening the profile instance channel for ANT HRM Sensor.
  236. *
  237. * Before calling this function, pages should be configured.
  238. *
  239. * @param[in] p_profile Pointer to the profile instance.
  240. *
  241. * @retval NRF_SUCCESS If the channel was successfully opened. Otherwise, an error code is returned.
  242. */
  243. ret_code_t ant_hrm_sens_open(ant_hrm_profile_t * p_profile);
  244. /**@brief Function for handling the sensor ANT events.
  245. *
  246. * @details This function handles all events from the ANT stack that are of interest to the Heart Rate Monitor Sensor profile.
  247. *
  248. * @param[in] p_ant_evt Event received from the ANT stack.
  249. * @param[in] p_context Pointer to the profile instance.
  250. */
  251. void ant_hrm_sens_evt_handler(ant_evt_t * p_ant_evt, void * p_context);
  252. /**@brief Function for handling the display ANT events.
  253. *
  254. * @details This function handles all events from the ANT stack that are of interest to the Heart Rate Monitor Display profile.
  255. *
  256. * @param[in] p_ant_evt Event received from the ANT stack.
  257. * @param[in] p_context Pointer to the profile instance.
  258. */
  259. void ant_hrm_disp_evt_handler(ant_evt_t * p_ant_evt, void * p_context);
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif // ANT_HRM_H__
  264. /** @} */