My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

fast_pwm.cpp 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifdef __STM32F1__
  23. #include "../../inc/MarlinConfigPre.h"
  24. #include <pwm.h>
  25. #include "HAL.h"
  26. #include "timers.h"
  27. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
  28. timer_dev *timer = PIN_MAP[pin].timer_device;
  29. uint16_t max_val = timer->regs.bas->ARR * v / v_size;
  30. if (invert) max_val = v_size - max_val;
  31. pwmWrite(pin, max_val);
  32. }
  33. #if NEEDS_HARDWARE_PWM
  34. void set_pwm_frequency(const pin_t pin, int f_desired) {
  35. if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
  36. timer_dev *timer = PIN_MAP[pin].timer_device;
  37. uint8_t channel = PIN_MAP[pin].timer_channel;
  38. // Protect used timers
  39. if (timer == get_timer_dev(TEMP_TIMER_NUM)) return;
  40. if (timer == get_timer_dev(STEP_TIMER_NUM)) return;
  41. #if PULSE_TIMER_NUM != STEP_TIMER_NUM
  42. if (timer == get_timer_dev(PULSE_TIMER_NUM)) return;
  43. #endif
  44. if (!(timer->regs.bas->SR & TIMER_CR1_CEN)) // Ensure the timer is enabled
  45. timer_init(timer);
  46. timer_set_mode(timer, channel, TIMER_PWM);
  47. uint16_t preload = 255; // Lock 255 PWM resolution for high frequencies
  48. int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
  49. if (prescaler > 65535) { // For low frequencies increase prescaler
  50. prescaler = 65535;
  51. preload = (HAL_TIMER_RATE) / (prescaler + 1) / f_desired - 1;
  52. }
  53. if (prescaler < 0) return; // Too high frequency
  54. timer_set_reload(timer, preload);
  55. timer_set_prescaler(timer, prescaler);
  56. }
  57. #endif // NEEDS_HARDWARE_PWM
  58. #endif // __STM32F1__