|
|
@@ -0,0 +1,78 @@
|
|
|
+#include "battery.h"
|
|
|
+#include "ti/driverlib/dl_i2c.h"
|
|
|
+#include "ti_msp_dl_config.h"
|
|
|
+
|
|
|
+
|
|
|
+Battery batteries[NUM_SLOTS];
|
|
|
+
|
|
|
+// Permissible charge temperature for LiIon battery is 0.0 degree Celsius to 45.0 degree Celsius
|
|
|
+// Correct temp_threshold yet to be analyzed
|
|
|
+#define TEMP_THRESHOLD (460)
|
|
|
+//#define VOLTAGE_THRESHOLD ()
|
|
|
+
|
|
|
+uint16_t charge_change_threshold;
|
|
|
+
|
|
|
+/*Initialize battery array and default parameters*/
|
|
|
+void Battery_Init(){
|
|
|
+ for(uint8_t i=0; i< NUM_SLOTS; i++){
|
|
|
+ batteries[i].slot_id= i;
|
|
|
+ batteries[i].state= STATE_EMPTY;
|
|
|
+ batteries[i].voltage= 0;
|
|
|
+ batteries[i].current= 0;
|
|
|
+ batteries[i].temperature= 0;
|
|
|
+ batteries[i].min_voltage= 0;
|
|
|
+ batteries[i].max_voltage= 0;
|
|
|
+ batteries[i].cut_off_current= 0;
|
|
|
+ batteries[i].capacitance= 0;
|
|
|
+ batteries[i].charge_fraction= 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Battery_Discharge_SafetyCheck(uint8_t slot_id){
|
|
|
+ //As of now declared a varaible with 0, in future it will call a function to read the output volatge of the boost converter
|
|
|
+ uint16_t boost_vout_mv= 0;
|
|
|
+ DischargeSafetyCondition condition;
|
|
|
+ Battery *battery= &batteries[slot_id];
|
|
|
+
|
|
|
+ if(battery->temperature > TEMPERATURE_MAX_C){
|
|
|
+ battery->condition= STATE_OVERTEMPERATURE;
|
|
|
+
|
|
|
+ }
|
|
|
+ //if battery
|
|
|
+ else if(boost_vout_mv >= BOOST_HOV_THRESHOLD_MV-HYSTERISIS){
|
|
|
+ battery->condition= STATE_HOV;
|
|
|
+ }
|
|
|
+ else if(boost_vout_mv <= BOOST_SOV_THRESHOLD_MV-HYSTERISIS){
|
|
|
+ battery->condition= STATE_SOV;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ battery->condition= STATE_OK;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Battery_ReadState(uint8_t slot_id) {
|
|
|
+
|
|
|
+ uint16_t voltage_mv = batteries[slot_id].voltage;
|
|
|
+ uint16_t min_voltage = batteries[slot_id].min_voltage;
|
|
|
+ uint16_t max_voltage = batteries[slot_id].max_voltage;
|
|
|
+
|
|
|
+ /*
|
|
|
+ Added another state: STATE_WAITING_FOR_LIMITS (0x03) so that Pi is aware that battery limits needs to be sent to the MCU
|
|
|
+ */
|
|
|
+ if(min_voltage == 0 || max_voltage == 0){
|
|
|
+ batteries[slot_id].state= STATE_WAITING_FOR_LIMITS;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (voltage_mv < BATTERY_THRESHOLD) {
|
|
|
+ batteries[slot_id].state = STATE_EMPTY;
|
|
|
+ } else if (voltage_mv >= BATTERY_THRESHOLD && voltage_mv < min_voltage) {
|
|
|
+ batteries[slot_id].state = STATE_BATTERY_DETECTED;
|
|
|
+ } else if (voltage_mv >= min_voltage && voltage_mv < max_voltage) {
|
|
|
+ batteries[slot_id].state = STATE_MEASUREMENT_IN_PROGRESS;
|
|
|
+ } else {
|
|
|
+ batteries[slot_id].state = STATE_OVERHEATING;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|