bsp_time.c 8.3 KB

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