My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fast_pwm.cpp 4.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../platforms.h"
  23. #ifdef HAL_STM32
  24. #include "../../inc/MarlinConfig.h"
  25. // Array to support sticky frequency sets per timer
  26. static uint16_t timer_freq[TIMER_NUM];
  27. void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
  28. const uint16_t duty = invert ? v_size - v : v;
  29. if (PWM_PIN(pin)) {
  30. const PinName pin_name = digitalPinToPinName(pin);
  31. TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
  32. const timer_index_t index = get_timer_index(Instance);
  33. const bool needs_freq = (HardwareTimer_Handle[index] == nullptr);
  34. if (needs_freq) // A new instance must be set to the default frequency of PWM_FREQUENCY
  35. HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
  36. HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
  37. const uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
  38. const TimerModes_t previousMode = HT->getMode(channel);
  39. if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
  40. HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
  41. if (needs_freq && timer_freq[index] == 0) // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY
  42. set_pwm_frequency(pin_name, PWM_FREQUENCY); // Set the frequency and save the value to the assigned index no.
  43. // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
  44. // If such a need were to manifest then we would need to calc the resolution based on the v_size parameter and add code for it.
  45. HT->setCaptureCompare(channel, duty, RESOLUTION_8B_COMPARE_FORMAT); // Set the duty, the calc is done in the library :)
  46. pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
  47. if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
  48. }
  49. else {
  50. pinMode(pin, OUTPUT);
  51. digitalWrite(pin, duty < v_size / 2 ? LOW : HIGH);
  52. }
  53. }
  54. void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
  55. if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
  56. const PinName pin_name = digitalPinToPinName(pin);
  57. TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
  58. const timer_index_t index = get_timer_index(Instance);
  59. // Protect used timers.
  60. #ifdef STEP_TIMER
  61. if (index == TIMER_INDEX(STEP_TIMER)) return;
  62. #endif
  63. #ifdef TEMP_TIMER
  64. if (index == TIMER_INDEX(TEMP_TIMER)) return;
  65. #endif
  66. #if defined(PULSE_TIMER) && MF_TIMER_PULSE != MF_TIMER_STEP
  67. if (index == TIMER_INDEX(PULSE_TIMER)) return;
  68. #endif
  69. if (HardwareTimer_Handle[index] == nullptr) // If frequency is set before duty we need to create a handle here.
  70. HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
  71. HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
  72. HT->setOverflow(f_desired, HERTZ_FORMAT);
  73. timer_freq[index] = f_desired; // Save the last frequency so duty will not set the default for this timer number.
  74. }
  75. #endif // HAL_STM32