|
|
@@ -201,122 +201,13 @@ void I2C_controller_INST_IRQHandler(void)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//void I2C_target_INST_IRQHandler(void) {
|
|
|
-
|
|
|
-// static bool DataRx= false;
|
|
|
-// static uint16_t registerAddress=0;
|
|
|
-
|
|
|
- //printf("I2C Interrupt Triggered to MCU (TARGET)!\n");
|
|
|
-
|
|
|
-// uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
|
|
|
-// switch (status) {
|
|
|
-
|
|
|
- /* START condition detected */
|
|
|
-// case DL_I2C_IIDX_TARGET_START:
|
|
|
- //printf("START condition detected.\n");
|
|
|
-// gTxCount= 0;
|
|
|
-// gRxCount= 0;
|
|
|
-// DataRx= false;
|
|
|
-// DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
-// break;
|
|
|
-
|
|
|
- /* STOP condition detected */
|
|
|
-// case DL_I2C_IIDX_TARGET_STOP:
|
|
|
- //printf("STOP condition detected.\n");
|
|
|
-// if (DataRx == true){
|
|
|
- //printf("Data received from Pi: ");
|
|
|
-// for (uint8_t i = 0; i < gRxCount; i++) {
|
|
|
-// //printf("0x%02X ", gRxBuffer[i]);
|
|
|
-
|
|
|
-// }
|
|
|
- //printf("\n");
|
|
|
-// DataRx= false;
|
|
|
-// }
|
|
|
-// DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
-// break;
|
|
|
-
|
|
|
- /* RX FIFO trigger (Pi is writing data to MCU) */
|
|
|
-// case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
|
|
|
- //printf("receiving data from Pi.\n");
|
|
|
-// DataRx= true;
|
|
|
-// while (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
|
|
|
-// if (gRxCount == 0) {
|
|
|
-// // First byte received is the register address
|
|
|
-// registerAddress = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
-// DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
- //printf("Register Address Set: 0x%02X\n", registerAddress);
|
|
|
-// }
|
|
|
-// else if (registerAddress < REGISTER_SIZE) {
|
|
|
- //Storing the received data from the controller correctly
|
|
|
-// registers[registerAddress] = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
- //printf("Stored 0x%02X in Register 0x%02X\n", registers[registerAddress], registerAddress);
|
|
|
- //gRxBuffer[gRxCount++] = DL_I2C_receiveTargetData(I2C_0_INST);
|
|
|
-
|
|
|
-// }
|
|
|
-// else {
|
|
|
-// printf("ERROR: RX Buffer Overflow!\n");
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// gRxCount++;
|
|
|
-// }
|
|
|
-// break;
|
|
|
-
|
|
|
- /* TX FIFO trigger (Pi is reading data from MCU) */
|
|
|
-// case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
|
|
|
-
|
|
|
- //printf("transmitting data to Pi...\n");
|
|
|
-// if(!DL_I2C_isTargetTXFIFOFull(I2C_target_INST)) {
|
|
|
-// if(registerAddress == 0x01){
|
|
|
- //DL_I2C_fillTargetTXFIFO(I2C_target_INST, battery_status, NUM_BATTERY_SLOT);
|
|
|
-// }
|
|
|
-// else if(registerAddress== 0x02){
|
|
|
- //DL_I2C_fillTargetTXFIFO(I2C_target_INST, &value , 1);
|
|
|
-// }
|
|
|
-
|
|
|
-// else {
|
|
|
- // **Fix: Avoid infinite while loop**
|
|
|
-// printf("WARNING: TX Buffer Underflow! Sending default value.\n");
|
|
|
-// DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t[]){0x00}, 1);
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// break;
|
|
|
-
|
|
|
- /* Arbitration lost or NACK */
|
|
|
-// case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
|
|
|
-// printf("Arbitration Lost.\n");
|
|
|
-// case DL_I2C_IIDX_CONTROLLER_NACK:
|
|
|
-// printf("I2C NACK Received! Possible communication failure.\n");
|
|
|
- //DL_I2C_stopControllerTransfer(I2C_controller_INST); // Force STOP
|
|
|
-// break;
|
|
|
-// default:
|
|
|
-// printf("Unknown Interrupt.\n");
|
|
|
-// break;
|
|
|
-// }
|
|
|
-//}
|
|
|
-
|
|
|
-void I2C_Target_Init(void) {
|
|
|
- // Set the target address
|
|
|
- DL_I2C_setTargetOwnAddress(I2C_target_INST, I2C_target_TARGET_OWN_ADDR);
|
|
|
-
|
|
|
- // Enable target mode
|
|
|
- DL_I2C_enableTarget(I2C_target_INST);
|
|
|
-
|
|
|
-
|
|
|
- /* Enable relevant I2C interrupts */
|
|
|
- DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_START);
|
|
|
- DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_RXFIFO_TRIGGER);
|
|
|
- DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER);
|
|
|
- DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_STOP);
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
void I2C_target_INST_IRQHandler(void) {
|
|
|
|
|
|
//printf("I2C Interrupt Triggered to MCU (TARGET)!\n");
|
|
|
static uint8_t receivedCommand= 0;
|
|
|
+ static uint8_t requestedSlot= 0;
|
|
|
uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
|
|
|
- ADC_PARAMS params;
|
|
|
+ //ADC_PARAMS params;
|
|
|
switch (status) {
|
|
|
|
|
|
/* START condition detected */
|
|
|
@@ -336,27 +227,30 @@ void I2C_target_INST_IRQHandler(void) {
|
|
|
|
|
|
/* TX FIFO trigger (Pi is reading data from MCU) */
|
|
|
case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
|
|
|
- switch(receivedCommand){
|
|
|
- case CMD_GET_BATTERY_STATUS:
|
|
|
- for(uint8_t slot=0; slot< NUM_SLOTS; slot++){
|
|
|
- Battery_StateUpdate(slot);
|
|
|
- uint8_t battery_status= batteries[slot].state;
|
|
|
- DL_I2C_fillTargetTXFIFO(I2C_target_INST, &battery_status, NUM_SLOTS);
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
|
|
|
- }
|
|
|
- break;
|
|
|
+ printf("Tx PI triggered!\n");
|
|
|
+ if (receivedCommand == CMD_GET_BATTERY_STATUS){
|
|
|
+ printf("Battery Status\n");
|
|
|
+ Battery_StateUpdate();
|
|
|
+ uint8_t battery_slots_status[NUM_SLOTS];
|
|
|
+ for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
|
|
|
+ battery_slots_status[slot]= batteries[slot].state;
|
|
|
+ }
|
|
|
+ DL_I2C_fillTargetTXFIFO(I2C_target_INST, battery_slots_status, NUM_SLOTS);
|
|
|
+ while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
|
|
|
|
|
|
- case CMD_GET_BATTERY_DATA:
|
|
|
+ }
|
|
|
+ else if (receivedCommand == CMD_GET_BATTERY_DATA){
|
|
|
+ printf("Battery Data!\n");
|
|
|
piTxCount= 0;
|
|
|
piTxComplete= false;
|
|
|
- // Create a data message and transmit it
|
|
|
- for(uint8_t slot_id; slot_id<NUM_SLOTS; slot_id++){
|
|
|
- battery_data.slot_id= slot_id;
|
|
|
- battery_data.voltage= batteries[slot_id].voltage;
|
|
|
+ if(requestedSlot < NUM_SLOTS){
|
|
|
+ BatteryData battery_data;
|
|
|
+ battery_data.slot_id= requestedSlot;
|
|
|
+ battery_data.voltage= batteries[requestedSlot].voltage;
|
|
|
+ // as of now set to 0 for testing
|
|
|
battery_data.current= 0;
|
|
|
battery_data.temperature=0;
|
|
|
}
|
|
|
- //printf("Sending Battery Data: Slot %d | Voltage %dmV| Current %d| Temperature %d.\n", slot_id, battery_data.voltage, battery_data.current, battery_data.temperature);
|
|
|
while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
|
|
|
//Casting the struct pointer to byte array: (uint8_t*)
|
|
|
DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t*)&battery_data, sizeof(BatteryData));
|
|
|
@@ -368,11 +262,24 @@ void I2C_target_INST_IRQHandler(void) {
|
|
|
piTxCount = 0; // Reset counter for next request
|
|
|
printf("Sent Full Battery Data\n");
|
|
|
}
|
|
|
- break;
|
|
|
}
|
|
|
|
|
|
/* TARGET_Rx FIFO trigger (Pi is writing data to MCU) */
|
|
|
case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
|
|
|
+ if (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
|
|
|
+ if(receivedCommand == CMD_SET_BATTERY_LIMIT){
|
|
|
+ BatteryLimitMsg battery_limits;
|
|
|
+ requestedSlot= DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ if(requestedSlot < NUM_SLOTS){
|
|
|
+ batteries[requestedSlot].min_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ batteries[requestedSlot].max_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ batteries[requestedSlot].cut_off_current= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ batteries[requestedSlot].capacitance= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
|
|
|
/* Arbitration lost or NACK */
|
|
|
@@ -410,12 +317,17 @@ int main(void)
|
|
|
{
|
|
|
// Initialize System and I2C
|
|
|
SYSCFG_DL_init();
|
|
|
+
|
|
|
+ // Initialize battery array and default params
|
|
|
+ Battery_Init();
|
|
|
+
|
|
|
//Reset_I2C_Bus();
|
|
|
- //NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
|
|
|
+ NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
|
|
|
NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
|
|
|
|
|
|
printf("............System Configuration Enabled...............\n");
|
|
|
|
|
|
+ //Multiplexer
|
|
|
Multiplexer_SelectChannel(I2C_CHANNEL);
|
|
|
//I2C_scanBus();
|
|
|
I2C_init(&gI2C);
|
|
|
@@ -428,14 +340,19 @@ int main(void)
|
|
|
.gain= 1
|
|
|
|
|
|
};
|
|
|
-
|
|
|
printf("Configuring ADC....\n");
|
|
|
//**Set Configuration Register for ADC**
|
|
|
ADC_SetConfigurationBytes(adc_params);
|
|
|
uint16_t channel_a_value= 3300; // in mVolts
|
|
|
+
|
|
|
+
|
|
|
while (1)
|
|
|
{
|
|
|
- Battery_UpdateCurrentVoltage(adc_params);
|
|
|
+
|
|
|
+ Battery_UpdateCurrentVoltage(adc_params);
|
|
|
+ for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot++){
|
|
|
+ CC_CV_ControlCharging(slot_id);
|
|
|
+ }
|
|
|
//delay_cycles(5000);
|
|
|
//DAC_fastWrite(channel_a_value);
|
|
|
delay_cycles(100000);
|