#include "src/battery_data/battery.h" #include "ti/driverlib/dl_i2c.h" #include "ti_msp_dl_config.h" //#include "ti/driverlib/dl_i2c.h" #include #include "src/interfaces/i2c_target.h" #include "src/config.h" #include "src/peripherals/adc.h" int8_t handle_read_pending_slot = -1; /**** Interrupt for Pi to MCU ****/ void I2C_target_INST_IRQHandler(void) { uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST); switch (status) { case DL_I2C_IIDX_TARGET_START: break; case DL_I2C_IIDX_TARGET_STOP: break; case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER: // only use this function if we are async (filling buffers) - the rest is handled by the mainloop if (handle_read_pending_slot == -1) { handle_read_pending_slot = mcu_i2c_handle(I2C_target_INST); } break; case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER: break; case DL_I2C_IIDX_TARGET_ARBITRATION_LOST: break; case DL_I2C_IIDX_TIMEOUT_A: case DL_I2C_IIDX_TIMEOUT_B: DL_I2C_flushTargetRXFIFO(I2C_target_INST); DL_I2C_flushTargetTXFIFO(I2C_target_INST); default: break; } } _iq15 qVTrim; int main(void) { SYSCFG_DL_init(); NVIC_EnableIRQ(I2C_target_INST_INT_IRQN); NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN); slot_init(); initialize_target_address(); qVTrim = _IQ15div(_IQ15mpy((_IQ15(DL_SYSCTL_getTempCalibrationConstant()) - _IQ15(0.5)), _IQ15(ADC_TEMP_VREF_VOLTAGE)), ((uint32_t)(1) << 27)); DL_ADC12_startConversion(ADC12_0_INST); DL_ADC12_enableConversions(ADC12_0_INST); delay_cycles(320000*4); slot.set_current = 800; while (1) { if (handle_read_pending_slot != -1) { mcu_i2c_handle_read(I2C_target_INST); handle_read_pending_slot = -1; } // step 1: update the voltage readings slot_read_state(); // step 2: control loop to adjust the dac / adc values, // but only if no error happens // (0x80 is the error flag of the state) if ((*slot.state & 0x80) == 0) { slot_adjust_current(); } else { slot_disable(); } delay_cycles(320000*4); } }