From 6d672d42b9cd4150ddf6cd87497c52b952c02393 Mon Sep 17 00:00:00 2001 From: Bayu Laksono Date: Tue, 1 Oct 2024 23:01:12 +1000 Subject: [PATCH] AP_HAL_ESP32: Add GPIO driver and buzzer --- libraries/AP_HAL_ESP32/GPIO.cpp | 141 +++++++++++++++++++++ libraries/AP_HAL_ESP32/GPIO.h | 45 +++++++ libraries/AP_HAL_ESP32/HAL_ESP32_Class.cpp | 3 +- 3 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 libraries/AP_HAL_ESP32/GPIO.cpp create mode 100644 libraries/AP_HAL_ESP32/GPIO.h diff --git a/libraries/AP_HAL_ESP32/GPIO.cpp b/libraries/AP_HAL_ESP32/GPIO.cpp new file mode 100644 index 0000000000..66cbff5b31 --- /dev/null +++ b/libraries/AP_HAL_ESP32/GPIO.cpp @@ -0,0 +1,141 @@ +/* + * This file is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Code by Bayu Laksono + */ + +#include "AP_HAL_ESP32.h" +#include "GPIO.h" + +#include "hal/gpio_types.h" +#include "driver/gpio.h" + +using namespace ESP32; + +static gpio_num_t gpio_by_pin_num(uint8_t pin) +{ + if (pin < GPIO_NUM_MAX) { + return (gpio_num_t)pin; + } + return GPIO_NUM_NC; +} + +GPIO::GPIO() +{} + +void GPIO::init() +{} + +void GPIO::pinMode(uint8_t pin, uint8_t output) +{ + gpio_num_t g = gpio_by_pin_num(pin); + if (g != GPIO_NUM_NC) { + gpio_config_t io_conf = {}; + io_conf.intr_type = GPIO_INTR_DISABLE; + io_conf.mode = output ? GPIO_MODE_OUTPUT : GPIO_MODE_INPUT; + io_conf.pin_bit_mask = 1ULL<. + */ + +#pragma once + +#include "AP_HAL_ESP32.h" + +class ESP32::GPIO : public AP_HAL::GPIO { +public: + GPIO(); + void init() override; + void pinMode(uint8_t pin, uint8_t output) override; + uint8_t read(uint8_t pin) override; + void write(uint8_t pin, uint8_t value) override; + void toggle(uint8_t pin) override; + + /* Alternative interface: */ + AP_HAL::DigitalSource* channel(uint16_t n) override; + + /* return true if USB cable is connected */ + bool usb_connected(void) override; +}; + +class ESP32::DigitalSource : public AP_HAL::DigitalSource { +public: + DigitalSource(uint8_t pin); + void mode(uint8_t output) override; + uint8_t read() override; + void write(uint8_t value) override; + void toggle() override; +private: + uint8_t _pin; +}; diff --git a/libraries/AP_HAL_ESP32/HAL_ESP32_Class.cpp b/libraries/AP_HAL_ESP32/HAL_ESP32_Class.cpp index fb37729dec..c0b039e386 100644 --- a/libraries/AP_HAL_ESP32/HAL_ESP32_Class.cpp +++ b/libraries/AP_HAL_ESP32/HAL_ESP32_Class.cpp @@ -25,6 +25,7 @@ #include "WiFiUdpDriver.h" #include "RCInput.h" #include "RCOutput.h" +#include "GPIO.h" #include "Storage.h" #include "AnalogIn.h" #include "Util.h" @@ -69,7 +70,7 @@ static Empty::Storage storageDriver; #else static ESP32::Storage storageDriver; #endif -static Empty::GPIO gpioDriver; +static ESP32::GPIO gpioDriver; #if AP_SIM_ENABLED static Empty::RCOutput rcoutDriver; #else