Просмотр исходного кода

Bugfix: now the controller doesn't restart in the interrupt of the mcu

Heinrich Blatt 6 месяцев назад
Родитель
Сommit
4998bcf2d0
3 измененных файлов с 25 добавлено и 17 удалено
  1. 5 4
      main_target.c
  2. 18 12
      src/interfaces/i2c_target.c
  3. 2 1
      src/interfaces/i2c_target.h

+ 5 - 4
main_target.c

@@ -8,7 +8,7 @@
 #include "src/interfaces/i2c_controller.h"
 #include "src/config.h"
 
-volatile bool mcu_CommandPending= false;
+int8_t handle_read_pending_slot = -1;
 
 void I2C_controller_INST_IRQHandler(void) {
   switch (DL_I2C_getPendingInterrupt(I2C_controller_INST)) { 
@@ -79,7 +79,7 @@ void I2C_target_INST_IRQHandler(void) {
   case DL_I2C_IIDX_TARGET_STOP:
     break;
   case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
-    mcu_i2c_handle(I2C_target_INST);
+    handle_read_pending_slot = mcu_i2c_handle(I2C_target_INST);
     break;
   case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
     break;
@@ -107,8 +107,9 @@ int main(void)
     while (1) {
 
         for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
-            if (slot != 0) {
-                continue;
+            if (handle_read_pending_slot != -1) {
+                mcu_i2c_handle_read(I2C_target_INST, handle_read_pending_slot);
+                handle_read_pending_slot = -1;
             }
         
             // step 1: update the voltage readings

+ 18 - 12
src/interfaces/i2c_target.c

@@ -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);
 }

+ 2 - 1
src/interfaces/i2c_target.h

@@ -15,5 +15,6 @@ typedef enum{
 
 void initialize_target_address();
 
-void mcu_i2c_handle(I2C_Regs *i2c);
+int8_t mcu_i2c_handle(I2C_Regs *i2c);
+void mcu_i2c_handle_read(I2C_Regs *i2c, uint8_t slot);
 #endif