GPIO_Sysfs.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include "AP_HAL_Linux.h"
  3. #include <AP_HAL/AP_HAL.h>
  4. #include <AP_HAL/utility/RingBuffer.h>
  5. #include "GPIO.h"
  6. namespace Linux {
  7. class DigitalSource_Sysfs : public AP_HAL::DigitalSource {
  8. friend class GPIO_Sysfs;
  9. public:
  10. ~DigitalSource_Sysfs();
  11. uint8_t read() override;
  12. void write(uint8_t value) override;
  13. void mode(uint8_t output) override;
  14. void toggle() override;
  15. private:
  16. /* Only GPIO_Sysfs will be able to instantiate */
  17. DigitalSource_Sysfs(unsigned pin, int value_fd);
  18. int _value_fd;
  19. unsigned _pin;
  20. };
  21. /**
  22. * Generic implementation of AP_HAL::GPIO for Linux based boards.
  23. */
  24. class GPIO_Sysfs : public AP_HAL::GPIO {
  25. friend class DigitalSource_Sysfs;
  26. public:
  27. /* Fill this table with the real pin numbers. */
  28. static const unsigned pin_table[];
  29. static const uint8_t n_pins;
  30. static GPIO_Sysfs *from(AP_HAL::GPIO *gpio) {
  31. return static_cast<GPIO_Sysfs*>(gpio);
  32. }
  33. void init() override;
  34. void pinMode(uint8_t vpin, uint8_t output) override;
  35. uint8_t read(uint8_t vpin) override;
  36. void write(uint8_t vpin, uint8_t value) override;
  37. void toggle(uint8_t vpin) override;
  38. /*
  39. * Export pin, instantiate a new DigitalSource_Sysfs and return its
  40. * pointer.
  41. */
  42. AP_HAL::DigitalSource *channel(uint16_t vpin) override;
  43. /*
  44. * Currently this function always returns false.
  45. */
  46. bool usb_connected() override;
  47. protected:
  48. void _pinMode(unsigned int pin, uint8_t output);
  49. int _open_pin_value(unsigned int pin, int flags);
  50. /*
  51. * Make pin available for use. This function should be called before
  52. * calling functions that use the pin number as parameter.
  53. *
  54. * Returns true if pin is exported successfully and false otherwise.
  55. *
  56. * Note: the pin is ignored if already exported.
  57. */
  58. static bool _export_pin(uint8_t vpin);
  59. #ifdef HAL_GPIO_SCRIPT
  60. /*
  61. support for calling external scripts based on GPIO writes
  62. */
  63. void _gpio_script_write(uint8_t vpin, uint8_t value);
  64. /*
  65. thread to run scripts
  66. */
  67. void _gpio_script_thread(void);
  68. /*
  69. control structures for _gpio_script_write
  70. */
  71. typedef struct {
  72. uint8_t pin;
  73. uint8_t value;
  74. } pin_value_t;
  75. struct {
  76. bool thread_created;
  77. ObjectBuffer<pin_value_t> pending{10};
  78. } _script;
  79. #endif // HAL_GPIO_SCRIPT
  80. };
  81. }