瀏覽代碼

Dry run, running correctly

namrota ghosh 7 月之前
父節點
當前提交
963e70ad7b
共有 5 個文件被更改,包括 126 次插入46 次删除
  1. 109 33
      main.c
  2. 10 10
      src/cc_cv_charging.c
  3. 2 2
      src/cc_cv_charging.h
  4. 1 0
      src/config.h
  5. 4 1
      src/pi/i2c_pi_target.c

+ 109 - 33
main.c

@@ -1,41 +1,117 @@
-/*
- * Copyright (c) 2023, Texas Instruments Incorporated
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * *  Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * *  Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * *  Neither the name of Texas Instruments Incorporated nor the names of
- *    its contributors may be used to endorse or promote products derived
- *    from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
 #include "ti_msp_dl_config.h"
+#include "src/pi/i2c_pi_target.h"
+#include "src/controller/controller.h"
+#include "ti/driverlib/dl_i2c.h"
+#include "src/battery_data/battery.h"
+#include "src/cc_cv_charging.h"
+#include <stdio.h>
+
+volatile bool mcuSendCommand = false;
+volatile bool picommandPending = false;
+
+// Interrupt for I2C instance -> MCU to Target
+
+void I2C_1_INST_IRQHandler(void)
+{
+    switch (DL_I2C_getPendingInterrupt(I2C_1_INST)) 
+    {   
+        case DL_I2C_IIDX_CONTROLLER_START:
+            DL_I2C_flushControllerRXFIFO(I2C_1_INST);
+            DL_I2C_flushControllerTXFIFO(I2C_1_INST);
+            break;
+
+        case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
+            mcuSendCommand= true;
+            /* Store bytes received from target in Rx Msg Buffer */
+            while (DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
+                if (rx_packet.rxCount < rx_packet.rxLen) {
+                    rx_packet.rxBuffer[rx_packet.rxCount] = DL_I2C_receiveControllerData(I2C_1_INST);
+                    rx_packet.rxCount++;
+                } else {
+                    /* Ignore and remove from FIFO if the buffer is full */
+                    DL_I2C_receiveControllerData(I2C_1_INST);
+                    
+                } 
+            }
+            break;
+
+        case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
+            /* Fill TX FIFO with bytes to send */
+            mcuSendCommand = true;
+            break;
+        case DL_I2C_IIDX_CONTROLLER_STOP:
+            mcuSendCommand = false;
+        case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
+        case DL_I2C_IIDX_CONTROLLER_NACK:
+            break; 
+        default:
+            break;
+    }
+}
+
+
+void I2C_0_INST_IRQHandler(void)
+{
+    switch (DL_I2C_getPendingInterrupt(I2C_0_INST)) 
+    {   
+        case DL_I2C_IIDX_TARGET_START:
+            DL_I2C_flushTargetRXFIFO(I2C_0_INST);
+            DL_I2C_flushTargetTXFIFO(I2C_0_INST);
+            break;
+
+        case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
+            picommandPending = true;
+            //printf("Rx Interrupt Triggered \n");
+            if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
+                return;
+            }
+            break;
+
+        case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
+            /* Fill TX FIFO with bytes to send */
+            picommandPending = true;
+            break;
+        case DL_I2C_IIDX_TARGET_STOP:
+            picommandPending = false;
+            break;
+        case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
+            break; 
+        default:
+            break;
+    }
+}
 
 int main(void)
 {
     SYSCFG_DL_init();
+    Battery_Init();
+    //Interrupt routine for Pi
+    NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
+    //Interrupt for target mcu
+    NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
 
-    while (1) {
+
+    while(1)
+    {
+        if(picommandPending)
+        {   printf("Pi Interrupt Triggered.\n");
+            pi_i2c_mcu();
+            picommandPending = false;
+        }
+        if(mcuSendCommand)
+        {   printf("MCU Interrupt Triggered.\n");
+            //GET command from the target: target_address, slot, data:
+            controller_GetBatteryMeasurement(0x48, 0, &battery_data[0].battery_measurement);
+            //Set the current to the target: command, slot, current_value
+            controller_SetCurrent(0x48,0,1000);
+            mcuSendCommand = false;
+        }
+        /*for(uint8_t slot=0; slot<NUM_SLOTS; slot++)
+        {
+            Battery_ReadState(slot);
+            CC_CV_ControlCharging(slot, );
+        }*/
+        
     }
 }
+

+ 10 - 10
src/cc_cv_charging.c

@@ -3,6 +3,8 @@
 #include "src/battery_data/battery.h"
 #include <stdio.h>
 #include "ti/driverlib/dl_gpio.h"
+#include "ti_msp_dl_config.h"
+#include "src/controller/controller.h"
 
 static ChargingState charging_state= STATE_IDLE;
 static uint16_t cycle_count = 0;
@@ -111,7 +113,6 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
     batt_cutoff_current = battery_data[slot_id].battery_limits.cut_off_current;
     batt_capacitance= battery_data[slot_id].battery_limits.capacitance;
     batt_charge_discharge= battery_data[slot_id].charge_discharge;
-
     
     //  DAC channel value:
     // dac_channel_value is for CV mode where the charge current is calibrated till battery current is less than (cutoff current+threshold)
@@ -125,11 +126,10 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
   
     // PRE CHARGE STATE: Battery Voltage is lesser than 3000 mVolts
     case STATE_PRE_CHARGE:
-
       DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
-      controller_SetCurrent(TARGET_ADDRESS, slot_id, charge_current);
+      controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
       if (true) {
-        printf("PRE CHARGING: Slot %d at %d mA.\n", slot_id, );
+        printf("PRE CHARGING: Slot %d at %d mA.\n", slot_id, charge_current);
       }
       break;
   
@@ -137,7 +137,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
     // reaches to (MAXIMUM_VOLTAGE-BATTERY_THRESHOLD)= 4150 mVolts
     case STATE_CC_CHARGING:
       
-      controller_SetCurrent(TARGET_ADDRESS, slot_id, charge_current);
+      controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
       printf("CC CHARGING: Slot %d, Current: %d mA, Voltage: %d mV.\n", slot_id, batt_current, batt_voltage);
       break;
   
@@ -154,22 +154,22 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
         dac_initialized= false;
       }
       last_voltage = batt_voltage;
-      controller_SetCurrent(TARGET_ADDRESS, slot_id, charge_current);
+      controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
       break;
   
     case STATE_DISCHARGING:
       DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
       DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
-      controller_SetCurrent(TARGET_ADDRESS, slot_id, charge_current);
+      controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
       printf("DISCHARGING: Slot %d at %d mA.\n", slot_id, batt_voltage);
       break;
   
     case STATE_FINAL_DISCHARGE:
       //Once the cycle gets done, the battery state transitions to "STATE_MEASUREMENT_DONE"
-      batteries[slot_id].state= STATE_MEASUREMENT_DONE;
+      battery_data[slot_id].battery_state= STATE_MEASUREMENT_DONE;
       DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
       DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
-      controller_SetCurrent(TARGET_ADDRESS, slot_id, 0);
+      controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, 0);
       charging_state = STATE_IDLE;
       break;
   
@@ -184,7 +184,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
       DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
       charging_state = STATE_PRE_CHARGE;
       break;
-      
+
     default:
       break;
     }

+ 2 - 2
src/cc_cv_charging.h

@@ -2,7 +2,7 @@
 
 #ifndef CC_CV_CHARGING_H_
 #define CC_CV_CHARGING_H_
-
+#define TARGET_MCU_ADDRESS (0x48)
 typedef enum{
     STATE_PRE_CHARGE,
     STATE_CC_CHARGING,
@@ -15,6 +15,6 @@ typedef enum{
 
 
 void CC_CV_UpdateChargingState(uint8_t slot_id);
-void CC_CV_ControlCharging(uint8_t slot_id);
+void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current);
 
 #endif

+ 1 - 0
src/config.h

@@ -8,6 +8,7 @@
 #define BATTERY_THRESHOLD (50)
 #define TEMPERATURE_MAX_C (60)
 #define MAX_CYCLES (2)
+#define TARGET_MCU_ADDRESS (0x48)
 
 typedef struct{
      uint8_t txBuffer[I2C_TX_MAX_PACKET_SIZE];

+ 4 - 1
src/pi/i2c_pi_target.c

@@ -4,7 +4,7 @@
 #include "src/battery_data/battery.h"
 #include "ti_msp_dl_config.h"
 #include <stdint.h>
-
+#include <stdio.h>
 // Global extern variable for buffers are defined in config.h and declared in i2c_hal.c:
 
 void pi_i2c_mcu(){
@@ -48,8 +48,11 @@ void pi_i2c_mcu(){
             /*rxBuffer[0]: slot_id
             */
             BatteryInfo *battery = &battery_data[rx_packet.rxBuffer[0]];
+            printf("Battery slot: %u\n", rx_packet.rxBuffer[0]);
             battery->battery_limits.min_voltage = rx_packet.rxBuffer[1];
+            printf("Battery min voltage: %u\n", rx_packet.rxBuffer[1]);
             battery->battery_limits.max_voltage = rx_packet.rxBuffer[2];
+            printf("Battery max voltage: %u\n", rx_packet.rxBuffer[1]);
             battery->battery_limits.cut_off_current = rx_packet.rxBuffer[3];
             battery->battery_limits.capacitance = rx_packet.rxBuffer[4];
             battery->battery_limits.charge_fraction = rx_packet.rxBuffer[5];