bsp_rtc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "bsp_rtc.h"
  2. #include "ble_conn_state.h"
  3. #include "nrf_ble_scan.h"
  4. #include "app_timer.h"
  5. #include "bsp_battery.h"
  6. #include "drv_mpu9250.h"
  7. #include "nrf_delay.h"
  8. #define COMPARE_COUNTERTIME (1UL) /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
  9. static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
  10. static uint16_t m_sleeptimes_S = 0;
  11. /** @brief: Function for handling the RTC0 interrupts.
  12. * Triggered on TICK and COMPARE0 match.
  13. */
  14. static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
  15. {
  16. uint32_t err_code;
  17. if (int_type == NRF_DRV_RTC_INT_COMPARE0)
  18. {
  19. nrf_drv_rtc_counter_clear(&rtc);
  20. err_code = nrf_drv_rtc_cc_set(&rtc,0,m_sleeptimes_S,true);
  21. APP_ERROR_CHECK(err_code);
  22. SEGGER_RTT_printf(0,"NRF_DRV_RTC_INT_COMPARE0(0x%X)\n",NRF_RTC0->COUNTER);
  23. }
  24. else if (int_type == NRF_DRV_RTC_INT_TICK)
  25. {
  26. // SEGGER_RTT_printf(0,"NRF_DRV_RTC_INT_TICK(0x%X)\n",NRF_RTC0->COUNTER);
  27. }
  28. }
  29. /** @brief Function initialization and configuration of RTC driver instance.
  30. */
  31. void RTC_init(void)
  32. {
  33. uint32_t err_code;
  34. //Initialize RTC instance
  35. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  36. config.prescaler = 0;// f(RTC) = 32.768kHZ/(prescaler+1) = 8HZ = 125ms
  37. err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
  38. APP_ERROR_CHECK(err_code);
  39. err_code = nrf_drv_rtc_cc_set(&rtc,0,300,true);
  40. APP_ERROR_CHECK(err_code);
  41. nrf_drv_rtc_counter_clear(&rtc);
  42. // //Enable tick event & interrupt
  43. // nrf_drv_rtc_tick_enable(&rtc,true);
  44. // //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
  45. // err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
  46. // APP_ERROR_CHECK(err_code);
  47. // //Power on RTC instance
  48. nrf_drv_rtc_enable(&rtc);
  49. }
  50. void rtc_start(uint16_t sleep_S)
  51. {
  52. uint32_t err_code;
  53. m_sleeptimes_S = sleep_S;
  54. //Set compare channel to trigger interrupt after sleep_S seconds
  55. err_code = nrf_drv_rtc_cc_set(&rtc,0,m_sleeptimes_S,true);
  56. APP_ERROR_CHECK(err_code);
  57. nrf_drv_rtc_counter_clear(&rtc);
  58. nrf_drv_rtc_enable(&rtc);
  59. // SEGGER_RTT_printf(0,"nrf_drv_rtc_enable(0x%X)\n",NRF_RTC0->COUNTER);
  60. }
  61. void rtc_stop(void)
  62. {
  63. nrf_drv_rtc_disable(&rtc);
  64. // SEGGER_RTT_printf(0,"nrf_drv_rtc_disable(0x%X)\n",NRF_RTC0->COUNTER);
  65. }
  66. /*
  67. @brief 睡眠函数
  68. @param sleep_ms :睡眠时间,units of 125 ms
  69. @return 实际睡眠时间
  70. */
  71. uint32_t rtc_sleep(uint32_t sleep_125ms)
  72. {
  73. uint32_t sleep_times,wakeup_times,actual_sleeptimes;
  74. //关闭蓝牙扫描
  75. // if(!ble_conn_state_central_conn_count())//作为主机没有连接情况下才停止扫描
  76. // {
  77. // nrf_ble_scan_stop();
  78. //// SEGGER_RTT_printf(0,"scan off...\n");
  79. // }
  80. //暂停软件定时器
  81. // app_timer_pause();
  82. // nrf_delay_ms(100);
  83. SEGGER_RTT_printf(0,"timer off...\n");
  84. //关闭ADC
  85. // ADC_Enable(false);
  86. // SEGGER_RTT_printf(0,"adc off...\n");
  87. //设定rtc唤醒时间且开启rtc
  88. rtc_start(sleep_125ms);
  89. // SEGGER_RTT_printf(0,"rtc start(%d)ms\n",sleep_125ms * 125);
  90. sleep_times = NRF_RTC0->COUNTER;
  91. //进入睡眠
  92. CRITICAL_REGION_ENTER();
  93. __SEV();
  94. __WFE();
  95. CRITICAL_REGION_EXIT();
  96. (void)sd_app_evt_wait();
  97. wakeup_times = NRF_RTC0->COUNTER;
  98. // SEGGER_RTT_printf(0,"===============>rtc sleep_times(%d)\n",sleep_times);
  99. // SEGGER_RTT_printf(0,"===============>rtc wakeup_times(%d)\n",wakeup_times);
  100. //停止rtc
  101. rtc_stop();
  102. // SEGGER_RTT_printf(0,"rtc end...\n");
  103. //开启ADC
  104. // ADC_Enable(true);
  105. // SEGGER_RTT_printf(0,"adc on...\n");
  106. //恢复软件定时器
  107. // app_timer_resume();
  108. SEGGER_RTT_printf(0,"timer on...\n");
  109. nrf_delay_ms(100);
  110. actual_sleeptimes = (wakeup_times - sleep_times)/32.768*1000;
  111. return actual_sleeptimes/1000;
  112. }