Skip to content

Commit

Permalink
Implement hardware PWM support.
Browse files Browse the repository at this point in the history
  • Loading branch information
phizev committed Dec 26, 2024
1 parent 728ed0b commit a46a9b2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
16 changes: 11 additions & 5 deletions Marlin/src/HAL/TEENSY40_41/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#undef PSTR
#define PSTR(str) ({static const char *data = (str); &data[0];})

#define HAL_CAN_SET_PWM_FREQ

// ------------------------
// Serial ports
// ------------------------
Expand Down Expand Up @@ -221,11 +223,15 @@ class MarlinHAL {

/**
* Set the PWM duty cycle for the pin to the given value.
* No option to invert the duty cycle [default = false]
* No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
* Optionally invert the duty cycle [default = false]
* Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
*/
static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
analogWrite(pin, v);
}
static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);

/**
* Set the PWM output frequency. This may affect multiple pins, though
* Teensy 4.x provides many timers affecting only a single pin.
* See: https://www.pjrc.com/teensy/td_pulse.html
*/
static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
};
57 changes: 57 additions & 0 deletions Marlin/src/HAL/TEENSY40_41/fast_pwm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program 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 program 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 <https://www.gnu.org/licenses/>.
*
*/

#ifdef __IMXRT1062__

#include "../../inc/MarlinConfig.h"

#include "HAL.h"

void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size /*=255*/, const bool invert) {

uint32_t bits = 1;
uint16_t value = v;

if (v > v_size) {
value = v_size;
}
value = invert ? v_size - value : value;

// Choose scale as smallest power of 2 which holds v_size.
uint32_t scale = 1;
while (scale < v_size) {
bits++;
scale *= 2;
}

uint32_t scaled_val = scale * value / v_size;

uint32_t prior = analogWriteResolution(bits);
analogWrite(pin, scaled_val);
analogWriteResolution(prior);
}

void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
analogWriteFrequency(pin, f_desired);
}

#endif // __IMXRT1062__
4 changes: 0 additions & 4 deletions Marlin/src/HAL/TEENSY40_41/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
#error "EMERGENCY_PARSER is not yet implemented for Teensy 4.0/4.1. Disable EMERGENCY_PARSER to continue."
#endif

#if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY
#error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported for Teensy 4.0/4.1."
#endif

#if HAS_TMC_SW_SERIAL
#error "TMC220x Software Serial is not supported for Teensy 4.0/4.1."
#endif
Expand Down

0 comments on commit a46a9b2

Please sign in to comment.