AnalogIn_IIO.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #include "AP_HAL_Linux.h"
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #define IIO_ANALOG_IN_COUNT 8
  6. #define IIO_ANALOG_IN_DIR "/sys/bus/iio/devices/iio:device0/"
  7. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF
  8. // Note that echo BB-ADC cape should be loaded
  9. #define IIO_VOLTAGE_SCALING 0.00142602816
  10. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
  11. #define IIO_VOLTAGE_SCALING 3.0*1.8/4095.0
  12. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE
  13. #define IIO_VOLTAGE_SCALING 1.8/4095.0
  14. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
  15. #define IIO_VOLTAGE_SCALING 3.0*1.8/4095.0
  16. #else
  17. #define IIO_VOLTAGE_SCALING 1.0
  18. #endif
  19. class AnalogSource_IIO : public AP_HAL::AnalogSource {
  20. public:
  21. friend class AnalogIn_IIO;
  22. AnalogSource_IIO(int16_t pin, float initial_value, float voltage_scaling);
  23. float read_average() override;
  24. float read_latest() override;
  25. void set_pin(uint8_t p) override;
  26. float voltage_average() override;
  27. float voltage_latest() override;
  28. float voltage_average_ratiometric() override { return voltage_average(); }
  29. private:
  30. float _value;
  31. float _latest;
  32. float _sum_value;
  33. float _voltage_scaling;
  34. uint8_t _sum_count;
  35. int16_t _pin;
  36. int _pin_fd;
  37. int fd_analog_sources[IIO_ANALOG_IN_COUNT];
  38. HAL_Semaphore _semaphore;
  39. void init_pins(void);
  40. void select_pin(void);
  41. static const char *analog_sources[];
  42. };
  43. class AnalogIn_IIO : public AP_HAL::AnalogIn {
  44. public:
  45. AnalogIn_IIO();
  46. void init() override;
  47. AP_HAL::AnalogSource* channel(int16_t n) override;
  48. // we don't yet know how to get the board voltage
  49. float board_voltage(void) override { return 5.0f; }
  50. };