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