|
@@ -36,14 +36,12 @@ static void batteryslots_init() {
|
|
|
// initialize data structures
|
|
// initialize data structures
|
|
|
battery_slots[0].timer = PWM_0_INST;
|
|
battery_slots[0].timer = PWM_0_INST;
|
|
|
battery_slots[0].adc_addr = ADC_TARGET_BASE_ADDRESS;
|
|
battery_slots[0].adc_addr = ADC_TARGET_BASE_ADDRESS;
|
|
|
-#if NUM_SLOTS == 4
|
|
|
|
|
battery_slots[1].timer = PWM_1_INST;
|
|
battery_slots[1].timer = PWM_1_INST;
|
|
|
battery_slots[1].adc_addr = ADC_TARGET_BASE_ADDRESS+4;
|
|
battery_slots[1].adc_addr = ADC_TARGET_BASE_ADDRESS+4;
|
|
|
battery_slots[2].timer = PWM_2_INST;
|
|
battery_slots[2].timer = PWM_2_INST;
|
|
|
battery_slots[2].adc_addr = ADC_TARGET_BASE_ADDRESS+2;
|
|
battery_slots[2].adc_addr = ADC_TARGET_BASE_ADDRESS+2;
|
|
|
battery_slots[3].timer = PWM_3_INST;
|
|
battery_slots[3].timer = PWM_3_INST;
|
|
|
battery_slots[3].adc_addr = ADC_TARGET_BASE_ADDRESS+6;
|
|
battery_slots[3].adc_addr = ADC_TARGET_BASE_ADDRESS+6;
|
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
for(uint8_t i=0; i< NUM_SLOTS; i++){
|
|
for(uint8_t i=0; i< NUM_SLOTS; i++){
|
|
|
|
|
|
|
@@ -144,22 +142,12 @@ static void batteryslots_read_state(uint8_t slot) {
|
|
|
|
|
|
|
|
static void batteryslots_adjust_current(uint8_t slot) {
|
|
static void batteryslots_adjust_current(uint8_t slot) {
|
|
|
|
|
|
|
|
-#ifdef DEBUG_TRACE_CTRL
|
|
|
|
|
- printf("Ctrl: Checking to adjust battery current. Set current: %d mA, measured current: %d mA\n",
|
|
|
|
|
- battery_slots[slot].set_current,
|
|
|
|
|
- battery_slots[slot].measurement.current
|
|
|
|
|
- );
|
|
|
|
|
-#endif
|
|
|
|
|
-
|
|
|
|
|
if (battery_slots[slot].set_current > 0) {
|
|
if (battery_slots[slot].set_current > 0) {
|
|
|
// positive current -> charge (with DAC)
|
|
// positive current -> charge (with DAC)
|
|
|
|
|
|
|
|
if (battery_slots[slot].pwm_value != 0) {
|
|
if (battery_slots[slot].pwm_value != 0) {
|
|
|
// seems like we switched from a charging before
|
|
// seems like we switched from a charging before
|
|
|
// -> disable DAC before getting active
|
|
// -> disable DAC before getting active
|
|
|
-#ifdef DEBUG_CTRL
|
|
|
|
|
- printf("Detected active pwm. Value: %d. Disabling.", battery_slots[slot].pwm_value);
|
|
|
|
|
-#endif
|
|
|
|
|
set_pwm(slot, 0);
|
|
set_pwm(slot, 0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -168,16 +156,10 @@ static void batteryslots_adjust_current(uint8_t slot) {
|
|
|
// exceeded to the upper limit
|
|
// exceeded to the upper limit
|
|
|
// -> update dac value, decrease the voltage
|
|
// -> update dac value, decrease the voltage
|
|
|
if (battery_slots[slot].dac_value-1 >= 0) {
|
|
if (battery_slots[slot].dac_value-1 >= 0) {
|
|
|
-#ifdef DEBUG_TRACE_CTRL
|
|
|
|
|
- printf("Ctrl: Decreasing DAC to %d\n", battery_slots[slot].dac_value-1);
|
|
|
|
|
-#endif
|
|
|
|
|
set_dac(slot, --battery_slots[slot].dac_value);
|
|
set_dac(slot, --battery_slots[slot].dac_value);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
// we want to give more current, but we can't ?!
|
|
// we want to give more current, but we can't ?!
|
|
|
-#ifdef DEBUG_CTRL
|
|
|
|
|
- printf("Ctrl: Unable to decrease DAC to %d\n", battery_slots[slot].dac_value-1);
|
|
|
|
|
-#endif
|
|
|
|
|
*battery_slots[slot].state = SLOT_WARN_LOWER_DAC_NOT_POSSIBLE;
|
|
*battery_slots[slot].state = SLOT_WARN_LOWER_DAC_NOT_POSSIBLE;
|
|
|
}
|
|
}
|
|
|
} else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
|
|
} else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
|
|
@@ -185,16 +167,10 @@ static void batteryslots_adjust_current(uint8_t slot) {
|
|
|
// exceeded to the upplowerer limit
|
|
// exceeded to the upplowerer limit
|
|
|
// -> update dac value, increase the voltage
|
|
// -> update dac value, increase the voltage
|
|
|
if (battery_slots[slot].dac_value+1 <= MAX_DAC_VALUE) {
|
|
if (battery_slots[slot].dac_value+1 <= MAX_DAC_VALUE) {
|
|
|
-#ifdef DEBUG_TRACE_CTRL
|
|
|
|
|
- printf("Ctrl: Increasing DAC to %d\n", battery_slots[slot].dac_value+1);
|
|
|
|
|
-#endif
|
|
|
|
|
set_dac(slot, ++battery_slots[slot].dac_value);
|
|
set_dac(slot, ++battery_slots[slot].dac_value);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
// we want to give more current, but we can't ?!
|
|
// we want to give more current, but we can't ?!
|
|
|
-#ifdef DEBUG_CTRL
|
|
|
|
|
- printf("Ctrl: Unable to increase DAC to %d\n", battery_slots[slot].dac_value+1);
|
|
|
|
|
-#endif
|
|
|
|
|
*battery_slots[slot].state = SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE;
|
|
*battery_slots[slot].state = SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -204,9 +180,6 @@ static void batteryslots_adjust_current(uint8_t slot) {
|
|
|
if (battery_slots[slot].dac_value != 0) {
|
|
if (battery_slots[slot].dac_value != 0) {
|
|
|
// seems like we switched from a charging before
|
|
// seems like we switched from a charging before
|
|
|
// -> disable DAC before getting active
|
|
// -> disable DAC before getting active
|
|
|
-#ifdef DEBUG_CTRL
|
|
|
|
|
- printf("Detected active dac. Value: %d. Disabling.", battery_slots[slot].dac_value);
|
|
|
|
|
-#endif
|
|
|
|
|
set_dac(slot, 0);
|
|
set_dac(slot, 0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -217,16 +190,10 @@ static void batteryslots_adjust_current(uint8_t slot) {
|
|
|
|
|
|
|
|
if (battery_slots[slot].pwm_value+1 <= MAX_PWM_CYCLE) {
|
|
if (battery_slots[slot].pwm_value+1 <= MAX_PWM_CYCLE) {
|
|
|
// pwm is inverse to the DAC since dragging more current means more negative
|
|
// pwm is inverse to the DAC since dragging more current means more negative
|
|
|
-#ifdef DEBUG_TRACE_CTRL
|
|
|
|
|
- printf("Ctrl: Increasing PWM to %d\n", battery_slots[slot].pwm_value+1);
|
|
|
|
|
-#endif
|
|
|
|
|
set_pwm(slot, ++battery_slots[slot].pwm_value);
|
|
set_pwm(slot, ++battery_slots[slot].pwm_value);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
// we want to give more current, but we can't ?!
|
|
// we want to give more current, but we can't ?!
|
|
|
-#ifdef DEBUG_CTRL
|
|
|
|
|
- printf("Ctrl: Unable to increase PWM to %d\n", battery_slots[slot].pwm_value+1);
|
|
|
|
|
-#endif
|
|
|
|
|
*battery_slots[slot].state = SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE;
|
|
*battery_slots[slot].state = SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE;
|
|
|
}
|
|
}
|
|
|
} else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
|
|
} else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
|
|
@@ -234,24 +201,15 @@ static void batteryslots_adjust_current(uint8_t slot) {
|
|
|
// exceeded to the upplowerer limit
|
|
// exceeded to the upplowerer limit
|
|
|
// -> update pwm value, increase the voltage
|
|
// -> update pwm value, increase the voltage
|
|
|
if (battery_slots[slot].pwm_value-1 >= 0) {
|
|
if (battery_slots[slot].pwm_value-1 >= 0) {
|
|
|
-#ifdef DEBUG_TRACE_CTRL
|
|
|
|
|
- printf("Ctrl: Decreasing PWM to %d\n", battery_slots[slot].pwm_value-1);
|
|
|
|
|
-#endif
|
|
|
|
|
set_pwm(slot, --battery_slots[slot].pwm_value);
|
|
set_pwm(slot, --battery_slots[slot].pwm_value);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
// we want to give more current, but we can't ?!
|
|
// we want to give more current, but we can't ?!
|
|
|
-#ifdef DEBUG_CTRL
|
|
|
|
|
- printf("Ctrl: Unable to decrease PWM to %d\n", battery_slots[slot].pwm_value-1);
|
|
|
|
|
-#endif
|
|
|
|
|
*battery_slots[slot].state = SLOT_WARN_LOWER_PWM_NOT_POSSIBLE;
|
|
*battery_slots[slot].state = SLOT_WARN_LOWER_PWM_NOT_POSSIBLE;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
// we have 0 -> stop charging and discharging
|
|
// we have 0 -> stop charging and discharging
|
|
|
-#ifdef DEBUG_CTRL
|
|
|
|
|
- printf("0 current -> Disabling slot.\n");
|
|
|
|
|
-#endif
|
|
|
|
|
batteryslots_disable(slot);
|
|
batteryslots_disable(slot);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|