system.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "system.h"
  2. #include "bsp_time.h"
  3. #include "nrf_delay.h"
  4. //系统函数
  5. void FPS_process(void)
  6. {
  7. #if DEBUG_FPS
  8. static uint32_t tem1 = 0;
  9. static uint32_t tem2 = 0;
  10. static uint32_t fps_max=0;
  11. static uint32_t cnt_max=0;
  12. uint32_t fps;
  13. tem2 = NRF_RTC0->COUNTER;
  14. if(tem2<tem1) tem2 += 16777216;
  15. fps = (tem2-tem1)/32.768;
  16. if(fps_max<fps){
  17. fps_max = fps;
  18. cnt_max = tem2-tem1;
  19. }
  20. static uint32_t tim=0;
  21. if(TIME_GetTicks()-tim>=1000){ tim = TIME_GetTicks();
  22. SEGGER_RTT_printf(0,"fps=%dms,cnt=%d\n",fps_max,cnt_max);
  23. fps_max = 0;
  24. }
  25. tem1 = NRF_RTC0->COUNTER;
  26. #endif
  27. }
  28. //休眠时间 ms
  29. uint32_t systermSleepTime = 1000;
  30. //休眠之前事件管理
  31. #define sleep_cb_max 200
  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(int 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. SEGGER_RTT_printf(0,"Sleep_Regist too many!\n");
  45. return -2;
  46. }
  47. void Sleep_Event(void)
  48. {
  49. for(int i=0;i<sleep_num;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  50. if(sleep_cb[i]){
  51. sleep_cb[i](systermSleepTime); //回调函数
  52. }
  53. }
  54. }
  55. //休眠之后事件管理
  56. #define wakeup_cb_max 255
  57. static uint8_t wakeup_num = 0;
  58. static Sleep_cb wakeup_cb[wakeup_cb_max]={0};
  59. int Wakeup_Regist(Sleep_cb cb)
  60. {
  61. for(int i=0;i<wakeup_cb_max;i++) {
  62. if(wakeup_cb[i]==cb) return -1;
  63. if(wakeup_cb[i]==0){
  64. wakeup_num++;
  65. wakeup_cb[i] = cb; //回调函数
  66. return 0;
  67. }
  68. }
  69. // SEGGER_RTT_printf(0,"Wakeup_Regist too many!\n");
  70. return -2;
  71. }
  72. void Wakeup_Event(void)
  73. {
  74. for(int i=0;i<wakeup_num;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  75. if(wakeup_cb[i]){
  76. wakeup_cb[i](systermSleepTime); //回调函数
  77. }
  78. }
  79. }
  80. ////后台进程管理
  81. //#define process_cb_max 255
  82. //static uint8_t process_num = 0;
  83. //static PROCESS_cb process_cb[process_cb_max]={0};
  84. //int Process_Regist(PROCESS_cb cb)
  85. //{
  86. // for(int i=0;i<process_cb_max;i++) {
  87. // if(process_cb[i]==cb) return -1;
  88. // if(process_cb[i]==0){
  89. // process_num++;
  90. // process_cb[i] = cb; //回调函数
  91. // return 0;
  92. // }
  93. // }
  94. // SEGGER_RTT_printf(0,"Process_Regist too many!\n");
  95. // return -2;
  96. //}
  97. //void Process_Background(void)
  98. //{
  99. // for(int i=0;i<process_num;i++) { //SEGGER_RTT_printf(0,"time_cb[%d]=%d\n",i,time_cb[i]);
  100. // if(process_cb[i]){
  101. // process_cb[i](); //回调函数
  102. // }
  103. // }
  104. //}
  105. //app进程管理
  106. #define process_max 255
  107. static uint16_t process_dex = 0;
  108. static PROCESS_t mProcess[process_max];
  109. static PROCESS_t* head_handle = 0;
  110. int Process_Start(uint32_t peroid,const char *name,PROCESS_cb cb)
  111. {
  112. PROCESS_t* target = head_handle;
  113. while(target){ //检查是否已经存在
  114. if(target->cb==cb) return -1;
  115. target = target->next ;
  116. }
  117. for(int i=0;i<process_dex;i++){ //检查是否存在过
  118. if(mProcess[i].cb==cb){
  119. mProcess[i].next = head_handle;
  120. head_handle = &mProcess[i];
  121. SEGGER_RTT_printf(0,"process was existed(%d)\n",cb);
  122. return 0;
  123. }
  124. }
  125. if(process_dex>=process_max) return -1;
  126. mProcess[process_dex].cb = cb;
  127. mProcess[process_dex].next = head_handle;
  128. mProcess[process_dex].Peroid = peroid;
  129. mProcess[process_dex].tim = TIME_GetTicks();
  130. #if ProcessTime_EN
  131. sprintf(mProcess[process_dex].name_t,"%s",name);
  132. #endif
  133. head_handle = &mProcess[process_dex];
  134. process_dex++;
  135. // SEGGER_RTT_printf(0,"process num(%d)\n",process_dex);
  136. return 0;
  137. }
  138. void Process_Stop(PROCESS_cb cb)
  139. {
  140. PROCESS_t** curr;
  141. for(curr=&head_handle;*curr;){
  142. PROCESS_t* entry = *curr;
  143. if(entry->cb==cb){
  144. *curr = entry->next;
  145. }else{
  146. curr = &entry->next;
  147. }
  148. }
  149. }
  150. void Process_SetHoldOn(PROCESS_cb cb,uint8_t holdon)
  151. {
  152. PROCESS_t* target = head_handle;
  153. while(target){ //检查是否已经存在
  154. if(target->cb==cb){ //存在
  155. if(target->holdon!=holdon) target->holdon = holdon;
  156. return;
  157. }
  158. target = target->next ;
  159. }
  160. }
  161. void Process_UpdatePeroid(PROCESS_cb cb,uint16_t Peroid)
  162. {
  163. PROCESS_t* target = head_handle;
  164. while(target){ //检查是否已经存在
  165. if(target->cb==cb){ //存在
  166. target->Peroid = Peroid;
  167. target->tim = TIME_GetTicks();
  168. return;
  169. }
  170. target = target->next ;
  171. }
  172. }
  173. uint16_t Process_GetPeroid(PROCESS_cb cb)
  174. {
  175. PROCESS_t* target = head_handle;
  176. while(target){ //检查是否已经存在
  177. if(target->cb==cb){ //存在
  178. return target->Peroid;
  179. }
  180. target = target->next ;
  181. }
  182. return 0;
  183. }
  184. void Process_SetError(PROCESS_cb cb,uint8_t error)
  185. {
  186. PROCESS_t* target = head_handle;
  187. while(target){ //检查是否已经存在
  188. if(target->cb==cb){ //存在
  189. target->error = error;
  190. }
  191. target = target->next ;
  192. }
  193. }
  194. int Process_App(void)
  195. {
  196. PROCESS_t* target;
  197. int ret = 0;
  198. for(target=head_handle;target;target=target->next){
  199. if(target->cb) {
  200. #if ProcessTime_EN
  201. target->cnt1_rtc = NRF_RTC0->COUNTER;
  202. #endif
  203. if(0 == target-> Peroid)target->cb();
  204. else if(TIME_GetTicks()-target->tim >= target-> Peroid ){
  205. target->tim = TIME_GetTicks();
  206. target->cb();
  207. }
  208. #if ProcessTime_EN
  209. target->cnt2_rtc = NRF_RTC0->COUNTER;
  210. #endif
  211. }
  212. if(target->holdon) ret = 1; //不能进入低功耗
  213. }
  214. #if ProcessTime_EN
  215. char Display[100]={0};
  216. static uint32_t tim =0;
  217. if(TIME_GetTicks()-tim>=DisInterval){ tim = TIME_GetTicks();
  218. for(target=head_handle;target;target=target->next){
  219. if(target->cb && target->cnt2_rtc >= (target->cnt1_rtc +2)) {
  220. sprintf(Display,"%s, time:%02f us\n",target->name_t,(target->cnt2_rtc-target->cnt1_rtc)/32.768 * 1000);
  221. SEGGER_RTT_printf(0,"%s",Display);
  222. memset(Display,0,sizeof(Display));
  223. }
  224. }
  225. }
  226. #endif
  227. return ret;
  228. }
  229. //大循环
  230. void USR_Process(void)
  231. {
  232. //app进程调度
  233. // static uint32_t time_t =0;
  234. if(Process_App()==0){ //进入低功耗模式
  235. // SEGGER_RTT_printf(0,"Sleep(%d)...\n",TIME_GetTicks()-time_t);
  236. Sleep_Event();
  237. // nrf_delay_ms(systermSleepTime); //模拟低功耗
  238. systermSleepTime = rtc_sleep();
  239. if(systermSleepTime==0){
  240. systermSleepTime = 13;
  241. nrf_delay_ms(systermSleepTime);
  242. }
  243. //time_t = TIME_GetTicks();
  244. Wakeup_Event();
  245. //SEGGER_RTT_printf(0,"Wakeup(%d)...\n",systermSleepTime);
  246. }
  247. FPS_process();
  248. }