bsp_time.c 8.1 KB

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