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)
- static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);
- static uint16_t m_sleeptimes_S = 0;
- 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)
- {
- }
- }
- void RTC_init(void)
- {
- uint32_t err_code;
-
- nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
- config.prescaler = 0;
- 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);
- nrf_drv_rtc_enable(&rtc);
- }
- void rtc_start(uint16_t sleep_S)
- {
- uint32_t err_code;
-
- m_sleeptimes_S = sleep_S;
-
- 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);
- }
- void rtc_stop(void)
- {
- nrf_drv_rtc_disable(&rtc);
- }
- uint32_t rtc_sleep(uint32_t sleep_125ms)
- {
- uint32_t sleep_times,wakeup_times,actual_sleeptimes;
-
-
-
-
- DEBUG_LOG("timer off...\n");
-
-
-
-
- rtc_start(sleep_125ms);
-
- sleep_times = NRF_RTC0->COUNTER;
-
-
- CRITICAL_REGION_ENTER();
- __SEV();
- __WFE();
- CRITICAL_REGION_EXIT();
- (void)sd_app_evt_wait();
- wakeup_times = NRF_RTC0->COUNTER;
-
- rtc_stop();
-
-
-
-
- DEBUG_LOG("timer on...\n");
- nrf_delay_ms(100);
- actual_sleeptimes = (wakeup_times - sleep_times)/32.768*1000;
-
- return actual_sleeptimes/1000;
- }
|