hrs_app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file hrs_app.c
  5. * @author MCD Application Team
  6. * @brief Heart Rate Service Application
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under Ultimate Liberty license
  14. * SLA0044, the "License"; You may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at:
  16. * www.st.com/SLA0044
  17. *
  18. ******************************************************************************
  19. */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "app_common.h"
  23. #include "ble.h"
  24. #include "hrs_app.h"
  25. #include "cmsis_os.h"
  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. typedef struct
  31. {
  32. HRS_BodySensorLocation_t BodySensorLocationChar;
  33. HRS_MeasVal_t MeasurementvalueChar;
  34. uint8_t ResetEnergyExpended;
  35. uint8_t TimerMeasurement_Id;
  36. } HRSAPP_Context_t;
  37. /* USER CODE BEGIN PTD */
  38. /* USER CODE END PTD */
  39. /* Private defines ------------------------------------------------------------*/
  40. /* USER CODE BEGIN PD */
  41. /* USER CODE END PD */
  42. /* Private macros ------------------------------------------------------------*/
  43. #define HRSAPP_MEASUREMENT_INTERVAL (1000000/CFG_TS_TICK_VAL) /**< 1s */
  44. /* USER CODE BEGIN PM */
  45. /* USER CODE END PM */
  46. /* Private variables ---------------------------------------------------------*/
  47. /**
  48. * START of Section BLE_APP_CONTEXT
  49. */
  50. PLACE_IN_SECTION("BLE_APP_CONTEXT") static HRSAPP_Context_t HRSAPP_Context;
  51. /**
  52. * END of Section BLE_APP_CONTEXT
  53. */
  54. osThreadId_t HrsProcessId;
  55. const osThreadAttr_t HrsProcess_attr = {
  56. .name = CFG_HRS_PROCESS_NAME,
  57. .attr_bits = CFG_HRS_PROCESS_ATTR_BITS,
  58. .cb_mem = CFG_HRS_PROCESS_CB_MEM,
  59. .cb_size = CFG_HRS_PROCESS_CB_SIZE,
  60. .stack_mem = CFG_HRS_PROCESS_STACK_MEM,
  61. .priority = CFG_HRS_PROCESS_PRIORITY,
  62. .stack_size = CFG_HRS_PROCESS_STACK_SIZE
  63. };
  64. /* USER CODE BEGIN PV */
  65. /* USER CODE END PV */
  66. /* Private functions prototypes-----------------------------------------------*/
  67. static void HrMeas( void );
  68. static void HrsProcess(void *argument);
  69. static void HRSAPP_Measurement(void);
  70. static uint32_t HRSAPP_Read_RTC_SSR_SS ( void );
  71. /* USER CODE BEGIN PFP */
  72. /* USER CODE END PFP */
  73. /* Functions Definition ------------------------------------------------------*/
  74. void HRS_Notification(HRS_App_Notification_evt_t *pNotification)
  75. {
  76. /* USER CODE BEGIN HRS_Notification_1 */
  77. /* USER CODE END HRS_Notification_1 */
  78. switch(pNotification->HRS_Evt_Opcode)
  79. {
  80. /* USER CODE BEGIN HRS_Notification_HRS_Evt_Opcode */
  81. /* USER CODE END HRS_Notification_HRS_Evt_Opcode */
  82. #if (BLE_CFG_HRS_ENERGY_EXPENDED_INFO_FLAG != 0)
  83. case HRS_RESET_ENERGY_EXPENDED_EVT:
  84. /* USER CODE BEGIN HRS_RESET_ENERGY_EXPENDED_EVT */
  85. HRSAPP_Context.MeasurementvalueChar.EnergyExpended = 0;
  86. HRSAPP_Context.ResetEnergyExpended = 1;
  87. /* USER CODE END HRS_RESET_ENERGY_EXPENDED_EVT */
  88. break;
  89. #endif
  90. case HRS_NOTIFICATION_ENABLED:
  91. /* USER CODE BEGIN HRS_NOTIFICATION_ENABLED */
  92. /**
  93. * It could be the enable notification is received twice without the disable notification in between
  94. */
  95. HW_TS_Stop(HRSAPP_Context.TimerMeasurement_Id);
  96. HW_TS_Start(HRSAPP_Context.TimerMeasurement_Id, HRSAPP_MEASUREMENT_INTERVAL);
  97. /* USER CODE END HRS_NOTIFICATION_ENABLED */
  98. break;
  99. case HRS_NOTIFICATION_DISABLED:
  100. /* USER CODE BEGIN HRS_NOTIFICATION_DISABLED */
  101. HW_TS_Stop(HRSAPP_Context.TimerMeasurement_Id);
  102. /* USER CODE END HRS_NOTIFICATION_DISABLED */
  103. break;
  104. #if (BLE_CFG_OTA_REBOOT_CHAR != 0)
  105. case HRS_STM_BOOT_REQUEST_EVT:
  106. /* USER CODE BEGIN HRS_STM_BOOT_REQUEST_EVT */
  107. *(uint32_t*)SRAM1_BASE = *(uint32_t*)pNotification->DataTransfered.pPayload;
  108. NVIC_SystemReset();
  109. /* USER CODE END HRS_STM_BOOT_REQUEST_EVT */
  110. break;
  111. #endif
  112. default:
  113. /* USER CODE BEGIN HRS_Notification_Default */
  114. /* USER CODE END HRS_Notification_Default */
  115. break;
  116. }
  117. /* USER CODE BEGIN HRS_Notification_2 */
  118. /* USER CODE END HRS_Notification_2 */
  119. return;
  120. }
  121. void HRSAPP_Init(void)
  122. {
  123. HrsProcessId = osThreadNew(HrsProcess, NULL, &HrsProcess_attr);
  124. /* USER CODE BEGIN HRSAPP_Init */
  125. /**
  126. * Set Body Sensor Location
  127. */
  128. HRSAPP_Context.ResetEnergyExpended = 0;
  129. HRSAPP_Context.BodySensorLocationChar = HRS_BODY_SENSOR_LOCATION_HAND;
  130. HRS_UpdateChar(SENSOR_LOCATION_UUID, (uint8_t *)&HRSAPP_Context.BodySensorLocationChar);
  131. /**
  132. * Set Flags for measurement value
  133. */
  134. HRSAPP_Context.MeasurementvalueChar.Flags = ( HRS_HRM_VALUE_FORMAT_UINT16 |
  135. HRS_HRM_SENSOR_CONTACTS_PRESENT |
  136. HRS_HRM_SENSOR_CONTACTS_SUPPORTED |
  137. HRS_HRM_ENERGY_EXPENDED_PRESENT |
  138. HRS_HRM_RR_INTERVAL_PRESENT );
  139. #if (BLE_CFG_HRS_ENERGY_EXPENDED_INFO_FLAG != 0)
  140. if(HRSAPP_Context.MeasurementvalueChar.Flags & HRS_HRM_ENERGY_EXPENDED_PRESENT)
  141. HRSAPP_Context.MeasurementvalueChar.EnergyExpended = 10;
  142. #endif
  143. #if (BLE_CFG_HRS_ENERGY_RR_INTERVAL_FLAG != 0)
  144. if(HRSAPP_Context.MeasurementvalueChar.Flags & HRS_HRM_RR_INTERVAL_PRESENT)
  145. {
  146. uint8_t i;
  147. HRSAPP_Context.MeasurementvalueChar.NbreOfValidRRIntervalValues = BLE_CFG_HRS_ENERGY_RR_INTERVAL_FLAG;
  148. for(i = 0; i < BLE_CFG_HRS_ENERGY_RR_INTERVAL_FLAG; i++)
  149. HRSAPP_Context.MeasurementvalueChar.aRRIntervalValues[i] = 1024;
  150. }
  151. #endif
  152. /**
  153. * Create timer for Heart Rate Measurement
  154. */
  155. HW_TS_Create(CFG_TIM_PROC_ID_ISR, &(HRSAPP_Context.TimerMeasurement_Id), hw_ts_Repeated, HrMeas);
  156. /* USER CODE END HRSAPP_Init */
  157. return;
  158. }
  159. static void HrsProcess(void *argument)
  160. {
  161. UNUSED(argument);
  162. for(;;)
  163. {
  164. osThreadFlagsWait( 1, osFlagsWaitAny, osWaitForever);
  165. HRSAPP_Measurement( );
  166. }
  167. }
  168. static void HRSAPP_Measurement(void)
  169. {
  170. /* USER CODE BEGIN HRSAPP_Measurement */
  171. uint32_t measurement;
  172. measurement = ((HRSAPP_Read_RTC_SSR_SS()) & 0x07) + 65;
  173. HRSAPP_Context.MeasurementvalueChar.MeasurementValue = measurement;
  174. #if (BLE_CFG_HRS_ENERGY_EXPENDED_INFO_FLAG != 0)
  175. if((HRSAPP_Context.MeasurementvalueChar.Flags & HRS_HRM_ENERGY_EXPENDED_PRESENT) &&
  176. (HRSAPP_Context.ResetEnergyExpended == 0))
  177. HRSAPP_Context.MeasurementvalueChar.EnergyExpended += 5;
  178. else if(HRSAPP_Context.ResetEnergyExpended == 1)
  179. HRSAPP_Context.ResetEnergyExpended = 0;
  180. #endif
  181. HRS_UpdateChar(HEART_RATE_MEASURMENT_UUID, (uint8_t *)&HRSAPP_Context.MeasurementvalueChar);
  182. /* USER CODE END HRSAPP_Measurement */
  183. return;
  184. }
  185. static void HrMeas( void )
  186. {
  187. /**
  188. * The code shall be executed in the background as aci command may be sent
  189. * The background is the only place where the application can make sure a new aci command
  190. * is not sent if there is a pending one
  191. */
  192. osThreadFlagsSet( HrsProcessId, 1 );
  193. /* USER CODE BEGIN HrMeas */
  194. /* USER CODE END HrMeas */
  195. return;
  196. }
  197. static uint32_t HRSAPP_Read_RTC_SSR_SS ( void )
  198. {
  199. return ((uint32_t)(READ_BIT(RTC->SSR, RTC_SSR_SS)));
  200. }
  201. /* USER CODE BEGIN FD */
  202. /* USER CODE END FD */
  203. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/