measure.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef MEASURE_H_
  2. #define MEASURE_H_
  3. #include "fixpt.h"
  4. #include "util.h"
  5. /* Multiplexer selections */
  6. #define MEAS_MUX_ADC0 ((0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (0 << MUX0))
  7. #define MEAS_MUX_ADC1 ((0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (1 << MUX0))
  8. #define MEAS_MUX_ADC2 ((0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0))
  9. #define MEAS_MUX_ADC3 ((0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (1 << MUX0))
  10. #define MEAS_MUX_ADC4 ((0 << MUX3) | (1 << MUX2) | (0 << MUX1) | (0 << MUX0))
  11. #define MEAS_MUX_ADC5 ((0 << MUX3) | (1 << MUX2) | (0 << MUX1) | (1 << MUX0))
  12. #define MEAS_MUX_ADC6 ((0 << MUX3) | (1 << MUX2) | (1 << MUX1) | (0 << MUX0))
  13. #define MEAS_MUX_ADC7 ((0 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0))
  14. #define MEAS_MUX_ADC8 ((1 << MUX3) | (0 << MUX2) | (0 << MUX1) | (0 << MUX0))
  15. #define MEAS_MUX_BG ((1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (0 << MUX0))
  16. #define MEAS_MUX_GND ((1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0))
  17. /* Digital-input-disable selections */
  18. #define MEAS_DID_NONE 0u
  19. #define MEAS_DID_ADC0 (1u << ADC0D)
  20. #define MEAS_DID_ADC1 (1u << ADC1D)
  21. #define MEAS_DID_ADC2 (1u << ADC2D)
  22. #define MEAS_DID_ADC3 (1u << ADC3D)
  23. #define MEAS_DID_ADC4 (1u << ADC4D)
  24. #define MEAS_DID_ADC5 (1u << ADC5D)
  25. /* Prescaler selections */
  26. #define MEAS_PS_2 ((0 << ADPS2) | (0 << ADPS1) | (1 << ADPS0))
  27. #define MEAS_PS_4 ((0 << ADPS2) | (1 << ADPS1) | (0 << ADPS0))
  28. #define MEAS_PS_8 ((0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
  29. #define MEAS_PS_16 ((1 << ADPS2) | (0 << ADPS1) | (0 << ADPS0))
  30. #define MEAS_PS_32 ((1 << ADPS2) | (0 << ADPS1) | (1 << ADPS0))
  31. #define MEAS_PS_64 ((1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0))
  32. #define MEAS_PS_128 ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
  33. /* Reference selection */
  34. #define MEAS_REF_AREF ((0 << REFS1) | (0 << REFS0))
  35. #define MEAS_REF_AVCC ((0 << REFS1) | (1 << REFS0))
  36. #define MEAS_REF_RESERVED ((1 << REFS1) | (0 << REFS0))
  37. #define MEAS_REF_INT1P1V ((1 << REFS1) | (1 << REFS0))
  38. #define MEASURE_MAX_RESULT 0x3FFu
  39. enum measure_chan {
  40. MEAS_CHAN_CURRENT,
  41. MEAS_CHAN_TEMP,
  42. };
  43. #define NR_MEAS_CHANS 2
  44. enum measure_plausibility {
  45. MEAS_PLAUSIBLE,
  46. MEAS_NOT_PLAUSIBLE,
  47. MEAS_PLAUS_TIMEOUT,
  48. };
  49. typedef void (*measure_result_cb_t)(fixpt_t measured_phys_value,
  50. enum measure_plausibility plaus);
  51. typedef uint16_t (*measure_filter_cb_t)(uint16_t raw_adc);
  52. struct measure_config {
  53. /* A short two-character name. Just for debugging. */
  54. char name[3];
  55. /* The ADC multiplexer setting. One of MEAS_MUX_... */
  56. uint8_t mux;
  57. /* The digital-input-disable setting. One of MEAS_DID_... */
  58. uint8_t did;
  59. /* The ADC prescaler setting. One of MEAS_PS_... */
  60. uint8_t ps;
  61. /* The ADC reference setting. One of MEAS_REF_... */
  62. uint8_t ref;
  63. /* The time (in milliseconds) we keep adding samples for the
  64. * averaging.
  65. * The actual sample rate is: F_CPU / prescaler / 13
  66. * The sum buffer is of size uint32_t.
  67. */
  68. uint16_t averaging_timeout_ms;
  69. /* The scale() parameters for raw -> phys scaling. */
  70. uint16_t scale_raw_lo;
  71. uint16_t scale_raw_hi;
  72. fixpt_t scale_phys_lo;
  73. fixpt_t scale_phys_hi;
  74. /* The plausibility limits. */
  75. fixpt_t plaus_neglim;
  76. fixpt_t plaus_poslim;
  77. uint16_t plaus_timeout_ms;
  78. };
  79. void measure_adjust_set(enum measure_chan chan,
  80. fixpt_t adjustment);
  81. fixpt_t measure_adjust_get(enum measure_chan chan);
  82. void measure_start(void);
  83. void measure_work(void);
  84. void measure_init(void);
  85. #endif /* MEASURE_H_ */