bsp_time.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "sdk_common.h"
  2. #include "app_timer.h"
  3. #include "bsp_time.h"
  4. #include "nrf_drv_timer.h"
  5. #include "SEGGER_RTT.h"
  6. #include "system.h"
  7. #include "app_timer.h"
  8. #include "nrf_drv_rtc.h"
  9. #include "nrf_pwr_mgmt.h"
  10. /********************** 变量区 *************************/
  11. static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);
  12. static uint8_t isSleep = 0;
  13. volatile static uint32_t time_1ms = 0;
  14. #define time_cb_max 32
  15. static TIME_callback time_cb[time_cb_max]={0};
  16. /********************** 函数声明区 *************************/
  17. static void rtc_tick_handler(nrf_drv_rtc_int_type_t int_type)
  18. {
  19. uint32_t err_code;
  20. if (int_type == NRF_DRV_RTC_INT_COMPARE0)
  21. {
  22. // SEGGER_RTT_printf(0,"1");
  23. // uint32_t tem1 = NRF_RTC0->COUNTER;
  24. nrf_drv_rtc_counter_clear(&rtc);
  25. err_code = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true); APP_ERROR_CHECK(err_code);
  26. if(isSleep==0){
  27. time_1ms+=HeartTime_Interval;
  28. for(int i=0;i<time_cb_max;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  29. if(time_cb[i]){
  30. time_cb[i]((uint32_t*)(&time_1ms)); //回调函数
  31. }
  32. }
  33. // uint32_t tem2 = NRF_RTC0->COUNTER;
  34. // if(tem2<tem1) tem2 += 16777216;
  35. // if(tem2-tem1>0) SEGGER_RTT_printf(0,"rtc_tick_handler(%d)\n",tem2-tem1);
  36. }
  37. }
  38. else if (int_type == NRF_DRV_RTC_INT_TICK)
  39. {
  40. // SEGGER_RTT_printf(0,"NRF_DRV_RTC_INT_TICK(0x%X)\n",NRF_RTC0->COUNTER);
  41. }
  42. }
  43. /********************** 外部函数声明区 *************************/
  44. //=== 系统滴答时间 ===//
  45. uint32_t TIME_GetTicks(void)
  46. {
  47. return time_1ms;
  48. }
  49. int TIME_Regist(TIME_callback cb)
  50. {
  51. for(int i=0;i<time_cb_max;i++) {
  52. if(time_cb[i]==cb) return -2;
  53. if(time_cb[i]==0){
  54. time_cb[i] = cb; //回调函数
  55. return 0;
  56. }
  57. }
  58. return -1;
  59. }
  60. int TIME_UnRegist(TIME_callback cb)
  61. {
  62. for(int i=0;i<time_cb_max;i++){
  63. if(time_cb[i] == cb){
  64. time_cb[i] = 0;
  65. return 0;
  66. }
  67. }
  68. return -1;
  69. }
  70. //int CheckPending_IRQ(int *result,int result_size)
  71. //{
  72. // int index = 0,i = 0;
  73. //
  74. // /* Check which interrupt is pending. Will not work properly if there are more than one pending */
  75. // for (i = POWER_CLOCK_IRQn; i <= FPU_IRQn; i++)
  76. // {
  77. // if (NVIC_GetPendingIRQ(i))
  78. // {
  79. // if(index < result_size)result[index++] = i;
  80. // }
  81. // }
  82. // return index;
  83. //}
  84. //static void idle_state_handle(void)
  85. //{
  86. // ret_code_t err_code;
  87. // int pending_number_before = 0,pending_number_after = 0;
  88. // int result[2][60],i;
  89. // uint32_t region_start,region_end,p_evt_id_before = 0xff,p_evt_id_after = 0xff,ispr0_before = 0,ispr0_after = 0,ispr1_before = 0,ispr1_after = 0;
  90. //// err_code = nrf_ble_lesc_request_handler();
  91. //// APP_ERROR_CHECK(err_code);
  92. //
  93. // CRITICAL_REGION_ENTER();
  94. //
  95. //
  96. // region_start = NRF_RTC2->COUNTER;
  97. // pending_number_before = CheckPending_IRQ(&result[0][0],60);
  98. // sd_evt_get(&p_evt_id_before);
  99. // ispr0_before = NVIC->ISPR[0];
  100. // ispr1_before = NVIC->ISPR[1];
  101. //
  102. // nrf_pwr_mgmt_run();
  103. //
  104. // ispr0_after = NVIC->ISPR[0];
  105. // ispr1_after = NVIC->ISPR[1];
  106. // sd_evt_get(&p_evt_id_after);
  107. // pending_number_after = CheckPending_IRQ(&result[1][0],60);
  108. // region_end = NRF_RTC2->COUNTER;
  109. //
  110. //
  111. // CRITICAL_REGION_EXIT();
  112. //
  113. // SEGGER_RTT_printf(0,"*******wakeup result start*******\n");
  114. //
  115. // if (pending_number_before)
  116. // {
  117. // for(i=0;i<pending_number_before;i++)SEGGER_RTT_printf(0,"pending_total_before[%d]:%d\n",i,result[0][i]);
  118. // memset(&result[0][0],0,60);
  119. // }
  120. //
  121. // if(p_evt_id_before != 0xff)SEGGER_RTT_printf(0,"p_evt_id_before:%d\n",p_evt_id_before);
  122. //
  123. // if (pending_number_after)
  124. // {
  125. // for(i=0;i<pending_number_after;i++)SEGGER_RTT_printf(0,"pending_total_after[%d]:%d\n",i,result[1][i]);
  126. // memset(&result[1][0],0,60);
  127. // }
  128. //
  129. // if(p_evt_id_after != 0xff)SEGGER_RTT_printf(0,"p_evt_id_after:%d\n",p_evt_id_after);
  130. //
  131. // SEGGER_RTT_printf(0,"before---NVIC->ISPR[0]:0x%X, NVIC->ISPR[1]:0x%X\n",ispr0_before,ispr1_before);
  132. // SEGGER_RTT_printf(0,"after----NVIC->ISPR[0]:0x%X, NVIC->ISPR[1]:0x%X\n",ispr0_after,ispr1_after);
  133. //
  134. // SEGGER_RTT_printf(0,"*******wakeup result end*******\n");
  135. //
  136. // SEGGER_RTT_printf (0,"region times: %d\n",region_end-region_start);
  137. //}
  138. uint32_t rtc_sleep(uint8_t is_wearshoes)
  139. {
  140. uint32_t tem1 = NRF_RTC0->COUNTER;
  141. uint32_t ret = 0;
  142. uint32_t err_code;
  143. isSleep = 1;
  144. if(is_wearshoes==0){ err_code = nrf_drv_rtc_cc_set(&rtc,0,StandByPower_Interval * 33,true); APP_ERROR_CHECK(err_code);}
  145. else { err_code = nrf_drv_rtc_cc_set(&rtc,0,LowPower_Interval * 33,true); APP_ERROR_CHECK(err_code);}
  146. nrf_drv_rtc_counter_clear(&rtc);
  147. while(nrf_drv_rtc_counter_get(&rtc) != 0);
  148. //进入睡眠
  149. for(int i=0;i<5;i++){
  150. uint32_t cnt1 = NRF_RTC0->COUNTER;
  151. nrf_pwr_mgmt_run();
  152. // idle_state_handle();
  153. uint32_t cnt2 = NRF_RTC0->COUNTER;
  154. if(cnt2<cnt1) cnt2 += 16777216;
  155. if(cnt2-cnt1>32) break;
  156. }
  157. err_code = nrf_drv_rtc_cc_set(&rtc,0,HeartTime_Interval*33,true); APP_ERROR_CHECK(err_code);
  158. nrf_drv_rtc_counter_clear(&rtc);
  159. while(nrf_drv_rtc_counter_get(&rtc) != 0);
  160. isSleep = 0;
  161. uint32_t tem2 = NRF_RTC0->COUNTER;
  162. if(tem2<tem1) tem2 += 16777216;
  163. ret = (tem2-tem1)/32.768;
  164. return ret;
  165. }
  166. static void cb_timeeWakeup(uint32_t t)
  167. {
  168. time_1ms += t;
  169. //SEGGER_RTT_printf(0,"cb_timeeWakeup(%d)\n",time_1ms);
  170. }
  171. //=== 系统滴答时间初始化 ===//
  172. void TIME_Init(void)
  173. {
  174. uint32_t err_code;
  175. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  176. config.interrupt_priority = RTC2_IRQ_PRIORITY;
  177. config.prescaler = 0;// f(RTC) = 32.768kHZ/(prescaler+1) = 8HZ = 125ms
  178. err_code = nrf_drv_rtc_init(&rtc, &config, rtc_tick_handler); APP_ERROR_CHECK(err_code);
  179. err_code = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true); APP_ERROR_CHECK(err_code);
  180. nrf_drv_rtc_counter_clear(&rtc);
  181. nrf_drv_rtc_enable(&rtc);
  182. Wakeup_Regist(cb_timeeWakeup);
  183. }