ml5238.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <string.h>
  2. #include "spi.h"
  3. #include "ml5238.h"
  4. #include "ml5238_reg.h"
  5. static int ml5238_write(uint8_t regaddr, uint8_t data);
  6. static int ml5238_read(uint8_t regaddr, uint8_t *data);
  7. void ml5238_init(void){
  8. spi0_init();
  9. ml5238_softreset();
  10. }
  11. /* disable the vmon output the cell voltage */
  12. int ml5238_disable_vmon(void){
  13. return ml5238_write(ML5238_VMON, 0x00);
  14. }
  15. /* select one cell (0-14) connect to vmon, so we can measure
  16. * the cell voltage from vmon pin
  17. */
  18. int ml5238_select_cell_to_vmon(int cell){
  19. return ml5238_write(ML5238_VMON, cell | VMON_OUT);
  20. }
  21. int ml5238_enable_discharger_mosfet(int enable){
  22. uint8_t data;
  23. if (ml5238_read(ML5238_FET, &data) == 0){
  24. if (data & FET_DF == enable){
  25. return 0; //alread enable/disabled
  26. }
  27. data &= ~(FET_DF);
  28. if (enable){
  29. data |= (FET_DF | FET_DRV);
  30. }
  31. return ml5238_write(ML5238_FET, data);
  32. }
  33. return -1;
  34. }
  35. /* when enable charger the discharger mosfet also must be enabled for charging */
  36. int ml5238_enable_charger_mosfet(int enable){
  37. uint8_t data;
  38. if (ml5238_read(ML5238_FET, &data) == 0){
  39. if (((data & FET_CF) >> 1) == enable){
  40. return 0; //alread enable/disabled
  41. }
  42. data &= ~(FET_CF);
  43. if (enable){
  44. data |= (FET_CF | FET_DRV);
  45. }
  46. return ml5238_write(ML5238_FET, data);
  47. }
  48. return -1;
  49. }
  50. int ml5238_short_current_detect(int mode){
  51. uint8_t rsense = 0;
  52. if (mode >= SHORT_CURRENT_MODE_33_3A){
  53. if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
  54. if (ml5238_write(ML5238_SETSC, mode) == 0){
  55. if (rsense & (RSENSE_ESC | RSENSE_ISC)){
  56. return 0; //already enabled short current detect
  57. }
  58. rsense |= (RSENSE_ESC | RSENSE_ISC);//enable short current detect && irq
  59. return ml5238_write(ML5238_SETSC, rsense);
  60. }
  61. }
  62. }else {
  63. if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
  64. if (rsense & RSENSE_ESC == 0){
  65. return 0; //already disabled
  66. }
  67. rsense &= ~(RSENSE_ESC|RSENSE_ISC);
  68. return ml5238_write(ML5238_SETSC, rsense);
  69. }
  70. }
  71. return -1;
  72. }
  73. void ml5238_softreset(void)
  74. {
  75. unsigned char i;
  76. for(i=0u;i<0x0Au;i++)
  77. {
  78. ml5238_write((uint8_t)(ML5238_VMON + i), 0x00u);
  79. }
  80. }
  81. void ml5238_irq_handler(void){
  82. }
  83. static int ml5238_write(uint8_t regaddr, uint8_t data){
  84. uint16_t send_data=(((uint16_t)regaddr)<<(0x08+1u))|((uint16_t)data);
  85. ml5238_cs(0);
  86. int ret = spi0_send_uint16(send_data, NULL);
  87. ml5238_cs(1);
  88. return ret;
  89. }
  90. static int ml5238_read(uint8_t regaddr, uint8_t *data){
  91. uint16_t send_data=((((uint16_t)regaddr)<<(0x08+1u))|0x0100u)|((uint16_t)0x00u);
  92. ml5238_cs(0);
  93. int ret = spi0_send_uint16(send_data, data);
  94. ml5238_cs(1);
  95. return ret;
  96. }