| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #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)
- {
- 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, );
- }*/
-
- }
- }
|