|
|
@@ -40,26 +40,12 @@
|
|
|
#include "battery.h"
|
|
|
#include "i2c_target.h"
|
|
|
#include "cc_cv_charging.h"
|
|
|
+#include <ti/drivers/GPIO.h>
|
|
|
+
|
|
|
|
|
|
I2C_Instance gI2C;
|
|
|
I2C_ResponseInfo gResponse;
|
|
|
BatteryData battery_data;
|
|
|
-
|
|
|
-/*****************/
|
|
|
-/*Register for the MCU*/
|
|
|
-#define REGISTER_SIZE 256
|
|
|
-uint8_t registers[REGISTER_SIZE]= {1, 2, 3, 4, 5};
|
|
|
-
|
|
|
-
|
|
|
-/*Counters for TX length and bytes sent*/
|
|
|
-uint32_t gTxLen, gTxCount;
|
|
|
-/* Counters for RX length and bytes sent*/
|
|
|
-uint32_t gRxLen, gRxCount;
|
|
|
-
|
|
|
-/* Boolean to know when a stop command was issued */
|
|
|
-bool gStopReceived = false;
|
|
|
-uint8_t gTxData[MAX_DATA_SIZE] = {0} ;
|
|
|
-
|
|
|
/*
|
|
|
Scans all the addresses of the peripherals:
|
|
|
*/
|
|
|
@@ -88,35 +74,6 @@ void I2C_scanBus() {
|
|
|
printf("I2C Scan Complete!\n");
|
|
|
}
|
|
|
|
|
|
-/*Function to get the RDY bit from ADC*/
|
|
|
-
|
|
|
-bool Check_ADC_Ready()
|
|
|
-{
|
|
|
- uint8_t config_byte = 0;
|
|
|
-
|
|
|
- /*
|
|
|
- READY BIT:
|
|
|
- This bit is the data ready flag. In read mode, this bit indicates if the output register has been updated
|
|
|
- with a latest conversion result. In One-Shot Conversion mode, writing this bit to “1” initiates a new
|
|
|
- conversion.
|
|
|
-
|
|
|
- 1= Output Register has not been updated
|
|
|
- 0= Output Register has been updated
|
|
|
-
|
|
|
- */
|
|
|
-
|
|
|
- DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
|
|
|
- config_byte = DL_I2C_receiveControllerData(I2C_controller_INST);
|
|
|
- return (config_byte & 0x80) == 0; // Check if RDY bit is cleared in bit 7
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
-Function to initialize MCU as target: Pi communicates as the Controller and MCU as the Target
|
|
|
-*/
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/*Interrupt for MCU -> ADC
|
|
|
* CASE: DL_I2C_IIDX_CONTROLLER_RX_DONE: ADC Reception Complete
|
|
|
- ADC has finished sending data and it's fully received.
|
|
|
@@ -166,28 +123,31 @@ void I2C_controller_INST_IRQHandler(void)
|
|
|
DL_I2C_receiveControllerData(I2C_controller_INST);
|
|
|
}
|
|
|
}
|
|
|
- if (gRxADCcount == gRxADClen){
|
|
|
+ if (gRxADCcount >= gRxADClen){
|
|
|
//printf("ADC Bytes Received!\n");
|
|
|
+ gRxComplete = true;
|
|
|
DL_I2C_enableInterrupt(I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
|
|
|
- //gRxComplete = true;
|
|
|
+
|
|
|
}
|
|
|
break;
|
|
|
/*TRANSMIT data to ADC*/
|
|
|
case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
|
|
|
- printf("TX FIFO with data!\n");
|
|
|
+ //printf("TX FIFO with data!\n");
|
|
|
gI2C.status= I2C_STATUS_TX_INPROGRESS;
|
|
|
if(gTxADCcount<gTxADClen){
|
|
|
gTxADCcount+= DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &gTxPacket[gTxADCcount], (gTxADClen-gTxADCcount));
|
|
|
} else{
|
|
|
/*Prevent overflow and just ignore data*/
|
|
|
DL_I2C_fillTargetTXFIFO(I2C_controller_INST, (uint8_t[]){0x00}, 1);
|
|
|
+ gTxComplete= true;
|
|
|
}
|
|
|
break;
|
|
|
/*STOP condition*/
|
|
|
case DL_I2C_IIDX_CONTROLLER_STOP:
|
|
|
gTxComplete= true;
|
|
|
gRxComplete = true;
|
|
|
- printf("I2C Stop Detected- RX Complete");
|
|
|
+ //printf("I2C Stop Detected- RX Complete");
|
|
|
+ break;
|
|
|
case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
|
|
|
//printf("Interrupt index for I2C controller Arbitration Lost!\n");
|
|
|
break;
|
|
|
@@ -224,12 +184,13 @@ void I2C_target_INST_IRQHandler(void) {
|
|
|
case DL_I2C_IIDX_TARGET_STOP:
|
|
|
piTxComplete= true;
|
|
|
piRxComplete = true;
|
|
|
+ DL_I2C_flushTargetTXFIFO(I2C_target_INST);
|
|
|
break;
|
|
|
|
|
|
/* TX FIFO trigger (Pi is reading data from MCU) */
|
|
|
- /*TXFIFO to 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
|
|
|
+ /* 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
|
|
|
@@ -238,23 +199,31 @@ void I2C_target_INST_IRQHandler(void) {
|
|
|
- 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_fillTargetTXFIFO()
|
|
|
- - Reset the TX counter for the next data.
|
|
|
+ - 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:
|
|
|
- printf("TX to PI triggered!\n");
|
|
|
- if(!DL_I2C_isTargetTXFIFOEmpty(I2C_target_INST)){
|
|
|
+ printf("Pi GET data from MCU!\n");
|
|
|
+ if(!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)){
|
|
|
receivedCommand= DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
printf("Received Command: 0x%02X\n", receivedCommand);
|
|
|
|
|
|
if (receivedCommand == CMD_GET_BATTERY_STATUS){
|
|
|
printf("Battery status received.\n");
|
|
|
- Battery_StateUpdate();
|
|
|
+ //Battery_StateUpdate();
|
|
|
+ piTxCount= 0;
|
|
|
+ piTxLen= NUM_SLOTS;
|
|
|
+ piTxComplete = false;
|
|
|
+ // 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);
|
|
|
+ piTxPacket[slot]= batteries[slot].state;
|
|
|
+ }
|
|
|
+ //Filling up the FIFO
|
|
|
if(piTxCount < piTxLen){
|
|
|
while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
|
|
|
- DL_I2C_fillTargetTXFIFO(I2C_target_INST, &piTxPacket[piTxCount], 1);
|
|
|
- piTxCount++;
|
|
|
-
|
|
|
+ piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST, &piTxPacket[piTxCount], (piTxLen-piTxCount));
|
|
|
}
|
|
|
else {
|
|
|
/*
|
|
|
@@ -263,12 +232,13 @@ void I2C_target_INST_IRQHandler(void) {
|
|
|
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!\n");
|
|
|
+ printf("Battery Data Requested for Slot %d!\n", requestedSlot);
|
|
|
piTxCount= 0;
|
|
|
piTxLen= sizeof(BatteryData);
|
|
|
BatteryData battery_data;
|
|
|
@@ -281,7 +251,11 @@ void I2C_target_INST_IRQHandler(void) {
|
|
|
|
|
|
while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
|
|
|
|
|
|
- piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t*)&battery_data, piTxLen);
|
|
|
+ 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);
|
|
|
|
|
|
@@ -317,7 +291,7 @@ void I2C_target_INST_IRQHandler(void) {
|
|
|
|
|
|
*/
|
|
|
case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
|
|
|
- printf("RX FIFO triggered from PI.\n");
|
|
|
+ printf("Pi SET Battery limit to MCU.....\n");
|
|
|
if(!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)){
|
|
|
receivedCommand= DL_I2C_receiveTargetData(I2C_target_INST);
|
|
|
printf("Received Command: 0x%02X\n", receivedCommand);
|
|
|
@@ -392,8 +366,6 @@ void Reset_I2C_Bus() {
|
|
|
printf("I2C Bus Status After Reset: 0x%08X\n", status);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/********MAIN function*************/
|
|
|
|
|
|
int main(void)
|
|
|
@@ -405,8 +377,8 @@ int main(void)
|
|
|
Battery_Init();
|
|
|
|
|
|
//Reset_I2C_Bus();
|
|
|
- NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
|
|
|
- //NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
|
|
|
+ //NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
|
|
|
+ NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
|
|
|
|
|
|
printf("............System Configuration Enabled...............\n");
|
|
|
|
|
|
@@ -415,30 +387,49 @@ int main(void)
|
|
|
//I2C_scanBus();
|
|
|
I2C_init(&gI2C);
|
|
|
|
|
|
- // *Configure ADC*
|
|
|
- ADC_PARAMS adc_params={
|
|
|
+ // *Configure ADC for voltage: Channel 1 and Channel 2*
|
|
|
+ ADC_PARAMS adc_voltage_params={
|
|
|
.channel= 1,
|
|
|
.resolution= 12,
|
|
|
.continuous= 1,
|
|
|
.gain= 1
|
|
|
-
|
|
|
};
|
|
|
- //printf("Configuring ADC....\n");
|
|
|
- //**Set Configuration Register for ADC**
|
|
|
- //ADC_SetConfigurationBytes(adc_params);
|
|
|
- //uint16_t channel_a_value= 3300; // in mVolts
|
|
|
|
|
|
+ ADC_PARAMS adc_current_params={
|
|
|
+ .channel= 2,
|
|
|
+ .resolution= 12,
|
|
|
+ .continuous= 1,
|
|
|
+ .gain= 1
|
|
|
+ };
|
|
|
|
|
|
+ uint16_t channel_a_value= 3300; // in mVolts
|
|
|
+ ADC_SetConfigurationBytes(adc_voltage_params);
|
|
|
+ delay_cycles(50000);
|
|
|
+ ADC_SetConfigurationBytes(adc_current_params);
|
|
|
+ delay_cycles(50000);
|
|
|
+
|
|
|
+ //GPIO_setConfig(GPIO_Battery_Charging_PIN_PB0_PIN , GPIO_CFG_OUT_STD| GPIO_CFG_OUT_HIGH| GPIO_Battery_Charging_PIN_PB0_IOMUX);
|
|
|
+ //GPIO_setConfig(GPIO_Battery_Discharging_PIN_PB8_PIN, GPIO_CFG_OUT_STD| GPIO_CFG_OUT_HIGH| GPIO_Battery_Discharging_PIN_PB8_IOMUX);
|
|
|
+
|
|
|
while (1)
|
|
|
- {
|
|
|
-
|
|
|
- //Battery_UpdateCurrentVoltage(adc_params);
|
|
|
- //for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
|
|
|
- // CC_CV_ControlCharging(slot_id);
|
|
|
- //}
|
|
|
- //delay_cycles(5000);
|
|
|
- //DAC_fastWrite(channel_a_value);
|
|
|
+ { //Looping through the ADC Channels
|
|
|
+ delay_cycles(50000);
|
|
|
+
|
|
|
+ Battery_UpdateVoltage(adc_voltage_params);
|
|
|
delay_cycles(100000);
|
|
|
+
|
|
|
+ Battery_UpdateCurrent(adc_current_params);
|
|
|
+ delay_cycles(50000);
|
|
|
+
|
|
|
+ //DAC_fastWrite(channel_a_value);
|
|
|
+ //delay_cycles(50000);
|
|
|
+
|
|
|
+ //CC-CV Cycle
|
|
|
+ for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
|
|
|
+ CC_CV_ControlCharging(slot_id);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|