#include "src/battery_data/battery.h" #include "src/peripherals/adc/adc.h" #include #include #include "src/peripherals/adc/adc_interface.h" #include "src/interfaces/i2c_controller.h" #include "src/config.h" //static ADC_Params adc_params; static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE; 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) { case ADC_STATE_CONFIGURE: adc_params.channel = channel; adc_params.continuous = ADC_MEASUREMENT_IS_CONTINUOUS; if (channel == 0 || channel == 3) { // voltage measurement // -> we can measure directly adc_params.gain = 1; adc_params.resolution = 12; adc_params.factor = 1; } else { // current measurement // -> maximum gain, max resolution adc_params.gain = 8; adc_params.resolution = 16; adc_params.factor = 1000; // get microvolts } //printf("Config: Memory address of batteries: %p\n", &batteries[0]); adc_hal.configure(slot, &adc_params); if (adc_params.continuous != 1) { // in one shot mode we wait first to get the result adc_state = ADC_STATE_WAIT; } else { // in continuous mode we can directly read adc_state = ADC_STATE_READ; delay_cycles(ADC_CONTINUOUS_DELAY_CYCLES); } break; case ADC_STATE_WAIT: if(adc_hal.is_ready(slot, &adc_params)){ adc_state = ADC_STATE_READ; } break; case ADC_STATE_READ: adc_voltage = adc_hal.read_voltage(slot, &adc_params); #ifdef DEBUG_ADC printf("[ADC] ADC reading completed. Slot %d Channel %d is %d \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); battery_slots[slot].measurement.current = adc_hal.convert_current(raw_adc_current, &adc_params); adc_state = ADC_STATE_DONE; #ifdef DEBUG_ADC printf("[ADC] Battery Current in slot %d is %d mA.\n", slot, battery_slots[slot].measurement.current); #endif } 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].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; adc_state = ADC_STATE_CONFIGURE; break; } } adc_state = ADC_STATE_CONFIGURE; return adc_voltage; }