bsp_time.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_ser_imu_mode_manage.h"
  11. #include "exception.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. #define time_cb_max 32
  17. static TIME_callback time_cb[time_cb_max]={0};
  18. /********************** 函数声明区 *************************/
  19. static void bsp_rtc_init_process(void)
  20. {
  21. if(Except_TxError(EXCEPT_RTC_INIT,"bsp_rtc_init_error\r\n") == 0)
  22. {
  23. Process_Stop(bsp_rtc_init_process);
  24. }
  25. }
  26. static void bsp_rtc_config_process(void)
  27. {
  28. if(Except_TxError(EXCEPT_RTC_INIT,"bsp_rtc_config_error\r\n") == 0)
  29. {
  30. Process_Stop(bsp_rtc_config_process);
  31. }
  32. }
  33. static void rtc_tick_handler(nrf_drv_rtc_int_type_t int_type)
  34. {
  35. uint32_t err_code;
  36. if (int_type == NRF_DRV_RTC_INT_COMPARE0)
  37. {
  38. // SEGGER_RTT_printf(0,"1");
  39. // uint32_t tem1 = NRF_RTC0->COUNTER;
  40. nrf_drv_rtc_counter_clear(&rtc);
  41. err_code = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
  42. if(err_code != NRF_SUCCESS)
  43. {
  44. //再来一次还是不行,就上报错误。
  45. err_code = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
  46. if(err_code != NRF_SUCCESS)
  47. {
  48. Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
  49. }
  50. }
  51. if(isSleep==0){
  52. time_1ms+=HeartTime_Interval;
  53. for(int i=0;i<time_cb_max;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  54. if(time_cb[i]){
  55. time_cb[i]((uint32_t*)(&time_1ms)); //回调函数
  56. }
  57. }
  58. // uint32_t tem2 = NRF_RTC0->COUNTER;
  59. // if(tem2<tem1) tem2 += 16777216;
  60. // if(tem2-tem1>0) SEGGER_RTT_printf(0,"rtc_tick_handler(%d)\n",tem2-tem1);
  61. }
  62. }
  63. else if (int_type == NRF_DRV_RTC_INT_TICK)
  64. {
  65. // SEGGER_RTT_printf(0,"NRF_DRV_RTC_INT_TICK(0x%X)\n",NRF_RTC0->COUNTER);
  66. }
  67. }
  68. /********************** 外部函数声明区 *************************/
  69. //=== 系统滴答时间 ===//
  70. uint32_t TIME_GetTicks(void)
  71. {
  72. return time_1ms;
  73. }
  74. int TIME_Regist(TIME_callback cb)
  75. {
  76. for(int i=0;i<time_cb_max;i++) {
  77. if(time_cb[i]==cb) return -2;
  78. if(time_cb[i]==0){
  79. time_cb[i] = cb; //回调函数
  80. return 0;
  81. }
  82. }
  83. return -1;
  84. }
  85. int TIME_UnRegist(TIME_callback cb)
  86. {
  87. for(int i=0;i<time_cb_max;i++){
  88. if(time_cb[i] == cb){
  89. time_cb[i] = 0;
  90. return 0;
  91. }
  92. }
  93. return -1;
  94. }
  95. //int CheckPending_IRQ(int *result,int result_size)
  96. //{
  97. // int index = 0,i = 0;
  98. //
  99. // /* Check which interrupt is pending. Will not work properly if there are more than one pending */
  100. // for (i = POWER_CLOCK_IRQn; i <= FPU_IRQn; i++)
  101. // {
  102. // if (NVIC_GetPendingIRQ(i))
  103. // {
  104. // if(index < result_size)result[index++] = i;
  105. // }
  106. // }
  107. // return index;
  108. //}
  109. //static void idle_state_handle(void)
  110. //{
  111. // ret_code_t err_code;
  112. // int pending_number_before = 0,pending_number_after = 0;
  113. // int result[2][60],i;
  114. // 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;
  115. //// err_code = nrf_ble_lesc_request_handler();
  116. //// APP_ERROR_CHECK(err_code);
  117. //
  118. // CRITICAL_REGION_ENTER();
  119. //
  120. //
  121. // region_start = NRF_RTC2->COUNTER;
  122. // pending_number_before = CheckPending_IRQ(&result[0][0],60);
  123. // sd_evt_get(&p_evt_id_before);
  124. // ispr0_before = NVIC->ISPR[0];
  125. // ispr1_before = NVIC->ISPR[1];
  126. //
  127. // nrf_pwr_mgmt_run();
  128. //
  129. // ispr0_after = NVIC->ISPR[0];
  130. // ispr1_after = NVIC->ISPR[1];
  131. // sd_evt_get(&p_evt_id_after);
  132. // pending_number_after = CheckPending_IRQ(&result[1][0],60);
  133. // region_end = NRF_RTC2->COUNTER;
  134. //
  135. //
  136. // CRITICAL_REGION_EXIT();
  137. //
  138. // SEGGER_RTT_printf(0,"*******wakeup result start*******\n");
  139. //
  140. // if (pending_number_before)
  141. // {
  142. // for(i=0;i<pending_number_before;i++)SEGGER_RTT_printf(0,"pending_total_before[%d]:%d\n",i,result[0][i]);
  143. // memset(&result[0][0],0,60);
  144. // }
  145. //
  146. // if(p_evt_id_before != 0xff)SEGGER_RTT_printf(0,"p_evt_id_before:%d\n",p_evt_id_before);
  147. //
  148. // if (pending_number_after)
  149. // {
  150. // for(i=0;i<pending_number_after;i++)SEGGER_RTT_printf(0,"pending_total_after[%d]:%d\n",i,result[1][i]);
  151. // memset(&result[1][0],0,60);
  152. // }
  153. //
  154. // if(p_evt_id_after != 0xff)SEGGER_RTT_printf(0,"p_evt_id_after:%d\n",p_evt_id_after);
  155. //
  156. // SEGGER_RTT_printf(0,"before---NVIC->ISPR[0]:0x%X, NVIC->ISPR[1]:0x%X\n",ispr0_before,ispr1_before);
  157. // SEGGER_RTT_printf(0,"after----NVIC->ISPR[0]:0x%X, NVIC->ISPR[1]:0x%X\n",ispr0_after,ispr1_after);
  158. //
  159. // SEGGER_RTT_printf(0,"*******wakeup result end*******\n");
  160. //
  161. // SEGGER_RTT_printf (0,"region times: %d\n",region_end-region_start);
  162. //}
  163. uint32_t rtc_sleep(uint8_t is_wearshoes)
  164. {
  165. uint32_t tem1 = NRF_RTC0->COUNTER;
  166. uint32_t ret = 0;
  167. uint32_t err_code;
  168. isSleep = 1;
  169. if(is_wearshoes==0)
  170. {
  171. err_code = nrf_drv_rtc_cc_set(&rtc,0,StandByPower_Interval * 33,true);
  172. if(err_code != NRF_SUCCESS)
  173. {
  174. //再来一次还是不行,就上报错误。
  175. err_code = nrf_drv_rtc_cc_set(&rtc,0,StandByPower_Interval * 33,true);
  176. if(err_code != NRF_SUCCESS)
  177. {
  178. Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
  179. }
  180. }
  181. }
  182. else
  183. {
  184. err_code = nrf_drv_rtc_cc_set(&rtc,0,LowPower_Interval * 33,true);
  185. if(err_code != NRF_SUCCESS)
  186. {
  187. //再来一次还是不行,就上报错误。
  188. err_code = nrf_drv_rtc_cc_set(&rtc,0,LowPower_Interval * 33,true);
  189. if(err_code != NRF_SUCCESS)
  190. {
  191. Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
  192. }
  193. }
  194. }
  195. nrf_drv_rtc_counter_clear(&rtc);
  196. while(nrf_drv_rtc_counter_get(&rtc) != 0);
  197. //进入睡眠
  198. for(int i=0;i<5;i++){
  199. uint32_t cnt1 = NRF_RTC0->COUNTER;
  200. nrf_pwr_mgmt_run();
  201. // idle_state_handle();
  202. uint32_t cnt2 = NRF_RTC0->COUNTER;
  203. if(cnt2<cnt1) cnt2 += 16777216;
  204. if(cnt2-cnt1>32) break;
  205. }
  206. err_code = nrf_drv_rtc_cc_set(&rtc,0,HeartTime_Interval*33,true);
  207. if(err_code != NRF_SUCCESS)
  208. {
  209. //再来一次还是不行,就上报错误。
  210. err_code = nrf_drv_rtc_cc_set(&rtc,0,HeartTime_Interval*33,true);
  211. if(err_code != NRF_SUCCESS)
  212. {
  213. Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
  214. }
  215. }
  216. nrf_drv_rtc_counter_clear(&rtc);
  217. while(nrf_drv_rtc_counter_get(&rtc) != 0);
  218. isSleep = 0;
  219. uint32_t tem2 = NRF_RTC0->COUNTER;
  220. if(tem2<tem1) tem2 += 16777216;
  221. ret = (tem2-tem1)/32.768;
  222. return ret;
  223. }
  224. static void cb_timeeWakeup(uint32_t t)
  225. {
  226. time_1ms += t;
  227. //SEGGER_RTT_printf(0,"cb_timeeWakeup(%d)\n",time_1ms);
  228. }
  229. //=== 系统滴答时间初始化 ===//
  230. void TIME_Init(void)
  231. {
  232. int ret = 0;
  233. ret_code_t errCode;
  234. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  235. config.interrupt_priority = RTC2_IRQ_PRIORITY;
  236. config.prescaler = 0;// f(RTC) = 32.768kHZ/(prescaler+1) = 8HZ = 125ms
  237. errCode = nrf_drv_rtc_init(&rtc, &config, rtc_tick_handler);
  238. if(errCode != NRF_SUCCESS)ret = -1;
  239. errCode = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
  240. if(errCode != NRF_SUCCESS)
  241. {
  242. //再来一次还是不行,就上报错误。
  243. errCode = nrf_drv_rtc_cc_set(&rtc,NRF_DRV_RTC_INT_COMPARE0,HeartTime_Interval*33,true);
  244. if(errCode != NRF_SUCCESS)
  245. {
  246. Process_Start(0,"bsp_rtc_config_process",bsp_rtc_config_process);
  247. }
  248. }
  249. nrf_drv_rtc_counter_clear(&rtc);
  250. nrf_drv_rtc_enable(&rtc);
  251. Wakeup_Regist(cb_timeeWakeup);
  252. if(ret == -1)
  253. {
  254. Process_Start(0,"bsp_rtc_init_process",bsp_rtc_init_process);
  255. }
  256. }