system.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "exception.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "nrf_delay.h"
  5. #include "hal_wearshoes.h"
  6. typedef struct _Task_UseTime
  7. {
  8. uint32_t task_id;
  9. uint32_t task_us;
  10. uint32_t total_us;
  11. } Task_UseTime_t;
  12. static int _report_task_useTime_code = 0;
  13. static bool is_report_task_useTime = false;
  14. static Task_UseTime_t last_round_task_max_useTime = {0xFFFFFFFF,0,0},cur_round_task_max_useTime = {0xFFFFFFFF,0,0},all_round_task_max_useTime = {0xFFFFFFFF,0,0};
  15. //系统函数
  16. void FPS_process(void)
  17. {
  18. #if DEBUG_FPS
  19. static uint32_t tem1 = 0;
  20. static uint32_t tem2 = 0;
  21. static uint32_t fps_max=0;
  22. static uint32_t cnt_max=0;
  23. uint32_t fps;
  24. tem2 = NRF_RTC0->COUNTER;
  25. if(tem2<tem1) tem2 += 16777216;
  26. fps = (tem2-tem1)/32.768;
  27. if(fps_max<fps){
  28. fps_max = fps;
  29. cnt_max = tem2-tem1;
  30. }
  31. static uint32_t tim=0;
  32. if(TIME_GetTicks()-tim>=5000){ tim = TIME_GetTicks();
  33. DEBUG_LOG("fps=%dms,cnt=%d\n",fps_max,cnt_max);
  34. fps_max = 0;
  35. }
  36. tem1 = NRF_RTC0->COUNTER;
  37. #endif
  38. }
  39. //休眠时间 ms
  40. static uint32_t systermSleepTime = 1000;
  41. static uint8_t sleep_num = 0;
  42. static Sleep_cb sleep_cb[sleep_cb_max]={0};
  43. int Sleep_Regist(Sleep_cb cb)
  44. {
  45. for(uint8_t i=0;i<sleep_cb_max;i++) {
  46. if(sleep_cb[i]==cb) return -1;
  47. if(sleep_cb[i]==0){
  48. sleep_num++;
  49. sleep_cb[i] = cb; //回调函数
  50. return 0;
  51. }
  52. }
  53. DEBUG_LOG("Sleep_Regist too many!\n");
  54. return -2;
  55. }
  56. void Sleep_Event(void)
  57. {
  58. for(uint8_t i=0;i<sleep_num;i++) { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
  59. if(sleep_cb[i]){
  60. sleep_cb[i](StandByPower_Interval); //回调函数
  61. }
  62. }
  63. }
  64. static uint8_t wakeup_num = 0;
  65. static Sleep_cb wakeup_cb[wakeup_cb_max]={0};
  66. int Wakeup_Regist(Sleep_cb cb)
  67. {
  68. for(uint8_t i=0;i<wakeup_cb_max;i++) {
  69. if(wakeup_cb[i]==cb) return -1;
  70. if(wakeup_cb[i]==0){
  71. wakeup_num++;
  72. wakeup_cb[i] = cb; //回调函数
  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_num;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. char buf[256];
  154. uint32_t task_useTime = 0;
  155. cur_round_task_max_useTime.task_id = 0xFFFFFFFF;
  156. cur_round_task_max_useTime.task_us = 0;
  157. cur_round_task_max_useTime.total_us = 0;
  158. for(i=0;i<process_dex;i++){
  159. Except_Get_Cur_Porcess_ID(i);
  160. if(mProcess[i].enable==0) continue;
  161. if(mProcess[i].cb){
  162. #if ProcessTime_EN
  163. uint32_t useTime = NRF_RTC0->COUNTER;
  164. #endif
  165. uint32_t cnt1 = NRF_RTC0->COUNTER;
  166. if(mProcess[i].Peroid ==0) mProcess[i].cb();
  167. else if(TIME_GetTicks()-mProcess[i].tim >= mProcess[i].Peroid ){
  168. mProcess[i].tim = TIME_GetTicks();
  169. mProcess[i].cb();
  170. }
  171. uint32_t cnt2 = NRF_RTC0->COUNTER;
  172. if(cnt2<cnt1) cnt2 += 16777216;
  173. task_useTime = (cnt2-cnt1)/32.768 * 1000;
  174. cur_round_task_max_useTime.total_us += task_useTime;
  175. if(task_useTime > cur_round_task_max_useTime.task_us)
  176. {
  177. cur_round_task_max_useTime.task_us = task_useTime;
  178. cur_round_task_max_useTime.task_id = i;
  179. }
  180. #if ProcessTime_EN
  181. if(NRF_RTC0->COUNTER<useTime) useTime = (NRF_RTC0->COUNTER+16777216-useTime);
  182. else useTime = (NRF_RTC0->COUNTER-useTime);
  183. useTime = useTime*1000/32.768;
  184. if(mProcess[i].useTime<useTime) mProcess[i].useTime = useTime;
  185. #endif
  186. }
  187. }
  188. if(cur_round_task_max_useTime.task_us > all_round_task_max_useTime.task_us)
  189. {
  190. all_round_task_max_useTime.task_us = cur_round_task_max_useTime.task_us;
  191. all_round_task_max_useTime.task_id = cur_round_task_max_useTime.task_id;
  192. }
  193. if(is_report_task_useTime)
  194. {
  195. sprintf(buf,"code:%d,report useTime:%d,%d,%d,%d,%d,%d,%d,%d\r\n",_report_task_useTime_code, \
  196. last_round_task_max_useTime.task_id, \
  197. last_round_task_max_useTime.task_us, \
  198. last_round_task_max_useTime.total_us, \
  199. cur_round_task_max_useTime.task_id, \
  200. cur_round_task_max_useTime.task_us, \
  201. cur_round_task_max_useTime.total_us, \
  202. all_round_task_max_useTime.task_id, \
  203. all_round_task_max_useTime.task_us);
  204. Except_TxError(EXCEPT_EXCEPTION,buf);
  205. is_report_task_useTime = false;
  206. }
  207. last_round_task_max_useTime.task_us = cur_round_task_max_useTime.task_us;
  208. last_round_task_max_useTime.total_us = cur_round_task_max_useTime.total_us;
  209. last_round_task_max_useTime.task_id = cur_round_task_max_useTime.task_id;
  210. for(i=0;i<process_dex;i++){
  211. if(mProcess[i].holdon){
  212. ret = 1; //不能进入低功耗
  213. // DEBUG_LOG("%s can no into low power :\n",mProcess[i].name);
  214. }
  215. }
  216. #if ProcessTime_EN
  217. static uint32_t tem1 = 0;
  218. static uint32_t tem2 = 0;
  219. static uint32_t fps_max=0;
  220. uint32_t fps;
  221. tem2 = NRF_RTC0->COUNTER;
  222. if(tem2<tem1) tem2 += 16777216;
  223. fps = (tem2-tem1)*1000/32.768;
  224. if(fps_max<fps){
  225. fps_max = fps;
  226. }
  227. static uint32_t tim =0;
  228. if(TIME_GetTicks()-tim>=DisInterval){ tim = TIME_GetTicks();
  229. DEBUG_LOG("\n============ Tatol(%d process) : %d us ==========\n",process_dex,fps_max);
  230. for(i=0;i<process_dex;i++){
  231. if(mProcess[i].useTime>500)
  232. DEBUG_LOG("[%d]%s : %d us\n",i,mProcess[i].name,mProcess[i].useTime);
  233. mProcess[i].useTime = 0;
  234. }
  235. DEBUG_LOG("\n",fps_max);
  236. fps_max = 0;
  237. }
  238. tem1 = NRF_RTC0->COUNTER;
  239. #endif
  240. return ret;
  241. }
  242. //触发上报上一轮、当前轮、所有轮任务中最大的耗时任务和ID,因为实时性的原因,所以不作保证性发送。
  243. void Process_report_task_useTime(int report_task_useTime_code)
  244. {
  245. _report_task_useTime_code = report_task_useTime_code;
  246. is_report_task_useTime = true;
  247. }
  248. //大循环
  249. void USR_Process(void)
  250. {
  251. //app进程调度
  252. if(Process_App()==0){ //进入低功耗模式
  253. Sleep_Event();
  254. systermSleepTime = rtc_sleep(hal_wearshoes_is_wearshoes());
  255. // DEBUG_LOG("process_sleep_time:%d\n",process_sleep_time);
  256. Wakeup_Event();
  257. }
  258. FPS_process();
  259. }
  260. void Process_All_Stop(void)
  261. {
  262. memset(mProcess,0,sizeof(mProcess));
  263. process_dex = 0;
  264. }