|
|
@@ -32,24 +32,19 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
return;
|
|
|
}
|
|
|
- // Struct for voltage, current and temperature
|
|
|
- BatteryMeasurementData battery_measure;
|
|
|
- // Battery *battery= &batteries[slot];
|
|
|
- // take the updated battery measurement from the battery struct and store
|
|
|
- // it in the battery_measure struct
|
|
|
- battery_measure.voltage = batteries[slot].voltage;
|
|
|
- battery_measure.current = batteries[slot].current;
|
|
|
- battery_measure.temperature = batteries[slot].temperature;
|
|
|
+
|
|
|
// Copying the memory block from battery_measure struct to tx_buffer:
|
|
|
- memcpy(tx_buffer, &battery_measure, sizeof(BatteryMeasurementData));
|
|
|
- DL_I2C_fillTargetTXFIFO(i2c, tx_buffer, sizeof(BatteryMeasurementData));
|
|
|
+ // @todo check if this memcpy is even needed, probably
|
|
|
+ // &battery_slots[slot].measurement can be directly used as buffer for DL_I2C_fillTargetTXFIFO
|
|
|
+ memcpy(tx_buffer, &battery_slots[slot].measurement, sizeof(BatteryMeasurement));
|
|
|
+ DL_I2C_fillTargetTXFIFO(i2c, tx_buffer, sizeof(BatteryMeasurement));
|
|
|
printf("Battery Measurement Sent to MCU. \n");
|
|
|
DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
+
|
|
|
} else if (receivedCommand == CMD_SET_CURRENT) {
|
|
|
- SetChargeDischargeCurrent set_current;
|
|
|
// Read incoming bytes from the Controller:
|
|
|
uint8_t rx_index = 0;
|
|
|
- while (rx_index < sizeof(SetChargeDischargeCurrent)+1) {
|
|
|
+ while (rx_index < 4) {
|
|
|
// TODO: Need to have a workaround, currently the code is getting stuck on
|
|
|
// the first trigger and provides result on the second trigger
|
|
|
if (!DL_I2C_isTargetRXFIFOEmpty(i2c)) {
|
|
|
@@ -57,30 +52,25 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
rx_index++;
|
|
|
}
|
|
|
}
|
|
|
- printf("index:%d\n", rx_index);
|
|
|
- // Byte array received from the Controller will be typecasted to (const
|
|
|
- // uint8_t *), treats the rx_buffer as an array of READ ONLY bytes because
|
|
|
- // of the const
|
|
|
- if (rx_index != sizeof(SetChargeDischargeCurrent)+1) {
|
|
|
- printf("ERROR: Incomplete I2C Rx: received %d%zu bytes\n", rx_index,
|
|
|
- sizeof(SetChargeDischargeCurrent)+1);
|
|
|
+ // we expect 4 bytes:
|
|
|
+ // 1. command
|
|
|
+ // 2. slot_id
|
|
|
+ // 3 + 4. current (int16)
|
|
|
+ if (rx_index != 4) {
|
|
|
+ printf("ERROR: Incomplete I2C Rx: received %d%zu bytes\n", rx_index, 4);
|
|
|
DL_I2C_flushTargetRXFIFO(i2c);
|
|
|
rx_index = 0;
|
|
|
return;
|
|
|
}
|
|
|
- printf("size: %d", sizeof(SetChargeDischargeCurrent));
|
|
|
- memcpy(&set_current, (const uint8_t *)rx_buffer+1,
|
|
|
- sizeof(SetChargeDischargeCurrent));
|
|
|
- uint8_t slot = set_current.slot_id;
|
|
|
- int16_t current = set_current.current;
|
|
|
- printf("Slot id: %d, Current: %" SCNd16 "\n", slot, current);
|
|
|
- if (current >= 0) {
|
|
|
- DAC_SingleWrite(slot, current);
|
|
|
- } else if (current < 0) {
|
|
|
+ uint8_t slot = rx_buffer[1]; // first byte is the slot id (0..3)
|
|
|
+ battery_slots[slot].set_current = *((rx_buffer)+2); // byte 3+4 is the current
|
|
|
+ printf("Slot id: %d, Current: %" SCNd16 "\n", slot, battery_slots[slot].set_current);
|
|
|
+ if (battery_slots[slot].set_current >= 0) {
|
|
|
+ DAC_SingleWrite(slot, battery_slots[slot].set_current);
|
|
|
+ } else if (battery_slots[slot].set_current < 0) {
|
|
|
|
|
|
DL_TimerG_startCounter(PWM_0_INST);
|
|
|
- DL_TimerG_setCaptureCompareValue(PWM_0_INST, -1*current, DL_TIMER_CC_0_INDEX); // update ccr0 value
|
|
|
- //DL_TimerG_setCaptureCompareValue(PWM_0_INST, 1000, DL_TIMER_CC_1_INDEX);
|
|
|
+ DL_TimerG_setCaptureCompareValue(PWM_0_INST, -1*battery_slots[slot].set_current, DL_TIMER_CC_0_INDEX); // update ccr0 value
|
|
|
|
|
|
} else {
|
|
|
// do nothing, charge or discharge
|