|
|
@@ -34,54 +34,27 @@ void initialize_target_address() {
|
|
|
// need to select the right one, passing a pointer as an argument
|
|
|
|
|
|
void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
+ if (DL_I2C_isTargetRXFIFOEmpty(i2c)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
uint8_t receivedByte = DL_I2C_receiveTargetData(i2c);
|
|
|
uint8_t receivedCommand = (receivedByte & 0x0F);
|
|
|
uint8_t slot = ((receivedByte & 0xF0) >> 4);
|
|
|
|
|
|
-#ifdef DEBUG_TARGET
|
|
|
- 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
|
|
|
- // variable out and is forced to do as told by the code
|
|
|
- volatile uint8_t rx_buffer[8] = {0};
|
|
|
- /*Handling GET commands with bitmasking*/
|
|
|
- // GET command for ADC(Battery Measurement): Voltage, Current, Temperature
|
|
|
if (receivedCommand == CMD_GET_MEASUREMENT) {
|
|
|
- uint32_t timeout= 10000;
|
|
|
- if (slot > NUM_SLOTS) {
|
|
|
- DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
- return;
|
|
|
- }
|
|
|
- while(!(DL_I2C_getTargetStatus(i2c) & DL_I2C_TARGET_STATUS_BUS_BUSY) && (timeout--));
|
|
|
- if(timeout==0){
|
|
|
- DL_I2C_reset(i2c);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
+ DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
DL_I2C_fillTargetTXFIFO(i2c, (uint8_t *)&battery_slots[slot].measurement, sizeof(BatteryMeasurement));
|
|
|
- //DL_I2C_enableInterrupt(i2c, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER);
|
|
|
-
|
|
|
-/*#ifdef DEBUG_TARGET
|
|
|
- if(DL_I2C_isTargetTXFIFOFull(i2c)== true){
|
|
|
- printf("TX FIFO FULL: Battery Measurement Sent to MCU. \n");
|
|
|
- }else if(DL_I2C_isTargetTXFIFOEmpty(i2c)== true){
|
|
|
- printf("TX FIFO EMPTY: Battery Measurement is not done being sent.\n");
|
|
|
- }
|
|
|
-
|
|
|
-#endif*/
|
|
|
- //printf("");
|
|
|
- //DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
-
|
|
|
} else if (receivedCommand == CMD_SET_CURRENT) {
|
|
|
// Read incoming bytes from the Controller:
|
|
|
uint8_t rx_index = 0;
|
|
|
+ uint8_t rx_buffer[2] = {0x00, 0x00};
|
|
|
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)) {
|
|
|
- rx_buffer[rx_index] = DL_I2C_receiveTargetData(i2c);
|
|
|
- rx_index++;
|
|
|
+ rx_buffer[rx_index] = DL_I2C_receiveTargetData(i2c);
|
|
|
+ rx_index++;
|
|
|
}
|
|
|
}
|
|
|
// we expect 3 bytes:
|
|
|
@@ -116,7 +89,7 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
|
|
|
;
|
|
|
#endif
|
|
|
} */
|
|
|
- } else if (receivedCommand == CMD_CELAR_ERR) {
|
|
|
+ } else if (receivedCommand == CMD_CLEAR_ERR) {
|
|
|
if (slot > NUM_SLOTS) {
|
|
|
DL_I2C_flushTargetTXFIFO(i2c);
|
|
|
return;
|