|
|
@@ -3,11 +3,15 @@
|
|
|
#include "ti/driverlib/dl_i2c.h"
|
|
|
#include "ti/driverlib/m0p/dl_core.h"
|
|
|
#include "ti_msp_dl_config.h"
|
|
|
-#include "adc.h"
|
|
|
+#include "src/interfaces/adc.h"
|
|
|
#include <stdint.h>
|
|
|
#include <stdio.h>
|
|
|
+#include "src/interfaces/adc_interface.h"
|
|
|
|
|
|
-volatile bool gRxComplete;
|
|
|
+
|
|
|
+static ADC_Params adc_params;
|
|
|
+static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE;
|
|
|
+/*volatile bool gRxComplete;
|
|
|
volatile bool gTxComplete;
|
|
|
uint8_t gTxPacket[I2C_TX_MAX_PACKET_SIZE];
|
|
|
uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];
|
|
|
@@ -15,15 +19,15 @@ uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];
|
|
|
uint32_t gTxADClen, gTxADCcount;
|
|
|
uint32_t gRxADClen, gRxADCcount;
|
|
|
|
|
|
-static ADC_PARAMS adc_params;
|
|
|
-static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE;
|
|
|
+*/
|
|
|
|
|
|
/*
|
|
|
* 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) {
|
|
|
+
|
|
|
+/*static uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params) {
|
|
|
|
|
|
uint8_t config = 0;
|
|
|
|
|
|
@@ -63,11 +67,12 @@ static uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params) {
|
|
|
|
|
|
return config;
|
|
|
}
|
|
|
+*/
|
|
|
|
|
|
/* Tansmit Data from MCU to ADC: Function to SET configuration to ADC over
|
|
|
* I2C*/
|
|
|
|
|
|
-void ADC_SetConfigurationBytes(ADC_PARAMS params) {
|
|
|
+/*void ADC_SetConfigurationBytes(ADC_PARAMS params) {
|
|
|
// **Construct Configuration Byte**
|
|
|
uint8_t config_byte = ADC_ConstructConfigBytes(params);
|
|
|
|
|
|
@@ -98,7 +103,7 @@ void ADC_SetConfigurationBytes(ADC_PARAMS params) {
|
|
|
// ;
|
|
|
// printf("Configuration Sent Successfully for 0x%X!\n", config_byte);
|
|
|
}
|
|
|
-
|
|
|
+*/
|
|
|
/*
|
|
|
READY BIT:
|
|
|
This bit is the data ready flag. In read mode, this bit indicates if the
|
|
|
@@ -110,7 +115,7 @@ Conversion mode, writing this bit to “1” initiates a new conversion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
-bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
+/*bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
// Buffer for ADC data (MSB, LSB, Config Byte)
|
|
|
uint8_t adc_data[3];
|
|
|
uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
|
|
|
@@ -139,7 +144,7 @@ bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
// config_adc_byte, ready);
|
|
|
return ready;
|
|
|
}
|
|
|
-
|
|
|
+*/
|
|
|
/*
|
|
|
Function to read ADC DATA: This function simply retrieves the ADC raw digital
|
|
|
output as 16-bit signed integer.
|
|
|
@@ -186,7 +191,7 @@ D15.
|
|
|
|
|
|
*/
|
|
|
|
|
|
-int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
+/*int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
|
|
|
// Buffer for ADC data (MSB, LSB, Config Byte)
|
|
|
uint8_t adc_data[3] = {0};
|
|
|
@@ -242,17 +247,18 @@ int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params) {
|
|
|
//printf("Raw ADC Value: 0x%0X (%d)\n", raw_adc, raw_adc);
|
|
|
return raw_adc;
|
|
|
}
|
|
|
-
|
|
|
+*/
|
|
|
// **Interrupt handler for ADC Read Completion**
|
|
|
-void I2C_ADC_IRQHandler(void) {
|
|
|
+/*void I2C_ADC_IRQHandler(void) {
|
|
|
if (DL_I2C_getPendingInterrupt(I2C_controller_INST) ==
|
|
|
DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER) {
|
|
|
gRxComplete = true;
|
|
|
}
|
|
|
}
|
|
|
+*/
|
|
|
|
|
|
/* Function to Convert ADC Reading to Voltage */
|
|
|
-uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
|
|
|
+/*uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
|
|
|
uint16_t measured_voltage = 0;
|
|
|
uint16_t LSB = 0;
|
|
|
uint32_t max_adc_value = 1;
|
|
|
@@ -274,9 +280,9 @@ uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
|
|
|
measured_voltage = (((uint32_t)adc_value) * 2.7);
|
|
|
return (uint16_t)measured_voltage;
|
|
|
}
|
|
|
-
|
|
|
+*/
|
|
|
/* Function to Convert ADC Reading to Voltage */
|
|
|
-uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
|
|
|
+/*uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
|
|
|
int16_t current_mA = 0;
|
|
|
// uint8_t r_shunt= 0.1;
|
|
|
// Convert ADC value to voltage across shunt resistor:
|
|
|
@@ -287,8 +293,10 @@ uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
|
|
|
// printf("Converted Current: %d mA.\n", current_mA);
|
|
|
return (int16_t)current_mA;
|
|
|
}
|
|
|
+*/
|
|
|
|
|
|
-void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
+void updateADCReading_multichannel(uint8_t slot, uint8_t channel) {
|
|
|
+
|
|
|
while (adc_state != ADC_STATE_DONE) {
|
|
|
switch (adc_state) {
|
|
|
|
|
|
@@ -298,12 +306,14 @@ void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
adc_params.resolution = 12;
|
|
|
adc_params.continuous = 0;
|
|
|
adc_params.gain = 1;
|
|
|
- ADC_SetConfigurationBytes(adc_params);
|
|
|
+ adc_hal.configure(slot, adc_params);
|
|
|
+ //ADC_SetConfigurationBytes(adc_params);
|
|
|
adc_state = ADC_STATE_WAIT;
|
|
|
break;
|
|
|
|
|
|
case ADC_STATE_WAIT:
|
|
|
- if (ADC_CheckReadyBit(slot, adc_params)) {
|
|
|
+ //if (ADC_CheckReadyBit(slot, adc_params)) {
|
|
|
+ if(adc_hal.is_ready(slot, adc_params)){
|
|
|
adc_state = ADC_STATE_READ;
|
|
|
}
|
|
|
break;
|
|
|
@@ -311,18 +321,18 @@ void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
case ADC_STATE_READ:
|
|
|
if (channel == 0) {
|
|
|
|
|
|
- int16_t raw_adc_voltage = ADC_ReadData(slot, adc_params);
|
|
|
+ int16_t raw_adc_voltage = adc_hal.read_raw(slot, adc_params);
|
|
|
batteries[slot].voltage =
|
|
|
- ADC_ConvertToVoltage(raw_adc_voltage, adc_params); // Double check voltage dividers later on to get the right value (possibly instead of 2)
|
|
|
+ adc_hal.convert_voltage(raw_adc_voltage, adc_params);
|
|
|
printf("[ADC] Battery voltage for slot %d is %u mV.\n", slot,
|
|
|
batteries[slot].voltage);
|
|
|
adc_state = ADC_STATE_DONE;
|
|
|
|
|
|
} else if (channel == 1) {
|
|
|
|
|
|
- int16_t raw_adc_current = ADC_ReadData(slot, adc_params);
|
|
|
+ int16_t raw_adc_current = adc_hal.read_raw(slot, adc_params);
|
|
|
batteries[slot].current =
|
|
|
- ADC_ConvertToCurrent(raw_adc_current, adc_params);
|
|
|
+ adc_hal.convert_current(raw_adc_current, adc_params);
|
|
|
printf("[ADC] Battery current for slot %d is %u mA.\n", slot,
|
|
|
batteries[slot].current);
|
|
|
|
|
|
@@ -338,25 +348,3 @@ void Battery_UpdateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
|
|
|
adc_state = ADC_STATE_CONFIGURE;
|
|
|
}
|
|
|
-
|
|
|
-void Battery_ReadState(uint8_t slot_id) {
|
|
|
-
|
|
|
- uint16_t voltage_mv = batteries[slot_id].voltage;
|
|
|
- uint16_t min_voltage = batteries[slot_id].min_voltage;
|
|
|
- uint16_t max_voltage = batteries[slot_id].max_voltage;
|
|
|
-
|
|
|
- /* Changing the battery states based on adc values*/
|
|
|
- if (voltage_mv < 500) {
|
|
|
- batteries[slot_id].state = STATE_EMPTY;
|
|
|
- } else if (voltage_mv >= 500 && voltage_mv < 3000) { //TODO: change the voltage_mv to min_voltage
|
|
|
- batteries[slot_id].state = STATE_BATTERY_DETECTED;
|
|
|
- } else if (voltage_mv >= 3000 && voltage_mv < 4200) {
|
|
|
- batteries[slot_id].state = STATE_MEASUREMENT_IN_PROGRESS;
|
|
|
- }
|
|
|
- // once MCU is done reading ADC:
|
|
|
- else if (adc_state== ADC_STATE_DONE && batteries[slot_id].state== STATE_MEASUREMENT_IN_PROGRESS) {
|
|
|
- batteries[slot_id].state = STATE_MEASUREMENT_DONE;
|
|
|
- } else {
|
|
|
- batteries[slot_id].state = STATE_OVERCHARGING;
|
|
|
- }
|
|
|
-}
|