本帖最后由 mars4zhu 于 2014-11-27 11:32 编辑
HRM_LowPowerRTC 是低功耗RTC,平时进入低功耗模式,RTC唤醒后,通过notify来通知手机更新心率数据(心率数据是模拟产生随机心率,即
hr = (rand()&0x1F)+60;)。
- hr = (rand()&0x1F)+60;
- __disable_irq(); /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
- send_hr_measurement = 0;
- __set_PRIMASK(uwPRIMASK_Bit); /**< Restore PRIMASK bit*/
- deviceState = APPL_WAIT;
- //HRProfile_Set_Sensor_Contact_Support_Bit(); //for test case TC_CN_BV_05
- Osal_MemSet((void*)&heartRateMeasurmentValue, 0, sizeof(heartRateMeasurmentValue));
- heartRateMeasurmentValue.valueformat = HRM_VALUE_FORMAT_UINT8;//HRM_VALUE_FORMAT_UINT16;
- heartRateMeasurmentValue.sensorContact = HRM_SENSOR_CONTACT_PRESENT;
- heartRateMeasurmentValue.energyExpendedStatus = HRM_ENERGY_EXPENDED_PRESENT;
- heartRateMeasurmentValue.rrIntervalStatus = HRM_RR_INTERVAL_PRESENT;
- heartRateMeasurmentValue.heartRateValue = hr;//(uint8)0xE4;
- heartRateMeasurmentValue.enrgyExpended = 0x00;
- heartRateMeasurmentValue.numOfRRIntervalvalues = 0x05;
- for (indx=0; indx<9; indx++)
- {
- heartRateMeasurmentValue.rrIntervalValues[indx] = (uint16)(indx + 0);
- }
- HRProfile_Send_HRM_Value (heartRateMeasurmentValue);
复制代码
通过进入睡眠模式,使用RTC的唤醒功能,每1s唤醒。
- /**
- * @brief Disable wakeup counter and set the wakeup time
- * @param None
- * @retval None
- */
- void LPM_EnterStopMode(void)
- {
- /* Disable Wakeup Counter */
- HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
- /*## Setting the Wake up time ############################################*/
- /* RTC Wakeup Interrupt Generation:
- Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI))
- Wakeup Time = Wakeup Time Base * WakeUpCounter
- = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter
- ==> WakeUpCounter = Wakeup Time / Wakeup Time Base
- To configure the wake up timer to 4s the WakeUpCounter is set to 0x1FFF:
- RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
- Wakeup Time Base = 16 /(~39.000KHz) = ~0,410 ms
- Wakeup Time = ~4s = 0,410ms * WakeUpCounter
- ==> WakeUpCounter = ~4s/0,410ms = 9750 = 0x2616 */
- //HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x2616, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Setting the Wake up time to 4s*/
- HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x987, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Setting the Wake up time to 1s*/
- //HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x262, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Setting the Wake up time to 0.25s*/
- }
复制代码
该例程必须使用X-CUBE-IDB04\Utilities\Android_Software\HRM_CentralBLE\STM32_BLE_Toolbox.apk进行测试。
效果如下:(必须在Option里面设置成BLE Service Display, 不然不停的连接、断开。。。。不知道这么做是为什么)
进入下列界面后, 必须点击notfi/indic按钮, 设置BlueNRG的心率notification功能, 这样基本上每隔一秒钟, 心率数据就会变化(随机数模仿的心率)
下载了程序后,再去调试, 发现无法连接, 经过N多次摸索后,终于发现原因为:
程序中复用了JTAG或SW复用引脚导致程序执行后出现启动配置错误或者调试引脚被占用而引起的芯片锁死
X-CUBE-IDB04\Projects\STM32L053R8-Nucleo\Applications\Bluetooth_LE\HRM_LowPowerRTC
这个程序为了实现低功耗, 把SWD引脚功能给设置成了Analog-In模式,也就是下载了第一次之后,SWD就不能正常用了。 必须是“under-reset”才能发现芯片, 但是这样的话就无法实现正常调试i运行了。
我把它的代码改了一下,就可以正常用SWD了。
把low_power_conf.c 第138行那一段,把PA所有IO口都设置为这种模式了:
- /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
- GPIO_InitStructure.Pin = GPIO_PIN_All;
- GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
复制代码
我改成以下, 不改变PA13和PA14(也就是维持SWD的SWCLK和SWDIO两个引脚的调试功能),就可以正常运行调试了。
- /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
- GPIO_InitStructure.Pin = GPIO_PIN_All;
- GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- // HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
复制代码
HRM的Profile标准提供了传感器位置的数据,原例程设置为BODY_SENSOR_LOCATION_HAND,我把它改为BODY_SENSOR_LOCATION_EAR_LOBE
修改hrm_profile_application.c中断额Init_Profile()函数,
- void Init_Profile()
- {
- #if (BLE_CURRENT_PROFILE_ROLES & HEART_RATE_SENSOR)
- PRINTF ("Test Application: Initializing Heart Rate Profile \n" );
- PRINTF ("Initializing Heart Rate Profile \n" );
- if (HRProfile_Init((uint8)0xFF,
- BLE_Profiles_Evt_Notify_Cb,
- BODY_SENSOR_LOCATION_EAR_LOBE) == BLE_STATUS_SUCCESS)
- {
- APPL_MESG_DBG(profiledbgfile,"Initialized Heart Rate Profile \n" );
- }
- else
- {
- APPL_MESG_DBG(profiledbgfile,"Failed Heart Rate Profile \n" );
- }
- #endif
- }/* end Init_Profile() */
复制代码