|
|
@@ -120,266 +120,125 @@ void I2C_controller_INST_IRQHandler(void) {
|
|
|
}
|
|
|
|
|
|
/**** Interrupt for Pi to MCU ****/
|
|
|
-
|
|
|
void I2C_target_INST_IRQHandler(void) {
|
|
|
-
|
|
|
- // printf("I2C Interrupt Triggered to MCU (TARGET)!\n");
|
|
|
uint8_t receivedCommand = 0;
|
|
|
+ uint8_t battery_state[8] = {0x00};
|
|
|
uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
|
|
|
- // ADC_PARAMS params;
|
|
|
+ printf("status: %d\n", status);
|
|
|
switch (status) {
|
|
|
-
|
|
|
- /* START condition detected */
|
|
|
case DL_I2C_IIDX_TARGET_START:
|
|
|
+ /*Initialize Tx or RX after Start condition is recieved*/
|
|
|
piTxCount = 0;
|
|
|
piRxCount = 0;
|
|
|
piTxComplete = false;
|
|
|
DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
break;
|
|
|
-
|
|
|
- /* STOP condition detected */
|
|
|
case DL_I2C_IIDX_TARGET_STOP:
|
|
|
piTxComplete = true;
|
|
|
piRxComplete = true;
|
|
|
DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
+ DL_I2C_flushTargetRXFIFO(I2C_target_INST);
|
|
|
break;
|
|
|
-
|
|
|
- /* TX FIFO trigger (Pi is reading data from MCU) */
|
|
|
- /* GET battery status is triggered when command is 0x01
|
|
|
- - Pi on request of 0x01 will get a response of the battery status for all
|
|
|
- the slots
|
|
|
- - Battery_StateUpdate function is called, which in turn calls the
|
|
|
- Battery_ReadState funtion to set the state of the batteries -Pi on command
|
|
|
- of [0x02, slot_id] will GET the 'Battery Data' which is voltage, current and
|
|
|
- temperature for a given slot.
|
|
|
- - MCU reads the slot_id from Pi using DL_I2C_receiveTargetData()
|
|
|
- - piTxCount is set to 0
|
|
|
- - piTxLen is the sizeof BatteryData struct which is 7 bytes
|
|
|
- - If the requested slot is correct then:
|
|
|
- - battery pointer variable points to the memory of the requested
|
|
|
- slot
|
|
|
- - the values of voltage, current and temperature are then stored in
|
|
|
- battery_data struct
|
|
|
- - Once the values are in BatteryData struct we wait for the bus to be
|
|
|
- free
|
|
|
- - Next we send the BatteryData to Pi using DL_I2C_fillTargetRXFIFO()
|
|
|
- - Reset the RX counter for the next data.
|
|
|
- */
|
|
|
-
|
|
|
- case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
|
|
|
- break;
|
|
|
- if (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
|
|
|
- receivedCommand = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
- // printf("Received Command: 0x%02X\n", receivedCommand);
|
|
|
-
|
|
|
- else {
|
|
|
- /*
|
|
|
- * Fill FIFO with 0x00 if more data is requested than expected piTxLen
|
|
|
- */
|
|
|
- while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
|
|
|
- ;
|
|
|
- }
|
|
|
- piTxComplete = true;
|
|
|
- } else if (receivedCommand == CMD_GET_BATTERY_DATA) {
|
|
|
-
|
|
|
- uint8_t requestedSlot = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) &
|
|
|
- DL_I2C_TARGET_STATUS_BUS_BUSY)
|
|
|
- ;
|
|
|
- // printf("Battery Data Requested for Slot %d!\n", requestedSlot);
|
|
|
- piTxCount = 0;
|
|
|
- piTxLen = sizeof(BatteryData);
|
|
|
- BatteryData battery_data;
|
|
|
- if (requestedSlot < NUM_SLOTS) {
|
|
|
- Battery *battery = &batteries[requestedSlot];
|
|
|
- battery_data.slot_id = battery->slot_id;
|
|
|
- battery_data.voltage = battery->voltage;
|
|
|
- battery_data.current = battery->current;
|
|
|
- battery_data.temperature = battery->temperature;
|
|
|
-
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) &
|
|
|
- DL_I2C_TARGET_STATUS_BUS_BUSY)
|
|
|
- ;
|
|
|
-
|
|
|
- DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data,
|
|
|
- sizeof(BatteryData));
|
|
|
-
|
|
|
- // piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST,
|
|
|
- // (uint8_t*)&battery_data, piTxLen);
|
|
|
-
|
|
|
- piTxComplete = true;
|
|
|
-
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) &
|
|
|
- DL_I2C_TARGET_STATUS_BUS_BUSY)
|
|
|
- ;
|
|
|
-
|
|
|
- if (piTxCount >= piTxLen) {
|
|
|
- piTxComplete = true;
|
|
|
- piTxCount = 0;
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
- // printf("Invalid Slot ID: %d\n.", requestedSlot);
|
|
|
- }
|
|
|
- }
|
|
|
+ case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
|
|
|
+ printf("Rx Interrupt Triggered \n");
|
|
|
+ if (DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ receivedCommand = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ if (receivedCommand == CMD_GET_BATTERY_STATUS) {
|
|
|
+ printf("Received Command: 0x%02X\n", receivedCommand);
|
|
|
+ for (uint8_t slot = 0; slot < NUM_SLOTS; slot++) {
|
|
|
+ battery_state[slot] = batteries[slot].state;
|
|
|
+ }
|
|
|
|
|
|
- break;
|
|
|
-
|
|
|
- /* TARGET_Rx FIFO trigger (Pi is writing data to MCU) */
|
|
|
- /*Pi SET battery data limits for each slot, where:
|
|
|
- - RXFIFO buffer is filled if the command from Pi is 0x03
|
|
|
- - Creating a temporary buffer named ´rxbuffer´
|
|
|
- - sizeof(BatteryLimitMsg): 11 bytes (1 byte: slot_id, 2 bytes: min_voltage;
|
|
|
- max_voltage; cut_off_current; capacitance; charge_fraction)
|
|
|
- - rx_buffer stores the data from Pi.
|
|
|
- - if all the expected bytes are received from Pi then,
|
|
|
- - memcpy() to copy the block of address from the temporary buffer to the
|
|
|
- BatteryLimitMsg structure
|
|
|
- - Why?, A: It copies the specified number of bytes from one memory
|
|
|
- location to another regardless of the type of the data stored.
|
|
|
- - verify if the received slot_id is less than NUM_SLOTS, where slot_id
|
|
|
- count starts from 0 then:
|
|
|
- - create a pointer variable for 'Battery'
|
|
|
- - battery_limits.slot_id: index of the battery slot to be updated
|
|
|
- - &batteries[battery_limits.slot_id]: gets the memory address of the
|
|
|
- battery in that slot
|
|
|
- - Accessing the structure members of Battery using -> operator. This
|
|
|
- allows efficient access to the structure's members without directly using
|
|
|
- the structure variable.
|
|
|
+ DL_I2C_fillTargetTXFIFO(I2C_target_INST, battery_state, 8);
|
|
|
+ while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
|
|
|
+ ;
|
|
|
+ printf("Sent Data\n");
|
|
|
+ }
|
|
|
|
|
|
+ /*
|
|
|
+ To receive the Battery Mesaurements from MCU, bit masking is applied
|
|
|
+ where:
|
|
|
+ - command type is upper 4 bits: 0010 (for CMD_GET_BATTERY_DATA)->
|
|
|
+ consistent
|
|
|
+ - slot_id is lower 3 bits, since NUM_SLOTS are 8 so it ranges from 000
|
|
|
+ till 111
|
|
|
+ - bit mask for command is 0xF0: 11110000
|
|
|
+ - bit mask for requested slot is 0x07: 00000111
|
|
|
+ receivedCommand now ranges from 0x20 till 0x27
|
|
|
*/
|
|
|
- case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
|
|
|
- if (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
|
|
|
|
|
|
- receivedCommand = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ else if ((receivedCommand & 0xF0) == 0x20) {
|
|
|
|
|
|
- if (receivedCommand == CMD_SET_BATTERY_LIMIT) {
|
|
|
- uint8_t rx_buffer[sizeof(BatteryLimitMsg)];
|
|
|
- uint8_t index = 0;
|
|
|
+ //printf("Received Command: 0x%02X\n", receivedCommand);
|
|
|
+ uint8_t requestedSlot = (receivedCommand & 0x07);
|
|
|
+ //printf("Requested slot:%d\n", requestedSlot);
|
|
|
+ // piTxCount = 0;
|
|
|
+ piTxLen = sizeof(BatteryData);
|
|
|
+ BatteryData battery_data;
|
|
|
|
|
|
- while (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
|
|
|
- if (index < sizeof(BatteryLimitMsg)) {
|
|
|
- rx_buffer[index] = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
- // printf("Received Byte[%d]: 0x%02X\n", index, rx_buffer[index]);
|
|
|
- index++;
|
|
|
-
|
|
|
- } else {
|
|
|
- DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // printf("Total Bytes Received: %d (Expected: %d)\n", index,
|
|
|
- // sizeof(BatteryLimitMsg));
|
|
|
-
|
|
|
- if (index == sizeof(BatteryLimitMsg)) {
|
|
|
- // printf("Received Battery Limits.\n");
|
|
|
- BatteryLimitMsg battery_limits;
|
|
|
- memcpy(&battery_limits, rx_buffer, sizeof(BatteryLimitMsg));
|
|
|
- if (battery_limits.slot_id < NUM_SLOTS) {
|
|
|
- Battery *battery = &batteries[battery_limits.slot_id];
|
|
|
- battery->min_voltage = battery_limits.min_voltage;
|
|
|
- battery->max_voltage = battery_limits.max_voltage;
|
|
|
- battery->cut_off_current = battery_limits.cut_off_current;
|
|
|
- battery->capacitance = battery_limits.capacitance;
|
|
|
- battery->charge_fraction = battery_limits.charge_fraction;
|
|
|
- /*printf("\n Received Battery Limits for slot %d: \n",
|
|
|
- battery_limits.slot_id); printf(" Min Voltage: %d mV
|
|
|
- (0x%04X)\n", battery_limits.min_voltage,
|
|
|
- battery_limits.min_voltage); printf(" Max Voltage: %d mV
|
|
|
- (0x%04X)\n", battery_limits.max_voltage,
|
|
|
- battery_limits.max_voltage); printf(" Cutoff Current: %d mA
|
|
|
- (0x%04X)\n", battery_limits.cut_off_current,
|
|
|
- battery_limits.cut_off_current); printf(" Capacitance: %d µF
|
|
|
- (0x%04X)\n", battery_limits.capacitance,
|
|
|
- battery_limits.capacitance); printf(" Charge Fraction: %d%%
|
|
|
- (0x%02X)\n", battery_limits.charge_fraction,
|
|
|
- battery_limits.charge_fraction);*/
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (receivedCommand == CMD_GET_BATTERY_STATUS) {
|
|
|
- uint8_t test[8] = {0x00};
|
|
|
- // for testing:
|
|
|
- Battery batteries[NUM_SLOTS] = {
|
|
|
- {0, STATE_BATTERY_DETECTED, 3700, 500, 25, 3000, 4200, 2000, 10000, 80}
|
|
|
- };
|
|
|
- //Battery_StateUpdate();
|
|
|
+ if (requestedSlot >= NUM_SLOTS) {
|
|
|
+ //printf("Requested Slot is not valid.\n");
|
|
|
+ DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // Prepare data to be sent to Pi:
|
|
|
- for (uint8_t slot = 0; slot < NUM_SLOTS; slot++) {
|
|
|
- // Read the battery status for each slot
|
|
|
- //Battery_ReadState(slot);
|
|
|
- test[slot] = batteries[slot].state;
|
|
|
- }
|
|
|
- // Filling up the FIFO
|
|
|
- DL_I2C_fillTargetTXFIFO(I2C_target_INST, &test, 8);
|
|
|
- while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
|
|
|
- ;
|
|
|
- printf("Sent Data\n");
|
|
|
+ Battery *battery = &batteries[requestedSlot];
|
|
|
+ battery_data.slot_id = battery->slot_id; // !TODO: recheck if necessary
|
|
|
+ //printf("slot_id:%u\n", battery_data.slot_id);
|
|
|
+ battery_data.voltage = battery->voltage;
|
|
|
+ //printf("voltage:%u\n", battery_data.voltage);
|
|
|
+ battery_data.current = battery->current;
|
|
|
+ //printf("current:%u\n", battery_data.current);
|
|
|
+ battery_data.temperature = battery->temperature;
|
|
|
+ //printf("temperature:%u\n", battery_data.temperature);
|
|
|
+ DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data,
|
|
|
+ piTxLen);
|
|
|
+
|
|
|
+ /*piTxComplete= true;
|
|
|
+ if(piTxCount >= piTxLen){
|
|
|
+ piTxComplete= true;
|
|
|
+ piTxCount= 0;
|
|
|
+ }*/
|
|
|
+ printf("Battery Measurement Sent. \n");
|
|
|
+ DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
|
|
|
+ }
|
|
|
|
|
|
+ else if (receivedCommand == CMD_SET_BATTERY_LIMIT) {
|
|
|
+ uint8_t rx_buffer[sizeof(BatteryLimitMsg)];
|
|
|
+ uint8_t index = 0;
|
|
|
|
|
|
- /*if (piTxCount < piTxLen) {
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) &
|
|
|
- DL_I2C_TARGET_STATUS_BUS_BUSY)
|
|
|
- ;
|
|
|
- piTxCount += DL_I2C_fillTargetTXFIFO(
|
|
|
- I2C_target_INST, &piTxPacket[piTxCount], (piTxLen - piTxCount));
|
|
|
+ while (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
|
|
|
+ if (index < sizeof(BatteryLimitMsg)) {
|
|
|
+ rx_buffer[index] = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ index++;
|
|
|
} else {
|
|
|
- /*
|
|
|
- * Fill FIFO with 0x00 if more data is requested than expected piTxLen
|
|
|
- */
|
|
|
- /*while (DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false)
|
|
|
- ;
|
|
|
- }*/
|
|
|
- } else if (receivedCommand == CMD_GET_BATTERY_DATA) {
|
|
|
-
|
|
|
- uint8_t requestedSlot = DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) &
|
|
|
- DL_I2C_TARGET_STATUS_BUS_BUSY)
|
|
|
- ;
|
|
|
- // printf("Battery Data Requested for Slot %d!\n", requestedSlot);
|
|
|
- piTxCount = 0;
|
|
|
- piTxLen = sizeof(BatteryData);
|
|
|
- BatteryData battery_data;
|
|
|
- if (requestedSlot < NUM_SLOTS) {
|
|
|
- Battery *battery = &batteries[requestedSlot];
|
|
|
- battery_data.slot_id = battery->slot_id;
|
|
|
- battery_data.voltage = battery->voltage;
|
|
|
- battery_data.current = battery->current;
|
|
|
- battery_data.temperature = battery->temperature;
|
|
|
-
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) &
|
|
|
- DL_I2C_TARGET_STATUS_BUS_BUSY)
|
|
|
- ;
|
|
|
-
|
|
|
- DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data,
|
|
|
- sizeof(BatteryData));
|
|
|
-
|
|
|
- // piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST,
|
|
|
- // (uint8_t*)&battery_data, piTxLen);
|
|
|
-
|
|
|
- piTxComplete = true;
|
|
|
-
|
|
|
- while (DL_I2C_getTargetStatus(I2C_target_INST) &
|
|
|
- DL_I2C_TARGET_STATUS_BUS_BUSY)
|
|
|
- ;
|
|
|
-
|
|
|
- if (piTxCount >= piTxLen) {
|
|
|
- piTxComplete = true;
|
|
|
- piTxCount = 0;
|
|
|
- }
|
|
|
+ DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ if (index == sizeof(BatteryLimitMsg)) {
|
|
|
+ BatteryLimitMsg battery_limits;
|
|
|
+ memcpy(&battery_limits, rx_buffer, sizeof(BatteryLimitMsg));
|
|
|
+ if (battery_limits.slot_id < NUM_SLOTS) {
|
|
|
+ Battery *battery = &batteries[battery_limits.slot_id];
|
|
|
+ battery->min_voltage = battery_limits.min_voltage;
|
|
|
+ battery->max_voltage = battery_limits.max_voltage;
|
|
|
+ battery->cut_off_current = battery_limits.cut_off_current;
|
|
|
+ battery->capacitance = battery_limits.capacitance;
|
|
|
+ battery->charge_fraction = battery_limits.charge_fraction;
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
break;
|
|
|
-
|
|
|
- /* Arbitration lost or NACK */
|
|
|
+ case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
|
|
|
+ break;
|
|
|
case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
|
|
|
- printf("Arbitration Lost.\n");
|
|
|
break;
|
|
|
default:
|
|
|
- printf("Unknown Interrupt.\n");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -410,15 +269,18 @@ int main(void) {
|
|
|
// delay_cycles(50000);
|
|
|
// DAC_ReadCurrentAddress();
|
|
|
|
|
|
- while (1) { // Looping through the ADC Channels
|
|
|
- /*for(uint8_t slot_id=0; slot_id< NUM_SLOTS; slot_id++){
|
|
|
- for(uint8_t adc_channel=0; adc_channel< ADC_CHANNEL_NUM; adc_channel++){
|
|
|
- batteries[slot_id].channel= adc_channel;
|
|
|
- Battery_UpdateADCReading(slot_id, batteries[slot_id].channel);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }*/
|
|
|
+ while (1) {
|
|
|
+ // Looping through the ADC Channels
|
|
|
+ for (uint8_t slot_id = 0; slot_id < NUM_SLOTS; slot_id++) {
|
|
|
+ for (uint8_t adc_channel = 0; adc_channel < ADC_CHANNEL_NUM;
|
|
|
+ adc_channel++) {
|
|
|
+ Battery_UpdateADCReading(slot_id, adc_channel);
|
|
|
+ }
|
|
|
+ Battery_ReadState(slot_id);
|
|
|
+ }
|
|
|
+ printf("Battery Details: Slot: %u, Voltage:%u, Current: %u \n, State:%u\n:",
|
|
|
+ batteries[0].slot_id, batteries[0].voltage, batteries[0].current,
|
|
|
+ batteries[0].state);
|
|
|
|
|
|
// CC-CV Cycle: maximum cycles is not yet implemented
|
|
|
// for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
|