Pārlūkot izejas kodu

move i2c message handling to separate function

Heinrich Blatt 7 mēneši atpakaļ
vecāks
revīzija
2fdb625a22
3 mainītis faili ar 11 papildinājumiem un 65 dzēšanām
  1. 1 28
      main_target.c
  2. 9 36
      src/interfaces/i2c_target.c
  3. 1 1
      src/interfaces/i2c_target.h

+ 1 - 28
main_target.c

@@ -72,34 +72,15 @@ void I2C_controller_INST_IRQHandler(void) {
 void I2C_target_INST_IRQHandler(void) {
 void I2C_target_INST_IRQHandler(void) {
 
 
   uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
   uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
-  //printf("interrupt %d", status);
-  //uint8_t slot= ((DL_I2C_receiveTargetData(I2C_target_INST) & 0xF0)>>4);
   switch (status) {
   switch (status) {
   case DL_I2C_IIDX_TARGET_START:
   case DL_I2C_IIDX_TARGET_START:
     break;
     break;
   case DL_I2C_IIDX_TARGET_STOP:
   case DL_I2C_IIDX_TARGET_STOP:
     break;
     break;
   case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
   case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
-    if (DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
-      return;
-    }
-        
-    uint8_t receivedByte = DL_I2C_receiveTargetData(I2C_target_INST);
-    uint8_t receivedCommand = (receivedByte & 0x0F);
-    uint8_t slot = ((receivedByte & 0xF0) >> 4);
-
-    if (receivedCommand == CMD_GET_MEASUREMENT) {
-        DL_I2C_flushTargetTXFIFO(I2C_target_INST);
-        DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_slots[slot].measurement, sizeof(BatteryMeasurement));
-    }
-
-    //mcu_CommandPending= true;
+    mcu_i2c_handle(I2C_target_INST);
     break;
     break;
   case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
   case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
-    
-    //mcu_CommandPending= true;
-    //DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_slots[slot].measurement, sizeof(BatteryMeasurement));
-    
     break;
     break;
   case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
   case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
     break;
     break;
@@ -121,14 +102,6 @@ int main(void)
     while (1) {
     while (1) {
 
 
         for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
         for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
-
-            if(mcu_CommandPending){
-#ifdef DEBUG_TARGET
-                printf("Calling MCU target action\n");
-#endif
-                mcu_i2c_handle(I2C_target_INST);
-                mcu_CommandPending = false;
-            }
         
         
             // step 1: update the voltage readings
             // step 1: update the voltage readings
             battery_slotmgr.read_state(slot);
             battery_slotmgr.read_state(slot);

+ 9 - 36
src/interfaces/i2c_target.c

@@ -34,54 +34,27 @@ void initialize_target_address() {
 // need to select the right one, passing a pointer as an argument
 // need to select the right one, passing a pointer as an argument
 
 
 void mcu_i2c_handle(I2C_Regs *i2c) {
 void mcu_i2c_handle(I2C_Regs *i2c) {
+    if (DL_I2C_isTargetRXFIFOEmpty(i2c)) {
+      return;
+    }
+        
     uint8_t receivedByte = DL_I2C_receiveTargetData(i2c);
     uint8_t receivedByte = DL_I2C_receiveTargetData(i2c);
     uint8_t receivedCommand = (receivedByte & 0x0F);
     uint8_t receivedCommand = (receivedByte & 0x0F);
     uint8_t slot = ((receivedByte & 0xF0) >> 4);
     uint8_t slot = ((receivedByte & 0xF0) >> 4);
 
 
-#ifdef DEBUG_TARGET
-    printf("[SLAVE] Received Byte: 0x%02X\n", receivedByte);
-#endif
-    uint8_t tx_buffer[8] = {0};
-    // changed to volatile variable, so that the compiler cannot optimize the
-    // variable out and is forced to do as told by the code
-    volatile uint8_t rx_buffer[8] = {0};
-    /*Handling GET commands with bitmasking*/
-    // GET command for ADC(Battery Measurement): Voltage, Current, Temperature
     if (receivedCommand == CMD_GET_MEASUREMENT) {
     if (receivedCommand == CMD_GET_MEASUREMENT) {
-        uint32_t timeout= 10000;  
-        if (slot > NUM_SLOTS) {
-            DL_I2C_flushTargetTXFIFO(i2c);
-            return;
-        }
-        while(!(DL_I2C_getTargetStatus(i2c) & DL_I2C_TARGET_STATUS_BUS_BUSY) && (timeout--));
-        if(timeout==0){
-            DL_I2C_reset(i2c);
-            return;
-        }
-       
+        DL_I2C_flushTargetTXFIFO(i2c);
         DL_I2C_fillTargetTXFIFO(i2c, (uint8_t *)&battery_slots[slot].measurement, sizeof(BatteryMeasurement));
         DL_I2C_fillTargetTXFIFO(i2c, (uint8_t *)&battery_slots[slot].measurement, sizeof(BatteryMeasurement));
-        //DL_I2C_enableInterrupt(i2c, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER);
-    
-/*#ifdef DEBUG_TARGET
-        if(DL_I2C_isTargetTXFIFOFull(i2c)== true){
-            printf("TX FIFO FULL: Battery Measurement Sent to MCU. \n");
-        }else if(DL_I2C_isTargetTXFIFOEmpty(i2c)== true){
-            printf("TX FIFO EMPTY: Battery Measurement is not done being sent.\n");
-        }
-        
-#endif*/
-        //printf("");
-        //DL_I2C_flushTargetTXFIFO(i2c);
-  
     } else if (receivedCommand == CMD_SET_CURRENT) {
     } else if (receivedCommand == CMD_SET_CURRENT) {
         // Read incoming bytes from the Controller:
         // Read incoming bytes from the Controller:
         uint8_t rx_index = 0;
         uint8_t rx_index = 0;
+        uint8_t rx_buffer[2] = {0x00, 0x00};
         while (rx_index < 2) {
         while (rx_index < 2) {
             // TODO: Need to have a workaround, currently the code is getting stuck on
             // TODO: Need to have a workaround, currently the code is getting stuck on
             // the first trigger and provides result on the second trigger
             // the first trigger and provides result on the second trigger
             if (!DL_I2C_isTargetRXFIFOEmpty(i2c)) {
             if (!DL_I2C_isTargetRXFIFOEmpty(i2c)) {
-              rx_buffer[rx_index] = DL_I2C_receiveTargetData(i2c);
-              rx_index++;
+                rx_buffer[rx_index] = DL_I2C_receiveTargetData(i2c);
+                rx_index++;
             }
             }
         }
         }
         // we expect 3 bytes:
         // we expect 3 bytes:
@@ -116,7 +89,7 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
             ;
             ;
 #endif
 #endif
         } */
         } */
-    } else if (receivedCommand == CMD_CELAR_ERR) {
+    } else if (receivedCommand == CMD_CLEAR_ERR) {
         if (slot > NUM_SLOTS) {
         if (slot > NUM_SLOTS) {
             DL_I2C_flushTargetTXFIFO(i2c);
             DL_I2C_flushTargetTXFIFO(i2c);
             return;
             return;

+ 1 - 1
src/interfaces/i2c_target.h

@@ -10,7 +10,7 @@
 typedef enum{
 typedef enum{
     CMD_SET_CURRENT= 0x05,
     CMD_SET_CURRENT= 0x05,
     CMD_GET_MEASUREMENT= 0x06, 
     CMD_GET_MEASUREMENT= 0x06, 
-    CMD_CELAR_ERR= 0x07
+    CMD_CLEAR_ERR= 0x07
 }mcu_I2C_command;
 }mcu_I2C_command;
 
 
 void initialize_target_address();
 void initialize_target_address();