ml5238.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. void ml5238_cell_start_balance(uint16_t balance_mask){
  105. ml5238_write(ML5238_CBALH, (balance_mask >> 8) & 0xFF);
  106. ml5238_write(ML5238_CBALL, balance_mask & 0xFF);
  107. }
  108. int ml5238_enable_discharger_mosfet(int enable){
  109. uint8_t data;
  110. if (ml5238_read(ML5238_FET, &data) == 0){
  111. if ((data & FET_DF) == enable){
  112. return 0; //alread enable/disabled
  113. }
  114. data &= ~(FET_DF);
  115. if (enable){
  116. data |= (FET_DF | FET_DRV);
  117. }else {
  118. if ((data & FET_CF) == 0){
  119. data &= ~(FET_DRV);
  120. }
  121. }
  122. return ml5238_write(ML5238_FET, data);
  123. }
  124. return -1;
  125. }
  126. /* when enable charger the discharger mosfet also must be enabled for charging */
  127. int ml5238_enable_charger_mosfet(int enable){
  128. uint8_t data;
  129. if (ml5238_read(ML5238_FET, &data) == 0){
  130. if (((data & FET_CF) >> 1) == enable){
  131. return 0; //alread enable/disabled
  132. }
  133. data &= ~(FET_CF);
  134. if (enable){
  135. data |= (FET_CF | FET_DRV);
  136. }else {
  137. if ((data & FET_DF) == 0){
  138. data &= ~(FET_DRV);
  139. }
  140. }
  141. return ml5238_write(ML5238_FET, data);
  142. }
  143. return -1;
  144. }
  145. int ml5238_short_current_detect(int mode){
  146. uint8_t rsense = 0;
  147. if (mode >= SHORT_CURRENT_MODE_33_3A){
  148. if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
  149. if (ml5238_write(ML5238_SETSC, mode) == 0){
  150. if (rsense & (RSENSE_ESC | RSENSE_ISC)){
  151. return 0; //already enabled short current detect
  152. }
  153. rsense |= (RSENSE_ESC | RSENSE_ISC);//enable short current detect && irq
  154. rsense &= ~RSENSE_RSC;
  155. return ml5238_write(ML5238_SETSC, rsense);
  156. }
  157. }
  158. }else {
  159. if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
  160. if ((rsense & RSENSE_ESC) == 0){
  161. return 0; //already disabled
  162. }
  163. rsense &= ~(RSENSE_ESC|RSENSE_ISC|RSENSE_RSC);
  164. return ml5238_write(ML5238_SETSC, rsense);
  165. }
  166. }
  167. return -1;
  168. }
  169. void ml5238_softreset(void)
  170. {
  171. unsigned char i;
  172. for(i=0u;i<0x0Au;i++)
  173. {
  174. ml5238_write((uint8_t)(ML5238_VMON + i), 0x00u);
  175. }
  176. }
  177. void ml5238_power_down(void){
  178. do {
  179. ml5238_write(ML5238_PSENSE, PSENSE_EPSH|PSENSE_IPSH); //before power down, we must enable charger detect
  180. ml5238_write(ML5238_POWER, POWER_PDWN);
  181. }while(1);
  182. }
  183. static void __inline__ call_handler(int event){
  184. if (_handler) {
  185. _handler(event);
  186. }
  187. }
  188. static void irq_hander_in_timer(shark_timer_t *timer){
  189. uint8_t status = 0;
  190. ml5238_read(ML5238_STATUS, &status);
  191. if (status & STATUS_RPSL){//chargering over current
  192. ml5238_enable_charger_detect(0);
  193. call_handler(ML5238_Event_Charger_Over_Current);
  194. }
  195. if (status & STATUS_RSC) { //short current detect, close charger/discharger mosfet
  196. if (_charger_mosfet_is_open()) {
  197. ml5238_enable_charger_mosfet(0);
  198. }
  199. if (_discharger_mosfet_is_open()) {
  200. ml5238_enable_discharger_mosfet(0);
  201. }
  202. ml5238_enable_irq(0, ICS_IRQ); //disable short current detect
  203. call_handler(ML5238_Event_Short_Current);
  204. }
  205. if (status & STATUS_RRS) {//load disconnect, if short detect, we must wait load disconnected, and then can open discharger
  206. ml5238_enable_irq(0, IRS_IRQ);
  207. call_handler(ML5238_Event_Load_Disconnect);
  208. }
  209. }
  210. void ml5238_irq_handler(void){
  211. shark_timer_post(&irq_task, 0);
  212. }
  213. static void ml5238_set_bits(uint8_t regaddr, uint8_t bit) {
  214. uint8_t value;
  215. ml5238_read(regaddr, &value);
  216. ml5238_write(regaddr, value|bit);
  217. }
  218. static void ml5238_clear_bits(uint8_t regaddr, uint8_t bit) {
  219. uint8_t value;
  220. ml5238_read(regaddr, &value);
  221. ml5238_write(regaddr, value&(~bit));
  222. }
  223. int ml5238_write(uint8_t regaddr, uint8_t data){
  224. uint16_t send_data=(((uint16_t)regaddr)<<(0x09))|((uint16_t)data);
  225. ml5238_cs(0);
  226. int ret = spi0_send_uint16(send_data, NULL);
  227. ml5238_cs(1);
  228. return ret;
  229. }
  230. static int ml5238_read(uint8_t regaddr, uint8_t *data){
  231. uint16_t send_data=((((uint16_t)regaddr)<<(0x09))|0x0100u)|((uint16_t)0x00u);
  232. ml5238_cs(0);
  233. int ret = spi0_send_uint16(send_data, data);
  234. ml5238_cs(1);
  235. return ret;
  236. }