|
|
@@ -33,9 +33,9 @@ void initialize_target_address() {
|
|
|
// The code has multiple i2c instances (multiple MCUs connected) from which we
|
|
|
// need to select the right one, passing a pointer as an argument
|
|
|
|
|
|
-void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
+int8_t mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
if (DL_I2C_isTargetRXFIFOEmpty(i2c)) {
|
|
|
- return;
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
uint8_t receivedByte = DL_I2C_receiveTargetData(i2c);
|
|
|
@@ -46,9 +46,23 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
DL_I2C_fillTargetTXFIFO(i2c, (uint8_t *)&battery_slots[slot].measurement, 8);
|
|
|
//wait until all bytes are written and sent to the Controller
|
|
|
- while(!(DL_I2C_isTargetTXFIFOEmpty(i2c)));
|
|
|
+ uint16_t timeout = 32000;
|
|
|
+ while(!(DL_I2C_isTargetTXFIFOEmpty(i2c)) && timeout-- > 0);
|
|
|
} else if (receivedCommand == CMD_SET_CURRENT) {
|
|
|
- // Read incoming bytes from the Controller:
|
|
|
+ return slot;
|
|
|
+ } else if (receivedCommand == CMD_CLEAR_ERR) {
|
|
|
+ if (slot > NUM_SLOTS) {
|
|
|
+ DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ *battery_slots[slot].state = SLOT_STATE_OK;
|
|
|
+ }
|
|
|
+ DL_I2C_flushTargetRXFIFO(i2c);
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+void mcu_i2c_handle_read(I2C_Regs *i2c, uint8_t slot) {
|
|
|
+ // Read incoming bytes from the Controller:
|
|
|
uint8_t rx_index = 0;
|
|
|
uint8_t rx_buffer[2] = {0x00, 0x00};
|
|
|
while (rx_index < 2) {
|
|
|
@@ -88,12 +102,4 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
;
|
|
|
#endif
|
|
|
} */
|
|
|
- } else if (receivedCommand == CMD_CLEAR_ERR) {
|
|
|
- if (slot > NUM_SLOTS) {
|
|
|
- DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
- return;
|
|
|
- }
|
|
|
- *battery_slots[slot].state = SLOT_STATE_OK;
|
|
|
- }
|
|
|
- DL_I2C_flushTargetRXFIFO(i2c);
|
|
|
}
|