|
@@ -14,6 +14,7 @@ static shark_timer_t irq_task = {.handler = irq_hander_in_timer};
|
|
|
void ml5238_init(void){
|
|
void ml5238_init(void){
|
|
|
spi0_init();
|
|
spi0_init();
|
|
|
ml5238_softreset();
|
|
ml5238_softreset();
|
|
|
|
|
+ ml5238_irq_enable(1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ml5238_register_notify_handler(ml5238_notify_hander handler){
|
|
void ml5238_register_notify_handler(ml5238_notify_hander handler){
|
|
@@ -27,7 +28,6 @@ int ml5238_charger_is_disconnect(int small_current_on){
|
|
|
uint8_t fet = 0;
|
|
uint8_t fet = 0;
|
|
|
ml5238_read(ML5238_FET, &fet);
|
|
ml5238_read(ML5238_FET, &fet);
|
|
|
ml5238_read(ML5238_PSENSE, &value);
|
|
ml5238_read(ML5238_PSENSE, &value);
|
|
|
- sys_error("fet 0x%x, PSENSE 0x%x\n", fet, value);
|
|
|
|
|
if ((fet & FET_DF) || small_current_on){
|
|
if ((fet & FET_DF) || small_current_on){
|
|
|
return (value & PSENSE_PSL);
|
|
return (value & PSENSE_PSL);
|
|
|
}
|
|
}
|
|
@@ -177,26 +177,41 @@ int ml5238_short_current_detect(int mode){
|
|
|
if (mode >= SHORT_CURRENT_MODE_50A_100A){
|
|
if (mode >= SHORT_CURRENT_MODE_50A_100A){
|
|
|
if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
|
|
if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
|
|
|
if (ml5238_write(ML5238_SETSC, mode) == 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
|
|
rsense |= (RSENSE_ESC | RSENSE_ISC);//enable short current detect && irq
|
|
|
rsense &= ~RSENSE_RSC;
|
|
rsense &= ~RSENSE_RSC;
|
|
|
- return ml5238_write(ML5238_SETSC, rsense);
|
|
|
|
|
|
|
+ return ml5238_write(ML5238_RSENSE, rsense);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}else {
|
|
}else {
|
|
|
if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
|
|
if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
|
|
|
- if ((rsense & RSENSE_ESC) == 0){
|
|
|
|
|
- return 0; //already disabled
|
|
|
|
|
- }
|
|
|
|
|
rsense &= ~(RSENSE_ESC|RSENSE_ISC|RSENSE_RSC);
|
|
rsense &= ~(RSENSE_ESC|RSENSE_ISC|RSENSE_RSC);
|
|
|
- return ml5238_write(ML5238_SETSC, rsense);
|
|
|
|
|
|
|
+ return ml5238_write(ML5238_RSENSE, rsense);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+int ml5238_is_short_current_enabled(int mode){
|
|
|
|
|
+ uint8_t value = 0;
|
|
|
|
|
+ if (ml5238_read(ML5238_SETSC, &value) < 0){
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (value != mode) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ value = 0;
|
|
|
|
|
+ if (ml5238_read(ML5238_RSENSE, &value) < 0){
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ((value & (RSENSE_ESC | RSENSE_ISC)) != (RSENSE_ESC | RSENSE_ISC)){
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (value & RSENSE_RSC){
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void ml5238_softreset(void)
|
|
void ml5238_softreset(void)
|
|
|
{
|
|
{
|
|
|
unsigned char i;
|
|
unsigned char i;
|
|
@@ -236,18 +251,20 @@ static void irq_hander_in_timer(shark_timer_t *timer){
|
|
|
uint8_t status = 0;
|
|
uint8_t status = 0;
|
|
|
ml5238_read(ML5238_STATUS, &status);
|
|
ml5238_read(ML5238_STATUS, &status);
|
|
|
if (status & STATUS_RPSL){//chargering over current
|
|
if (status & STATUS_RPSL){//chargering over current
|
|
|
|
|
+ sys_error("charger over current\n");
|
|
|
ml5238_enable_charger_detect(0, 0);
|
|
ml5238_enable_charger_detect(0, 0);
|
|
|
ml5238_enable_charger_detect(1, 0);
|
|
ml5238_enable_charger_detect(1, 0);
|
|
|
call_handler(ML5238_Event_Charger_Over_Current);
|
|
call_handler(ML5238_Event_Charger_Over_Current);
|
|
|
}
|
|
}
|
|
|
if (status & STATUS_RSC) { //short current detect, close charger/discharger mosfet
|
|
if (status & STATUS_RSC) { //short current detect, close charger/discharger mosfet
|
|
|
|
|
+ sys_error("short current\n");
|
|
|
if (_charger_mosfet_is_open()) {
|
|
if (_charger_mosfet_is_open()) {
|
|
|
ml5238_enable_charger_mosfet(0);
|
|
ml5238_enable_charger_mosfet(0);
|
|
|
}
|
|
}
|
|
|
if (_discharger_mosfet_is_open()) {
|
|
if (_discharger_mosfet_is_open()) {
|
|
|
ml5238_enable_discharger_mosfet(0);
|
|
ml5238_enable_discharger_mosfet(0);
|
|
|
}
|
|
}
|
|
|
- ml5238_enable_irq(0, ICS_IRQ); //disable short current detect
|
|
|
|
|
|
|
+ ml5238_short_current_detect(SHORT_CURRENT_MODE_DISABLE);
|
|
|
call_handler(ML5238_Event_Short_Current);
|
|
call_handler(ML5238_Event_Short_Current);
|
|
|
}
|
|
}
|
|
|
if (status & STATUS_RRS) {//load disconnect, if short detect, we must wait load disconnected, and then can open discharger
|
|
if (status & STATUS_RRS) {//load disconnect, if short detect, we must wait load disconnected, and then can open discharger
|