ml5238.c 8.7 KB

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