adc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "src/battery_data/battery.h"
  2. #include "src/peripherals/adc/adc.h"
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include "src/peripherals/adc/adc_interface.h"
  6. #include "src/interfaces/i2c_controller.h"
  7. #include "src/config.h"
  8. //static ADC_Params adc_params;
  9. static ADC_MeasurementState adc_state = ADC_STATE_CONFIGURE;
  10. uint16_t read_adc_channel(uint8_t slot, uint8_t channel) {
  11. //printf("Slot: %d, Channel: %d\n", slot, channel);
  12. ADC_Params adc_params= {0};
  13. uint16_t adc_voltage = 0;
  14. while (adc_state != ADC_STATE_DONE) {
  15. switch (adc_state) {
  16. case ADC_STATE_CONFIGURE:
  17. adc_params.channel = channel;
  18. adc_params.continuous = ADC_MEASUREMENT_IS_CONTINUOUS;
  19. if (channel == 0 || channel == 3) {
  20. // voltage measurement
  21. // -> we can measure directly
  22. adc_params.gain = 1;
  23. adc_params.resolution = 12;
  24. adc_params.factor = 1;
  25. } else {
  26. // current measurement
  27. // -> maximum gain, max resolution
  28. adc_params.gain = 8;
  29. adc_params.resolution = 16;
  30. adc_params.factor = 1000; // get microvolts
  31. }
  32. //printf("Config: Memory address of batteries: %p\n", &batteries[0]);
  33. adc_hal.configure(slot, &adc_params);
  34. if (adc_params.continuous != 1) {
  35. // in one shot mode we wait first to get the result
  36. adc_state = ADC_STATE_WAIT;
  37. } else {
  38. // in continuous mode we can directly read
  39. adc_state = ADC_STATE_READ;
  40. delay_cycles(ADC_CONTINUOUS_DELAY_CYCLES);
  41. }
  42. break;
  43. case ADC_STATE_WAIT:
  44. if(adc_hal.is_ready(slot, &adc_params)){
  45. adc_state = ADC_STATE_READ;
  46. }
  47. break;
  48. case ADC_STATE_READ:
  49. adc_voltage = adc_hal.read_voltage(slot, &adc_params);
  50. #ifdef DEBUG_ADC
  51. printf("[ADC] ADC reading completed. Slot %d Channel %d is %d \n", slot, channel, adc_voltage);
  52. #endif
  53. adc_state = ADC_STATE_DONE;
  54. /*
  55. } else if (channel == 1) {
  56. int16_t raw_adc_current = adc_hal.read_raw(slot, &adc_params);
  57. battery_slots[slot].measurement.current =
  58. adc_hal.convert_current(raw_adc_current, &adc_params);
  59. adc_state = ADC_STATE_DONE;
  60. #ifdef DEBUG_ADC
  61. printf("[ADC] Battery Current in slot %d is %d mA.\n", slot, battery_slots[slot].measurement.current);
  62. #endif
  63. } else if (channel == 2) {
  64. // @fixme: this is the third adc channel, needed for current meausrement on disharge mode
  65. int16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
  66. battery_slots[slot].high_side_voltage =
  67. adc_hal.convert_voltage(raw_adc_voltage, &adc_params);
  68. adc_state = ADC_STATE_DONE;
  69. #ifdef DEBUG_ADC
  70. printf("[ADC] Ch3 Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
  71. #endif
  72. } else if (channel == 3) {
  73. // @fixme: this is the third adc channel, needed for current meausrement on disharge mode
  74. int16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
  75. int16_t high_i = adc_hal.convert_voltage(raw_adc_voltage, &adc_params);
  76. battery_slots[slot].measurement.current = high_i*battery_slots[slot].high_side_voltage/battery_slots[slot].measurement.voltage;
  77. adc_state = ADC_STATE_DONE;
  78. #ifdef DEBUG_ADC
  79. printf("[ADC] Ch4 Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
  80. #endif
  81. }*/
  82. break;
  83. default:
  84. channel = 0;
  85. adc_state = ADC_STATE_CONFIGURE;
  86. break;
  87. }
  88. }
  89. adc_state = ADC_STATE_CONFIGURE;
  90. return adc_voltage;
  91. }