GPIO.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <stdint.h>
  3. #include "AP_HAL_Namespace.h"
  4. #define HAL_GPIO_INPUT 0
  5. #define HAL_GPIO_OUTPUT 1
  6. #define HAL_GPIO_ALT 2
  7. class AP_HAL::DigitalSource {
  8. public:
  9. virtual void mode(uint8_t output) = 0;
  10. virtual uint8_t read() = 0;
  11. virtual void write(uint8_t value) = 0;
  12. virtual void toggle() = 0;
  13. };
  14. class AP_HAL::GPIO {
  15. public:
  16. GPIO() {}
  17. virtual void init() = 0;
  18. virtual void pinMode(uint8_t pin, uint8_t output) = 0;
  19. // optional interface on some boards
  20. virtual void pinMode(uint8_t pin, uint8_t output, uint8_t alt) {};
  21. virtual uint8_t read(uint8_t pin) = 0;
  22. virtual void write(uint8_t pin, uint8_t value) = 0;
  23. virtual void toggle(uint8_t pin) = 0;
  24. /* Alternative interface: */
  25. virtual AP_HAL::DigitalSource* channel(uint16_t n) = 0;
  26. enum INTERRUPT_TRIGGER_TYPE {
  27. INTERRUPT_NONE,
  28. INTERRUPT_FALLING,
  29. INTERRUPT_RISING,
  30. INTERRUPT_BOTH,
  31. };
  32. /* Interrupt interface: */
  33. // ret , pin , state,timestamp
  34. // where:
  35. // ret indicates the functor must return void
  36. // pin is the pin which has triggered the interrupt
  37. // state is the new state of the pin
  38. // timestamp is the time in microseconds the interrupt occured
  39. FUNCTOR_TYPEDEF(irq_handler_fn_t, void, uint8_t, bool, uint32_t);
  40. virtual bool attach_interrupt(uint8_t pin,
  41. irq_handler_fn_t fn,
  42. INTERRUPT_TRIGGER_TYPE mode) {
  43. return false;
  44. }
  45. virtual bool attach_interrupt(uint8_t pin,
  46. AP_HAL::Proc proc,
  47. INTERRUPT_TRIGGER_TYPE mode) {
  48. return false;
  49. }
  50. bool detach_interrupt(uint8_t pin) {
  51. if (attach_interrupt(pin, (irq_handler_fn_t)nullptr, AP_HAL::GPIO::INTERRUPT_NONE)) {
  52. return true;
  53. }
  54. return attach_interrupt(pin, (AP_HAL::Proc)nullptr, AP_HAL::GPIO::INTERRUPT_NONE);
  55. }
  56. /* return true if USB cable is connected */
  57. virtual bool usb_connected(void) = 0;
  58. };