|
|
@@ -11,9 +11,10 @@
|
|
|
static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE;
|
|
|
|
|
|
|
|
|
-void updateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
+uint16_t read_adc_channel(uint8_t slot, uint8_t channel) {
|
|
|
//printf("Slot: %d, Channel: %d\n", slot, channel);
|
|
|
ADC_Params adc_params= {0};
|
|
|
+ uint16_t adc_voltage = 0;
|
|
|
while (adc_state != ADC_STATE_DONE) {
|
|
|
switch (adc_state) {
|
|
|
|
|
|
@@ -21,7 +22,17 @@ void updateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
adc_params.channel = channel;
|
|
|
adc_params.resolution = 12;
|
|
|
adc_params.continuous = ADC_MEASUREMENT_IS_CONTINUOUS;
|
|
|
- adc_params.gain = 1;
|
|
|
+ if (channel == 0 || channel == 3) {
|
|
|
+ // voltage measurement
|
|
|
+ // -> we can measure directly
|
|
|
+ adc_params.gain = 1;
|
|
|
+ adc_params.resolution = 12;
|
|
|
+ } else {
|
|
|
+ // current measurement
|
|
|
+ // -> maximum gain, max resolution
|
|
|
+ adc_params.gain = 8;
|
|
|
+ adc_params.resolution = 16;
|
|
|
+ }
|
|
|
//printf("Config: Memory address of batteries: %p\n", &batteries[0]);
|
|
|
adc_hal.configure(slot, &adc_params);
|
|
|
if (adc_params.continuous == 1) {
|
|
|
@@ -41,16 +52,14 @@ void updateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
break;
|
|
|
|
|
|
case ADC_STATE_READ:
|
|
|
- if (channel == 0) {
|
|
|
-
|
|
|
- int16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
|
|
|
- battery_slots[slot].measurement.voltage =
|
|
|
- adc_hal.convert_voltage(raw_adc_voltage, &adc_params);
|
|
|
- adc_state = ADC_STATE_DONE;
|
|
|
+ uint16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
|
|
|
+ adc_voltage = adc_hal.get_voltage(raw_adc_voltage, &adc_params);
|
|
|
#ifdef DEBUG_ADC
|
|
|
- printf("[ADC] Battery Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
|
|
|
+ printf("[ADC] ADC reading completed. Slot %d Channel %d is %d mV.\n", slot, channel, adc_voltage);
|
|
|
#endif
|
|
|
+ adc_state = ADC_STATE_DONE;
|
|
|
|
|
|
+ /*
|
|
|
} else if (channel == 1) {
|
|
|
|
|
|
int16_t raw_adc_current = adc_hal.read_raw(slot, &adc_params);
|
|
|
@@ -63,14 +72,22 @@ void updateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
} else if (channel == 2) {
|
|
|
// @fixme: this is the third adc channel, needed for current meausrement on disharge mode
|
|
|
int16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
|
|
|
- battery_slots[slot].measurement.voltage =
|
|
|
+ battery_slots[slot].high_side_voltage =
|
|
|
adc_hal.convert_voltage(raw_adc_voltage, &adc_params);
|
|
|
adc_state = ADC_STATE_DONE;
|
|
|
#ifdef DEBUG_ADC
|
|
|
printf("[ADC] Ch3 Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
|
|
|
#endif
|
|
|
-
|
|
|
- }
|
|
|
+ } else if (channel == 3) {
|
|
|
+ // @fixme: this is the third adc channel, needed for current meausrement on disharge mode
|
|
|
+ int16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
|
|
|
+ int16_t high_i = adc_hal.convert_voltage(raw_adc_voltage, &adc_params);
|
|
|
+ battery_slots[slot].measurement.current = high_i*battery_slots[slot].high_side_voltage/battery_slots[slot].measurement.voltage;
|
|
|
+ adc_state = ADC_STATE_DONE;
|
|
|
+#ifdef DEBUG_ADC
|
|
|
+ printf("[ADC] Ch4 Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
|
|
|
+#endif
|
|
|
+ }*/
|
|
|
break;
|
|
|
default:
|
|
|
channel = 0;
|
|
|
@@ -80,4 +97,5 @@ void updateADCReading(uint8_t slot, uint8_t channel) {
|
|
|
}
|
|
|
|
|
|
adc_state = ADC_STATE_CONFIGURE;
|
|
|
+ return adc_voltage;
|
|
|
}
|