|
|
@@ -43,36 +43,40 @@ void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_
|
|
|
|
|
|
//Get battery measurement: Voltage, Current, Temperature including slot state:
|
|
|
bool controller_GetBatteryMeasurement(uint8_t slot_id){
|
|
|
-
|
|
|
+
|
|
|
+ //I2cControllerStatus= I2C_STATUS_IDLE;
|
|
|
uint8_t target_address= TARGET_BASE_ADDRESS + ((slot_id & 0b00000100) >> 2);
|
|
|
printf("Target Address 0x%02X \n", target_address);
|
|
|
//Initializing BatteryMeasurement structure from battery.h of size 8
|
|
|
BatteryMeasurement measurement;
|
|
|
|
|
|
//Flush the TX FIFO and make the buffer ready to transmit data:
|
|
|
- DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
- DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
+ //DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
+ //DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
|
|
|
//Write Command to the target
|
|
|
//Set the command in the tx buffer in bit masked format to the target: Upper Nibble-> Slot and Lower Nibble -> Command
|
|
|
- txPacket.txBuffer[0]= (slot_id << 4)|(CMD_GET_MEASUREMENT & 0x0F); //shift slot_id to the left by 4 bits
|
|
|
+ //txPacket.txBuffer[0]= (slot_id << 4)|(CMD_GET_MEASUREMENT & 0x0F); //shift slot_id to the left by 4 bits
|
|
|
txPacket.txLen= 1;
|
|
|
- txPacket.txCount= 0;
|
|
|
+ //Number of bytes to be written to TX FIFO:
|
|
|
+ txPacket.txCount= DL_I2C_fillControllerTXFIFO(I2C_1_INST, &txPacket.txBuffer[0] , txPacket.txLen);
|
|
|
txPacket.txComplete= false;
|
|
|
|
|
|
- DL_I2C_fillControllerTXFIFO(I2C_1_INST, &txPacket.txBuffer[0], txPacket.txLen);
|
|
|
+ //DL_I2C_fillControllerTXFIFO(I2C_1_INST, &txPacket.txBuffer[0], txPacket.txLen);
|
|
|
|
|
|
+ //Enable the interrupt:
|
|
|
+ DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
// Wait for the bus to be idle
|
|
|
- uint32_t timeout = 10000;
|
|
|
+ uint32_t timeout = 32000;
|
|
|
while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
|
|
|
if(timeout == 0){
|
|
|
printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
|
|
|
DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
//Send command bytes to the target
|
|
|
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
+ //delay_cycles(10000000);
|
|
|
+
|
|
|
DL_I2C_startControllerTransferAdvanced(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen, DL_I2C_CONTROLLER_START_ENABLE, DL_I2C_CONTROLLER_STOP_DISABLE, DL_I2C_CONTROLLER_ACK_ENABLE);
|
|
|
printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
|
|
|
|
|
|
@@ -86,29 +90,26 @@ bool controller_GetBatteryMeasurement(uint8_t slot_id){
|
|
|
|
|
|
|
|
|
// Wait until TX FIFO is empty (TX complete)
|
|
|
- timeout = 10000;
|
|
|
- while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout--);
|
|
|
+ timeout = 32000;
|
|
|
+ while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY) && timeout--);
|
|
|
if (timeout == 0) {
|
|
|
printf("Bus stuck during TX.\n");
|
|
|
DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- //delay_cycles(1000);
|
|
|
-
|
|
|
//Re-initializing te timeout value for Rx:
|
|
|
- timeout= 10000;
|
|
|
+ timeout= 32000;
|
|
|
rxPacket.rxLen= sizeof(BatteryMeasurement);
|
|
|
- rxPacket.rxCount= 0;
|
|
|
rxPacket.rxComplete= false;
|
|
|
-
|
|
|
+ rxPacket.rxCount= 0;
|
|
|
|
|
|
//BatteryMeasurement size is 8 similar to the target side
|
|
|
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
+ //DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
//DL_I2C_startControllerTransferAdvanced(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen, DL_I2C_CONTROLLER_START_ENABLE, DL_I2C_CONTROLLER_STOP_ENABLE, DL_I2C_CONTROLLER_ACK_DISABLE);
|
|
|
- DL_I2C_startControllerTransfer(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen);
|
|
|
+ //DL_I2C_startControllerTransfer(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen);
|
|
|
|
|
|
- while((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout--);
|
|
|
+ while((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY) && timeout--);
|
|
|
if(timeout == 0 || (DL_I2C_getSCLStatus(I2C_1_INST)== DL_I2C_CONTROLLER_SCL_LOW)){
|
|
|
printf("Bus stuck during Rx transmit or SCL held LOW.\n");
|
|
|
DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
@@ -177,4 +178,57 @@ void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *me
|
|
|
printf("[Power Burning PWM] initial state: %d\n", battery_data[slot_id].pwm_value);
|
|
|
DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+bool getBatteryMeasurement_test(){
|
|
|
+
|
|
|
+ //DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
+ //Enable the interrupt:
|
|
|
+ DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
+
|
|
|
+ /* Wait for I2C to be Idle */
|
|
|
+ uint32_t timeout = 10000;
|
|
|
+ while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
|
|
|
+ if(timeout == 0){
|
|
|
+ printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
|
|
|
+ DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Send the packet to the controller.
|
|
|
+ * This function will send Start + Stop automatically.
|
|
|
+ */
|
|
|
+ DL_I2C_startControllerTransferAdvanced(I2C_1_INST,
|
|
|
+ 0x49,
|
|
|
+ DL_I2C_CONTROLLER_DIRECTION_TX,
|
|
|
+ 1,
|
|
|
+ DL_I2C_CONTROLLER_START_ENABLE,
|
|
|
+ DL_I2C_CONTROLLER_STOP_ENABLE,
|
|
|
+ DL_I2C_CONTROLLER_ACK_ENABLE);
|
|
|
+
|
|
|
+ printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
|
|
|
+
|
|
|
+ /* Poll until the Controller writes all bytes */
|
|
|
+ //while (!(DL_I2C_getControllerStatus(I2C_1_INST) &
|
|
|
+ // DL_I2C_CONTROLLER_STATUS_IDLE));
|
|
|
+ while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY);
|
|
|
+
|
|
|
+ /* Add delay between transfers */
|
|
|
+ //delay_cycles(320000);
|
|
|
+ rxPacket.rxCount= 0;
|
|
|
+ rxPacket.rxLen= 8;
|
|
|
+ /* Send a read request to Target */
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, 0x49, DL_I2C_CONTROLLER_DIRECTION_RX, 8);
|
|
|
+ /*DL_I2C_startControllerTransferAdvanced(I2C_1_INST,
|
|
|
+ 0x49,
|
|
|
+ DL_I2C_CONTROLLER_DIRECTION_RX,
|
|
|
+ 8,
|
|
|
+ DL_I2C_CONTROLLER_START_ENABLE,
|
|
|
+ DL_I2C_CONTROLLER_STOP_ENABLE,
|
|
|
+ DL_I2C_CONTROLLER_ACK_ENABLE);*/
|
|
|
+
|
|
|
+ while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY);
|
|
|
+
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|