| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include <string.h>
- #include "spi.h"
- #include "ml5238.h"
- static int ml5238_read(uint8_t regaddr, uint8_t *data);
- void ml5238_init(void){
- spi0_init();
- ml5238_softreset();
- }
- int ml5238_enable_discharger_mosfet(int enable){
- uint8_t data;
- if (ml5238_read(ML5238_FET, &data) == 0){
- if (data & FET_DF == enable){
- return 0; //alread enable/disabled
- }
- data &= ~(FET_DF);
- if (enable){
- data |= (FET_DF | FET_DRV);
- }else {
- if (data & FET_CF == 0){
- data &= ~(FET_DRV);
- }
- }
- return ml5238_write(ML5238_FET, data);
- }
- return -1;
- }
- /* when enable charger the discharger mosfet also must be enabled for charging */
- int ml5238_enable_charger_mosfet(int enable){
- uint8_t data;
- if (ml5238_read(ML5238_FET, &data) == 0){
- if (((data & FET_CF) >> 1) == enable){
- return 0; //alread enable/disabled
- }
- data &= ~(FET_CF);
- if (enable){
- data |= (FET_CF | FET_DRV);
- }else {
- if (data & FET_DF == 0){
- data &= ~(FET_DRV);
- }
- }
- return ml5238_write(ML5238_FET, data);
- }
- return -1;
- }
- int ml5238_short_current_detect(int mode){
- uint8_t rsense = 0;
- if (mode >= SHORT_CURRENT_MODE_33_3A){
- if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
- if (ml5238_write(ML5238_SETSC, mode) == 0){
- if (rsense & (RSENSE_ESC | RSENSE_ISC)){
- return 0; //already enabled short current detect
- }
- rsense |= (RSENSE_ESC | RSENSE_ISC);//enable short current detect && irq
- return ml5238_write(ML5238_SETSC, rsense);
- }
- }
- }else {
- if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
- if (rsense & RSENSE_ESC == 0){
- return 0; //already disabled
- }
- rsense &= ~(RSENSE_ESC|RSENSE_ISC);
- return ml5238_write(ML5238_SETSC, rsense);
- }
- }
- return -1;
- }
- void ml5238_softreset(void)
- {
- unsigned char i;
-
- for(i=0u;i<0x0Au;i++)
- {
- ml5238_write((uint8_t)(ML5238_VMON + i), 0x00u);
- }
- }
- void ml5238_irq_handler(void){
-
- }
- int ml5238_write(uint8_t regaddr, uint8_t data){
- uint16_t send_data=(((uint16_t)regaddr)<<(0x09))|((uint16_t)data);
- ml5238_cs(0);
- int ret = spi0_send_uint16(send_data, NULL);
- ml5238_cs(1);
- return ret;
- }
- static int ml5238_read(uint8_t regaddr, uint8_t *data){
- uint16_t send_data=((((uint16_t)regaddr)<<(0x09))|0x0100u)|((uint16_t)0x00u);
- ml5238_cs(0);
- int ret = spi0_send_uint16(send_data, data);
- ml5238_cs(1);
- return ret;
- }
|