ml5238.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <string.h>
  2. #include "libs/shark_libs.h"
  3. #include "spi.h"
  4. #include "ml5238.h"
  5. static int ml5238_read(uint8_t regaddr, uint8_t *data);
  6. static void ml5238_clear_bits(uint8_t regaddr, uint8_t bit);
  7. static void ml5238_set_bits(uint8_t regaddr, uint8_t bit);
  8. static void irq_hander_in_timer(shark_timer_t *timer);
  9. static ml5238_notify_hander _handler;
  10. static shark_timer_t irq_task = {.handler = irq_hander_in_timer};
  11. void ml5238_init(void){
  12. spi0_init();
  13. ml5238_softreset();
  14. }
  15. void ml5238_register_notify_handler(ml5238_notify_hander handler){
  16. _handler = handler;
  17. }
  18. //小电流打开等效discharger mos打开
  19. int ml5238_charger_is_disconnect(int small_current_on){
  20. uint8_t value = 0;
  21. uint8_t fet = 0;
  22. ml5238_read(ML5238_FET, &fet);
  23. ml5238_read(ML5238_PSENSE, &value);
  24. if ((fet & FET_DF) || small_current_on){
  25. return (value & PSENSE_PSL);
  26. }
  27. return (value & PSENSE_PSH);
  28. }
  29. int ml5238_enable_load_detect(int enable){
  30. ml5238_clear_bits(ML5238_RSENSE, RSENSE_RRS);
  31. if (enable){
  32. ml5238_set_bits(ML5238_RSENSE, RSENSE_ERS);
  33. }else {
  34. ml5238_clear_bits(ML5238_RSENSE, RSENSE_ERS | RSENSE_IRS);
  35. }
  36. return 0;
  37. }
  38. int ml5238_is_load_disconnect(void){
  39. uint8_t value = 0;
  40. ml5238_read(ML5238_RSENSE, &value);
  41. return (value & RSENSE_RS);
  42. }
  43. #define IRS_IRQ 1 //load disconnect中断
  44. #define IPSL_IRQ 2 //charger over current
  45. #define ICS_IRQ 3 //短路中断
  46. int ml5238_enable_irq(int enable, int irq){
  47. if (irq == IRS_IRQ){
  48. ml5238_clear_bits(ML5238_RSENSE, RSENSE_RRS);
  49. if (enable){
  50. ml5238_set_bits(ML5238_RSENSE, RSENSE_IRS);
  51. }else {
  52. ml5238_clear_bits(ML5238_RSENSE, RSENSE_IRS);
  53. }
  54. }
  55. if (irq == IPSL_IRQ){
  56. ml5238_clear_bits(ML5238_PSENSE, PSENSE_RPSL);
  57. if (enable){
  58. ml5238_set_bits(ML5238_PSENSE, PSENSE_IPSL);
  59. }else {
  60. ml5238_clear_bits(ML5238_PSENSE, PSENSE_IPSL);
  61. }
  62. }
  63. if (irq == ICS_IRQ){
  64. ml5238_clear_bits(ML5238_RSENSE, RSENSE_RSC);
  65. if (enable){
  66. ml5238_set_bits(ML5238_RSENSE, RSENSE_ISC);
  67. }else {
  68. ml5238_clear_bits(ML5238_RSENSE, RSENSE_ISC);
  69. }
  70. }
  71. return 0;
  72. }
  73. //小电流打开等效discharger mos打开
  74. int ml5238_enable_charger_detect(int small_current_on, int enable){
  75. uint8_t fet = 0;
  76. ml5238_read(ML5238_FET, &fet);
  77. if ((fet & FET_DF) || small_current_on){ //discharger is on, used to detect charger over current
  78. ml5238_clear_bits(ML5238_PSENSE, PSENSE_RPSL);
  79. if (enable){
  80. ml5238_set_bits(ML5238_PSENSE, PSENSE_EPSL);
  81. }else {
  82. ml5238_clear_bits(ML5238_PSENSE, PSENSE_EPSL);
  83. }
  84. }else { //discharger if off, used when powerdown, charger is insert
  85. ml5238_clear_bits(ML5238_PSENSE, PSENSE_RPSH);
  86. if (enable){
  87. ml5238_set_bits(ML5238_PSENSE, PSENSE_EPSH);
  88. }else {
  89. ml5238_clear_bits(ML5238_PSENSE, PSENSE_EPSH);
  90. }
  91. }
  92. return 0;
  93. }
  94. static int __inline__ _charger_mosfet_is_open(void){
  95. uint8_t data;
  96. ml5238_read(ML5238_FET, &data);
  97. return (data & FET_CF) != 0;
  98. }
  99. static int __inline__ _discharger_mosfet_is_open(void){
  100. uint8_t data;
  101. ml5238_read(ML5238_FET, &data);
  102. return (data & FET_DF) != 0;
  103. }
  104. int ml5238_is_charging(void){
  105. return _charger_mosfet_is_open();
  106. }
  107. int ml5238_is_discharging(void){
  108. return _discharger_mosfet_is_open();
  109. }
  110. void ml5238_cell_start_balance(uint16_t balance_mask){
  111. ml5238_write(ML5238_CBALH, (balance_mask >> 8) & 0xFF);
  112. ml5238_write(ML5238_CBALL, balance_mask & 0xFF);
  113. }
  114. int ml5238_enable_discharger_mosfet(int enable){
  115. uint8_t data;
  116. if (ml5238_read(ML5238_FET, &data) == 0){
  117. if ((data & FET_DF) == enable){
  118. return 0; //alread enable/disabled
  119. }
  120. data &= ~(FET_DF);
  121. if (enable){
  122. data |= (FET_DF | FET_DRV);
  123. }else {
  124. if ((data & FET_CF) == 0){
  125. data &= ~(FET_DRV);
  126. }
  127. }
  128. return ml5238_write(ML5238_FET, data);
  129. }
  130. return -1;
  131. }
  132. /* when enable charger the discharger mosfet also must be enabled for charging */
  133. int ml5238_enable_charger_mosfet(int enable){
  134. uint8_t data;
  135. if (ml5238_read(ML5238_FET, &data) == 0){
  136. if (((data & FET_CF) >> 1) == enable){
  137. return 0; //alread enable/disabled
  138. }
  139. data &= ~(FET_CF);
  140. if (enable){
  141. data |= (FET_CF | FET_DRV);
  142. }else {
  143. if ((data & FET_DF) == 0){
  144. data &= ~(FET_DRV);
  145. }
  146. }
  147. return ml5238_write(ML5238_FET, data);
  148. }
  149. return -1;
  150. }
  151. int ml5238_short_current_detect(int mode){
  152. uint8_t rsense = 0;
  153. if (mode >= SHORT_CURRENT_MODE_50A_100A){
  154. if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
  155. if (ml5238_write(ML5238_SETSC, mode) == 0){
  156. if (rsense & (RSENSE_ESC | RSENSE_ISC)){
  157. return 0; //already enabled short current detect
  158. }
  159. rsense |= (RSENSE_ESC | RSENSE_ISC);//enable short current detect && irq
  160. rsense &= ~RSENSE_RSC;
  161. return ml5238_write(ML5238_SETSC, rsense);
  162. }
  163. }
  164. }else {
  165. if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
  166. if ((rsense & RSENSE_ESC) == 0){
  167. return 0; //already disabled
  168. }
  169. rsense &= ~(RSENSE_ESC|RSENSE_ISC|RSENSE_RSC);
  170. return ml5238_write(ML5238_SETSC, rsense);
  171. }
  172. }
  173. return -1;
  174. }
  175. void ml5238_softreset(void)
  176. {
  177. unsigned char i;
  178. for(i=0u;i<0x0Au;i++)
  179. {
  180. ml5238_write((uint8_t)(ML5238_VMON + i), 0x00u);
  181. }
  182. }
  183. void ml5238_power_down(void){
  184. do {
  185. ml5238_write(ML5238_PSENSE, PSENSE_EPSH|PSENSE_IPSH); //before power down, we must enable charger detect
  186. ml5238_write(ML5238_POWER, POWER_PDWN);
  187. }while(1);
  188. }
  189. void ml5238_power_save(int save){
  190. if (save) {
  191. ml5238_write(ML5238_PSENSE, 0);
  192. ML5238_VMON_DISABLE();
  193. ML5238_IMON_DISABLE();
  194. ml5238_write(ML5238_POWER, POWER_PSV);
  195. ml5238_irq_enable(1); //enable charger detect irq, to wakeup bms when charger insert
  196. }else {
  197. ml5238_write(ML5238_POWER, 0);
  198. }
  199. }
  200. static void __inline__ call_handler(int event){
  201. if (_handler) {
  202. _handler(event);
  203. }
  204. }
  205. static void irq_hander_in_timer(shark_timer_t *timer){
  206. uint8_t status = 0;
  207. ml5238_read(ML5238_STATUS, &status);
  208. if (status & STATUS_RPSL){//chargering over current
  209. ml5238_enable_charger_detect(0, 0);
  210. ml5238_enable_charger_detect(1, 0);
  211. call_handler(ML5238_Event_Charger_Over_Current);
  212. }
  213. if (status & STATUS_RSC) { //short current detect, close charger/discharger mosfet
  214. if (_charger_mosfet_is_open()) {
  215. ml5238_enable_charger_mosfet(0);
  216. }
  217. if (_discharger_mosfet_is_open()) {
  218. ml5238_enable_discharger_mosfet(0);
  219. }
  220. ml5238_enable_irq(0, ICS_IRQ); //disable short current detect
  221. call_handler(ML5238_Event_Short_Current);
  222. }
  223. if (status & STATUS_RRS) {//load disconnect, if short detect, we must wait load disconnected, and then can open discharger
  224. ml5238_enable_irq(0, IRS_IRQ);
  225. call_handler(ML5238_Event_Load_Disconnect);
  226. }
  227. }
  228. void ml5238_irq_handler(void){
  229. shark_timer_post(&irq_task, 0);
  230. }
  231. static void ml5238_set_bits(uint8_t regaddr, uint8_t bit) {
  232. uint8_t value;
  233. ml5238_read(regaddr, &value);
  234. ml5238_write(regaddr, value|bit);
  235. }
  236. static void ml5238_clear_bits(uint8_t regaddr, uint8_t bit) {
  237. uint8_t value;
  238. ml5238_read(regaddr, &value);
  239. ml5238_write(regaddr, value&(~bit));
  240. }
  241. int ml5238_write(uint8_t regaddr, uint8_t data){
  242. uint16_t send_data=(((uint16_t)regaddr)<<(0x09))|((uint16_t)data);
  243. ml5238_cs(0);
  244. int ret = spi0_send_uint16(send_data, NULL);
  245. ml5238_cs(1);
  246. return ret;
  247. }
  248. static int ml5238_read(uint8_t regaddr, uint8_t *data){
  249. uint16_t send_data=((((uint16_t)regaddr)<<(0x09))|0x0100u)|((uint16_t)0x00u);
  250. ml5238_cs(0);
  251. int ret = spi0_send_uint16(send_data, data);
  252. ml5238_cs(1);
  253. return ret;
  254. }