GPIO.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Code by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #pragma once
  18. #include "AP_HAL_ChibiOS.h"
  19. #ifndef HAL_GPIO_LED_ON
  20. #define HAL_GPIO_LED_ON 0
  21. #endif
  22. #ifndef HAL_GPIO_LED_OFF
  23. #define HAL_GPIO_LED_OFF 1
  24. #endif
  25. class ChibiOS::GPIO : public AP_HAL::GPIO {
  26. public:
  27. GPIO();
  28. void init() override;
  29. void pinMode(uint8_t pin, uint8_t output) override;
  30. uint8_t read(uint8_t pin) override;
  31. void write(uint8_t pin, uint8_t value) override;
  32. void toggle(uint8_t pin) override;
  33. /* Alternative interface: */
  34. AP_HAL::DigitalSource* channel(uint16_t n) override;
  35. /* Interrupt interface - fast, for RCOutput and SPI radios */
  36. bool attach_interrupt(uint8_t interrupt_num,
  37. AP_HAL::Proc p,
  38. INTERRUPT_TRIGGER_TYPE mode) override;
  39. /* Interrupt interface - for AP_HAL::GPIO */
  40. bool attach_interrupt(uint8_t pin,
  41. irq_handler_fn_t fn,
  42. INTERRUPT_TRIGGER_TYPE mode) override;
  43. /* return true if USB cable is connected */
  44. bool usb_connected(void) override;
  45. void set_usb_connected() { _usb_connected = true; }
  46. /* attach interrupt via ioline_t */
  47. bool _attach_interrupt(ioline_t line, AP_HAL::Proc p, uint8_t mode);
  48. private:
  49. bool _usb_connected;
  50. bool _ext_started;
  51. bool _attach_interrupt(ioline_t line, palcallback_t cb, void *p, uint8_t mode);
  52. };
  53. class ChibiOS::DigitalSource : public AP_HAL::DigitalSource {
  54. public:
  55. DigitalSource(ioline_t line);
  56. void mode(uint8_t output) override;
  57. uint8_t read() override;
  58. void write(uint8_t value) override;
  59. void toggle() override;
  60. private:
  61. ioline_t line;
  62. };