|
|
@@ -47,108 +47,6 @@ 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);
|
|
|
-
|
|
|
- //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.txLen= 1;
|
|
|
- //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);
|
|
|
-
|
|
|
- //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 = 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
|
|
|
- //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]);
|
|
|
-
|
|
|
-
|
|
|
- //If I2C Bus is stuck then reset the controller:
|
|
|
- if(DL_I2C_getControllerStatus(I2C_1_INST)&(DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
|
- printf("ERROR ***I2C Write Error: Bus is stuck***\n");
|
|
|
- DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // Wait until TX FIFO is empty (TX complete)
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- //Re-initializing te timeout value for Rx:
|
|
|
- timeout= 32000;
|
|
|
- rxPacket.rxLen= sizeof(BatteryMeasurement);
|
|
|
- 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_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);
|
|
|
-
|
|
|
- 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);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- //while(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
|
|
|
-
|
|
|
- //DEBUG
|
|
|
- printf("Rx Count: %d\n", rxPacket.rxCount);
|
|
|
- printf("Rx Complete: %d\n", rxPacket.rxComplete);
|
|
|
- //Check if all the data is received then store the battery limits in BatteryInfo struct:
|
|
|
- if(rxPacket.rxCount== (sizeof(BatteryMeasurement))){
|
|
|
- measurement.voltage= rxPacket.rxBuffer[0] | (rxPacket.rxBuffer[1] << 8);
|
|
|
- measurement.current= rxPacket.rxBuffer[2]|(rxPacket.rxBuffer[3] << 8);
|
|
|
- measurement.temperature = rxPacket.rxBuffer[4] | (rxPacket.rxBuffer[5] << 8);
|
|
|
- measurement.slot_state = (SlotState)(rxPacket.rxBuffer[6]);
|
|
|
-
|
|
|
- battery_data[slot_id].battery_measurement= measurement;
|
|
|
- //DEBUG
|
|
|
- printf("[I2C] Successfully read %d bytes from target 0x%02X\n", rxPacket.rxCount, target_address);
|
|
|
- printf("Voltage: %u\n", battery_data[slot_id].battery_measurement.voltage);
|
|
|
- printf("Current: %d\n", battery_data[slot_id].battery_measurement.current);
|
|
|
- printf("Temp: %u\n", battery_data[slot_id].battery_measurement.temperature);
|
|
|
- printf("Slot state: %u\n", battery_data[slot_id].battery_measurement.slot_state);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- return false;
|
|
|
-}
|
|
|
-
|
|
|
//Clear error flag to the target to change it back to SLOT_STATE_OK
|
|
|
//Format: command + ((slot_id) + data (optional))
|
|
|
void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
|
|
|
@@ -187,64 +85,16 @@ void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *me
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/*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]);
|
|
|
-
|
|
|
- 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;
|
|
|
-
|
|
|
- DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
-
|
|
|
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
-
|
|
|
- // Send a read request to Targe
|
|
|
- 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);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- printf("Received Bytes[%d]: 0x%02X\n", rxPacket.rxCount, rxPacket.rxBuffer[rxPacket.rxCount]);
|
|
|
-
|
|
|
- return true;
|
|
|
-}*/
|
|
|
|
|
|
// Following code worked with ADC as the target with now clocking issues:
|
|
|
-bool getBatteryMeasurement_test(uint8_t slot_id){
|
|
|
+bool getBatteryMeasurement(uint8_t slot_id){
|
|
|
BatteryMeasurement measurement;
|
|
|
|
|
|
+ //Dynamic addressing:
|
|
|
+
|
|
|
+ uint8_t target_address= TARGET_BASE_ADDRESS + ((slot_id & 0b00000100) >> 2);
|
|
|
+ printf("Target Address 0x%02X \n", target_address);
|
|
|
+
|
|
|
//Enable the Interrupt:
|
|
|
DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
|
|
|
@@ -252,8 +102,9 @@ bool getBatteryMeasurement_test(uint8_t slot_id){
|
|
|
txPacket.txComplete= false;
|
|
|
txPacket.txLen= 1;
|
|
|
txPacket.txBuffer[0] = (0<<4) | (CMD_GET_MEASUREMENT & 0x0F);
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
|
|
|
|
|
|
/* Wait for I2C to be Idle */
|
|
|
uint32_t timeout = 10000;
|
|
|
@@ -264,6 +115,8 @@ bool getBatteryMeasurement_test(uint8_t slot_id){
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/* Send the packet to the controller.
|
|
|
* This function will send Start + Stop automatically.
|
|
|
*/
|
|
|
@@ -293,7 +146,7 @@ bool getBatteryMeasurement_test(uint8_t slot_id){
|
|
|
rxPacket.rxComplete= false;
|
|
|
timeout= 32000;
|
|
|
|
|
|
- //DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
+ //Make RX Buffer ready to receive the data
|
|
|
DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
|
|
|
DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
@@ -317,13 +170,16 @@ bool getBatteryMeasurement_test(uint8_t slot_id){
|
|
|
measurement.current= rxPacket.rxBuffer[2]|(rxPacket.rxBuffer[3] << 8);
|
|
|
measurement.temperature = rxPacket.rxBuffer[4] | (rxPacket.rxBuffer[5] << 8);
|
|
|
measurement.slot_state = (SlotState)(rxPacket.rxBuffer[6]);
|
|
|
+
|
|
|
+ //Storing the measurements in Battery structure:
|
|
|
battery_data[slot_id].battery_measurement= measurement;
|
|
|
|
|
|
//DEBUG
|
|
|
- printf("[I2C] Successfully read %d bytes from target 0x%02X\n", rxPacket.rxCount, TARGET_BASE_ADDRESS);
|
|
|
+ //printf("[I2C] Successfully read %d bytes from target 0x%02X\n", rxPacket.rxCount, TARGET_BASE_ADDRESS);
|
|
|
printf("Voltage: %u\n", battery_data[slot_id].battery_measurement.voltage);
|
|
|
printf("Current: %d\n", battery_data[slot_id].battery_measurement.current);
|
|
|
printf("Temp: %u\n", battery_data[slot_id].battery_measurement.temperature);
|
|
|
printf("Slot state: %u\n", battery_data[slot_id].battery_measurement.slot_state);
|
|
|
+
|
|
|
return true;
|
|
|
}
|