#include "battery.h" #include "ti/driverlib/dl_i2c.h" #include "ti/driverlib/dl_timerg.h" #include "ti/driverlib/dl_comp.h" #include "ti_msp_dl_config.h" #include "src/config.h" BatterySlot slot; static void set_dac(uint16_t value) { slot.dac_value = value; DL_COMP_setDACCode0(COMP_0_INST, value); } static void set_pwm(uint16_t value) { slot.pwm_value = value; DL_TimerG_setCaptureCompareValue(PWM_INST, value, DL_TIMER_CC_1_INDEX); if (value > 0 && !DL_TimerG_isRunning(PWM_INST)) { DL_TimerG_startCounter(PWM_INST); } if (value == 0 && DL_TimerG_isRunning(PWM_INST)) { DL_TimerG_stopCounter(PWM_INST); } } void slot_disable() { if (slot.dac_value != 0) { set_dac(0); } if (slot.pwm_value != 0) { set_pwm(0); } } /*Initialize battery array and default parameters*/ void slot_init() { slot.measurement.state = SLOT_STATE_OK; // convinience trick: // with that we can set *battery_slots[i].state = SLOT_STATE_* or SLOT_ERR_* // like e.g. *battery_slots[i].state = SLOT_ERR_OVERTEMPERATURE slot.state = &slot.measurement.state; slot.measurement.voltage = 0; slot.measurement.current = 0; slot.measurement.temperature = 0; slot.set_current = 0; set_pwm(0); set_dac(0); } void slot_read_state() { ; } void slot_adjust_current() { if (slot.set_current > 0) { // positive current -> charge (with DAC) if (slot.pwm_value != 0) { // seems like we switched from a charging before // -> disable DAC before getting active set_pwm(0); } if (slot.set_current + BATTERY_CURRENT_THRESHOLD < slot.measurement.current) { // we are outside of the tolerance band // exceeded to the upper limit // -> update dac value, decrease the voltage if (slot.dac_value-1 >= 0) { set_dac(--slot.dac_value); } else { // we want to give more current, but we can't ?! *slot.state = SLOT_WARN_LOWER_DAC_NOT_POSSIBLE; } } else if (slot.set_current - BATTERY_CURRENT_THRESHOLD > slot.measurement.current) { // we are outside of the tolerance band // exceeded to the upplowerer limit // -> update dac value, increase the voltage if (slot.dac_value+1 <= MAX_DAC_VALUE) { set_dac(++slot.dac_value); } else { // we want to give more current, but we can't ?! *slot.state = SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE; } } // no else statement here: we are ok, since we are in the tolerance measure } else if (slot.set_current < 0) { // negative current -> discharge (with PWM) if (slot.dac_value != 0) { // seems like we switched from a charging before // -> disable DAC before getting active set_dac(0); } if (slot.set_current + BATTERY_CURRENT_THRESHOLD < slot.measurement.current) { // we are outside of the tolerance band // exceeded to the upper limit // -> update pwm value, decrease the voltage if (slot.pwm_value+1 <= MAX_PWM_CYCLE) { // pwm is inverse to the DAC since dragging more current means more negative set_pwm(++slot.pwm_value); } else { // we want to give more current, but we can't ?! *slot.state = SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE; } } else if (slot.set_current - BATTERY_CURRENT_THRESHOLD > slot.measurement.current) { // we are outside of the tolerance band // exceeded to the upplowerer limit // -> update pwm value, increase the voltage if (slot.pwm_value-1 >= 0) { set_pwm(--slot.pwm_value); } else { // we want to give more current, but we can't ?! *slot.state = SLOT_WARN_LOWER_PWM_NOT_POSSIBLE; } } } else { // we have 0 -> stop charging and discharging slot_disable(); } }