|
@@ -1,8 +1,9 @@
|
|
|
#include "battery.h"
|
|
#include "battery.h"
|
|
|
|
|
+#include "src/interfaces/i2c_controller_interface.h"
|
|
|
#include "ti/driverlib/dl_i2c.h"
|
|
#include "ti/driverlib/dl_i2c.h"
|
|
|
#include "ti/driverlib/m0p/dl_core.h"
|
|
#include "ti/driverlib/m0p/dl_core.h"
|
|
|
#include "ti_msp_dl_config.h"
|
|
#include "ti_msp_dl_config.h"
|
|
|
-#include <adc.h>
|
|
|
|
|
|
|
+#include "adc.h"
|
|
|
#include <stdint.h>
|
|
#include <stdint.h>
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
|
@@ -13,10 +14,16 @@ uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];
|
|
|
|
|
|
|
|
uint32_t gTxADClen, gTxADCcount;
|
|
uint32_t gTxADClen, gTxADCcount;
|
|
|
uint32_t gRxADClen, gRxADCcount;
|
|
uint32_t gRxADClen, gRxADCcount;
|
|
|
|
|
+
|
|
|
static ADC_PARAMS adc_params;
|
|
static ADC_PARAMS adc_params;
|
|
|
static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE;
|
|
static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE;
|
|
|
|
|
|
|
|
-uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params) {
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+* Creating Configuartion Register as mentioned in the datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22226a.pdf
|
|
|
|
|
+* Under section 5.2 Configuration Register
|
|
|
|
|
+* The function is private to ADC and is can only be called in this file
|
|
|
|
|
+*/
|
|
|
|
|
+static uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params) {
|
|
|
|
|
|
|
|
uint8_t config = 0;
|
|
uint8_t config = 0;
|
|
|
|
|
|
|
@@ -76,17 +83,19 @@ void ADC_SetConfigurationBytes(ADC_PARAMS params) {
|
|
|
gTxADCcount = 0;
|
|
gTxADCcount = 0;
|
|
|
gTxComplete = false;
|
|
gTxComplete = false;
|
|
|
|
|
|
|
|
- DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
|
|
|
|
|
- DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t *)&gTxPacket, 1);
|
|
|
|
|
- DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS,
|
|
|
|
|
- DL_I2C_CONTROLLER_DIRECTION_TX, gTxADClen);
|
|
|
|
|
|
|
+ i2c_hal.write(ADC_TARGET_BASE_ADDRESS, gTxPacket, gTxADClen);
|
|
|
|
|
+
|
|
|
|
|
+ //DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
|
|
|
|
|
+ //DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t *)&gTxPacket, 1);
|
|
|
|
|
+ //DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS,
|
|
|
|
|
+ // DL_I2C_CONTROLLER_DIRECTION_TX, gTxADClen);
|
|
|
// DL_I2C_enableInterrupt(I2C_controller_INST,
|
|
// DL_I2C_enableInterrupt(I2C_controller_INST,
|
|
|
// DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
// DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
// while(!gTxComplete);
|
|
// while(!gTxComplete);
|
|
|
// **Ensure STOP Condition is Sent**
|
|
// **Ensure STOP Condition is Sent**
|
|
|
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
- ;
|
|
|
|
|
|
|
+ //while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
+ // DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
+ // ;
|
|
|
// printf("Configuration Sent Successfully for 0x%X!\n", config_byte);
|
|
// printf("Configuration Sent Successfully for 0x%X!\n", config_byte);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -110,15 +119,17 @@ bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
gRxADCcount = 0;
|
|
gRxADCcount = 0;
|
|
|
gRxComplete = false;
|
|
gRxComplete = false;
|
|
|
|
|
|
|
|
- DL_I2C_startControllerTransfer(I2C_controller_INST, adc_address,
|
|
|
|
|
- DL_I2C_CONTROLLER_DIRECTION_RX, gRxADClen);
|
|
|
|
|
- DL_I2C_enableInterrupt(I2C_controller_INST,
|
|
|
|
|
- DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
|
|
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
- ;
|
|
|
|
|
- while (!gRxComplete)
|
|
|
|
|
- ;
|
|
|
|
|
|
|
+ i2c_hal.read(adc_address, gRxADClen);
|
|
|
|
|
+
|
|
|
|
|
+ //DL_I2C_startControllerTransfer(I2C_controller_INST, adc_address,
|
|
|
|
|
+ // DL_I2C_CONTROLLER_DIRECTION_RX, gRxADClen);
|
|
|
|
|
+ //DL_I2C_enableInterrupt(I2C_controller_INST,
|
|
|
|
|
+ // DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
|
|
+ //while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
+ // DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
+ // ;
|
|
|
|
|
+ //while (!gRxComplete)
|
|
|
|
|
+ // ;
|
|
|
|
|
|
|
|
uint8_t config_adc_byte = gRxPacket[2];
|
|
uint8_t config_adc_byte = gRxPacket[2];
|
|
|
|
|
|
|
@@ -184,30 +195,34 @@ int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
|
|
uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
|
|
|
// printf("Reading ADC Data from MCP3428 for channel: %u\n", params.channel);
|
|
// printf("Reading ADC Data from MCP3428 for channel: %u\n", params.channel);
|
|
|
|
|
|
|
|
- if (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
|
|
|
|
|
- printf("Error: I2C bus stuck! Resetting..\n");
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //if (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
+ // DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
|
|
|
|
|
+ // printf("Error: I2C bus stuck! Resetting..\n");
|
|
|
// DL_I2C_resetControllerTransfer(I2C_controller_INST);
|
|
// DL_I2C_resetControllerTransfer(I2C_controller_INST);
|
|
|
- }
|
|
|
|
|
|
|
+ //}
|
|
|
|
|
|
|
|
gRxADClen = bytes_to_read;
|
|
gRxADClen = bytes_to_read;
|
|
|
gRxADCcount = 0;
|
|
gRxADCcount = 0;
|
|
|
gRxComplete = false;
|
|
gRxComplete = false;
|
|
|
|
|
|
|
|
- DL_I2C_startControllerTransfer(I2C_controller_INST, adc_address,
|
|
|
|
|
- DL_I2C_CONTROLLER_DIRECTION_RX, gRxADClen);
|
|
|
|
|
- DL_I2C_enableInterrupt(I2C_controller_INST,
|
|
|
|
|
- DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
|
|
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
- ;
|
|
|
|
|
- while (!gRxComplete)
|
|
|
|
|
- ;
|
|
|
|
|
|
|
+ i2c_hal.read(adc_address, gRxADClen);
|
|
|
|
|
+
|
|
|
|
|
+ //DL_I2C_startControllerTransfer(I2C_controller_INST, adc_address,
|
|
|
|
|
+ // DL_I2C_CONTROLLER_DIRECTION_RX, gRxADClen);
|
|
|
|
|
+ //DL_I2C_enableInterrupt(I2C_controller_INST,
|
|
|
|
|
+ // DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
|
|
+ //while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
+ // DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
+ // ;
|
|
|
|
|
+ //while (!gRxComplete)
|
|
|
|
|
+ // ;
|
|
|
|
|
|
|
|
uint8_t msb = gRxPacket[0];
|
|
uint8_t msb = gRxPacket[0];
|
|
|
uint8_t lsb = gRxPacket[1];
|
|
uint8_t lsb = gRxPacket[1];
|
|
|
uint8_t config_adc_byte = gRxPacket[2];
|
|
uint8_t config_adc_byte = gRxPacket[2];
|
|
|
-
|
|
|
|
|
|
|
+ printf("MSB: 0x%02X, LSB: 0x%02X\n", msb, lsb);
|
|
|
uint8_t gain_setting = (config_adc_byte & 0x03);
|
|
uint8_t gain_setting = (config_adc_byte & 0x03);
|
|
|
uint8_t gain_multiplier = (1 << gain_setting); // Gain values: 1, 2, 4, 8
|
|
uint8_t gain_multiplier = (1 << gain_setting); // Gain values: 1, 2, 4, 8
|
|
|
|
|
|
|
@@ -256,7 +271,7 @@ uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
|
|
|
printf("Error: Unknown ADC Resolution!\n");
|
|
printf("Error: Unknown ADC Resolution!\n");
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
- measured_voltage = (((uint32_t)adc_value * ADC_VREF_MV) * 3) / max_adc_value;
|
|
|
|
|
|
|
+ measured_voltage = (((uint32_t)adc_value) * 2.7);
|
|
|
return (uint16_t)measured_voltage;
|
|
return (uint16_t)measured_voltage;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -268,7 +283,7 @@ uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
|
|
|
uint16_t voltage_mV = ADC_ConvertToVoltage(adc_value, params);
|
|
uint16_t voltage_mV = ADC_ConvertToVoltage(adc_value, params);
|
|
|
uint8_t gain_multiplier = (1 << (params.gain - 1));
|
|
uint8_t gain_multiplier = (1 << (params.gain - 1));
|
|
|
// Convert voltage drop across shunt resistor to current
|
|
// Convert voltage drop across shunt resistor to current
|
|
|
- current_mA = (voltage_mV) / (0.1 * gain_multiplier);
|
|
|
|
|
|
|
+ current_mA = (adc_value) * (10 * gain_multiplier);
|
|
|
// printf("Converted Current: %d mA.\n", current_mA);
|
|
// printf("Converted Current: %d mA.\n", current_mA);
|
|
|
return (int16_t)current_mA;
|
|
return (int16_t)current_mA;
|
|
|
}
|
|
}
|
|
@@ -279,9 +294,9 @@ void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
|
|
|
|
|
case ADC_STATE_CONFIGURE:
|
|
case ADC_STATE_CONFIGURE:
|
|
|
adc_params.channel = channel;
|
|
adc_params.channel = channel;
|
|
|
- // printf("Channel: %d\n", adc_params.channel);
|
|
|
|
|
|
|
+ printf("Channel: %d\n", adc_params.channel);
|
|
|
adc_params.resolution = 12;
|
|
adc_params.resolution = 12;
|
|
|
- adc_params.continuous = 1;
|
|
|
|
|
|
|
+ adc_params.continuous = 0;
|
|
|
adc_params.gain = 1;
|
|
adc_params.gain = 1;
|
|
|
ADC_SetConfigurationBytes(adc_params);
|
|
ADC_SetConfigurationBytes(adc_params);
|
|
|
adc_state = ADC_STATE_WAIT;
|
|
adc_state = ADC_STATE_WAIT;
|
|
@@ -298,7 +313,7 @@ void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
|
|
|
|
|
int16_t raw_adc_voltage = ADC_ReadData(slot, adc_params);
|
|
int16_t raw_adc_voltage = ADC_ReadData(slot, adc_params);
|
|
|
batteries[slot].voltage =
|
|
batteries[slot].voltage =
|
|
|
- ADC_ConvertToVoltage(raw_adc_voltage, adc_params);
|
|
|
|
|
|
|
+ ADC_ConvertToVoltage(raw_adc_voltage, adc_params); // Double check voltage dividers later on to get the right value (possibly instead of 2)
|
|
|
printf("Battery voltage for slot %d is %u mV.\n", slot,
|
|
printf("Battery voltage for slot %d is %u mV.\n", slot,
|
|
|
batteries[slot].voltage);
|
|
batteries[slot].voltage);
|
|
|
adc_state = ADC_STATE_DONE;
|
|
adc_state = ADC_STATE_DONE;
|