adc.h 713 B

1234567891011121314151617181920212223
  1. #pragma once
  2. #include <inttypes.h>
  3. #include "config.h"
  4. /*
  5. http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
  6. */
  7. #define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255)
  8. #define BX_(x) ((x) - (((x)>>1)&0x77777777) - (((x)>>2)&0x33333333) - (((x)>>3)&0x11111111))
  9. #define ADC_PIN_IDX(pin) BITCOUNT(ADC_CHAN_MSK & ((1 << (pin)) - 1))
  10. #if BITCOUNT(ADC_CHAN_MSK) != ADC_CHAN_CNT
  11. # error "ADC_CHAN_MSK oes not match ADC_CHAN_CNT"
  12. #endif
  13. extern volatile uint8_t adc_channel;
  14. extern volatile uint16_t adc_values[ADC_CHAN_CNT];
  15. extern void adc_init();
  16. extern void adc_start_cycle(); //should be called from an atomic context only
  17. static inline bool adc_cycle_done() { return adc_channel >= ADC_CHAN_CNT; }