driver_charge.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * @File name : driver_charge.c
  3. * @Author : Bluetrum IOT Team
  4. * @Date : 2023-06-20
  5. * @Description : This file provides functions to manage the most functionalities
  6. * of the Charge module.
  7. *
  8. * Copyright (c) by Bluetrum, All Rights Reserved.
  9. */
  10. #include "driver_charge.h"
  11. static void charge_DC_online_handle(void);
  12. static bool charge_detect_off_condition(void);
  13. // charging process parameter struct
  14. static struct charge_status_def{
  15. u8 sta;
  16. u8 mode;
  17. u8 cutoff_vol;
  18. u8 cutoff_cur;
  19. u8 const_cur;
  20. u8 trickle_en;
  21. u8 trickle_cur;
  22. } charge_status;
  23. // charging process control struct
  24. static struct charge_ctrl_def{
  25. u8 dc_insert;
  26. u8 dc_in_debounce_cnt;
  27. u8 reach_trick_curr_state_condition_debounce_cnt;
  28. u8 reach_const_curr_state_condition_debounce_cnt;
  29. u8 reach_cutoff_curr_debounce_cnt;
  30. u8 reach_cutoff_vol_debounce_cnt;
  31. u8 reach_cutoff_debounce_cnt;
  32. u16 cutoff_vol_2_stop_time;
  33. u16 cutoff_vol_2_stop_time_cnt;
  34. u32 vddio_tmp;
  35. } charge_ctrl;
  36. /**
  37. * @brief Initializes the charge according to the specified
  38. * parameters in the charge_init_struct.
  39. * @param charge_init_struct: pointer to a charge_init_typedef structure that
  40. * contains the configuration information for the charge register.
  41. * @retval None
  42. */
  43. void charge_init(charge_init_typedef *charge_init_struct)
  44. {
  45. charge_init_struct->cutoff_curr = charge_cutoff_curr_trim(charge_init_struct->cutoff_curr >> 11) << 11;
  46. charge_init_struct->const_curr = charge_const_curr_trim(charge_init_struct->const_curr);
  47. RTCCON8 &= ~((uint32_t)(0x03 << 8));
  48. RTCCON8 |= (uint32_t)(charge_init_struct->cutoff_volt);
  49. RTCCON7 &= ~((uint32_t)(0x0f << 11));
  50. RTCCON7 |= (uint32_t)(charge_init_struct->cutoff_curr);
  51. RTCCON &= ~((uint32_t)(0x01 << 6));
  52. RTCCON |= (uint32_t)charge_init_struct->dcin_reset;
  53. // set VUSB leakage current.
  54. RTCCON8 &= ~(0x07 << 16);
  55. RTCCON8 |= (0x02 << 16);
  56. memset(&charge_status, 0, sizeof(struct charge_status_def));
  57. memset(&charge_ctrl, 0, sizeof(struct charge_ctrl_def));
  58. // storage the configure value of charge.
  59. charge_status.trickle_en = charge_init_struct->trick_curr_en;
  60. charge_status.trickle_cur = charge_init_struct->trick_curr;
  61. charge_status.const_cur = charge_init_struct->const_curr;
  62. charge_status.mode = charge_init_struct->mode;
  63. // set the charge initial status to CHARGE_STA_OFF.
  64. charge_status.sta = CHARGE_STA_OFF;
  65. }
  66. /**
  67. * @brief De-initialize the specified uart peripheral.
  68. * @retval None
  69. */
  70. void charge_deinit(void)
  71. {
  72. RTCCON8 &= ~(0x01 << 6);
  73. RTCCON8 |= (0x01 << 1);
  74. charge_status.sta = CHARGE_STA_UNINIT;
  75. }
  76. /**
  77. * @brief process state switch during charging, this func should be called continuously
  78. * during charging.
  79. * @param None.
  80. * @retval None
  81. */
  82. AT(.text.app.proc.charge)
  83. void charge_process(void)
  84. {
  85. // exit if charge uninitial.
  86. if (charge_status.sta == CHARGE_STA_UNINIT) {
  87. return;
  88. }
  89. // detect whether DC insert
  90. if ((charge_detect_dc()) != charge_ctrl.dc_insert) {
  91. charge_ctrl.dc_in_debounce_cnt++;
  92. if (charge_ctrl.dc_in_debounce_cnt > 5) {
  93. charge_ctrl.dc_insert = charge_detect_dc();
  94. charge_ctrl.dc_in_debounce_cnt = 0;
  95. }
  96. } else {
  97. charge_ctrl.dc_in_debounce_cnt = 0;
  98. }
  99. if (charge_ctrl.dc_insert == CHARGE_DC_STATUS_ONLINE) {
  100. charge_DC_online_handle();
  101. } else {
  102. if (charge_status.sta != CHARGE_STA_OFF) {
  103. charge_status_update(CHARGE_STA_OFF);
  104. }
  105. }
  106. }
  107. /**
  108. * @brief Process event when VUSB insert.
  109. * @param None.
  110. * @retval None
  111. */
  112. static void charge_DC_online_handle(void)
  113. {
  114. // if the charging is not begin.
  115. if (charge_status.sta == CHARGE_STA_OFF) {
  116. if (charge_status.trickle_en == DISABLE || (PWRCON1 & (1 << 29))) { // trickle disable or battery higher then trickle voltage.
  117. if (charge_ctrl.reach_const_curr_state_condition_debounce_cnt > 10) {
  118. charge_ctrl.reach_const_curr_state_condition_debounce_cnt = 0;
  119. charge_status_update(CHARGE_STA_ON_CON_CURR); // constant current charging.
  120. } else {
  121. charge_ctrl.reach_const_curr_state_condition_debounce_cnt++;
  122. }
  123. charge_ctrl.reach_trick_curr_state_condition_debounce_cnt = 0;
  124. } else {
  125. if (charge_ctrl.reach_trick_curr_state_condition_debounce_cnt > 10) {
  126. charge_ctrl.reach_trick_curr_state_condition_debounce_cnt = 0;
  127. charge_status_update(CHARGE_STA_ON_TRICKLE); // trickle current charging.
  128. } else {
  129. charge_ctrl.reach_trick_curr_state_condition_debounce_cnt++;
  130. }
  131. charge_ctrl.reach_const_curr_state_condition_debounce_cnt = 0;
  132. }
  133. // if the charging is begin.
  134. } else if (charge_status.sta == CHARGE_STA_ON_TRICKLE) {
  135. if (PWRCON1 & (1 << 29)) {
  136. charge_ctrl.reach_const_curr_state_condition_debounce_cnt++;
  137. } else {
  138. charge_ctrl.reach_const_curr_state_condition_debounce_cnt = 0;
  139. }
  140. if (charge_ctrl.reach_const_curr_state_condition_debounce_cnt > 10) {
  141. charge_status_update(CHARGE_STA_ON_CON_CURR);
  142. }
  143. // if the charging at the state[con_curr, con_vol], detect stop condition.
  144. } else if (charge_status.sta > CHARGE_STA_ON_TRICKLE) {
  145. if (charge_detect_off_condition()) {
  146. charge_status_update(CHARGE_STA_OFF_BUT_DC_IN);
  147. }
  148. }
  149. }
  150. /**
  151. * @brief Update the charging status if needed, and perform relevant configuration.
  152. * @param status: the charging status that need to update.
  153. * this parameter can be one of the following values:
  154. * @arg CHARGE_STA_UNINIT: charge parameter uninitialized flag.
  155. * @arg CHARGE_STA_OFF: charge off flag.
  156. * @arg CHARGE_STA_OFF_BUT_DC_IN: charge off but VUSB still insert flag.
  157. * @arg CHARGE_STA_ON_TRICKLE: charge on trickle stage flag.
  158. * @arg CHARGE_STA_ON_CON_CURR: charge on const current stage flag.
  159. * @arg CHARGE_STA_ON_CON_VOL: charge on const voltage stage flag.
  160. * @retval None
  161. */
  162. void charge_status_update(u8 status)
  163. {
  164. u32 reg_tmp;
  165. if (status == CHARGE_STA_OFF) {
  166. // Restore VDDIO Trim
  167. PWRCON0 = (PWRCON0 & ~(0x0f<<5)) | (charge_ctrl.vddio_tmp << 5);
  168. // VUSB to VDDIO LDO Disable
  169. RTCCON8 &= ~(1 << 0);
  170. // Stop Charge
  171. RTCCON8 &= ~(1 << 6);
  172. RTCCON8 |= (1 << 1);
  173. } else if (status == CHARGE_STA_OFF_BUT_DC_IN) {
  174. if (charge_status.mode == CHARGE_MODE_FULL_DISCONNECT) {
  175. RTCCON8 |= (1 << 1);
  176. }
  177. } else if (status == CHARGE_STA_ON_TRICKLE || status == CHARGE_STA_ON_CON_CURR) {
  178. reg_tmp = RTCCON7;
  179. if ((charge_status.sta != CHARGE_STA_ON_TRICKLE) && (charge_status.sta != CHARGE_STA_ON_CON_CURR)) {
  180. charge_ctrl.vddio_tmp = (PWRCON0 >> 5) & 0x0f;
  181. }
  182. // Enable VUSB to VDDIO LDO
  183. RTCCON8 |= (1 << 0);
  184. if (status == CHARGE_STA_ON_TRICKLE) {
  185. // Config VUSB to VDDIO LDO Voltage,Ensure VUSB_VDDIO > VBAT_VDDIO
  186. // 2.6:0.1:3.3 0x02 means 2.8V
  187. RTCCON8 = (RTCCON8 & ~(0x07<<20)) | (2 << 20);
  188. PWRCON0 &= ~(0x0f << 5); //VBAT_VDDIO 2.4V
  189. reg_tmp &= ~(uint32_t)0x3f;
  190. reg_tmp |= charge_status.trickle_cur & 0x3f;
  191. } else {
  192. // Config VUSB to VDDIO LDO Voltage,Ensure VUSB_VDDIO > VBAT_VDDIO
  193. // 2.6:0.1:3.3 0x05 means 3.1V
  194. RTCCON8 = (RTCCON8 & ~(0x07<<20)) | (5 << 20);
  195. PWRCON0 = (PWRCON0 & ~(0x0f<<5)) | (4 << 5); //VBAT_VDDIO 2.8V
  196. reg_tmp &= ~(uint32_t)0x3f;
  197. reg_tmp |= charge_status.const_cur & 0x3f;
  198. }
  199. charge_ctrl.reach_cutoff_curr_debounce_cnt = 0;
  200. charge_ctrl.reach_cutoff_vol_debounce_cnt = 0;
  201. RTCCON7 = reg_tmp;
  202. RTCCON8 |= (0x01 << 6);
  203. RTCCON8 &= ~(0x01 << 1);
  204. } else if (status == CHARGE_STA_ON_CON_VOL) {
  205. RTCCON8 |= (0x01 << 6);
  206. RTCCON8 &= ~(0x01 << 1);
  207. }
  208. charge_status.sta = status;
  209. }
  210. /**
  211. * @brief detect charge off condition and enter const voltage stage condition.
  212. * @param None.
  213. * @retval true or false that can stop charge.
  214. */
  215. static bool charge_detect_off_condition(void)
  216. {
  217. if (charge_ctrl.reach_cutoff_debounce_cnt && (charge_status.sta != CHARGE_STA_ON_CON_VOL)) {
  218. charge_ctrl.reach_cutoff_debounce_cnt--;
  219. // judge if the charging current lower then setting val.
  220. if (PWRCON1 & (1 << 30)) { //I
  221. charge_ctrl.reach_cutoff_curr_debounce_cnt++;
  222. }
  223. // judge if the charging voltage higher then setting val.
  224. if (PWRCON1 & (1 << 28)) { //V
  225. charge_ctrl.reach_cutoff_vol_debounce_cnt++;
  226. }
  227. }
  228. if (charge_ctrl.reach_cutoff_curr_debounce_cnt >= 98) {
  229. return true;
  230. } else if (charge_ctrl.reach_cutoff_vol_debounce_cnt >= 98) {
  231. if (charge_status.sta != CHARGE_STA_ON_CON_VOL) {
  232. charge_status_update(CHARGE_STA_ON_CON_VOL);
  233. charge_ctrl.reach_cutoff_debounce_cnt = 0;
  234. }
  235. if (charge_ctrl.reach_cutoff_debounce_cnt == 0) {
  236. charge_ctrl.reach_cutoff_debounce_cnt = 100;
  237. charge_ctrl.reach_cutoff_curr_debounce_cnt = 0;
  238. } else {
  239. charge_ctrl.reach_cutoff_debounce_cnt--;
  240. if (PWRCON1 & (1 << 30)) { //I
  241. charge_ctrl.reach_cutoff_curr_debounce_cnt++;
  242. }
  243. }
  244. if (charge_ctrl.cutoff_vol_2_stop_time) {
  245. charge_ctrl.cutoff_vol_2_stop_time_cnt++;
  246. if (charge_ctrl.cutoff_vol_2_stop_time_cnt > charge_ctrl.cutoff_vol_2_stop_time) {
  247. charge_ctrl.cutoff_vol_2_stop_time_cnt = 0;
  248. return true;
  249. }
  250. }
  251. } else {
  252. if (charge_ctrl.reach_cutoff_debounce_cnt == 0) {
  253. charge_ctrl.reach_cutoff_debounce_cnt = 100;
  254. charge_ctrl.reach_cutoff_curr_debounce_cnt = 0;
  255. charge_ctrl.reach_cutoff_vol_debounce_cnt = 0;
  256. }
  257. }
  258. return false;
  259. }
  260. /**
  261. * @brief Update the charging status if needed, and perform relevant configuration.
  262. * @param None.
  263. * @retval the status of charge stage. the value will be one in the CHARGE_STATUS_TYPEDEF.
  264. */
  265. AT(.com_periph.charge)
  266. u8 charge_get_status(void) {
  267. return charge_status.sta;
  268. }
  269. /**
  270. * @brief select the event to do when charge is finish.
  271. * @param mode: charge mode.
  272. * this parameter can be one of the following values:
  273. * @arg CHARGE_MODE_FULL_KEEP: keep VUSB connect when charge finish.
  274. * @arg CHARGE_MODE_FULL_DISCONNECT: disconnect VUSB when charge finish.
  275. * @retval None
  276. */
  277. void charge_change_mode(u8 mode)
  278. {
  279. if ((mode == CHARGE_MODE_FULL_DISCONNECT) || (mode == CHARGE_MODE_FULL_KEEP)) {
  280. charge_status.mode = mode;
  281. }
  282. }
  283. /**
  284. * @brief Update the charging mode if needed, and perform relevant configuration.
  285. * @param None.
  286. * @retval mode of charge stage. the value will be one in the CHARGE_MODE_TYPEDEF.
  287. */
  288. u8 charge_get_mode(void)
  289. {
  290. return charge_status.mode;
  291. }
  292. /**
  293. * @brief get state VUSB insert.
  294. * @param None.
  295. * @retval VUSB insertion statue.
  296. */
  297. AT(.com_periph.charge)
  298. CHARGE_DC_STATUS_TYPEDEF charge_detect_dc(void)
  299. {
  300. u8 status = (RTCCON >> 19 & 0x02) | (RTCCON >> 22 & 0x01);
  301. if (status > 1) {
  302. return CHARGE_DC_STATUS_ONLINE;
  303. } else {
  304. return status & 0x03;
  305. }
  306. }
  307. /**
  308. * @brief Judge if the charging current reach the cutoff current.
  309. * @param None.
  310. * @retval the condition SET of RESET.
  311. */
  312. FLAG_STATE charge_cutoff_cur_condition(void)
  313. {
  314. if ((PWRCON1 & (1 << 30)) != RESET) {
  315. return SET;
  316. } else {
  317. return RESET;
  318. }
  319. }
  320. /**
  321. * @brief Judge if the charging voltage reach the cutoff voltage.
  322. * @param None.
  323. * @retval the condition SET of RESET.
  324. */
  325. FLAG_STATE charge_cutoff_vol_condition(void)
  326. {
  327. if ((PWRCON1 & (1 << 28)) != RESET) {
  328. return SET;
  329. } else {
  330. return RESET;
  331. }
  332. }
  333. /**
  334. * @brief Judge if the charging voltage reach the trickle voltage.
  335. * @param None.
  336. * @retval the condition SET of RESET.
  337. */
  338. FLAG_STATE charge_trickle_vol_condition(void)
  339. {
  340. if ((PWRCON1 & (1 << 29)) != RESET) {
  341. return SET;
  342. } else {
  343. return RESET;
  344. }
  345. }
  346. /**
  347. * @brief Set the time from the cut-off voltage to stop charging.
  348. * @param time, unit:100ms.
  349. */
  350. void charge_cutoff_vol_to_stop_time_set(uint16_t time)
  351. {
  352. charge_ctrl.cutoff_vol_2_stop_time = time;
  353. }
  354. /**
  355. * @brief Select charge current when power up charge.
  356. */
  357. void charge_pwrup_charge_cur_sel(CHARGE_PWRUP_CHARGE_CUR_TYPEDEF pwrup_charge_cur)
  358. {
  359. RTCCON7 = (RTCCON7 & ~(0x03<<6)) | (pwrup_charge_cur);
  360. }