PWM based motor driver added from other project
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
idf_component_register(
|
||||
SRC_DIRS "src"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES freeRtosApi
|
||||
project_bases
|
||||
esp_common
|
||||
esp_driver_gpio
|
||||
esp_driver_ledc
|
||||
esp_driver_gptimer
|
||||
)
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* @file gpioPort.hpp
|
||||
* @author Gustice
|
||||
* @brief GPIO-Port classes
|
||||
* @details C++ Wrapper for GPIO port
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "portBase.hpp"
|
||||
|
||||
/**
|
||||
* @brief Digital-Port BaseClass
|
||||
*/
|
||||
class GpioPort : public PortBase {
|
||||
public:
|
||||
enum PullResistor {
|
||||
Floating = 0,
|
||||
PullUp,
|
||||
PullDown,
|
||||
};
|
||||
|
||||
enum Logic {
|
||||
Normal = 0, // Normal logic
|
||||
Inverted, // Inverted logic -> Set low on active state
|
||||
};
|
||||
|
||||
enum Interrupt {
|
||||
PositiveEdge = gpio_int_type_t::GPIO_INTR_POSEDGE,
|
||||
NegativeEdge = gpio_int_type_t::GPIO_INTR_NEGEDGE,
|
||||
AnyEdge = gpio_int_type_t::GPIO_INTR_ANYEDGE
|
||||
};
|
||||
|
||||
GpioPort(gpio_num_t port);
|
||||
~GpioPort();
|
||||
|
||||
uint64_t GetReserved(void) { return _reservedPorts; }
|
||||
|
||||
protected:
|
||||
const gpio_num_t _port;
|
||||
|
||||
private:
|
||||
static uint64_t _reservedPorts;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Input-Port
|
||||
* @details Provides abstract reading functionality
|
||||
*/
|
||||
class InputPort : public GpioPort {
|
||||
public:
|
||||
InputPort(gpio_num_t port, GpioPort::PullResistor resistor = PullResistor::Floating);
|
||||
void setLogic(GpioPort::Logic logic) {
|
||||
_logic = logic;
|
||||
}
|
||||
~InputPort() = default;
|
||||
|
||||
bool read(void);
|
||||
|
||||
private:
|
||||
GpioPort::Logic _logic = GpioPort::Logic::Normal;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Output-Port
|
||||
* @details Provides abstract setting functionality
|
||||
*/
|
||||
class OutputPort : public GpioPort {
|
||||
public:
|
||||
OutputPort(gpio_num_t port, GpioPort::Logic mode = Logic::Normal);
|
||||
~OutputPort(){};
|
||||
|
||||
void write(int active);
|
||||
|
||||
private:
|
||||
const GpioPort::Logic _mode = Normal;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Interrupt-Port
|
||||
*/
|
||||
class TriggeredInput : public InputPort {
|
||||
public:
|
||||
TriggeredInput(gpio_num_t port,
|
||||
GpioPort::PullResistor resistor = GpioPort::PullResistor::Floating,
|
||||
TriggeredInput::Interrupt edge = TriggeredInput::Interrupt::AnyEdge);
|
||||
~TriggeredInput(){};
|
||||
|
||||
void RegisterIsr(gpio_isr_t pIsrHandle);
|
||||
|
||||
private:
|
||||
static bool _isrServiceInitiated;
|
||||
};
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @file gpioPort.cpp
|
||||
* @author Gustice
|
||||
* @brief C++ Wrapper for GPIO port
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*/
|
||||
|
||||
#include "gpioPort.hpp"
|
||||
#include "esp_log.h"
|
||||
|
||||
///
|
||||
/// Gpio - Implementation
|
||||
///
|
||||
uint64_t GpioPort::_reservedPorts = 0;
|
||||
|
||||
GpioPort::GpioPort(gpio_num_t port) : _port(port) {
|
||||
uint64_t p = 1ULL << port;
|
||||
if ((_reservedPorts & p) != 0) {
|
||||
ESP_LOGW("HAL", "GpioPort Port %d is already in use!", port);
|
||||
}
|
||||
|
||||
_reservedPorts |= p;
|
||||
}
|
||||
|
||||
GpioPort::~GpioPort() {
|
||||
uint64_t p = 1ULL << _port;
|
||||
_reservedPorts &= ~p;
|
||||
}
|
||||
|
||||
///
|
||||
/// InputPort - Implementation
|
||||
///
|
||||
InputPort::InputPort(gpio_num_t port, GpioPort::PullResistor resistor) : GpioPort(port) {
|
||||
gpio_config_t config = {
|
||||
.pin_bit_mask = 1ULL << port,
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = resistor == PullUp ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = resistor == PullDown ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(gpio_config(&config));
|
||||
}
|
||||
|
||||
bool InputPort::read(void) {
|
||||
auto value = gpio_get_level(_port);
|
||||
return ((value ^ _logic) & 0x01) == 1;
|
||||
}
|
||||
|
||||
///
|
||||
/// TriggeredInput - Implementation
|
||||
///
|
||||
bool TriggeredInput::_isrServiceInitiated = false;
|
||||
|
||||
TriggeredInput::TriggeredInput(gpio_num_t port, GpioPort::PullResistor resistor,
|
||||
TriggeredInput::Interrupt edge)
|
||||
: InputPort(port, resistor) {
|
||||
assert(gpio_set_intr_type(_port, (gpio_int_type_t)edge) == ESP_OK);
|
||||
|
||||
if (!_isrServiceInitiated) {
|
||||
_isrServiceInitiated = true;
|
||||
esp_err_t err = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL2);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI("HAL", "ISR-service installed successfully for gpio-port");
|
||||
} else {
|
||||
ESP_LOGE("HAL", "ISR-service failed to install. Code '%s'", esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TriggeredInput::RegisterIsr(gpio_isr_t pIsrHandle) {
|
||||
ESP_LOGI("HAL", "Hooking ISR-Function to gpio-port %d", _port);
|
||||
gpio_isr_handler_add(_port, pIsrHandle, nullptr);
|
||||
}
|
||||
|
||||
///
|
||||
/// OutputPort - Implementation
|
||||
///
|
||||
OutputPort::OutputPort(gpio_num_t port, GpioPort::Logic mode) : GpioPort(port), _mode(mode) {
|
||||
gpio_config_t io_conf = {
|
||||
.pin_bit_mask = 1ULL << port,
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
gpio_config(&io_conf);
|
||||
}
|
||||
|
||||
void OutputPort::write(int active) {
|
||||
if (_mode == GpioPort::Normal) {
|
||||
gpio_set_level(_port, active & 1);
|
||||
} else {
|
||||
active ^= 1;
|
||||
gpio_set_level(_port, active & 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user