| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #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;
- 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;
- break;
- default:
- channel = 0;
- adc_state = ADC_STATE_CONFIGURE;
- break;
- }
- }
- adc_state = ADC_STATE_CONFIGURE;
- return adc_voltage;
- }
|