/* References: https://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c */ #include "src/battery_data/battery.h" #include "mcu_slave_interface.h" #include "ti/driverlib/dl_i2c.h" #include #include /*Function to Rx and Tx data from Target to Controller*/ // 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) { printf("MCU interrupt triggered\n"); uint8_t receivedCommand = DL_I2C_receiveTargetData(i2c); printf("[SLAVE] Received Command: 0x%02X\n", receivedCommand); 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 & 0xF0) == 0x60) { uint8_t slot = receivedCommand & 0x0F; if (slot > NUM_SLOTS) { DL_I2C_flushTargetTXFIFO(i2c); return; } // Struct for voltage, current and temperature BatteryMeasurementData battery_measure; // take the updated battery measurement from the battery struct and store it // in the battery_measure struct battery_measure.voltage = batteries[slot].voltage; battery_measure.current = batteries[slot].current; battery_measure.temperature = batteries[slot].temperature; // Copying the memory block from battery_measure struct to tx_buffer: memcpy(tx_buffer, &battery_measure, sizeof(BatteryMeasurementData)); DL_I2C_fillTargetTXFIFO(i2c, tx_buffer, sizeof(BatteryMeasurementData)); printf("Battery Measurement Sent to MCU. \n"); DL_I2C_flushTargetTXFIFO(i2c); } }