tick.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "sdk_common.h"
  2. #include "app_timer.h"
  3. #include "tick.h"
  4. #include "nrf_drv_timer.h"
  5. #include "nrf_drv_rtc.h"
  6. #include "nrf_pwr_mgmt.h"
  7. /********************** 变量区 *************************/
  8. static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);
  9. static uint8_t isSleep = 0;
  10. volatile static uint32_t time_1ms = 0;
  11. #define time_cb_max 32
  12. static Tick_callback time_cb[time_cb_max]={0};
  13. /********************** 函数声明区 *************************/
  14. static void rtc_tick_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. // SEGGER_RTT_printf(0,"1");
  20. // uint32_t tem1 = NRF_RTC0->COUNTER;
  21. nrf_drv_rtc_counter_clear(&rtc);
  22. err_code = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true); APP_ERROR_CHECK(err_code);
  23. if(isSleep==0){
  24. time_1ms+=HeartTime_Interval;
  25. for(int i=0;i<time_cb_max;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  26. if(time_cb[i]){
  27. time_cb[i]((uint32_t*)(&time_1ms)); //回调函数
  28. }
  29. }
  30. // uint32_t tem2 = NRF_RTC0->COUNTER;
  31. // if(tem2<tem1) tem2 += 16777216;
  32. // if(tem2-tem1>0) SEGGER_RTT_printf(0,"rtc_tick_handler(%d)\n",tem2-tem1);
  33. }
  34. }
  35. else if (int_type == NRF_DRV_RTC_INT_TICK)
  36. {
  37. // SEGGER_RTT_printf(0,"NRF_DRV_RTC_INT_TICK(0x%X)\n",NRF_RTC0->COUNTER);
  38. }
  39. }
  40. /********************** 外部函数声明区 *************************/
  41. //=== 系统滴答时间 ===//
  42. uint32_t Tick_GetTicks(void)
  43. {
  44. return time_1ms;
  45. }
  46. int Tick_Regist(Tick_callback cb)
  47. {
  48. for(int i=0;i<time_cb_max;i++) {
  49. if(time_cb[i]==cb) return -2;
  50. if(time_cb[i]==0){
  51. time_cb[i] = cb; //回调函数
  52. return 0;
  53. }
  54. }
  55. return -1;
  56. }
  57. int Tick_UnRegist(Tick_callback cb)
  58. {
  59. for(int i=0;i<time_cb_max;i++){
  60. if(time_cb[i] == cb){
  61. time_cb[i] = 0;
  62. return 0;
  63. }
  64. }
  65. return -1;
  66. }
  67. uint32_t rtc_sleep(uint8_t is_wearshoes)
  68. {
  69. uint32_t tem1 = NRF_RTC0->COUNTER;
  70. uint32_t ret = 0;
  71. uint32_t err_code;
  72. isSleep = 1;
  73. if(is_wearshoes==0){ err_code = nrf_drv_rtc_cc_set(&rtc,0,StandByPower_Interval * 33,true); APP_ERROR_CHECK(err_code);}
  74. else { err_code = nrf_drv_rtc_cc_set(&rtc,0,LowPower_Interval * 33,true); APP_ERROR_CHECK(err_code);}
  75. nrf_drv_rtc_counter_clear(&rtc);
  76. while(nrf_drv_rtc_counter_get(&rtc) != 0);
  77. //进入睡眠
  78. for(int i=0;i<5;i++){
  79. uint32_t cnt1 = NRF_RTC0->COUNTER;
  80. nrf_pwr_mgmt_run();
  81. // idle_state_handle();
  82. uint32_t cnt2 = NRF_RTC0->COUNTER;
  83. if(cnt2<cnt1) cnt2 += 16777216;
  84. if(cnt2-cnt1>32) break;
  85. }
  86. err_code = nrf_drv_rtc_cc_set(&rtc,0,HeartTime_Interval*33,true); APP_ERROR_CHECK(err_code);
  87. nrf_drv_rtc_counter_clear(&rtc);
  88. while(nrf_drv_rtc_counter_get(&rtc) != 0);
  89. isSleep = 0;
  90. uint32_t tem2 = NRF_RTC0->COUNTER;
  91. if(tem2<tem1) tem2 += 16777216;
  92. ret = (tem2-tem1)/32.768;
  93. return ret;
  94. }
  95. static void cb_timeeWakeup(uint32_t t)
  96. {
  97. time_1ms += t;
  98. //SEGGER_RTT_printf(0,"cb_timeeWakeup(%d)\n",time_1ms);
  99. }
  100. //=== 系统滴答时间初始化 ===//
  101. void Tick_Init(void)
  102. {
  103. uint32_t err_code;
  104. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  105. config.interrupt_priority = RTC2_IRQ_PRIORITY;
  106. config.prescaler = 0;// f(RTC) = 32.768kHZ/(prescaler+1) = 8HZ = 125ms
  107. err_code = nrf_drv_rtc_init(&rtc, &config, rtc_tick_handler); APP_ERROR_CHECK(err_code);
  108. err_code = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true); APP_ERROR_CHECK(err_code);
  109. nrf_drv_rtc_counter_clear(&rtc);
  110. nrf_drv_rtc_enable(&rtc);
  111. // Wakeup_Regist(cb_timeeWakeup);
  112. }