|
|
@@ -13,15 +13,15 @@ Format: command + ((slot_id) + data (optional))
|
|
|
//Send command to set charge and discharge current to the target
|
|
|
void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_t current_mA){
|
|
|
//Bitmasked: Slot id + Command
|
|
|
- tx_packet.txBuffer[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
|
|
|
+ txPacket.txBuffer[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
|
|
|
//Filling the buffer with current value
|
|
|
- *((int16_t*)(&tx_packet.txBuffer[1])) = current_mA;
|
|
|
+ *((int16_t*)(&txPacket.txBuffer[1])) = current_mA;
|
|
|
|
|
|
/*I2C Communication for transmitting Charging/Discharging current for the slots*/
|
|
|
//Length is calculated as 1 byte for the bitmasked slot and command+ 2 bytes of current + 1 byte of padding
|
|
|
- tx_packet.txLen= 4;
|
|
|
+ txPacket.txLen= 4;
|
|
|
DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
- DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, tx_packet.txLen);
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen);
|
|
|
/*printf("Packet Sent:: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X \n", TARGET_ADDRESS, tx_packet.txBuffer[0], tx_packet.txBuffer[1],tx_packet.txBuffer[2], tx_packet.txBuffer[3]);
|
|
|
DL_I2C_fillControllerTXFIFO(I2C_1_INST, tx_packet.txBuffer, tx_packet.txLen);*/
|
|
|
|
|
|
@@ -48,14 +48,20 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
|
|
|
DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
|
|
|
//Write Command to the target
|
|
|
+ //Clearing out TX FIFO
|
|
|
+ DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
//Set the command in the tx buffer in bit masked format to the target: Upper Nibble-> Slot and Lower Nibbel -> Command
|
|
|
- tx_packet.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.txCount= 0;
|
|
|
+ txPacket.txLen= 1;
|
|
|
+
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_1_INST, &txPacket.txBuffer[0], txPacket.txLen);
|
|
|
|
|
|
//Send command bytes to the target
|
|
|
DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
|
|
|
- printf("[I2C] TX Packet Sent:: 0x%02X\n", tx_packet.txBuffer[0]);
|
|
|
- DL_I2C_fillControllerTXFIFO(I2C_1_INST, tx_packet.txBuffer, 1);
|
|
|
+ printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
|
|
|
+
|
|
|
|
|
|
// Wait for the write transaction to complete
|
|
|
uint32_t tx_timeout = 100000;
|
|
|
@@ -84,8 +90,8 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
|
|
|
while(rx_index < sizeof(BatteryMeasurement)){
|
|
|
if(!DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST)){
|
|
|
//Get byte from the I2C RX FIFO of the target
|
|
|
- rx_packet.rxBuffer[rx_index]= DL_I2C_receiveControllerData(I2C_1_INST);
|
|
|
- printf("Received Bytes[%d]: 0x%02X\n", rx_index, rx_packet.rxBuffer[rx_index]);
|
|
|
+ rxPacket.rxBuffer[rx_index]= DL_I2C_receiveControllerData(I2C_1_INST);
|
|
|
+ printf("Received Bytes[%d]: 0x%02X\n", rx_index, rxPacket.rxBuffer[rx_index]);
|
|
|
rx_index++;
|
|
|
}
|
|
|
}
|
|
|
@@ -93,10 +99,10 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
|
|
|
printf("RX Index: %d\n", rx_index);
|
|
|
//Check if all the data is received then store the battery limits in BatteryInfo struct:
|
|
|
if(rx_index== (sizeof(BatteryMeasurement))){
|
|
|
- measurement.voltage= rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8);
|
|
|
- measurement.current= rx_packet.rxBuffer[2]|(rx_packet.rxBuffer[3] << 8);
|
|
|
- measurement.temperature = rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8);
|
|
|
- measurement.slot_state = (SlotState)(rx_packet.rxBuffer[6]);
|
|
|
+ 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", sizeof(BatteryMeasurement), TARGET_ADDRESS);
|
|
|
@@ -115,12 +121,12 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
|
|
|
void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
|
|
|
uint8_t command= CMD_CLEAR_ERR| (slot_id<<4); //shift slot_id to the left by 4 bits
|
|
|
printf("[MCU] Clear Error Bitmasked Command:: 0x%02X\n", command);
|
|
|
- tx_packet.txBuffer[0]= command;
|
|
|
- tx_packet.txBuffer[1]= slot_id;
|
|
|
- tx_packet.txLen= sizeof(tx_packet.txBuffer);
|
|
|
+ txPacket.txBuffer[0]= command;
|
|
|
+ txPacket.txBuffer[1]= slot_id;
|
|
|
+ txPacket.txLen= sizeof(txPacket.txBuffer);
|
|
|
while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
|
|
|
- DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, tx_packet.txLen);
|
|
|
- DL_I2C_fillControllerTXFIFO(I2C_1_INST, tx_packet.txBuffer, tx_packet.txLen);
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen);
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
|
|
|
while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
|
|
|
DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
}
|