ml5238.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include <string.h>
  2. #include "libs/shark_libs.h"
  3. #include "spi.h"
  4. #include "ml5238.h"
  5. static shark_timer_t irq_task;
  6. static int ml5238_read(uint8_t regaddr, uint8_t *data);
  7. static void ml5238_clear_bits(uint8_t regaddr, uint8_t bit);
  8. static void ml5238_set_bits(uint8_t regaddr, uint8_t bit);
  9. static void irq_hander_in_timer(shark_timer_t *timer);
  10. static ml5238_notify_hander _handler;
  11. void ml5238_init(void){
  12. spi0_init();
  13. ml5238_softreset();
  14. irq_task.handler = irq_hander_in_timer;
  15. }
  16. void ml5238_register_notify_handler(ml5238_notify_hander handler){
  17. _handler = handler;
  18. }
  19. //used to detect charger over current
  20. int ml5238_charger_is_disconnect(void){
  21. uint8_t value = 0;
  22. uint8_t fet = 0;
  23. ml5238_read(ML5238_FET, &fet);
  24. ml5238_read(ML5238_PSENSE, &value);
  25. if (fet & FET_DF){
  26. return (value & PSENSE_PSH);
  27. }
  28. return (value & PSENSE_PSL);
  29. }
  30. int ml5238_enable_load_detect(int enable){
  31. ml5238_clear_bits(ML5238_RSENSE, RSENSE_RRS);
  32. if (enable){
  33. ml5238_set_bits(ML5238_RSENSE, RSENSE_ERS);
  34. }else {
  35. ml5238_clear_bits(ML5238_RSENSE, RSENSE_ERS | RSENSE_IRS);
  36. }
  37. return 0;
  38. }
  39. int ml5238_is_load_disconnect(void){
  40. uint8_t value = 0;
  41. ml5238_read(ML5238_RSENSE, &value);
  42. return (value & RSENSE_RS);
  43. }
  44. #define IRS_IRQ 1 //load disconnectÖжÏ
  45. #define IPSL_IRQ 2 //charger over current
  46. #define ICS_IRQ 3 //¶Ì·ÖжÏ
  47. int ml5238_enable_irq(int enable, int irq){
  48. if (irq == IRS_IRQ){
  49. ml5238_clear_bits(ML5238_RSENSE, RSENSE_RRS);
  50. if (enable){
  51. ml5238_set_bits(ML5238_RSENSE, RSENSE_IRS);
  52. }else {
  53. ml5238_clear_bits(ML5238_RSENSE, RSENSE_IRS);
  54. }
  55. }
  56. if (irq == IPSL_IRQ){
  57. ml5238_clear_bits(ML5238_PSENSE, PSENSE_RPSL);
  58. if (enable){
  59. ml5238_set_bits(ML5238_PSENSE, PSENSE_IPSL);
  60. }else {
  61. ml5238_clear_bits(ML5238_PSENSE, PSENSE_IPSL);
  62. }
  63. }
  64. if (irq == ICS_IRQ){
  65. ml5238_clear_bits(ML5238_RSENSE, RSENSE_RSC);
  66. if (enable){
  67. ml5238_set_bits(ML5238_RSENSE, RSENSE_ISC);
  68. }else {
  69. ml5238_clear_bits(ML5238_RSENSE, RSENSE_ISC);
  70. }
  71. }
  72. return 0;
  73. }
  74. int ml5238_enable_charger_detect(int enable){
  75. uint8_t fet = 0;
  76. ml5238_read(ML5238_FET, &fet);
  77. if (fet & FET_DF){ //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. ml5238_write(ML5238_PSENSE, PSENSE_EPSH|PSENSE_IPSH);
  191. if (save) {
  192. ml5238_write(ML5238_POWER, POWER_PSV);
  193. ml5238_irq_enable(1); //enable charger detect irq, to wakeup bms when charger insert
  194. }else {
  195. ml5238_write(ML5238_POWER, 0);
  196. }
  197. }
  198. static void __inline__ call_handler(int event){
  199. if (_handler) {
  200. _handler(event);
  201. }
  202. }
  203. static void irq_hander_in_timer(shark_timer_t *timer){
  204. uint8_t status = 0;
  205. ml5238_read(ML5238_STATUS, &status);
  206. if (status & STATUS_RPSL){//chargering over current
  207. ml5238_enable_charger_detect(0);
  208. call_handler(ML5238_Event_Charger_Over_Current);
  209. }
  210. if (status & STATUS_RSC) { //short current detect, close charger/discharger mosfet
  211. if (_charger_mosfet_is_open()) {
  212. ml5238_enable_charger_mosfet(0);
  213. }
  214. if (_discharger_mosfet_is_open()) {
  215. ml5238_enable_discharger_mosfet(0);
  216. }
  217. ml5238_enable_irq(0, ICS_IRQ); //disable short current detect
  218. call_handler(ML5238_Event_Short_Current);
  219. }
  220. if (status & STATUS_RRS) {//load disconnect, if short detect, we must wait load disconnected, and then can open discharger
  221. ml5238_enable_irq(0, IRS_IRQ);
  222. call_handler(ML5238_Event_Load_Disconnect);
  223. }
  224. }
  225. void ml5238_irq_handler(void){
  226. shark_timer_post(&irq_task, 0);
  227. }
  228. static void ml5238_set_bits(uint8_t regaddr, uint8_t bit) {
  229. uint8_t value;
  230. ml5238_read(regaddr, &value);
  231. ml5238_write(regaddr, value|bit);
  232. }
  233. static void ml5238_clear_bits(uint8_t regaddr, uint8_t bit) {
  234. uint8_t value;
  235. ml5238_read(regaddr, &value);
  236. ml5238_write(regaddr, value&(~bit));
  237. }
  238. int ml5238_write(uint8_t regaddr, uint8_t data){
  239. uint16_t send_data=(((uint16_t)regaddr)<<(0x09))|((uint16_t)data);
  240. ml5238_cs(0);
  241. int ret = spi0_send_uint16(send_data, NULL);
  242. ml5238_cs(1);
  243. return ret;
  244. }
  245. static int ml5238_read(uint8_t regaddr, uint8_t *data){
  246. uint16_t send_data=((((uint16_t)regaddr)<<(0x09))|0x0100u)|((uint16_t)0x00u);
  247. ml5238_cs(0);
  248. int ret = spi0_send_uint16(send_data, data);
  249. ml5238_cs(1);
  250. return ret;
  251. }