system.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "exception.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "nrf_delay.h"
  5. #include "hal_wearshoes.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>=5000){ tim = TIME_GetTicks();
  24. DEBUG_LOG("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. static 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(uint8_t 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. DEBUG_LOG("Sleep_Regist too many!\n");
  45. return -2;
  46. }
  47. void Sleep_Event(void)
  48. {
  49. for(uint8_t i=0;i<sleep_num;i++) { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
  50. if(sleep_cb[i]){
  51. sleep_cb[i](StandByPower_Interval); //回调函数
  52. }
  53. }
  54. }
  55. static Sleep_cb wakeup_cb[wakeup_cb_max]={0};
  56. int Wakeup_Regist(Sleep_cb cb)
  57. {
  58. for(uint8_t i=0;i<wakeup_cb_max;i++) {
  59. if(wakeup_cb[i]==cb) return -1;
  60. if(wakeup_cb[i]==0){
  61. wakeup_cb[i] = cb; //回调函数
  62. return 0;
  63. }
  64. }
  65. // DEBUG_LOG("Wakeup_Regist too many!\n");
  66. return -2;
  67. }
  68. int Wakeup_Delete(Sleep_cb cb)
  69. {
  70. for(uint8_t i=0;i<wakeup_cb_max;i++) {
  71. if(wakeup_cb[i]==cb){
  72. wakeup_cb[i] = 0; //回调函数
  73. return 0;
  74. }
  75. }
  76. // DEBUG_LOG("Wakeup_Regist too many!\n");
  77. return -2;
  78. }
  79. void Wakeup_Event(void)
  80. {
  81. for(uint8_t i=0;i<wakeup_cb_max;i++) { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
  82. if(wakeup_cb[i]){
  83. wakeup_cb[i](systermSleepTime); //回调函数
  84. }
  85. }
  86. }
  87. //app进程管理
  88. static uint8_t process_dex = 0;
  89. static PROCESS_t mProcess[process_max];
  90. int Process_Start(uint16_t peroid,const char *name,PROCESS_cb cb)
  91. {
  92. for(uint8_t i=0;i<process_dex;i++){
  93. if(mProcess[i].cb==cb){ //已经存在
  94. mProcess[i].enable =1;
  95. mProcess[i].Peroid = peroid;
  96. mProcess[i].tim = TIME_GetTicks();
  97. return 1;
  98. }
  99. }
  100. if(process_dex>=process_max) return -1;
  101. mProcess[process_dex].cb = cb;
  102. mProcess[process_dex].enable =1;
  103. mProcess[process_dex].Peroid = peroid;
  104. mProcess[process_dex].tim = TIME_GetTicks();
  105. #if ProcessTime_EN
  106. mProcess[process_dex].name = name;
  107. #endif
  108. process_dex++;
  109. // DEBUG_LOG("process num(%d)\n",process_dex);
  110. return 0;
  111. }
  112. void Process_Stop(PROCESS_cb cb)
  113. {
  114. for(uint8_t i=0;i<process_dex;i++){
  115. if(mProcess[i].cb==cb){ //找到
  116. mProcess[i].enable = 0;
  117. mProcess[i].holdon = 0;
  118. return;
  119. }
  120. }
  121. }
  122. void Process_SetHoldOn(PROCESS_cb cb,uint8_t holdon)
  123. {
  124. for(uint8_t i=0;i<process_dex;i++){
  125. if(mProcess[i].cb==cb){ //找到
  126. if(mProcess[i].holdon!=holdon) mProcess[i].holdon = (holdon&0x01);
  127. return;
  128. }
  129. }
  130. }
  131. void Process_UpdatePeroid(PROCESS_cb cb,uint16_t Peroid)
  132. {
  133. for(uint8_t i=0;i<process_dex;i++){
  134. if(mProcess[i].cb==cb){ //找到
  135. mProcess[i].Peroid = Peroid;
  136. return;
  137. }
  138. }
  139. }
  140. uint16_t Process_GetPeroid(PROCESS_cb cb)
  141. {
  142. for(uint8_t i=0;i<process_dex;i++){
  143. if(mProcess[i].cb==cb){ //找到
  144. return mProcess[i].Peroid;
  145. }
  146. }
  147. return 0;
  148. }
  149. int Process_App(void)
  150. {
  151. int ret = 0;
  152. uint8_t i = 0;
  153. for(i=0;i<process_dex;i++){
  154. Except_Get_Cur_Porcess_ID(i);
  155. if(mProcess[i].enable==0) continue;
  156. if(mProcess[i].cb){
  157. #if ProcessTime_EN
  158. uint32_t useTime = NRF_RTC0->COUNTER;
  159. #endif
  160. if(mProcess[i].Peroid ==0) mProcess[i].cb();
  161. else if(TIME_GetTicks()-mProcess[i].tim >= mProcess[i].Peroid ){
  162. mProcess[i].tim = TIME_GetTicks();
  163. mProcess[i].cb();
  164. }
  165. #if ProcessTime_EN
  166. if(NRF_RTC0->COUNTER<useTime) useTime = (NRF_RTC0->COUNTER+16777216-useTime);
  167. else useTime = (NRF_RTC0->COUNTER-useTime);
  168. useTime = useTime*1000/32.768;
  169. if(mProcess[i].useTime<useTime) mProcess[i].useTime = useTime;
  170. #endif
  171. }
  172. }
  173. for(i=0;i<process_dex;i++){
  174. if(mProcess[i].holdon){
  175. ret = 1; //不能进入低功耗
  176. // DEBUG_LOG("%s can no into low power :\n",mProcess[i].name);
  177. }
  178. }
  179. #if ProcessTime_EN
  180. static uint32_t tem1 = 0;
  181. static uint32_t tem2 = 0;
  182. static uint32_t fps_max=0;
  183. uint32_t fps;
  184. tem2 = NRF_RTC0->COUNTER;
  185. if(tem2<tem1) tem2 += 16777216;
  186. fps = (tem2-tem1)*1000/32.768;
  187. if(fps_max<fps){
  188. fps_max = fps;
  189. }
  190. static uint32_t tim =0;
  191. if(TIME_GetTicks()-tim>=DisInterval){ tim = TIME_GetTicks();
  192. DEBUG_LOG("\n============ Tatol(%d process) : %d us ==========\n",process_dex,fps_max);
  193. for(i=0;i<process_dex;i++){
  194. if(mProcess[i].useTime>500)
  195. DEBUG_LOG("[%d]%s : %d us\n",i,mProcess[i].name,mProcess[i].useTime);
  196. mProcess[i].useTime = 0;
  197. }
  198. DEBUG_LOG("\n",fps_max);
  199. fps_max = 0;
  200. }
  201. tem1 = NRF_RTC0->COUNTER;
  202. #endif
  203. return ret;
  204. }
  205. //大循环
  206. void USR_Process(void)
  207. {
  208. //app进程调度
  209. if(Process_App()==0){ //进入低功耗模式
  210. Sleep_Event();
  211. systermSleepTime = rtc_sleep(hal_wearshoes_is_wearshoes());
  212. // DEBUG_LOG("process_sleep_time:%d\n",process_sleep_time);
  213. Wakeup_Event();
  214. }
  215. FPS_process();
  216. }