Skip to content

esp32: add support for ESP-IDF v5.5 #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ sentence=Allows Arduino boards to control a variety of servo motors.
paragraph=This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer.<br />On the Arduino Due you can control up to 60 servos.
category=Device Control
url=https://www.arduino.cc/reference/en/libraries/servo/
architectures=avr,megaavr,sam,samd,nrf52,stm32f4,mbed,mbed_nano,mbed_portenta,mbed_rp2040,renesas,renesas_portenta,renesas_uno
architectures=avr,megaavr,sam,samd,nrf52,stm32f4,mbed,mbed_nano,mbed_portenta,mbed_rp2040,renesas,renesas_portenta,renesas_uno,esp32
36 changes: 26 additions & 10 deletions src/esp32/Servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,42 @@

class ServoImpl {
uint8_t pin;
uint8_t channel;

public:
ServoImpl(const uint8_t _pin, const uint8_t _channel) : pin(_pin) {
// Setup timer
ServoImpl(const uint8_t _pin, const uint8_t _channel) : pin(_pin), channel(_channel) {
#if ESP_IDF_VERSION_MAJOR < 5
ledcSetup(_channel, (1000000 / REFRESH_INTERVAL), LEDC_MAX_BIT_WIDTH);

// Attach timer to a LED pin
ledcAttachPin(pin, _channel);
#else
ledcAttachChannel(pin, (1000000 / REFRESH_INTERVAL), LEDC_MAX_BIT_WIDTH, channel);
#endif
}

~ServoImpl() {
#if ESP_IDF_VERSION_MAJOR < 5
ledcDetachPin(pin);
#else
ledcDetach(pin);
#endif
}

void set(const uint8_t _channel, const uint32_t duration_us) {
ledcWrite(_channel, LEDC_US_TO_TICKS(duration_us));
void set(const uint32_t duration_us) {
ledcWrite(ref(), LEDC_US_TO_TICKS(duration_us));
}

uint32_t get(const uint8_t _channel) const {
return LEDC_TICKS_TO_US(ledcRead(_channel));
uint32_t get() const {
return LEDC_TICKS_TO_US(ledcRead(ref()));
}

private:
// read/write channel argument changed to pin between 4.x and 5.x
uint8_t ref() const {
#if ESP_IDF_VERSION_MAJOR < 5
return channel;
#else
return pin;
#endif
}
};

Expand Down Expand Up @@ -121,7 +137,7 @@ void Servo::writeMicroseconds(int value)
else if (value > SERVO_MAX())
value = SERVO_MAX();

servos[this->servoIndex]->set(this->servoIndex, value);
servos[this->servoIndex]->set(value);
}
}

Expand All @@ -135,7 +151,7 @@ int Servo::readMicroseconds()
if (!servos[this->servoIndex]) {
return 0;
}
return servos[this->servoIndex]->get(this->servoIndex);
return servos[this->servoIndex]->get();
}

bool Servo::attached()
Expand Down
6 changes: 5 additions & 1 deletion src/esp32/ServoTimers.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#define MAX_PWM_SERVOS 16

#if ESP_IDF_VERSION_MAJOR < 5
#define LEDC_MAX_BIT_WIDTH SOC_LEDC_TIMER_BIT_WIDE_NUM
#else
#define LEDC_MAX_BIT_WIDTH SOC_LEDC_TIMER_BIT_WIDTH
#endif

constexpr uint32_t BIT_RESOLUTION = (1 << LEDC_MAX_BIT_WIDTH) - 1;

#define LEDC_US_TO_TICKS(us) static_cast<uint32_t>((us * BIT_RESOLUTION) / REFRESH_INTERVAL)
#define LEDC_TICKS_TO_US(ticks) static_cast<uint32_t>((ticks * REFRESH_INTERVAL) / BIT_RESOLUTION)
#define LEDC_TICKS_TO_US(ticks) static_cast<uint32_t>((ticks * REFRESH_INTERVAL) / BIT_RESOLUTION)