system.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include "exception.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "nrf_delay.h"
  5. #include "hal_wearshoes.h"
  6. #include "hal_ble_host.h"
  7. #include "hal_ble_client.h"
  8. #include "hal_ble_host.h"
  9. #include "app_flash.h"
  10. #include "ble_comm.h"
  11. //系统函数
  12. void FPS_process(void)
  13. {
  14. #if DEBUG_FPS
  15. static uint32_t tem1 = 0;
  16. static uint32_t tem2 = 0;
  17. static uint32_t fps_max=0;
  18. static uint32_t cnt_max=0;
  19. uint32_t fps;
  20. tem2 = NRF_RTC0->COUNTER;
  21. if(tem2<tem1) tem2 += 16777216;
  22. fps = (tem2-tem1)/32.768;
  23. if(fps_max<fps){
  24. fps_max = fps;
  25. cnt_max = tem2-tem1;
  26. }
  27. static uint32_t tim=0;
  28. if(TIME_GetTicks()-tim>=5000){ tim = TIME_GetTicks();
  29. DEBUG_LOG("fps=%dms,cnt=%d\n",fps_max,cnt_max);
  30. fps_max = 0;
  31. }
  32. tem1 = NRF_RTC0->COUNTER;
  33. #endif
  34. }
  35. //休眠时间 ms
  36. static uint32_t systermSleepTime = 1000;
  37. static uint8_t sleep_num = 0;
  38. static Sleep_cb sleep_cb[sleep_cb_max]={0};
  39. int Sleep_Regist(Sleep_cb cb)
  40. {
  41. for(uint8_t i=0;i<sleep_cb_max;i++) {
  42. if(sleep_cb[i]==cb) return -1;
  43. if(sleep_cb[i]==0){
  44. sleep_num++;
  45. sleep_cb[i] = cb; //回调函数
  46. return 0;
  47. }
  48. }
  49. DEBUG_LOG("Sleep_Regist too many!\n");
  50. return -2;
  51. }
  52. void Sleep_Event(void)
  53. {
  54. for(uint8_t i=0;i<sleep_num;i++) { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
  55. if(sleep_cb[i]){
  56. sleep_cb[i](StandByPower_Interval); //回调函数
  57. }
  58. }
  59. }
  60. static uint8_t wakeup_num = 0;
  61. static Sleep_cb wakeup_cb[wakeup_cb_max]={0};
  62. int Wakeup_Regist(Sleep_cb cb)
  63. {
  64. for(uint8_t i=0;i<wakeup_cb_max;i++) {
  65. if(wakeup_cb[i]==cb) return -1;
  66. if(wakeup_cb[i]==0){
  67. wakeup_num++;
  68. wakeup_cb[i] = cb; //回调函数
  69. return 0;
  70. }
  71. }
  72. // DEBUG_LOG("Wakeup_Regist too many!\n");
  73. return -2;
  74. }
  75. void Wakeup_Event(void)
  76. {
  77. for(uint8_t i=0;i<wakeup_num;i++) { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
  78. if(wakeup_cb[i]){
  79. wakeup_cb[i](systermSleepTime); //回调函数
  80. }
  81. }
  82. }
  83. //app进程管理
  84. static uint8_t process_dex = 0;
  85. static PROCESS_t mProcess[process_max];
  86. int Process_Start(uint16_t peroid,const char *name,PROCESS_cb cb)
  87. {
  88. for(uint8_t i=0;i<process_dex;i++){
  89. if(mProcess[i].cb==cb){ //已经存在
  90. mProcess[i].enable =1;
  91. mProcess[i].Peroid = peroid;
  92. mProcess[i].tim = TIME_GetTicks();
  93. return 1;
  94. }
  95. }
  96. if(process_dex>=process_max) return -1;
  97. mProcess[process_dex].cb = cb;
  98. mProcess[process_dex].enable =1;
  99. mProcess[process_dex].Peroid = peroid;
  100. mProcess[process_dex].tim = TIME_GetTicks();
  101. #if ProcessTime_EN
  102. mProcess[process_dex].name = name;
  103. #endif
  104. process_dex++;
  105. // DEBUG_LOG("process num(%d)\n",process_dex);
  106. return 0;
  107. }
  108. void Process_Stop(PROCESS_cb cb)
  109. {
  110. for(uint8_t i=0;i<process_dex;i++){
  111. if(mProcess[i].cb==cb){ //找到
  112. mProcess[i].enable = 0;
  113. mProcess[i].holdon = 0;
  114. return;
  115. }
  116. }
  117. }
  118. void Process_SetHoldOn(PROCESS_cb cb,uint8_t holdon)
  119. {
  120. for(uint8_t i=0;i<process_dex;i++){
  121. if(mProcess[i].cb==cb){ //找到
  122. if(mProcess[i].holdon!=holdon) mProcess[i].holdon = (holdon&0x01);
  123. return;
  124. }
  125. }
  126. }
  127. void Process_UpdatePeroid(PROCESS_cb cb,uint16_t Peroid)
  128. {
  129. for(uint8_t i=0;i<process_dex;i++){
  130. if(mProcess[i].cb==cb){ //找到
  131. mProcess[i].Peroid = Peroid;
  132. return;
  133. }
  134. }
  135. }
  136. uint16_t Process_GetPeroid(PROCESS_cb cb)
  137. {
  138. for(uint8_t i=0;i<process_dex;i++){
  139. if(mProcess[i].cb==cb){ //找到
  140. return mProcess[i].Peroid;
  141. }
  142. }
  143. return 0;
  144. }
  145. int Process_App(void)
  146. {
  147. int ret = 0;
  148. uint8_t i = 0;
  149. for(i=0;i<process_dex;i++){
  150. Except_Get_Cur_Porcess_ID(i);
  151. if(mProcess[i].enable==0) continue;
  152. if(mProcess[i].cb){
  153. #if ProcessTime_EN
  154. uint32_t useTime = NRF_RTC0->COUNTER;
  155. #endif
  156. if(mProcess[i].Peroid ==0) mProcess[i].cb();
  157. else if(TIME_GetTicks()-mProcess[i].tim >= mProcess[i].Peroid ){
  158. mProcess[i].tim = TIME_GetTicks();
  159. mProcess[i].cb();
  160. }
  161. #if ProcessTime_EN
  162. if(NRF_RTC0->COUNTER<useTime) useTime = (NRF_RTC0->COUNTER+16777216-useTime);
  163. else useTime = (NRF_RTC0->COUNTER-useTime);
  164. useTime = useTime*1000/32.768;
  165. if(mProcess[i].useTime<useTime) mProcess[i].useTime = useTime;
  166. #endif
  167. }
  168. }
  169. for(i=0;i<process_dex;i++){
  170. if(mProcess[i].holdon){
  171. ret = 1; //不能进入低功耗
  172. // DEBUG_LOG("%s can no into low power :\n",mProcess[i].name);
  173. }
  174. }
  175. #if ProcessTime_EN
  176. // static uint32_t tem1 = 0;
  177. // static uint32_t tem2 = 0;
  178. // static uint32_t fps_max=0;
  179. // uint32_t fps;
  180. //
  181. // tem2 = NRF_RTC0->COUNTER;
  182. // if(tem2<tem1) tem2 += 16777216;
  183. // fps = (tem2-tem1)*1000/32.768;
  184. // if(fps_max<fps){
  185. // fps_max = fps;
  186. // }
  187. //
  188. // static uint32_t tim =0;
  189. // if(TIME_GetTicks()-tim>=DisInterval){ tim = TIME_GetTicks();
  190. // DEBUG_LOG("\n============ Tatol(%d process) : %d us ==========\n",process_dex,fps_max);
  191. // for(i=0;i<process_dex;i++){
  192. // if(mProcess[i].useTime>500)
  193. // DEBUG_LOG("[%d]%s : %d us\n",i,mProcess[i].name,mProcess[i].useTime);
  194. // mProcess[i].useTime = 0;
  195. // }
  196. // DEBUG_LOG("\n",fps_max);
  197. // fps_max = 0;
  198. // }
  199. // tem1 = NRF_RTC0->COUNTER;
  200. #endif
  201. return ret;
  202. }
  203. static uint32_t process_sleep_time =0;
  204. ////!!!!!!!!测试使用
  205. ////!!!!!!!!!!!!!!!
  206. //static void cb_Client_PROCESS_TIME(void* handle)
  207. //{
  208. // char buf[100]={0};
  209. // uint8_t i=0;
  210. // BLE_Client_Rx_t* target = handle;
  211. // DEBUG_LOG("cb_Client_PROCESS_TIME:%d,%d\n",target->pDat[0],app_Get_isHost());
  212. // if(0x00 == target->pDat[0] && app_Get_isHost()){//获取左鞋线程耗时的情况
  213. // memset(buf,0,sizeof(buf));
  214. // sprintf(buf,"left sleep time : %d us\n",process_sleep_time);
  215. // send_bytes_client((uint8_t *)buf,strlen(buf));
  216. // for(i=0;i<process_dex;i++){
  217. // memset(buf,0,sizeof(buf));
  218. // #if ProcessTime_EN
  219. // sprintf(buf,"[%d]%s : %d us\n",i,mProcess[i].name,mProcess[i].useTime);
  220. // send_bytes_client((uint8_t *)buf,strlen(buf));
  221. // #endif
  222. // }
  223. // BLE_Host_Tx_Send(0,BLE_PROCESS_TIME,target->pDat,target->datLen);
  224. // }
  225. // else if(0x01 == target->pDat[0] && !app_Get_isHost()){//获取右鞋线程耗时的情况
  226. // memset(buf,0,sizeof(buf));
  227. // sprintf(buf,"Right sleep time : %d us\n",process_sleep_time);
  228. // BLE_Client_Tx_Send(0,BLE_PROCESS_TIME,(uint8_t *)buf,strlen(buf));
  229. // for(i=0;i<process_dex;i++){
  230. // memset(buf,0,sizeof(buf));
  231. // #if ProcessTime_EN
  232. // sprintf(buf,"[%d]%s : %d us\n",i,mProcess[i].name,mProcess[i].useTime);
  233. // BLE_Client_Tx_Send(0,BLE_PROCESS_TIME,(uint8_t *)buf,strlen(buf));
  234. // mProcess[i].useTime = 0;
  235. // #endif
  236. // }
  237. // }
  238. // else if(0x02 == target->pDat[0]){//清除电量计算的信息
  239. // extern battercb_t battery_record;
  240. // extern void cb_init(void);
  241. // extern void printbatter_cb(battercb_t *c,battercb_t *C_flash);
  242. // memset(&battery_record,0,sizeof(battercb_t));
  243. // memset(&mFlash.mbattercb_t,0,sizeof(battercb_t));
  244. // cb_init();
  245. // BLE_Host_Tx_Send(0,BLE_PROCESS_TIME,target->pDat,target->datLen);
  246. // }
  247. // else BLE_Host_Tx_Send(0,BLE_PROCESS_TIME,target->pDat,target->datLen);
  248. //
  249. // if(0x02 == target->pDat[0]){
  250. // for(i=0;i<process_dex;i++){
  251. // mProcess[i].useTime = 0;
  252. // }
  253. // }
  254. //}
  255. //static void cb_Host_PROCESS_TIME(void* handle)
  256. //{
  257. // BLE_Host_Rx_t* target = handle;
  258. // send_bytes_client(target->pDat,target->datLen);
  259. //}
  260. //大循环
  261. void USR_Process(void)
  262. {
  263. //app进程调度
  264. if(Process_App()==0){ //进入低功耗模式
  265. Sleep_Event();
  266. systermSleepTime = rtc_sleep(hal_wearshoes_is_wearshoes());
  267. process_sleep_time = systermSleepTime;
  268. // DEBUG_LOG("process_sleep_time:%d\n",process_sleep_time);
  269. Wakeup_Event();
  270. }
  271. else process_sleep_time =0;
  272. FPS_process();
  273. // static uint8_t state =0;
  274. // if(0 == state){state =1;
  275. // BLE_Client_Rx_Regist(BLE_PROCESS_TIME,cb_Client_PROCESS_TIME);
  276. // BLE_Host_Rx_Regist(BLE_PROCESS_TIME,cb_Host_PROCESS_TIME);
  277. // }
  278. }