Selaa lähdekoodia

Logic added for Dynamic Addressing

namrota ghosh 7 kuukautta sitten
vanhempi
commit
7a3e099df6
2 muutettua tiedostoa jossa 45 lisäystä ja 3 poistoa
  1. 43 3
      src/pi/i2c_pi_target.c
  2. 2 0
      src/pi/i2c_pi_target.h

+ 43 - 3
src/pi/i2c_pi_target.c

@@ -1,5 +1,6 @@
 #include "i2c_pi_target.h"
 #include "src/config.h"
+#include "ti/driverlib/dl_gpio.h"
 #include "ti/driverlib/dl_i2c.h"
 #include "src/battery_data/battery.h"
 #include "ti_msp_dl_config.h"
@@ -8,12 +9,51 @@
 #include <string.h>
 // Global extern variable for buffers are defined in config.h and declared in i2c_hal.c:
 
+
+
+/*
+05.05.25:
+Working on the Dynamic Addressing for I2C communication between MCU and Pi:
+1. Base Address of 0x10 is set in the configuration of the MCU as a target
+2. GPIO Pins are configured from Pin PA14 to Pin PA17 for Dynamic Addressing
+3. If all the Pins are connected to GND then address is the BASE ADDRESS, if not then based on pull up the GPIO pins are set HIGH or LOW
+4. Initialize GPIO pins
+5. Read GPIO Pins for GND or Pull Up Resistor
+6. Set the offset and the base address to the Target and enable
+*/
+
+void GPIO_Addr_Init(){
+   //Configure GPIO Pins as input:
+   DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_0_IOMUX); 
+   DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_1_IOMUX);
+   DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_2_IOMUX);
+   DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_3_IOMUX);
+}
+
+//Dynamic Addressing pins for MCU:
+void dynamic_gpio_addressing(){
+    //Initialize the GPIO pins
+    GPIO_Addr_Init();
+    // using ternary operators to verify if pin is high then 1 else 0
+    uint8_t offset= 0;
+    offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_3_PIN) ? 1 : 0)<<3);
+    offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_2_PIN) ? 1 : 0)<<2);
+    offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_1_PIN) ? 1 : 0)<<1);
+    offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_0_PIN) ? 1 : 0)<<0);
+    // Target Address as configured in the syscfg file
+    uint8_t i2c_dynamic_address= I2C_0_TARGET_OWN_ADDR + offset;
+    //Configure as target with the dynamic address
+    DL_I2C_setTargetAddress(I2C_0_INST, i2c_dynamic_address);
+    //Enable Target Mode
+    DL_I2C_enableTarget(I2C_0_INST);
+}
+
 void pi_i2c_mcu(){
 
     uint8_t receivedCommand= DL_I2C_receiveTargetData(I2C_0_INST);
     printf("Received Command: 0x%02X\n", receivedCommand);
     if(receivedCommand == CMD_GET_BATTERY_STATUS){
-        //Example: i2cget -y 1 0x10 0x01 i 4
+        //Example: i2cget -y 1 0x20 0x01 i 4
         uint8_t status_buffer[NUM_SLOTS];
         //GET battery state from battery.c file
         for(uint8_t slot=0; slot<NUM_SLOTS; slot++){
@@ -24,7 +64,7 @@ void pi_i2c_mcu(){
     }
     //bitmasked GET command:
     else if((receivedCommand & 0xF0)== 0x20){
-        //I2Ctools command: i2cget -y 1 0x10 0x20 i 7
+        //I2Ctools command: i2cget -y 1 0x20 0x20 i 7
         //Get Battery Measurement data for slot_id: Voltage, Current, Tempertaure
         uint8_t requestedSlot = receivedCommand & 0x0F;
         printf("Requested slot:%d\n", requestedSlot);
@@ -48,7 +88,7 @@ void pi_i2c_mcu(){
     else if(receivedCommand== CMD_SET_BATTERY_LIMIT){
 
         //slot_id is another element
-        //Example i2ctools: i2cset -y 1 0x10 0x03 0x00 0x2C 0x01 0x68 0x10 0xFA 0xD0 0x07 0x19 i
+        //Example i2ctools: i2cset -y 1 0x20 0x03 0x00 0x2C 0x01 0x68 0x10 0xFA 0xD0 0x07 0x19 i
         /*
         * min_voltage: 300 (2C 01)
         * max_voltage: 4200 (68 10)

+ 2 - 0
src/pi/i2c_pi_target.h

@@ -25,6 +25,8 @@ typedef struct __attribute__((packed)){
     uint8_t charge_fraction;
 } BatteryLimitMsg;
 
+
+void dynamic_gpio_addressing();
 void pi_i2c_mcu();
 
 #endif