system.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "system.h"
  2. #include "bsp_time.h"
  3. #include "nrf_delay.h"
  4. #include "hal_imu.h"
  5. #include "hal_ble_client.h"
  6. #include "hal_ble_host.h"
  7. //系统函数
  8. void FPS_process(void)
  9. {
  10. #if DEBUG_FPS
  11. static uint32_t tem1 = 0;
  12. static uint32_t tem2 = 0;
  13. static uint32_t fps_max=0;
  14. static uint32_t cnt_max=0;
  15. uint32_t fps;
  16. tem2 = NRF_RTC0->COUNTER;
  17. if(tem2<tem1) tem2 += 16777216;
  18. fps = (tem2-tem1)/32.768;
  19. if(fps_max<fps){
  20. fps_max = fps;
  21. cnt_max = tem2-tem1;
  22. }
  23. static uint32_t tim=0;
  24. if(TIME_GetTicks()-tim>=1000){ tim = TIME_GetTicks();
  25. SEGGER_RTT_printf(0,"fps=%dms,cnt=%d\n",fps_max,cnt_max);
  26. fps_max = 0;
  27. }
  28. tem1 = NRF_RTC0->COUNTER;
  29. #endif
  30. }
  31. uint16_t lose_pack_all =0;
  32. #if LOSSPACK_ENANBLE
  33. void LossPack_process(void){
  34. static uint16_t pack_l =0;
  35. pack_l = lose_pack_all;
  36. // SEGGER_RTT_printf(0,">>>>>loss pack,%d:%d\n",pack_l,rssi);
  37. uint8_t buf[8];
  38. uint8_t L = 0;
  39. buf[L++] = (uint8_t)(pack_l>>8);
  40. buf[L++] = (uint8_t)(pack_l);
  41. buf[L++] = (uint8_t)(IMU_GetHostRssi());
  42. BLE_Client_Tx_Send(0,0x03,buf,L);
  43. static uint8_t temp =0;
  44. temp++;
  45. if(temp >= 100){
  46. lose_pack_all =0;
  47. temp =0;
  48. }
  49. }
  50. #endif
  51. //休眠时间 ms
  52. uint32_t systermSleepTime = 1000;
  53. static uint8_t sleep_num = 0;
  54. static Sleep_cb sleep_cb[sleep_cb_max]={0};
  55. int Sleep_Regist(Sleep_cb cb)
  56. {
  57. for(int i=0;i<sleep_cb_max;i++) {
  58. if(sleep_cb[i]==cb) return -1;
  59. if(sleep_cb[i]==0){
  60. sleep_num++;
  61. sleep_cb[i] = cb; //回调函数
  62. return 0;
  63. }
  64. }
  65. SEGGER_RTT_printf(0,"Sleep_Regist too many!\n");
  66. return -2;
  67. }
  68. void Sleep_Event(void)
  69. {
  70. for(int i=0;i<sleep_num;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  71. if(sleep_cb[i]){
  72. sleep_cb[i](systermSleepTime); //回调函数
  73. }
  74. }
  75. }
  76. static uint8_t wakeup_num = 0;
  77. static Sleep_cb wakeup_cb[wakeup_cb_max]={0};
  78. int Wakeup_Regist(Sleep_cb cb)
  79. {
  80. for(int i=0;i<wakeup_cb_max;i++) {
  81. if(wakeup_cb[i]==cb) return -1;
  82. if(wakeup_cb[i]==0){
  83. wakeup_num++;
  84. wakeup_cb[i] = cb; //回调函数
  85. return 0;
  86. }
  87. }
  88. // SEGGER_RTT_printf(0,"Wakeup_Regist too many!\n");
  89. return -2;
  90. }
  91. void Wakeup_Event(void)
  92. {
  93. for(int i=0;i<wakeup_num;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  94. if(wakeup_cb[i]){
  95. wakeup_cb[i](systermSleepTime); //回调函数
  96. }
  97. }
  98. }
  99. static uint16_t process_dex = 0;
  100. static PROCESS_t mProcess[process_max];
  101. int Process_Start(uint32_t peroid,const char *name,PROCESS_cb cb)
  102. {
  103. for(uint8_t i=0;i<process_dex;i++){
  104. if(mProcess[i].cb==cb){ //已经存在
  105. mProcess[i].enable =1;
  106. mProcess[i].Peroid = peroid;
  107. mProcess[i].tim = TIME_GetTicks();
  108. return 1;
  109. }
  110. }
  111. if(process_dex>=process_max) return -1;
  112. mProcess[process_dex].cb = cb;
  113. mProcess[process_dex].enable =1;
  114. mProcess[process_dex].Peroid = peroid;
  115. mProcess[process_dex].tim = TIME_GetTicks();
  116. #if ProcessTime_EN
  117. sprintf(mProcess[process_dex].name_t,"%s",name);
  118. #endif
  119. process_dex++;
  120. // SEGGER_RTT_printf(0,"process num(%d)\n",process_dex);
  121. return 0;
  122. }
  123. void Process_Stop(PROCESS_cb cb)
  124. {
  125. for(uint8_t i=0;i<process_dex;i++){
  126. if(mProcess[i].cb==cb){ //找到
  127. mProcess[i].enable = 0;
  128. return;
  129. }
  130. }
  131. }
  132. void Process_SetHoldOn(PROCESS_cb cb,uint8_t holdon)
  133. {
  134. for(uint8_t i=0;i<process_dex;i++){
  135. if(mProcess[i].cb==cb){ //找到
  136. if(mProcess[i].holdon!=holdon) mProcess[i].holdon = (holdon&0x01);
  137. return;
  138. }
  139. }
  140. }
  141. void Process_UpdatePeroid(PROCESS_cb cb,uint16_t Peroid)
  142. {
  143. for(uint8_t i=0;i<process_dex;i++){
  144. if(mProcess[i].cb==cb){ //找到
  145. mProcess[i].Peroid = Peroid;
  146. return;
  147. }
  148. }
  149. }
  150. uint16_t Process_GetPeroid(PROCESS_cb cb)
  151. {
  152. for(uint8_t i=0;i<process_dex;i++){
  153. if(mProcess[i].cb==cb){ //找到
  154. return mProcess[i].Peroid;
  155. }
  156. }
  157. return 0;
  158. }
  159. int Process_App(void)
  160. {
  161. int ret = 0;
  162. uint8_t i = 0;
  163. for(i=0;i<process_dex;i++){
  164. if(mProcess[i].enable==0) continue;
  165. if(mProcess[i].cb){
  166. #if ProcessTime_EN
  167. target->cnt1_rtc = NRF_RTC0->COUNTER;
  168. #endif
  169. if(mProcess[i].Peroid ==0) mProcess[i].cb();
  170. else if(TIME_GetTicks()-mProcess[i].tim >= mProcess[i].Peroid ){
  171. mProcess[i].tim = TIME_GetTicks();
  172. mProcess[i].cb();
  173. }
  174. #if ProcessTime_EN
  175. target->cnt2_rtc = NRF_RTC0->COUNTER;
  176. #endif
  177. }
  178. if(mProcess[i].holdon) ret = 1; //不能进入低功耗
  179. }
  180. #if ProcessTime_EN
  181. char Display[100]={0};
  182. static uint32_t tim =0;
  183. if(TIME_GetTicks()-tim>=DisInterval){ tim = TIME_GetTicks();
  184. for(target=head_handle;target;target=target->next){
  185. if(target->cb && target->cnt2_rtc >= (target->cnt1_rtc +2)) {
  186. sprintf(Display,"%s, time:%02f us\n",target->name_t,(target->cnt2_rtc-target->cnt1_rtc)/32.768 * 1000);
  187. SEGGER_RTT_printf(0,"%s",Display);
  188. memset(Display,0,sizeof(Display));
  189. }
  190. }
  191. }
  192. #endif
  193. return ret;
  194. }
  195. //大循环
  196. void USR_Process(void)
  197. {
  198. //app进程调度
  199. if(Process_App()==0){ //进入低功耗模式
  200. Sleep_Event();
  201. systermSleepTime = rtc_sleep();
  202. if(systermSleepTime==0){
  203. systermSleepTime = 13;
  204. nrf_delay_ms(systermSleepTime);
  205. }
  206. Wakeup_Event();
  207. }
  208. FPS_process();
  209. }