ml5238.c 2.4 KB

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