From bb53280341ed810e701cd21cd570a062475546cc Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Thu, 24 Jun 2021 08:14:16 -1000 Subject: [PATCH] Swag at handling PWM invert in hardware TODO: 1. if it works, do the same for other users of LEDC 2. Remove invert_pwm --- Grbl_Esp32/src/Spindles/PWMSpindle.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp index 2c79d142..0fc7eb67 100644 --- a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp @@ -28,6 +28,8 @@ #include "../GCode.h" // gc_state.modal #include "../Logging.h" +extern "C" void __pinMode(uint8_t pin, uint8_t mode); + // ======================= PWM ============================== /* This gets called at startup or whenever a spindle setting changes @@ -62,7 +64,14 @@ namespace Spindles { auto outputNative = _output_pin.getNative(Pin::Capabilities::PWM); ledcSetup(_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel - ledcAttachPin(outputNative, _pwm_chan_num); // attach the PWM to the pin + + // This is equivalent to ledcAttachPin with the addition of + // using the hardware inversion function in the GPIO matrix. + // We use that to apply the active low function in hardware. + __pinMode(outputNative, OUTPUT); + uint8_t function = ((_pwm_chan_num / 8) ? LEDC_LS_SIG_OUT0_IDX : LEDC_HS_SIG_OUT0_IDX) + (_pwm_chan_num % 8); + bool isActiveLow = _output_pin.getAttr().has(Pin::Attr::ActiveLow); + pinMatrixOutAttach(outputNative, function, isActiveLow, false); _enable_pin.setAttr(Pin::Attr::Output); _direction_pin.setAttr(Pin::Attr::Output);