| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include "src/battery_data/battery.h"
- #include "src/peripherals/adc/adc.h"
- #include <stdint.h>
- #include <stdio.h>
- #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;
- void updateADCReading(uint8_t slot, uint8_t channel) {
- //printf("Slot: %d, Channel: %d\n", slot, channel);
- ADC_Params adc_params= {0};
- while (adc_state != ADC_STATE_DONE) {
- switch (adc_state) {
-
- case ADC_STATE_CONFIGURE:
- adc_params.channel = channel;
- adc_params.resolution = 12;
- adc_params.continuous = ADC_MEASUREMENT_IS_CONTINUOUS;
- adc_params.gain = 1;
- //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:
- 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;
- #ifdef DEBUG_ADC
- printf("[ADC] Battery Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
- #endif
- } 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].measurement.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
- }
- break;
- default:
- channel = 0;
- adc_state = ADC_STATE_CONFIGURE;
- break;
- }
- }
- adc_state = ADC_STATE_CONFIGURE;
- }
|