system - 副本 (2).c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. ////后台进程管理
  100. //#define process_cb_max 255
  101. //static uint8_t process_num = 0;
  102. //static PROCESS_cb process_cb[process_cb_max]={0};
  103. //int Process_Regist(PROCESS_cb cb)
  104. //{
  105. // for(int i=0;i<process_cb_max;i++) {
  106. // if(process_cb[i]==cb) return -1;
  107. // if(process_cb[i]==0){
  108. // process_num++;
  109. // process_cb[i] = cb; //回调函数
  110. // return 0;
  111. // }
  112. // }
  113. // SEGGER_RTT_printf(0,"Process_Regist too many!\n");
  114. // return -2;
  115. //}
  116. //void Process_Background(void)
  117. //{
  118. // for(int i=0;i<process_num;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  119. // if(process_cb[i]){
  120. // process_cb[i](); //回调函数
  121. // }
  122. // }
  123. //}
  124. //app进程管理
  125. static uint16_t process_dex = 0;
  126. static PROCESS_t mProcess[process_max];
  127. static PROCESS_t* head_handle = 0;
  128. int Process_Start(uint32_t peroid,const char *name,PROCESS_cb cb)
  129. {
  130. for(int i=0;i<process_dex;i++){ //检查是否存在过
  131. if(mProcess[i].cb==cb){
  132. mProcess[i].next = head_handle;
  133. mProcess[i].runflag =1;
  134. head_handle = &mProcess[i];
  135. SEGGER_RTT_printf(0,"process was existed(%d)\n",cb);
  136. return 0;
  137. }
  138. }
  139. PROCESS_t* target = head_handle;
  140. while(target){ //检查是否已经存在
  141. if(target->cb==cb) return -1;
  142. target = target->next ;
  143. }
  144. if(process_dex>=process_max) return -1;
  145. mProcess[process_dex].cb = cb;
  146. mProcess[process_dex].runflag =1;
  147. mProcess[process_dex].next = head_handle;
  148. mProcess[process_dex].Peroid = peroid;
  149. mProcess[process_dex].tim = TIME_GetTicks();
  150. #if ProcessTime_EN
  151. sprintf(mProcess[process_dex].name_t,"%s",name);
  152. #endif
  153. head_handle = &mProcess[process_dex];
  154. process_dex++;
  155. // SEGGER_RTT_printf(0,"process num(%d)\n",process_dex);
  156. return 0;
  157. }
  158. void Process_Stop(PROCESS_cb cb)
  159. {
  160. PROCESS_t* target = head_handle;
  161. while(target){ //检查是否已经存在
  162. if(target->cb==cb){ //存在
  163. target->Peroid = 0;
  164. target->runflag = 0;
  165. return;
  166. }
  167. target = target->next ;
  168. }
  169. }
  170. void Process_SetHoldOn(PROCESS_cb cb,uint8_t holdon)
  171. {
  172. PROCESS_t* target = head_handle;
  173. while(target){ //检查是否已经存在
  174. if(target->cb==cb){ //存在
  175. if(target->holdon!=holdon) target->holdon = holdon;
  176. return;
  177. }
  178. target = target->next ;
  179. }
  180. }
  181. void Process_UpdatePeroid(PROCESS_cb cb,uint16_t Peroid)
  182. {
  183. PROCESS_t* target = head_handle;
  184. while(target){ //检查是否已经存在
  185. if(target->cb==cb){ //存在
  186. target->Peroid = Peroid;
  187. target->tim = TIME_GetTicks();
  188. return;
  189. }
  190. target = target->next ;
  191. }
  192. }
  193. uint16_t Process_GetPeroid(PROCESS_cb cb)
  194. {
  195. PROCESS_t* target = head_handle;
  196. while(target){ //检查是否已经存在
  197. if(target->cb==cb){ //存在
  198. return target->Peroid;
  199. }
  200. target = target->next ;
  201. }
  202. return 0;
  203. }
  204. void Process_SetError(PROCESS_cb cb,uint8_t error)
  205. {
  206. PROCESS_t* target = head_handle;
  207. while(target){ //检查是否已经存在
  208. if(target->cb==cb){ //存在
  209. target->error = error;
  210. }
  211. target = target->next ;
  212. }
  213. }
  214. int Process_App(void)
  215. {
  216. PROCESS_t* target;
  217. int ret = 0;
  218. for(target=head_handle;target;target=target->next){
  219. if(target->cb && target->runflag) {
  220. #if ProcessTime_EN
  221. target->cnt1_rtc = NRF_RTC0->COUNTER;
  222. #endif
  223. if(0 == target-> Peroid)target->cb();
  224. else if(TIME_GetTicks()-target->tim >= target-> Peroid ){
  225. target->tim = TIME_GetTicks();
  226. target->cb();
  227. }
  228. #if ProcessTime_EN
  229. target->cnt2_rtc = NRF_RTC0->COUNTER;
  230. #endif
  231. }
  232. if(target->holdon) ret = 1; //不能进入低功耗
  233. }
  234. #if ProcessTime_EN
  235. char Display[100]={0};
  236. static uint32_t tim =0;
  237. if(TIME_GetTicks()-tim>=DisInterval){ tim = TIME_GetTicks();
  238. for(target=head_handle;target;target=target->next){
  239. if(target->cb && target->cnt2_rtc >= (target->cnt1_rtc +2)) {
  240. sprintf(Display,"%s, time:%02f us\n",target->name_t,(target->cnt2_rtc-target->cnt1_rtc)/32.768 * 1000);
  241. SEGGER_RTT_printf(0,"%s",Display);
  242. memset(Display,0,sizeof(Display));
  243. }
  244. }
  245. }
  246. #endif
  247. return ret;
  248. }
  249. //大循环
  250. void USR_Process(void)
  251. {
  252. //app进程调度
  253. // static uint32_t time_t =0;
  254. if(Process_App()==0){ //进入低功耗模式
  255. // SEGGER_RTT_printf(0,"Sleep(%d)...\n",TIME_GetTicks()-time_t);
  256. // SEGGER_RTT_printf(0,"process_dex:%d,sleep_cb_max:%d,wakeup_cb_max:%d...\n",process_dex,sleep_num,wakeup_num);
  257. Sleep_Event();
  258. // nrf_delay_ms(systermSleepTime); //模拟低功耗
  259. systermSleepTime = rtc_sleep();
  260. if(systermSleepTime==0){
  261. systermSleepTime = 13;
  262. nrf_delay_ms(systermSleepTime);
  263. }
  264. //time_t = TIME_GetTicks();
  265. Wakeup_Event();
  266. //SEGGER_RTT_printf(0,"Wakeup(%d)...\n",systermSleepTime);
  267. }
  268. FPS_process();
  269. }