system.c 5.1 KB

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