فهرست منبع

update ml5238

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 سال پیش
والد
کامیت
9b70960015
5فایلهای تغییر یافته به همراه122 افزوده شده و 4 حذف شده
  1. 20 0
      Application/app/iv_measure.c
  2. 5 0
      Application/app/iv_measure.h
  3. 5 0
      Application/bsp/irqs.c
  4. 78 2
      Application/bsp/ml5238.c
  5. 14 2
      Application/bsp/ml5238.h

+ 20 - 0
Application/app/iv_measure.c

@@ -0,0 +1,20 @@
+#include "iv_measure.h"
+#include "bsp/ml5238.h"
+#include "bsp/cs1180.h"
+/* measure the current & voltage for battery pack by using
+ * ms5238 & cs1180(only used when bms is in small current loading)
+*/
+
+/* this is the inited gain set to the ms5238, but the really gain is  calibrated
+*  by measure_system_calibrate
+*/
+#define IMON_INITED_GAIN 10
+static float imon_gain = IMON_INITED_GAIN;
+
+/*calibrate when startup && temperature is changed more than 5? degree
+* calibrate the ms5238's IMON output voltage gain
+*/
+int measure_system_calibrate(void){
+
+}
+

+ 5 - 0
Application/app/iv_measure.h

@@ -0,0 +1,5 @@
+#ifndef _IV_Measure_H__
+#define _IV_Measure_H__
+
+#endif /* _IV_Measure_H__ */
+

+ 5 - 0
Application/bsp/irqs.c

@@ -88,6 +88,10 @@ void EXTI2_3_IRQHandler(void){
 	}	
 	}	
 }
 }
 
 
+void __weak ml5238_irq_handler(void){
+
+}
+
 void EXTI4_15_IRQHandler(void){
 void EXTI4_15_IRQHandler(void){
 	if(RESET != exti_interrupt_flag_get(EXTI_4)){
 	if(RESET != exti_interrupt_flag_get(EXTI_4)){
 		exti_interrupt_flag_clear(EXTI_4);
 		exti_interrupt_flag_clear(EXTI_4);
@@ -109,6 +113,7 @@ void EXTI4_15_IRQHandler(void){
 	}		
 	}		
 	if(RESET != exti_interrupt_flag_get(EXTI_10)){
 	if(RESET != exti_interrupt_flag_get(EXTI_10)){
 		exti_interrupt_flag_clear(EXTI_10);
 		exti_interrupt_flag_clear(EXTI_10);
+		ml5238_irq_handler();
 	}
 	}
 	if(RESET != exti_interrupt_flag_get(EXTI_11)){
 	if(RESET != exti_interrupt_flag_get(EXTI_11)){
 		exti_interrupt_flag_clear(EXTI_11);
 		exti_interrupt_flag_clear(EXTI_11);

+ 78 - 2
Application/bsp/ml5238.c

@@ -3,11 +3,83 @@
 #include "ml5238.h"
 #include "ml5238.h"
 #include "ml5238_reg.h"
 #include "ml5238_reg.h"
 
 
+static int ml5238_write(uint8_t regaddr, uint8_t data);
+static int ml5238_read(uint8_t regaddr, uint8_t *data);
+
+
 void ml5238_init(void){
 void ml5238_init(void){
 	spi0_init();
 	spi0_init();
 	ml5238_softreset();
 	ml5238_softreset();
 }
 }
 
 
+/* disable the vmon output the cell voltage */
+int ml5238_disable_vmon(void){
+	return ml5238_write(ML5238_VMON, 0x00);
+}
+
+/* select one cell (0-14) connect to vmon, so we can measure 
+ * the cell voltage from vmon pin
+*/
+int ml5238_select_cell_to_vmon(int cell){
+	return ml5238_write(ML5238_VMON, cell | VMON_OUT);
+}
+
+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);
+		}
+		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);
+		}
+		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)
 void ml5238_softreset(void)
 {
 {
 	unsigned char i;
 	unsigned char i;
@@ -18,8 +90,12 @@ void ml5238_softreset(void)
 	}
 	}
 }
 }
 
 
+void ml5238_irq_handler(void){
+	
+}
+
 
 
-int ml5238_write(uint8_t regaddr, uint8_t data){
+static int ml5238_write(uint8_t regaddr, uint8_t data){
 	uint16_t send_data=(((uint16_t)regaddr)<<(0x08+1u))|((uint16_t)data);
 	uint16_t send_data=(((uint16_t)regaddr)<<(0x08+1u))|((uint16_t)data);
 	ml5238_cs(0);
 	ml5238_cs(0);
 	int ret = spi0_send_uint16(send_data, NULL);
 	int ret = spi0_send_uint16(send_data, NULL);
@@ -27,7 +103,7 @@ int ml5238_write(uint8_t regaddr, uint8_t data){
 	return ret;
 	return ret;
 }
 }
 
 
-int ml5238_read(uint8_t regaddr, uint8_t *data){
+static int ml5238_read(uint8_t regaddr, uint8_t *data){
 	uint16_t send_data=((((uint16_t)regaddr)<<(0x08+1u))|0x0100u)|((uint16_t)0x00u);
 	uint16_t send_data=((((uint16_t)regaddr)<<(0x08+1u))|0x0100u)|((uint16_t)0x00u);
 	ml5238_cs(0);
 	ml5238_cs(0);
 	int ret = spi0_send_uint16(send_data, data);
 	int ret = spi0_send_uint16(send_data, data);

+ 14 - 2
Application/bsp/ml5238.h

@@ -2,8 +2,20 @@
 #define _ML5238_H__
 #define _ML5238_H__
 void ml5238_init(void);
 void ml5238_init(void);
 void ml5238_softreset(void);
 void ml5238_softreset(void);
-int ml5238_write(uint8_t regaddr, uint8_t data);
-int ml5238_read(uint8_t regaddr, uint8_t *data);
+int ml5238_enable_discharger_mosfet(int enable);
+int ml5238_enable_charger_mosfet(int enable);
+int ml5238_short_current_detect(int mode);
 
 
+
+#define SHORT_CURRENT_MODE_DISABLE -1
+#define SHORT_CURRENT_MODE_33_3A 0
+#define SHORT_CURRENT_MODE_66_6A 1
+#define SHORT_CURRENT_MODE_100A 2
+#define SHORT_CURRENT_MODE_133_3A 3
+
+/* ml5238 will amplify the vmon val by 0.5, so we need 
+ * multi 2 to the adc value, to get the real vol 
+ */
+#define cell_real_vol(v) (v * 2)
 #endif /* _ML5238_H__ */
 #endif /* _ML5238_H__ */