#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);
        SEGGER_RTT_printf(0,"NRF_DRV_RTC_INT_COMPARE0(0x%X)\n",NRF_RTC0->COUNTER);
    }
    else if (int_type == NRF_DRV_RTC_INT_TICK)
    {
//        SEGGER_RTT_printf(0,"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);
//	SEGGER_RTT_printf(0,"nrf_drv_rtc_enable(0x%X)\n",NRF_RTC0->COUNTER);
}

void rtc_stop(void)
{
	nrf_drv_rtc_disable(&rtc);
//	SEGGER_RTT_printf(0,"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();
////		SEGGER_RTT_printf(0,"scan off...\n");
//	}
	
	//暂停软件定时器
	
//	app_timer_pause();
//	nrf_delay_ms(100);
	SEGGER_RTT_printf(0,"timer off...\n");
	
	//关闭ADC
//	ADC_Enable(false);
//	SEGGER_RTT_printf(0,"adc off...\n");
	
	//设定rtc唤醒时间且开启rtc
	rtc_start(sleep_125ms);
//	SEGGER_RTT_printf(0,"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;
//	SEGGER_RTT_printf(0,"===============>rtc sleep_times(%d)\n",sleep_times);
//	SEGGER_RTT_printf(0,"===============>rtc wakeup_times(%d)\n",wakeup_times);
	//停止rtc
	rtc_stop();
//	SEGGER_RTT_printf(0,"rtc end...\n");
	
	//开启ADC
//	ADC_Enable(true);
//	SEGGER_RTT_printf(0,"adc on...\n");
	
	//恢复软件定时器
//	app_timer_resume();
	SEGGER_RTT_printf(0,"timer on...\n");
	nrf_delay_ms(100);
	actual_sleeptimes = (wakeup_times - sleep_times)/32.768*1000;
	
	return  actual_sleeptimes/1000;
}





