|
|
@@ -33,9 +33,12 @@ void initialize_target_address() {
|
|
|
// need to select the right one, passing a pointer as an argument
|
|
|
|
|
|
void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
- uint8_t receivedCommand = DL_I2C_receiveTargetData(i2c);
|
|
|
+ uint8_t receivedByte = DL_I2C_receiveTargetData(i2c);
|
|
|
+ uint8_t receivedCommand = (receivedByte & 0x0F);
|
|
|
+ uint8_t slot = (receivedByte & 0xF0);
|
|
|
+
|
|
|
#ifdef DEBUG_TARGET
|
|
|
- printf("[SLAVE] Received Command: 0x%02X\n", receivedCommand);
|
|
|
+ printf("[SLAVE] Received Byte: 0x%02X\n", receivedByte);
|
|
|
#endif
|
|
|
uint8_t tx_buffer[8] = {0};
|
|
|
// changed to volatile variable, so that the compiler cannot optimize the
|
|
|
@@ -43,8 +46,7 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
volatile uint8_t rx_buffer[8] = {0};
|
|
|
/*Handling GET commands with bitmasking*/
|
|
|
// GET command for ADC(Battery Measurement): Voltage, Current, Temperature
|
|
|
- if ((receivedCommand & 0x0F) == CMD_GET_MEASUREMENT) {
|
|
|
- uint8_t slot = receivedCommand & 0xF0;
|
|
|
+ if (receivedCommand == CMD_GET_MEASUREMENT) {
|
|
|
if (slot > NUM_SLOTS) {
|
|
|
DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
return;
|
|
|
@@ -61,7 +63,7 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
} else if (receivedCommand == CMD_SET_CURRENT) {
|
|
|
// Read incoming bytes from the Controller:
|
|
|
uint8_t rx_index = 0;
|
|
|
- while (rx_index < 4) {
|
|
|
+ while (rx_index < 2) {
|
|
|
// TODO: Need to have a workaround, currently the code is getting stuck on
|
|
|
// the first trigger and provides result on the second trigger
|
|
|
if (!DL_I2C_isTargetRXFIFOEmpty(i2c)) {
|
|
|
@@ -69,23 +71,21 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
rx_index++;
|
|
|
}
|
|
|
}
|
|
|
- // we expect 4 bytes:
|
|
|
- // 1. command
|
|
|
- // 2. slot_id
|
|
|
- // 3 + 4. current (int16)
|
|
|
- if (rx_index != 4) {
|
|
|
+ // we expect 3 bytes:
|
|
|
+ // 1. <slot_id><command>
|
|
|
+ // 2 + 3. current (int16)
|
|
|
+ if (rx_index != 2) {
|
|
|
#ifdef DEBUG_TARGET
|
|
|
- printf("ERROR: Incomplete I2C Rx: received %d%zu bytes\n", rx_index, 4);
|
|
|
+ printf("ERROR: Incomplete I2C Rx: received %d bytes\n", rx_index);
|
|
|
#endif
|
|
|
DL_I2C_flushTargetRXFIFO(i2c);
|
|
|
rx_index = 0;
|
|
|
return;
|
|
|
}
|
|
|
- uint8_t slot = rx_buffer[1]; // first byte is the slot id (0..3)
|
|
|
- battery_slots[slot].set_current = *((int16_t*)(&rx_buffer[2])); // byte 3+4 is the current
|
|
|
+ battery_slots[slot].set_current = *((int16_t*)(&rx_buffer[0])); // byte 2+3 is the current
|
|
|
|
|
|
#ifdef DEBUG_TARGET
|
|
|
- printf("Slot id: %d, Current: %" SCNd16 "\n", slot, battery_slots[slot].set_current);
|
|
|
+ printf("Slot id: %d, Current: %" SCNd16 " (Bytes 0x%02X 0x%02X)\n", slot, battery_slots[slot].set_current, rx_buffer[0], rx_buffer[1]);
|
|
|
#endif
|
|
|
/*
|
|
|
// This code is for debugging:
|
|
|
@@ -104,7 +104,6 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
#endif
|
|
|
} */
|
|
|
} else if (receivedCommand == CMD_CELAR_ERR) {
|
|
|
- uint8_t slot = receivedCommand & 0xF0;
|
|
|
if (slot > NUM_SLOTS) {
|
|
|
DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
return;
|