bsp_time.c 6.5 KB

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