hal_battery_NoPowerEnPin.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "usr_config.h"
  5. #include "hal_battery.h"
  6. #include "cli.h"
  7. #include "hal_charge.h"
  8. #include "fml_adc.h"
  9. #include "app_chargerpin_conn_detect.h"
  10. #include "drv_trigger.h"
  11. #define PRINTBLE 0
  12. #define IIDUAN 1
  13. #include "ble_comm.h"
  14. #include "exception.h"
  15. #include "ringframe.h"
  16. RINGFRAME_DEF(battlog, ringframe_size_4096);
  17. char logbuftemp[50];
  18. int logbuftemp_len = 0;
  19. #define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); while(ringframe_in(&battlog,logbuftemp,logbuftemp_len)!=0){ringframe_throw(&battlog);}}
  20. #include "hal_ble_client.h"
  21. char print_log = 0;
  22. void cb_BLE_Client_ERR(void* handle)
  23. {
  24. DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n", 1, 1);
  25. print_log = 1;
  26. }
  27. battercb_t* battercb = NULL;
  28. void printbatter_cb(battercb_t* c, battercb_t* C_flash)
  29. {
  30. // char bytes[256];
  31. // int len = 0;
  32. // len = sprintf(bytes, "%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%d,%d\r\n",
  33. // c->preBestResult_Voltage2power,
  34. // c->preBestResult_chargeV2P_f,
  35. // c->P_mAh,
  36. // c->kg,
  37. // c->P2,
  38. // c->P1,
  39. // c->Battery_capacity_mAh,
  40. // c->adc_tp4056_power,
  41. // c->init,
  42. // c->sta,
  43. // c->chargeV2P_f_init,
  44. // c->Voltage2power_init);
  45. // SEGGER_RTT_Write(0, bytes, len);
  46. // len = sprintf(bytes, "%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%d,%d\r\n",
  47. // C_flash->preBestResult_Voltage2power,
  48. // C_flash->preBestResult_chargeV2P_f,
  49. // C_flash->P_mAh,
  50. // C_flash->kg,
  51. // C_flash->P2,
  52. // C_flash->P1,
  53. // C_flash->Battery_capacity_mAh,
  54. // C_flash->adc_tp4056_power,
  55. // C_flash->init,
  56. // C_flash->sta,
  57. // C_flash->chargeV2P_f_init,
  58. // C_flash->Voltage2power_init);
  59. // SEGGER_RTT_Write(0, bytes, len);
  60. // DEBUG_LOG("\r\n");
  61. }
  62. void cb_init(void)
  63. {
  64. battercb = Except_Get_Battery_Record_Buff();
  65. if (battercb->init != 3)
  66. {
  67. battercb->init = 3;
  68. battercb->P1 = 100.0f;
  69. battercb->adc_tp4056_power = 0;
  70. battercb->Battery_capacity_mAh = 0;
  71. battercb->kg = 1;
  72. battercb->P2 = 0;
  73. battercb->preBestResult_chargeV2P_f = 0;
  74. battercb->preBestResult_Voltage2power = 0;
  75. battercb->sta = 0;
  76. battercb->P_mAh = 0;
  77. battercb->Voltage2power_init = 1;
  78. battercb->chargeV2P_f_init = 1;
  79. }
  80. }
  81. //返回5V信号,有5V的话返回1,没有的话返回0
  82. static char charge_in(void)
  83. {
  84. if (drv_GetChargeTrig())
  85. {
  86. return 1;
  87. }
  88. else
  89. {
  90. return 0;
  91. }
  92. }
  93. static float filter(float value, float kg, float* preBestResult)
  94. {
  95. float new_v = value;
  96. new_v = *preBestResult * (1.0f - kg) + value * kg;
  97. *preBestResult = new_v;
  98. return new_v;
  99. }
  100. #if IIDUAN == 0
  101. //鞋子ADC
  102. static const float poo1o[] = { 0, 0, 0.0279893723606430, 0.174605323652602, 0.325796538285416, 0.495164949358988, 0.661918800578876, 0.829024800123971, 1.00225498989324, 1.17936073685608, 1.37258677752597, 1.56525700069634, 1.78680072433224, 2.00361106262195, 2.24466616203811, 2.46699160701705, 2.77834696254638, 3.12186809827754, 3.58442625993982, 4.15025435558636, 4.75855743544068, 5.51189718744822, 6.35834306864975, 7.38461196888009, 8.48997478633724, 9.43096936165977, 10.3817319764220, 11.4116388420216, 12.3939372566211, 13.5048186806524, 14.5904959858255, 15.5237940825920, 16.4790857938893, 17.8137595522187, 18.9982251103467, 20.3392608271850, 21.5817542329461, 22.7218253119165, 23.9444316340532, 25.2939077624602, 26.6264082603126, 27.6802415218000, 29.0022881606974, 30.1783424265851, 31.1179209268523, 32.2887764986448, 33.3732790985050, 34.2380544358441, 35.2041112278740, 36.0163848326001, 36.8624779801428, 37.6634899287154, 38.5186413495501, 39.4878256764553, 40.2471232681709, 41.2081417271725, 42.3322924899204, 43.7047997876243, 44.9058976548061, 46.5044971286874, 47.8927266715832, 49.8558978793141, 51.9022338412845, 54.2586141300707, 56.3903798469888, 58.7696803719223, 60.8764981712366, 62.2358527791606, 63.8383633243999, 65.5021323737117, 67.1556090613014, 69.0159229136298, 70.1420773342446, 71.2282683025524, 72.4548338447843, 73.6556507850819, 74.8128040906371, 75.8695501837768, 77.1323517287879, 78.6365237973046, 80.3752005495001, 82.8468947240450, 86.6163997907370, 91.2910588313494, 93.9702969410882, 95.4930183746766, 96.9114001488224, 97.8493292727541, 98.7169169431273, 99.3270162091455, 99.6869018017068, 99.9917942993789, 99.9689500363163, 99.97, 99.98, 99.98, 99.98, 99.98, 99.99, 99.99, 99.99 };
  103. static float interp1(float x)
  104. {
  105. int absx = (int)x - 320;
  106. float max = poo1o[absx + 1];
  107. float min = poo1o[absx];
  108. float temp = x - (float)absx - 320.0f;
  109. return (max - min) * temp + min;
  110. }
  111. #else
  112. #define BAT_100_P 401.073951f
  113. #define BAT_90_P 395.453010f
  114. #define BAT_80_P 389.242398f
  115. #define BAT_70_P 382.361362f
  116. #define BAT_60_P 376.311623f
  117. #define BAT_50_P 369.299206f
  118. #define BAT_40_P 363.746813f
  119. #define BAT_30_P 358.456566f
  120. #define BAT_20_P 353.006251f
  121. #define BAT_10_P 346.644176f
  122. #define BAT_0_P 322.992736f
  123. //#define BAT_100_P 405.533003f
  124. //#define BAT_90_P 400.213597f
  125. //#define BAT_80_P 393.844613f
  126. //#define BAT_70_P 386.347668f
  127. //#define BAT_60_P 379.609008f
  128. //#define BAT_50_P 372.386100f
  129. //#define BAT_40_P 365.633721f
  130. //#define BAT_30_P 359.847035f
  131. //#define BAT_20_P 353.966445f
  132. //#define BAT_10_P 347.139688f
  133. //#define BAT_0_P 322.992736f
  134. //#define BAT_100_P 408.5f
  135. //#define BAT_90_P 402.6f
  136. //#define BAT_80_P 393.4f
  137. //#define BAT_70_P 388.0f
  138. //#define BAT_60_P 382.0f
  139. //#define BAT_50_P 372.0f
  140. //#define BAT_40_P 365.0f
  141. //#define BAT_30_P 361.0f
  142. //#define BAT_20_P 354.0f
  143. //#define BAT_10_P 345.0f
  144. //#define BAT_0_P 320.0f
  145. #define CHA_100_P 415.0f //408
  146. #define CHA_90_P 412.0f //402
  147. #define CHA_80_P 408.0f //393
  148. #define CHA_70_P 403.0f //388
  149. #define CHA_60_P 397.0f //382
  150. #define CHA_50_P 387.0f //372
  151. #define CHA_40_P 380.0f //365
  152. #define CHA_30_P 377.0f //361
  153. #define CHA_20_P 369.0f //354
  154. #define CHA_10_P 360.0f //345
  155. #define CHA_0_P 335.0f
  156. static float interp1(float x)
  157. {
  158. if (x > BAT_100_P)
  159. {
  160. return 100.0f;
  161. }
  162. else if ((x <= BAT_100_P) && (x > BAT_90_P))
  163. {
  164. return (x - BAT_90_P) / (BAT_100_P - BAT_90_P) * 10.0f + 90.0f;
  165. }
  166. else if ((x <= BAT_90_P) && (x > BAT_80_P))
  167. {
  168. return (x - BAT_80_P) / (BAT_90_P - BAT_80_P) * 10.0f + 80.0f;
  169. }
  170. else if ((x <= BAT_80_P) && (x > BAT_70_P))
  171. {
  172. return (x - BAT_70_P) / (BAT_80_P - BAT_70_P) * 10.0f + 70.0f;
  173. }
  174. else if ((x <= BAT_70_P) && (x > BAT_60_P))
  175. {
  176. return (x - BAT_60_P) / (BAT_70_P - BAT_60_P) * 10.0f + 60.0f;
  177. }
  178. else if ((x <= BAT_60_P) && (x > BAT_50_P))
  179. {
  180. return (x - BAT_50_P) / (BAT_60_P - BAT_50_P) * 10.0f + 50.0f;
  181. }
  182. else if ((x <= BAT_50_P) && (x > BAT_40_P))
  183. {
  184. return (x - BAT_40_P) / (BAT_50_P - BAT_40_P) * 10.0f + 40.0f;
  185. }
  186. else if ((x <= BAT_40_P) && (x > BAT_30_P))
  187. {
  188. return (x - BAT_30_P) / (BAT_40_P - BAT_30_P) * 10.0f + 30.0f;
  189. }
  190. else if ((x <= BAT_30_P) && (x > BAT_20_P))
  191. {
  192. return (x - BAT_20_P) / (BAT_30_P - BAT_20_P) * 10.0f + 20.0f;
  193. }
  194. else if ((x <= BAT_20_P) && (x > BAT_10_P))
  195. {
  196. return (x - BAT_10_P) / (BAT_20_P - BAT_10_P) * 10.0f + 10.0f;
  197. }
  198. else if ((x <= BAT_10_P) && (x > BAT_0_P))
  199. {
  200. return (x - BAT_0_P) / (BAT_10_P - BAT_0_P) * 10.0f + 00.0f;
  201. }
  202. else
  203. {
  204. return 0.0f;
  205. }
  206. }
  207. #endif
  208. //返回电压百分比
  209. static float Voltage2power(float mV)
  210. {
  211. float rev = 0;
  212. float k = 0;
  213. if (battercb->Voltage2power_init)
  214. {
  215. battercb->preBestResult_Voltage2power = mV / 10;
  216. battercb->Voltage2power_init = 0;
  217. }
  218. k = filter(mV / 10, 0.01, &battercb->preBestResult_Voltage2power);
  219. if (k < 320.0f)
  220. {
  221. rev = 0;
  222. }
  223. else if (k > 420.0f)
  224. {
  225. rev = 100;
  226. }
  227. else
  228. {
  229. rev = interp1(k);
  230. }
  231. return rev;
  232. }
  233. #if IIDUAN == 0
  234. static const float chargeV2P[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.606184684199367, 0.938307744774148, 1.30769659743727, 1.87378869014097, 2.39210582883903, 2.92471759425620, 3.79782257541779, 4.54241638183623, 5.09886597521223, 5.98930945903392, 6.49613916536905, 7.57967916311061, 8.42757721701290, 9.26589943580526, 10.3408809248851, 11.4278317273525, 12.3098089504989, 13.8095299712012, 15.4373756551577, 16.8805115746597, 18.4871688551768, 20.0578462608782, 21.6336381358003, 22.9916066660508, 24.4652374859292, 25.6565902258290, 26.8453581267414, 27.9070922567514, 28.9713295466563, 30.0209078713242, 30.9380166385130, 32.0419258329631, 33.1827748787620, 33.8691297160915, 34.8949274023278, 35.8840553861847, 36.9638277084030, 37.8687458690322, 38.9939274175310, 40.4336758333268, 41.1114865403869, 42.6160733592240, 44.2493873543177, 45.6026105469954, 47.4664557187522, 49.0734043728910, 51.2998758679562, 52.8880367841713, 54.7587352342972, 57.0178625862682, 58.8608281071146, 60.3657347075748, 62.4281274954232, 64.3086188700345, 65.8044424890286, 68.6475622104224, 72.7352289117192, 76.9893033499930, 84.0930819869950, 92.2923770276700, 96.3286261252036, 99.7542287364423 };
  235. static float interp1_chargeV2P(float x)
  236. {
  237. int absx = (int)x - 350;
  238. float max = chargeV2P[absx + 1];
  239. float min = chargeV2P[absx];
  240. float temp = x - (float)absx - 350.0f;
  241. return (max - min) * temp + min;
  242. }
  243. #else
  244. static float interp1_chargeV2P(float x)
  245. {
  246. if (x > CHA_100_P)
  247. {
  248. return 100.0f;
  249. }
  250. else if ((x <= CHA_100_P) && (x > CHA_90_P))
  251. {
  252. return (x - CHA_90_P) / (CHA_100_P - CHA_90_P) * 10.0f + 90.0f;
  253. }
  254. else if ((x <= CHA_90_P) && (x > CHA_80_P))
  255. {
  256. return (x - CHA_80_P) / (CHA_90_P - CHA_80_P) * 10.0f + 80.0f;
  257. }
  258. else if ((x <= CHA_80_P) && (x > CHA_70_P))
  259. {
  260. return (x - CHA_70_P) / (CHA_80_P - CHA_70_P) * 10.0f + 70.0f;
  261. }
  262. else if ((x <= CHA_70_P) && (x > CHA_60_P))
  263. {
  264. return (x - CHA_60_P) / (CHA_70_P - CHA_60_P) * 10.0f + 60.0f;
  265. }
  266. else if ((x <= CHA_60_P) && (x > CHA_50_P))
  267. {
  268. return (x - CHA_50_P) / (CHA_60_P - CHA_50_P) * 10.0f + 50.0f;
  269. }
  270. else if ((x <= CHA_50_P) && (x > CHA_40_P))
  271. {
  272. return (x - CHA_40_P) / (CHA_50_P - CHA_40_P) * 10.0f + 40.0f;
  273. }
  274. else if ((x <= CHA_40_P) && (x > CHA_30_P))
  275. {
  276. return (x - CHA_30_P) / (CHA_40_P - CHA_30_P) * 10.0f + 30.0f;
  277. }
  278. else if ((x <= CHA_30_P) && (x > CHA_20_P))
  279. {
  280. return (x - CHA_20_P) / (CHA_30_P - CHA_20_P) * 10.0f + 20.0f;
  281. }
  282. else if ((x <= CHA_20_P) && (x > CHA_10_P))
  283. {
  284. return (x - CHA_10_P) / (CHA_20_P - CHA_10_P) * 10.0f + 10.0f;
  285. }
  286. else if ((x <= CHA_10_P) && (x > CHA_0_P))
  287. {
  288. return (x - CHA_0_P) / (BAT_10_P - CHA_0_P) * 10.0f + 0.0f;
  289. }
  290. else
  291. {
  292. return 0.0f;
  293. }
  294. }
  295. #endif
  296. //返回电压百分比
  297. static float chargeV2P_f(float mV)
  298. {
  299. float rev = 0;
  300. float k = 0;
  301. if (battercb->chargeV2P_f_init)
  302. {
  303. battercb->preBestResult_chargeV2P_f = mV / 10;
  304. battercb->chargeV2P_f_init = 0;
  305. }
  306. k = filter(mV / 10, 0.05, &battercb->preBestResult_chargeV2P_f);
  307. if (k < 350.0f)
  308. {
  309. rev = 0;
  310. }
  311. else if (k > 415.0f)
  312. {
  313. rev = 100;
  314. }
  315. else
  316. {
  317. rev = interp1_chargeV2P(k);
  318. }
  319. return rev;
  320. }
  321. //返回电量百分比
  322. static float Voltage2mah(float mah, float storage_capacity)
  323. {
  324. return mah / storage_capacity * 100.0f;
  325. }
  326. //返回电压剩余绝对容量
  327. static float mah2Voltage(float P, float storage_capacity)
  328. {
  329. return storage_capacity * P / 100.0f;
  330. }
  331. static void Charge(float mV, float* mAh, float interval_s)
  332. {
  333. float A = mV / 3000.0f * 1.1f;
  334. float dmAh = A * 1000.0f * interval_s / 3600.0f;
  335. *mAh = *mAh + dmAh;
  336. }
  337. static float Power_management(float mV_Battery, float mV_Charge)
  338. {
  339. float storage_capacity = 350;
  340. switch (battercb->sta)
  341. {
  342. case 0:
  343. if (mV_Charge > 20)
  344. {
  345. battercb->sta = 2; //充电过程
  346. battercb->P1 = chargeV2P_f(mV_Battery);
  347. battercb->Battery_capacity_mAh = mah2Voltage(battercb->P1, storage_capacity);
  348. }
  349. else
  350. {
  351. battercb->P1 = Voltage2power(mV_Battery);
  352. battercb->sta = 1; //放电过程
  353. if ((interp1(mV_Battery / 10.0f) - battercb->P1 > 15.0f) || (interp1(mV_Battery / 10.0f) - battercb->P1 < -15.0f))
  354. {
  355. battercb->P1 = interp1(mV_Battery / 10.0f);
  356. }
  357. }
  358. break;
  359. case 1://放电
  360. if (mV_Charge > 20)
  361. {
  362. battercb->sta = 2; //充电过程
  363. battercb->kg = 0;
  364. battercb->Battery_capacity_mAh = mah2Voltage(battercb->P1, storage_capacity);
  365. }
  366. else
  367. {
  368. battercb->P2 = Voltage2power(mV_Battery);
  369. if ((battercb->P2 - battercb->P1 > 15.0f) || (battercb->P2 - battercb->P1 < -15.0f))
  370. {
  371. battercb->P1 = battercb->P2;
  372. }
  373. else
  374. {
  375. if (battercb->P1 > battercb->P2)
  376. {
  377. battercb->P1 = battercb->P2;
  378. }
  379. }
  380. }
  381. break;
  382. case 2://充电
  383. if (mV_Charge < 20)
  384. {
  385. battercb->sta = 1; //放电过程
  386. //初始化滤波器波器
  387. battercb->preBestResult_Voltage2power = mV_Battery / 10;
  388. // #ifdef PIN_BATFULL
  389. // if ((battercb->P1 > 99.1f) && (charge_in()) && (nrf_gpio_pin_read(PIN_BATFULL) == 0))
  390. // #else
  391. if ((battercb->P1 > 99.1f) && (charge_in()))
  392. // #endif
  393. {
  394. battercb->P1 = 100.0f;
  395. }
  396. }
  397. else
  398. {
  399. if ((battercb->P1 > 99.9f))
  400. {
  401. DEBUG_LOG("((battercb->P1 == 100.0f)&&( mV_Charge < 200.0f))()\r\n");
  402. break;
  403. }
  404. //---------------------------------------------------
  405. Charge(mV_Charge, &battercb->Battery_capacity_mAh, 1);
  406. battercb->P_mAh = Voltage2mah(battercb->Battery_capacity_mAh, storage_capacity);
  407. //---------------------------------------------------
  408. battercb->P2 = chargeV2P_f(mV_Battery);
  409. battercb->kg = mV_Charge / 1000.0f;
  410. if (battercb->kg > 1.0f)
  411. {
  412. battercb->kg = 1.0f;
  413. }
  414. battercb->P2 = (1.0f - battercb->kg) * battercb->P2 + battercb->kg * battercb->P_mAh;
  415. if (battercb->P1 < battercb->P2) //过滤刚插上充电线时候的虚低
  416. {
  417. battercb->P1 = battercb->P2;
  418. }
  419. if (battercb->P1 >= 100.0f)
  420. {
  421. battercb->P1 = 99.9;
  422. }
  423. }
  424. break;
  425. }
  426. return battercb->P1;
  427. }
  428. static int16_t BatadcVal = 0;
  429. int16_t hal_GetBatttery_Adc(void)
  430. {
  431. return ADC_RESULT_IN_MILLI_VOLTS(BatadcVal) * 5 / 3;
  432. }
  433. static void hal_battery_Process(void)
  434. {
  435. static int count = 0;
  436. static int adc_midal = 0;
  437. static int cprign = 0;
  438. #if PRINTBLE
  439. char buff[256];
  440. unsigned char len = 0;
  441. #endif
  442. int16_t CHARGMEASURE;
  443. int16_t volTemp;
  444. int16_t volTemp_CHARGMEASURE;
  445. if (-1 == fml_adc_get_value(PIN_ADC_BAT_CHANNEL, &BatadcVal))
  446. {
  447. return;
  448. }
  449. else if (0 == BatadcVal)
  450. {
  451. return;
  452. }
  453. // DEBUG_LOG("hal_GetBatttery_Adc:%d\n", BatadcVal);
  454. volTemp = ADC_RESULT_IN_MILLI_VOLTS(BatadcVal) * 5 / 3; // 电池电压转换计算
  455. if (-1 == fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL, &CHARGMEASURE))
  456. {
  457. return;
  458. }
  459. volTemp_CHARGMEASURE = ADC_RESULT_IN_MILLI_VOLTS(CHARGMEASURE);// 电池电压转换计算
  460. if (count < 10)
  461. {
  462. adc_midal += volTemp;
  463. DEBUG_LOG(0, "N %d %5d battery %d mV\r\n", count, adc_midal, volTemp);
  464. count++;
  465. return;
  466. }
  467. else if (count == 10)
  468. {
  469. Process_UpdatePeroid(hal_battery_Process, 1000);
  470. volTemp = adc_midal / count;
  471. DEBUG_LOG(0, "average battery %d mV %d\r\n", volTemp, count);
  472. count++;
  473. }
  474. battercb->adc_tp4056_power = Power_management((float)volTemp, (float)volTemp_CHARGMEASURE);
  475. #if PRINTBLE
  476. len = sprintf(buff, "%4d ,%4d ,%4d,%f\r\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power);
  477. send_bytes_client((unsigned char*)buff, len);
  478. #endif
  479. if (cprign % 600 == 0)//600
  480. {
  481. log("%4d,%4d,%4d,%2.1f,%d,%2.1f,%2.1f,%2.1f\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power, battercb->sta, battercb->P_mAh, battercb->P2, battercb->kg);
  482. logbuftemp[logbuftemp_len] = 0;
  483. // SEGGER_RTT_Write(0,logbuftemp,logbuftemp_len);
  484. Except_TxError(EXCEPT_DATA_BATTERY, logbuftemp);
  485. }
  486. cprign++;
  487. if (print_log == 1)
  488. {
  489. log("%4d,%4d,%4d,%2.1f,%d,%2.1f,%2.1f,%2.1f\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power, battercb->sta, battercb->P_mAh, battercb->P2, battercb->kg);
  490. logbuftemp[logbuftemp_len] = 0;
  491. Except_TxError(EXCEPT_DATA_BATTERY, logbuftemp);
  492. print_log = 2;
  493. }
  494. if (print_log == 2)
  495. {
  496. unsigned char length = 0;
  497. while (ringframe_peek(&battlog, logbuftemp, &length) == 0)
  498. {
  499. if (send_bytes_client((unsigned char*)logbuftemp, length) != 0)
  500. {
  501. return;
  502. }
  503. ringframe_throw(&battlog);
  504. }
  505. print_log = 0;
  506. }
  507. }
  508. //返回的电量范围: 0~100 表示电量百分比
  509. uint8_t GetBatteryPersent(void)
  510. {
  511. uint8_t persent = 0;
  512. persent = (uint8_t)(battercb->adc_tp4056_power);
  513. if (persent > 10)
  514. {
  515. persent = persent / 10;
  516. persent = persent * 10;
  517. }
  518. if (persent > 100)
  519. {
  520. persent = 100;
  521. }
  522. else if (persent <= 0)
  523. {
  524. persent = 0;
  525. }
  526. return persent;
  527. }
  528. void hal_battery_init(void)
  529. {
  530. cb_init();
  531. #ifdef PIN_BATFULL
  532. nrf_gpio_cfg_input(PIN_BATFULL, NRF_GPIO_PIN_PULLUP);
  533. #endif
  534. BLE_Client_Rx_Regist(BLE_ERR, cb_BLE_Client_ERR);
  535. Process_Start(10, "hal_battery", hal_battery_Process);
  536. }