Browse Source

pi read adc voltage for Slot 0: 0x00 0x02 0xc0 0x03 0x00 0x00 0x00 0x00

namrota ghosh 10 tháng trước cách đây
mục cha
commit
3ff3afa918
10 tập tin đã thay đổi với 153 bổ sung285 xóa
  1. 2 1
      .gitignore
  2. 27 8
      adc.c
  3. 2 3
      adc.h
  4. 6 0
      adc_driver.c
  5. 8 0
      adc_driver.h
  6. 3 0
      battery.c
  7. 1 2
      battery.h
  8. 97 235
      i2c_controller.c
  9. 6 36
      i2c_target.c
  10. 1 0
      i2c_target.h

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
 .json
-.idx
+.idx
+Debug

+ 27 - 8
adc.c

@@ -13,7 +13,6 @@ uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];
 
 uint32_t gTxADClen, gTxADCcount;
 uint32_t gRxADClen, gRxADCcount;
-
 static ADC_PARAMS adc_params;
 static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE;
 
@@ -61,7 +60,6 @@ uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params) {
 /* Tansmit Data from MCU to ADC:  Function to SET configuration to ADC over
  * I2C*/
 
-
 void ADC_SetConfigurationBytes(ADC_PARAMS params) {
   // **Construct Configuration Byte**
   uint8_t config_byte = ADC_ConstructConfigBytes(params);
@@ -79,13 +77,13 @@ void ADC_SetConfigurationBytes(ADC_PARAMS params) {
   gTxComplete = false;
 
   DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
-  DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t*) &gTxPacket, 1);
+  DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t *)&gTxPacket, 1);
   DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS,
                                  DL_I2C_CONTROLLER_DIRECTION_TX, gTxADClen);
-  //DL_I2C_enableInterrupt(I2C_controller_INST,
-  //                       DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
-  // while(!gTxComplete);
-  //  **Ensure STOP Condition is Sent**
+  // DL_I2C_enableInterrupt(I2C_controller_INST,
+  //                        DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
+  //  while(!gTxComplete);
+  //   **Ensure STOP Condition is Sent**
   while (DL_I2C_getControllerStatus(I2C_controller_INST) &
          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
     ;
@@ -314,7 +312,6 @@ void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
                batteries[slot].current);
 
         adc_state = ADC_STATE_DONE;
-
       }
       break;
     default:
@@ -326,3 +323,25 @@ void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
 
   adc_state = ADC_STATE_CONFIGURE;
 }
+
+void Battery_ReadState(uint8_t slot_id) {
+
+  uint16_t voltage_mv = batteries[slot_id].voltage;
+  uint16_t min_voltage = batteries[slot_id].min_voltage;
+  uint16_t max_voltage = batteries[slot_id].max_voltage;
+
+  /* Changing the battery states based on adc values*/
+  if (voltage_mv < 500) {
+    batteries[slot_id].state = STATE_EMPTY;
+  } else if (voltage_mv >= 500 && voltage_mv < 3000) { //TODO: change the voltage_mv to min_voltage
+    batteries[slot_id].state = STATE_BATTERY_DETECTED;
+  } else if (voltage_mv >= min_voltage && voltage_mv < max_voltage) {
+    batteries[slot_id].state = STATE_MEASUREMENT_IN_PROGRESS;
+  }
+  // once MCU is done reading ADC:
+  else if (adc_state== ADC_STATE_DONE && batteries[slot_id].state== STATE_MEASUREMENT_IN_PROGRESS) {
+    batteries[slot_id].state = STATE_MEASUREMENT_DONE;
+  } else {
+    batteries[slot_id].state = STATE_OVERCHARGING;
+  }
+}

+ 2 - 3
adc.h

@@ -6,8 +6,7 @@
 #define ADC_TARGET_BASE_ADDRESS (0x68)
 #define ADC_VREF_MV (2048)
 #define DELAY (100000) //define timeout limit
-
-//defining the configurations
+#define ADC_CHANNEL_NUM (2)
 
 //Maximum packet sizes
 #define I2C_TX_MAX_PACKET_SIZE (16)
@@ -45,5 +44,5 @@ int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params);
 void Battery_UpdateVoltage(ADC_PARAMS params);
 void Battery_UpdateCurrent(ADC_PARAMS params);
 void Battery_UpdateADCReading(uint8_t slot, uint8_t channel);
-
+void Battery_ReadState(uint8_t slot_id);
 #endif

+ 6 - 0
adc_driver.c

@@ -0,0 +1,6 @@
+#include "adc_driver.h"
+#include "adc.h"
+#include "ti_msp_dl_config.h"
+#include "ti/driverlib/dl_i2c.h"
+
+

+ 8 - 0
adc_driver.h

@@ -0,0 +1,8 @@
+#ifndef ADC_DRIVER_H_
+#define ADC_DRIVER_H_
+#include<stdint.h>
+#include<stdbool.h>
+
+
+
+#endif

+ 3 - 0
battery.c

@@ -1,4 +1,6 @@
 #include "battery.h"
+#include "ti/driverlib/dl_i2c.h"
+#include "ti_msp_dl_config.h"
 
 Battery batteries[NUM_SLOTS];
 
@@ -37,3 +39,4 @@ void Battery_SafetyCheck(uint8_t slot){
 }
 
 
+

+ 1 - 2
battery.h

@@ -6,7 +6,7 @@
 //for testing
 #define NUM_SLOTS (1)
 #define BATTERY_THRESHOLD (50)
-#define ADC_CHANNEL_NUM (2)
+
 
 //Battery states
 typedef enum{
@@ -24,7 +24,6 @@ typedef enum{
 typedef struct{
     uint8_t slot_id;
     BatteryState state;
-    uint8_t channel;
     uint16_t voltage;
     int16_t current;
     uint16_t temperature;

+ 97 - 235
i2c_controller.c

@@ -120,266 +120,125 @@ void I2C_controller_INST_IRQHandler(void) {
 }
 
 /**** Interrupt for Pi to MCU ****/
-
 void I2C_target_INST_IRQHandler(void) {
-
-  // printf("I2C Interrupt Triggered to MCU (TARGET)!\n");
   uint8_t receivedCommand = 0;
+  uint8_t battery_state[8] = {0x00};
   uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
-  // ADC_PARAMS params;
+  printf("status: %d\n", status);
   switch (status) {
-
-  /* START condition detected */
   case DL_I2C_IIDX_TARGET_START:
+    /*Initialize Tx or RX after Start condition is recieved*/
     piTxCount = 0;
     piRxCount = 0;
     piTxComplete = false;
     DL_I2C_flushTargetTXFIFO(I2C_target_INST);
     break;
-
-  /* STOP condition detected */
   case DL_I2C_IIDX_TARGET_STOP:
     piTxComplete = true;
     piRxComplete = true;
     DL_I2C_flushTargetTXFIFO(I2C_target_INST);
+    DL_I2C_flushTargetRXFIFO(I2C_target_INST);
     break;
-
-    /* TX FIFO trigger (Pi is reading data from MCU) */
-    /* GET battery status is triggered when command is 0x01
-       - Pi on request of 0x01 will get a response of the battery status for all
-    the slots
-       - Battery_StateUpdate function is called, which in turn calls the
-    Battery_ReadState funtion to set the state of the batteries -Pi on command
-    of [0x02, slot_id] will GET the 'Battery Data' which is voltage, current and
-    temperature for a given slot.
-        - MCU reads the slot_id from Pi using DL_I2C_receiveTargetData()
-        - piTxCount is set to 0
-        - piTxLen is the sizeof BatteryData struct which is 7 bytes
-        - If the requested slot is correct then:
-            - battery pointer variable points to the memory of the requested
-    slot
-            - the values of voltage, current and temperature are then stored in
-    battery_data struct
-        - Once the values are in BatteryData struct we wait for the bus to be
-    free
-        - Next we send the BatteryData to Pi using DL_I2C_fillTargetRXFIFO()
-        - Reset the RX counter for the next data.
-    */
-
-  case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
-    break;
-    if (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
-      receivedCommand = DL_I2C_receiveTargetData(I2C_target_INST);
-      // printf("Received Command: 0x%02X\n", receivedCommand);
-
-       else {
-          /*
-           * Fill FIFO with 0x00 if more data is requested than expected piTxLen
-           */
-          while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
-            ;
-        }
-        piTxComplete = true;
-      } else if (receivedCommand == CMD_GET_BATTERY_DATA) {
-
-        uint8_t requestedSlot = DL_I2C_receiveTargetData(I2C_target_INST);
-        while (DL_I2C_getTargetStatus(I2C_target_INST) &
-               DL_I2C_TARGET_STATUS_BUS_BUSY)
-          ;
-        // printf("Battery Data Requested for Slot %d!\n", requestedSlot);
-        piTxCount = 0;
-        piTxLen = sizeof(BatteryData);
-        BatteryData battery_data;
-        if (requestedSlot < NUM_SLOTS) {
-          Battery *battery = &batteries[requestedSlot];
-          battery_data.slot_id = battery->slot_id;
-          battery_data.voltage = battery->voltage;
-          battery_data.current = battery->current;
-          battery_data.temperature = battery->temperature;
-
-          while (DL_I2C_getTargetStatus(I2C_target_INST) &
-                 DL_I2C_TARGET_STATUS_BUS_BUSY)
-            ;
-
-          DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data,
-                                  sizeof(BatteryData));
-
-          // piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST,
-          // (uint8_t*)&battery_data, piTxLen);
-
-          piTxComplete = true;
-
-          while (DL_I2C_getTargetStatus(I2C_target_INST) &
-                 DL_I2C_TARGET_STATUS_BUS_BUSY)
-            ;
-
-          if (piTxCount >= piTxLen) {
-            piTxComplete = true;
-            piTxCount = 0;
-          }
-
-        } else {
-          // printf("Invalid Slot ID: %d\n.", requestedSlot);
-        }
-      }
+  case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
+    printf("Rx Interrupt Triggered \n");
+    if (DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
+      return;
     }
+    receivedCommand = DL_I2C_receiveTargetData(I2C_target_INST);
+    if (receivedCommand == CMD_GET_BATTERY_STATUS) {
+      printf("Received Command: 0x%02X\n", receivedCommand);
+      for (uint8_t slot = 0; slot < NUM_SLOTS; slot++) {
+        battery_state[slot] = batteries[slot].state;
+      }
 
-    break;
-
-    /* TARGET_Rx FIFO trigger (Pi is writing data to MCU) */
-    /*Pi SET battery data limits for each slot, where:
-    - RXFIFO buffer is filled if the command from Pi is 0x03
-    - Creating a temporary buffer named ´rxbuffer´
-    - sizeof(BatteryLimitMsg): 11 bytes (1 byte: slot_id, 2 bytes: min_voltage;
-    max_voltage; cut_off_current; capacitance; charge_fraction)
-    - rx_buffer stores the data from Pi.
-    - if all the expected bytes are received from Pi then,
-       - memcpy() to copy the block of address from the temporary buffer to the
-    BatteryLimitMsg structure
-       - Why?, A: It copies the specified number of bytes from one memory
-    location to another regardless of the type of the data stored.
-       - verify if the received slot_id is less than NUM_SLOTS, where slot_id
-    count starts from 0 then:
-           - create a pointer variable for 'Battery'
-           - battery_limits.slot_id: index of the battery slot to be updated
-           - &batteries[battery_limits.slot_id]: gets the memory address of the
-    battery in that slot
-           - Accessing the structure members of Battery using -> operator. This
-    allows efficient access to the structure's members without directly using
-    the structure variable.
+      DL_I2C_fillTargetTXFIFO(I2C_target_INST, battery_state, 8);
+      while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
+        ;
+      printf("Sent Data\n");
+    }
 
+    /*
+    To receive the Battery Mesaurements from MCU, bit masking is applied
+    where:
+    - command type is upper 4 bits: 0010 (for CMD_GET_BATTERY_DATA)->
+    consistent
+    - slot_id is lower 3 bits, since NUM_SLOTS are 8 so it ranges from 000
+    till 111
+    - bit mask for command is 0xF0: 11110000
+    - bit mask for requested slot is 0x07: 00000111
+    receivedCommand now ranges from 0x20 till 0x27
     */
-  case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
-    if (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
 
-      receivedCommand = DL_I2C_receiveTargetData(I2C_target_INST);
+    else if ((receivedCommand & 0xF0) == 0x20) {
 
-      if (receivedCommand == CMD_SET_BATTERY_LIMIT) {
-        uint8_t rx_buffer[sizeof(BatteryLimitMsg)];
-        uint8_t index = 0;
+      //printf("Received Command: 0x%02X\n", receivedCommand);
+      uint8_t requestedSlot = (receivedCommand & 0x07);
+      //printf("Requested slot:%d\n", requestedSlot);
+      // piTxCount = 0;
+      piTxLen = sizeof(BatteryData);
+      BatteryData battery_data;
 
-        while (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
-          if (index < sizeof(BatteryLimitMsg)) {
-            rx_buffer[index] = DL_I2C_receiveTargetData(I2C_target_INST);
-            // printf("Received Byte[%d]: 0x%02X\n", index, rx_buffer[index]);
-            index++;
-
-          } else {
-            DL_I2C_receiveTargetData(I2C_target_INST);
-          }
-        }
-
-        // printf("Total Bytes Received: %d (Expected: %d)\n", index,
-        // sizeof(BatteryLimitMsg));
-
-        if (index == sizeof(BatteryLimitMsg)) {
-          // printf("Received Battery Limits.\n");
-          BatteryLimitMsg battery_limits;
-          memcpy(&battery_limits, rx_buffer, sizeof(BatteryLimitMsg));
-          if (battery_limits.slot_id < NUM_SLOTS) {
-            Battery *battery = &batteries[battery_limits.slot_id];
-            battery->min_voltage = battery_limits.min_voltage;
-            battery->max_voltage = battery_limits.max_voltage;
-            battery->cut_off_current = battery_limits.cut_off_current;
-            battery->capacitance = battery_limits.capacitance;
-            battery->charge_fraction = battery_limits.charge_fraction;
-            /*printf("\n Received Battery Limits for slot %d: \n",
-            battery_limits.slot_id); printf("  Min Voltage:      %d mV
-            (0x%04X)\n", battery_limits.min_voltage,
-            battery_limits.min_voltage); printf("  Max Voltage:      %d mV
-            (0x%04X)\n", battery_limits.max_voltage,
-            battery_limits.max_voltage); printf("  Cutoff Current:   %d mA
-            (0x%04X)\n", battery_limits.cut_off_current,
-            battery_limits.cut_off_current); printf("  Capacitance:      %d µF
-            (0x%04X)\n", battery_limits.capacitance,
-            battery_limits.capacitance); printf("  Charge Fraction:  %d%%
-            (0x%02X)\n", battery_limits.charge_fraction,
-            battery_limits.charge_fraction);*/
-          }
-        }
-      } else if (receivedCommand == CMD_GET_BATTERY_STATUS) {
-        uint8_t test[8] = {0x00};
-        // for testing:
-        Battery batteries[NUM_SLOTS] = {
-            {0, STATE_BATTERY_DETECTED, 3700, 500, 25, 3000, 4200, 2000, 10000, 80}
-        };
-        //Battery_StateUpdate();
+      if (requestedSlot >= NUM_SLOTS) {
+        //printf("Requested Slot is not valid.\n");
+        DL_I2C_flushTargetTXFIFO(I2C_target_INST);
+        return;
+      }
 
-        // Prepare data to be sent to Pi:
-        for (uint8_t slot = 0; slot < NUM_SLOTS; slot++) {
-          // Read the battery status for each slot
-          //Battery_ReadState(slot);
-          test[slot] = batteries[slot].state;
-        }
-        // Filling up the FIFO
-        DL_I2C_fillTargetTXFIFO(I2C_target_INST, &test, 8);
-        while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
-            ;
-                printf("Sent Data\n");
+      Battery *battery = &batteries[requestedSlot];
+      battery_data.slot_id = battery->slot_id; // !TODO: recheck if necessary
+      //printf("slot_id:%u\n", battery_data.slot_id);
+      battery_data.voltage = battery->voltage;
+      //printf("voltage:%u\n", battery_data.voltage);
+      battery_data.current = battery->current;
+      //printf("current:%u\n", battery_data.current);
+      battery_data.temperature = battery->temperature;
+      //printf("temperature:%u\n", battery_data.temperature);
+      DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data,
+                              piTxLen);
+
+      /*piTxComplete= true;
+      if(piTxCount >= piTxLen){
+          piTxComplete= true;
+          piTxCount= 0;
+      }*/
+      printf("Battery Measurement Sent. \n");
+      DL_I2C_flushTargetTXFIFO(I2C_target_INST);
 
+    }
 
+    else if (receivedCommand == CMD_SET_BATTERY_LIMIT) {
+      uint8_t rx_buffer[sizeof(BatteryLimitMsg)];
+      uint8_t index = 0;
 
-        /*if (piTxCount < piTxLen) {
-          while (DL_I2C_getTargetStatus(I2C_target_INST) &
-                 DL_I2C_TARGET_STATUS_BUS_BUSY)
-            ;
-          piTxCount += DL_I2C_fillTargetTXFIFO(
-              I2C_target_INST, &piTxPacket[piTxCount], (piTxLen - piTxCount));
+      while (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
+        if (index < sizeof(BatteryLimitMsg)) {
+          rx_buffer[index] = DL_I2C_receiveTargetData(I2C_target_INST);
+          index++;
         } else {
-          /*
-           * Fill FIFO with 0x00 if more data is requested than expected piTxLen
-           */
-          /*while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
-            ;
-        }*/
-      } else if (receivedCommand == CMD_GET_BATTERY_DATA) {
-
-        uint8_t requestedSlot = DL_I2C_receiveTargetData(I2C_target_INST);
-        while (DL_I2C_getTargetStatus(I2C_target_INST) &
-               DL_I2C_TARGET_STATUS_BUS_BUSY)
-          ;
-        // printf("Battery Data Requested for Slot %d!\n", requestedSlot);
-        piTxCount = 0;
-        piTxLen = sizeof(BatteryData);
-        BatteryData battery_data;
-        if (requestedSlot < NUM_SLOTS) {
-          Battery *battery = &batteries[requestedSlot];
-          battery_data.slot_id = battery->slot_id;
-          battery_data.voltage = battery->voltage;
-          battery_data.current = battery->current;
-          battery_data.temperature = battery->temperature;
-
-          while (DL_I2C_getTargetStatus(I2C_target_INST) &
-                 DL_I2C_TARGET_STATUS_BUS_BUSY)
-            ;
-
-          DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data,
-                                  sizeof(BatteryData));
-
-          // piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST,
-          // (uint8_t*)&battery_data, piTxLen);
-
-          piTxComplete = true;
-
-          while (DL_I2C_getTargetStatus(I2C_target_INST) &
-                 DL_I2C_TARGET_STATUS_BUS_BUSY)
-            ;
-
-          if (piTxCount >= piTxLen) {
-            piTxComplete = true;
-            piTxCount = 0;
-          }
+          DL_I2C_receiveTargetData(I2C_target_INST);
+        }
+      }
 
+      if (index == sizeof(BatteryLimitMsg)) {
+        BatteryLimitMsg battery_limits;
+        memcpy(&battery_limits, rx_buffer, sizeof(BatteryLimitMsg));
+        if (battery_limits.slot_id < NUM_SLOTS) {
+          Battery *battery = &batteries[battery_limits.slot_id];
+          battery->min_voltage = battery_limits.min_voltage;
+          battery->max_voltage = battery_limits.max_voltage;
+          battery->cut_off_current = battery_limits.cut_off_current;
+          battery->capacitance = battery_limits.capacitance;
+          battery->charge_fraction = battery_limits.charge_fraction;
         }
+      }
+    }
 
     break;
-
-  /* Arbitration lost or NACK */
+  case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
+    break;
   case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
-    printf("Arbitration Lost.\n");
     break;
   default:
-    printf("Unknown Interrupt.\n");
     break;
   }
 }
@@ -410,15 +269,18 @@ int main(void) {
   // delay_cycles(50000);
   // DAC_ReadCurrentAddress();
 
-  while (1) { // Looping through the ADC Channels
-    /*for(uint8_t slot_id=0; slot_id< NUM_SLOTS; slot_id++){
-        for(uint8_t adc_channel=0; adc_channel< ADC_CHANNEL_NUM; adc_channel++){
-            batteries[slot_id].channel= adc_channel;
-            Battery_UpdateADCReading(slot_id, batteries[slot_id].channel);
-
-        }
-
-    }*/
+  while (1) {
+    // Looping through the ADC Channels
+    for (uint8_t slot_id = 0; slot_id < NUM_SLOTS; slot_id++) {
+      for (uint8_t adc_channel = 0; adc_channel < ADC_CHANNEL_NUM;
+           adc_channel++) {
+        Battery_UpdateADCReading(slot_id, adc_channel);
+      }
+      Battery_ReadState(slot_id);
+    }
+    printf("Battery Details: Slot: %u, Voltage:%u, Current: %u \n, State:%u\n:",
+           batteries[0].slot_id, batteries[0].voltage, batteries[0].current,
+           batteries[0].state);
 
     // CC-CV Cycle: maximum cycles is not yet implemented
     // for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){

+ 6 - 36
i2c_target.c

@@ -1,18 +1,18 @@
 
 #include "i2c_target.h"
-#include "battery.h"
 #include "adc.h"
+#include "battery.h"
 #include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
-#include <stdio.h>
 #include <stdint.h>
+#include <stdio.h>
 
-//Global buffer for I2C 
+// Global buffer for I2C
 
 BatteryData battery_data;
 BatteryLimitMsg battery_limits;
 
-//Flag for Pi INTERRUPTS:
+// Flag for Pi INTERRUPTS:
 volatile bool piRxComplete;
 volatile bool piTxComplete;
 
@@ -22,7 +22,8 @@ uint8_t piRxPacket[I2C_RX_MAX_PACKET_SIZE_PI];
 uint32_t piTxLen, piTxCount;
 uint32_t piRxLen, piRxCount;
 /*
-- command: as defined in the Wiki for Pi 0x01, 0x02, 0x03, refer to i2c_target.h file
+- command: as defined in the Wiki for Pi 0x01, 0x02, 0x03, refer to i2c_target.h
+file
 - slot_id: battery slot numner from 0 to NUM_SLOTS-1
 - data: pointer to SET battery limits
 - len: length of the data
@@ -30,36 +31,5 @@ uint32_t piRxLen, piRxCount;
 */
 
 
-// for testing:
-//Battery batteries[NUM_SLOTS] = {
-//    {0, STATE_BATTERY_DETECTED, 3700, 500, 25, 3000, 4200, 2000, 10000, 80}
-//};
-
-
-void Battery_ReadState(uint8_t slot_id){
-    
-    uint16_t voltage_mv= batteries[slot_id].voltage;
-    uint16_t min_voltage= batteries[slot_id].min_voltage;
-    uint16_t max_voltage= batteries[slot_id].max_voltage;
-
-    //Testing:
-    if(voltage_mv< 500){
-        batteries[slot_id].state= STATE_EMPTY;
-    }
 
-    else if(voltage_mv>=500 && voltage_mv< 3000){
-        batteries[slot_id].state= STATE_BATTERY_DETECTED;
-    }
-    else if(voltage_mv >=3000 && voltage_mv< 4200){
-        batteries[slot_id].state= STATE_MEASUREMENT_IN_PROGRESS;
-    }
-    // once MCU is done reading ADC:
-    else if(!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS)) {
-        batteries[slot_id].state= STATE_MEASUREMENT_DONE;
-    }
-    else{
-        batteries[slot_id].state= STATE_OVERCHARGING;
-    }    
-}
 
- 

+ 1 - 0
i2c_target.h

@@ -44,4 +44,5 @@ typedef struct{
 
 //void I2C_Init();
 void Battery_ReadState(uint8_t slot_id);
+void I2C_target_communication();
 #endif