system.c 5.2 KB

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