system.c 4.5 KB

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