diff --git a/DCCTimer.h b/DCCTimer.h index 2214851..7a9d940 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -1,7 +1,7 @@ /* - * © 2022 Paul M. Antoine + * © 2022-2023 Paul M. Antoine * © 2021 Mike S - * © 2021-2022 Harald Barth + * © 2021-2023 Harald Barth * © 2021 Fred Decker * All rights reserved. * @@ -62,6 +62,9 @@ class DCCTimer { static bool isPWMPin(byte pin); static void setPWM(byte pin, bool high); static void clearPWM(); + static void DCCEXanalogWriteFrequency(uint8_t pin, uint32_t frequency); + static void DCCEXanalogWrite(uint8_t pin, int value); + // Update low ram level. Allow for extra bytes to be specified // by estimation or inspection, that may be used by other // called subroutines. Must be called with interrupts disabled. diff --git a/DCCTimerESP.cpp b/DCCTimerESP.cpp index caaae9d..cf5978e 100644 --- a/DCCTimerESP.cpp +++ b/DCCTimerESP.cpp @@ -150,6 +150,45 @@ int DCCTimer::freeMemory() { void DCCTimer::reset() { ESP.restart(); } + +#include "esp32-hal.h" +#include "soc/soc_caps.h" + + +#ifdef SOC_LEDC_SUPPORT_HS_MODE +#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM<<1) +#else +#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM) +#endif + +static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 }; +static int cnt_channel = LEDC_CHANNELS; + +void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t frequency) { + if (pin < SOC_GPIO_PIN_COUNT) { + if (pin_to_channel[pin] != 0) { + ledcSetup(pin_to_channel[pin], frequency, 8); + } + } +} + +void DCCTimer::DCCEXanalogWrite(uint8_t pin, int value) { + if (pin < SOC_GPIO_PIN_COUNT) { + if (pin_to_channel[pin] == 0) { + if (!cnt_channel) { + log_e("No more PWM channels available! All %u already used", LEDC_CHANNELS); + return; + } + pin_to_channel[pin] = --cnt_channel; + ledcAttachPin(pin, cnt_channel); + ledcSetup(cnt_channel, 1000, 8); + } else { + ledcAttachPin(pin, pin_to_channel[pin]); + } + ledcWrite(pin_to_channel[pin], value); + } +} + int ADCee::init(uint8_t pin) { pinMode(pin, ANALOG); adc1_config_width(ADC_WIDTH_BIT_12); diff --git a/ESP32-fixes.cpp b/ESP32-fixes.cpp deleted file mode 100644 index e3c350c..0000000 --- a/ESP32-fixes.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * © 2022 Harald Barth - * All rights reserved. - * - * This file is part of CommandStation-EX - * - * This 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. - * - * It 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 CommandStation. If not, see . - */ -#ifdef ARDUINO_ARCH_ESP32 -#include -#include "ESP32-fixes.h" - -#include "esp32-hal.h" -#include "soc/soc_caps.h" - - -#ifdef SOC_LEDC_SUPPORT_HS_MODE -#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM<<1) -#else -#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM) -#endif - -static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 }; -static int cnt_channel = LEDC_CHANNELS; - -void DCCEXanalogWriteFrequency(uint8_t pin, uint32_t frequency) { - if (pin < SOC_GPIO_PIN_COUNT) { - if (pin_to_channel[pin] != 0) { - ledcSetup(pin_to_channel[pin], frequency, 8); - } - } -} - -void DCCEXanalogWrite(uint8_t pin, int value) { - if (pin < SOC_GPIO_PIN_COUNT) { - if (pin_to_channel[pin] == 0) { - if (!cnt_channel) { - log_e("No more PWM channels available! All %u already used", LEDC_CHANNELS); - return; - } - pin_to_channel[pin] = --cnt_channel; - ledcAttachPin(pin, cnt_channel); - ledcSetup(cnt_channel, 1000, 8); - } else { - ledcAttachPin(pin, pin_to_channel[pin]); - } - ledcWrite(pin_to_channel[pin], value); - } -} -#endif diff --git a/ESP32-fixes.h b/ESP32-fixes.h deleted file mode 100644 index 0fd4a7f..0000000 --- a/ESP32-fixes.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * © 2022 Harald Barth - * All rights reserved. - * - * This file is part of CommandStation-EX - * - * This 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. - * - * It 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 CommandStation. If not, see . - */ -#ifdef ARDUINO_ARCH_ESP32 -#pragma once -#include -void DCCEXanalogWriteFrequency(uint8_t pin, uint32_t frequency); -void DCCEXanalogWrite(uint8_t pin, int value); -#endif - diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 28ecba6..9f39123 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -1,8 +1,8 @@ /* - * © 2022 Paul M Antoine + * © 2022-2023 Paul M Antoine * © 2021 Mike S * © 2021 Fred Decker - * © 2020-2022 Harald Barth + * © 2020-2023 Harald Barth * © 2020-2021 Chris Harlow * All rights reserved. * @@ -27,10 +27,6 @@ #include "DCCTimer.h" #include "DIAG.h" -#if defined(ARDUINO_ARCH_ESP32) -#include "ESP32-fixes.h" -#endif - bool MotorDriver::commonFaultPin=false; volatile portreg_t shadowPORTA; @@ -286,7 +282,7 @@ void MotorDriver::setDCSignal(byte speedcode) { f = taurustones[ (tSpeed-2)/2 ] ; } } - DCCEXanalogWriteFrequency(brakePin, f); // set DC PWM frequency to 100Hz XXX May move to setup + DCCTimer::DCCEXanalogWriteFrequency(brakePin, f); // set DC PWM frequency to 100Hz XXX May move to setup } #endif if (tSpeed <= 1) brake = 255; @@ -295,7 +291,7 @@ void MotorDriver::setDCSignal(byte speedcode) { if (invertBrake) brake=255-brake; #if defined(ARDUINO_ARCH_ESP32) - DCCEXanalogWrite(brakePin,brake); + DCCTimer::DCCEXanalogWrite(brakePin,brake); #else analogWrite(brakePin,brake); #endif