123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- #include "sdk_common.h"
- #include "app_timer.h"
- #include "bsp_time.h"
- #include "nrf_drv_timer.h"
- #include "SEGGER_RTT.h"
- #include "system.h"
- #include "app_timer.h"
- #include "nrf_drv_rtc.h"
- #include "nrf_pwr_mgmt.h"
- #include "exception.h"
- static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);
- static uint8_t isSleep = 0;
- volatile static uint32_t time_1ms = 0;
- #define time_cb_max 10
- static TIME_callback time_cb[time_cb_max]={0};
- static void bsp_rtc_init_process(void)
- {
- if(Except_TxError(EXCEPT_RTC_INIT,"bsp_rtc_init_error\r\n") == 0)
- {
- Process_Stop(bsp_rtc_init_process);
- }
- }
- static void bsp_rtc_config_process(void)
- {
- if(Except_TxError(EXCEPT_RTC_INIT,"bsp_rtc_config_error\r\n") == 0)
- {
- Process_Stop(bsp_rtc_config_process);
- }
- }
- static void rtc_tick_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,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
-
- if(err_code != NRF_SUCCESS)
- {
-
- err_code = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
- if(err_code != NRF_SUCCESS)
- {
- Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
- }
- }
- if(isSleep==0){
- time_1ms+=HeartTime_Interval;
- for(int i=0;i<time_cb_max;i++) {
- if(time_cb[i]){
- time_cb[i]((uint32_t*)(&time_1ms));
- }
- }
- }
- }
- else if (int_type == NRF_DRV_RTC_INT_TICK)
- {
- }
- }
- uint32_t TIME_GetTicks(void)
- {
- return time_1ms;
- }
- int TIME_Regist(TIME_callback cb)
- {
- for(uint8_t i=0;i<time_cb_max;i++) {
- if(time_cb[i]==cb) return -2;
- if(time_cb[i]==0){
- time_cb[i] = cb;
- return 0;
- }
- }
- return -1;
- }
- int TIME_UnRegist(TIME_callback cb)
- {
- uint8_t b =0;
- for(uint8_t i=0;i<time_cb_max;i++){
- if(time_cb[i] == cb){
- for(b=i ; b < (time_cb_max-1);b++){
- time_cb[b] = time_cb[b+1];
- }
- return 0;
- }
- }
- return -1;
- }
- uint32_t rtc_sleep(uint8_t is_wearshoes)
- {
- uint32_t tem1 = NRF_RTC0->COUNTER;
- uint32_t ret = 0;
- uint32_t err_code;
- isSleep = 1;
- if(is_wearshoes==0)
- {
- err_code = nrf_drv_rtc_cc_set(&rtc,0,StandByPower_Interval * 33,true);
- if(err_code != NRF_SUCCESS)
- {
-
- err_code = nrf_drv_rtc_cc_set(&rtc,0,StandByPower_Interval * 33,true);
- if(err_code != NRF_SUCCESS)
- {
- Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
- }
- }
- }
- else
- {
- err_code = nrf_drv_rtc_cc_set(&rtc,0,LowPower_Interval * 33,true);
- if(err_code != NRF_SUCCESS)
- {
-
- err_code = nrf_drv_rtc_cc_set(&rtc,0,LowPower_Interval * 33,true);
- if(err_code != NRF_SUCCESS)
- {
- Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
- }
- }
- }
- nrf_drv_rtc_counter_clear(&rtc);
- while(nrf_drv_rtc_counter_get(&rtc) != 0);
-
- for(int i=0;i<5;i++){
- uint32_t cnt1 = NRF_RTC0->COUNTER;
- nrf_pwr_mgmt_run();
- uint32_t cnt2 = NRF_RTC0->COUNTER;
- if(cnt2<cnt1) cnt2 += 16777216;
- if(cnt2-cnt1>32) break;
- }
-
- err_code = nrf_drv_rtc_cc_set(&rtc,0,HeartTime_Interval*33,true);
- if(err_code != NRF_SUCCESS)
- {
-
- err_code = nrf_drv_rtc_cc_set(&rtc,0,HeartTime_Interval*33,true);
- if(err_code != NRF_SUCCESS)
- {
- Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
- }
- }
- nrf_drv_rtc_counter_clear(&rtc);
- while(nrf_drv_rtc_counter_get(&rtc) != 0);
- isSleep = 0;
- uint32_t tem2 = NRF_RTC0->COUNTER;
- if(tem2<tem1) tem2 += 16777216;
- ret = (tem2-tem1)/32.768;
- return ret;
- }
- static void cb_timeeWakeup(uint32_t t)
- {
- time_1ms += t;
-
- }
- void TIME_Init(void)
- {
- int ret = 0;
- ret_code_t errCode;
-
- nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
- config.interrupt_priority = RTC2_IRQ_PRIORITY;
- config.prescaler = 0;
- errCode = nrf_drv_rtc_init(&rtc, &config, rtc_tick_handler);
- if(errCode != NRF_SUCCESS)ret = -1;
-
- errCode = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
- if(errCode != NRF_SUCCESS)
- {
-
- errCode = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
- if(errCode != NRF_SUCCESS)
- {
- Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
- }
- }
- nrf_drv_rtc_counter_clear(&rtc);
- nrf_drv_rtc_enable(&rtc);
- Wakeup_Regist(cb_timeeWakeup);
-
- if(ret == -1)
- {
- Process_Start(0,"bsp_rtc_init_process",bsp_rtc_init_process);
- }
- }
|