123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #include "bsp_rtc.h"
- #include "ble_conn_state.h"
- #include "nrf_ble_scan.h"
- #include "app_timer.h"
- #include "bsp_battery.h"
- #include "drv_mpu9250.h"
- #include "nrf_delay.h"
- #define COMPARE_COUNTERTIME (1UL) /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
- static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
- static uint16_t m_sleeptimes_S = 0;
- /** @brief: Function for handling the RTC0 interrupts.
- * Triggered on TICK and COMPARE0 match.
- */
- static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
- {
- uint32_t err_code;
- if (int_type == NRF_DRV_RTC_INT_COMPARE0)
- {
- nrf_drv_rtc_counter_clear(&rtc);
- err_code = nrf_drv_rtc_cc_set(&rtc,0,m_sleeptimes_S,true);
- APP_ERROR_CHECK(err_code);
- DEBUG_LOG("NRF_DRV_RTC_INT_COMPARE0(0x%X)\n",NRF_RTC0->COUNTER);
- }
- else if (int_type == NRF_DRV_RTC_INT_TICK)
- {
- // DEBUG_LOG("NRF_DRV_RTC_INT_TICK(0x%X)\n",NRF_RTC0->COUNTER);
- }
- }
- /** @brief Function initialization and configuration of RTC driver instance.
- */
- void RTC_init(void)
- {
- uint32_t err_code;
- //Initialize RTC instance
- nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
- config.prescaler = 0;// f(RTC) = 32.768kHZ/(prescaler+1) = 8HZ = 125ms
- err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
- APP_ERROR_CHECK(err_code);
-
- err_code = nrf_drv_rtc_cc_set(&rtc,0,300,true);
- APP_ERROR_CHECK(err_code);
-
- nrf_drv_rtc_counter_clear(&rtc);
- // //Enable tick event & interrupt
- // nrf_drv_rtc_tick_enable(&rtc,true);
- // //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
- // err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
- // APP_ERROR_CHECK(err_code);
- // //Power on RTC instance
- nrf_drv_rtc_enable(&rtc);
- }
- void rtc_start(uint16_t sleep_S)
- {
- uint32_t err_code;
-
- m_sleeptimes_S = sleep_S;
- //Set compare channel to trigger interrupt after sleep_S seconds
- err_code = nrf_drv_rtc_cc_set(&rtc,0,m_sleeptimes_S,true);
- APP_ERROR_CHECK(err_code);
-
- nrf_drv_rtc_counter_clear(&rtc);
-
- nrf_drv_rtc_enable(&rtc);
- // DEBUG_LOG("nrf_drv_rtc_enable(0x%X)\n",NRF_RTC0->COUNTER);
- }
- void rtc_stop(void)
- {
- nrf_drv_rtc_disable(&rtc);
- // DEBUG_LOG("nrf_drv_rtc_disable(0x%X)\n",NRF_RTC0->COUNTER);
- }
- /*
- @brief 睡眠函数
- @param sleep_ms :睡眠时间,units of 125 ms
- @return 实际睡眠时间
- */
- uint32_t rtc_sleep(uint32_t sleep_125ms)
- {
- uint32_t sleep_times,wakeup_times,actual_sleeptimes;
- //关闭蓝牙扫描
- // if(!ble_conn_state_central_conn_count())//作为主机没有连接情况下才停止扫描
- // {
- // nrf_ble_scan_stop();
- //// DEBUG_LOG("scan off...\n");
- // }
-
- //暂停软件定时器
-
- // app_timer_pause();
- // nrf_delay_ms(100);
- DEBUG_LOG("timer off...\n");
-
- //关闭ADC
- // ADC_Enable(false);
- // DEBUG_LOG("adc off...\n");
-
- //设定rtc唤醒时间且开启rtc
- rtc_start(sleep_125ms);
- // DEBUG_LOG("rtc start(%d)ms\n",sleep_125ms * 125);
-
- sleep_times = NRF_RTC0->COUNTER;
-
- //进入睡眠
- CRITICAL_REGION_ENTER();
- __SEV();
- __WFE();
- CRITICAL_REGION_EXIT();
- (void)sd_app_evt_wait();
- wakeup_times = NRF_RTC0->COUNTER;
- // DEBUG_LOG("===============>rtc sleep_times(%d)\n",sleep_times);
- // DEBUG_LOG("===============>rtc wakeup_times(%d)\n",wakeup_times);
- //停止rtc
- rtc_stop();
- // DEBUG_LOG("rtc end...\n");
-
- //开启ADC
- // ADC_Enable(true);
- // DEBUG_LOG("adc on...\n");
-
- //恢复软件定时器
- // app_timer_resume();
- DEBUG_LOG("timer on...\n");
- nrf_delay_ms(100);
- actual_sleeptimes = (wakeup_times - sleep_times)/32.768*1000;
-
- return actual_sleeptimes/1000;
- }
|