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 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/MarlinConfig.h"
  24. #include <pwm.h>
  25. #define NR_TIMERS TERN(STM32_XL_DENSITY, 14, 8) // Maple timers, 14 for STM32_XL_DENSITY (F/G chips), 8 for HIGH density (C D E)
  26. static uint16_t timer_freq[NR_TIMERS];
  27. inline uint8_t timer_and_index_for_pin(const pin_t pin, timer_dev **timer_ptr) {
  28. *timer_ptr = PIN_MAP[pin].timer_device;
  29. for (uint8_t i = 0; i < NR_TIMERS; i++) if (*timer_ptr == HAL_get_timer_dev(i))
  30. return i;
  31. return 0;
  32. }
  33. void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
  34. const uint16_t duty = invert ? v_size - v : v;
  35. if (PWM_PIN(pin)) {
  36. timer_dev *timer; UNUSED(timer);
  37. if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0)
  38. set_pwm_frequency(pin, PWM_FREQUENCY);
  39. const uint8_t channel = PIN_MAP[pin].timer_channel;
  40. timer_set_compare(timer, channel, duty);
  41. timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
  42. }
  43. else {
  44. pinMode(pin, OUTPUT);
  45. digitalWrite(pin, duty < v_size / 2 ? LOW : HIGH);
  46. }
  47. }
  48. void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
  49. if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
  50. timer_dev *timer; UNUSED(timer);
  51. timer_freq[timer_and_index_for_pin(pin, &timer)] = f_desired;
  52. // Protect used timers
  53. if (timer == HAL_get_timer_dev(MF_TIMER_TEMP)) return;
  54. if (timer == HAL_get_timer_dev(MF_TIMER_STEP)) return;
  55. #if MF_TIMER_PULSE != MF_TIMER_STEP
  56. if (timer == HAL_get_timer_dev(MF_TIMER_PULSE)) return;
  57. #endif
  58. if (!(timer->regs.bas->SR & TIMER_CR1_CEN)) // Ensure the timer is enabled
  59. timer_init(timer);
  60. const uint8_t channel = PIN_MAP[pin].timer_channel;
  61. timer_set_mode(timer, channel, TIMER_PWM);
  62. // Preload (resolution) cannot be equal to duty of 255 otherwise it may not result in digital off or on.
  63. uint16_t preload = 254;
  64. int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
  65. if (prescaler > 65535) { // For low frequencies increase prescaler
  66. prescaler = 65535;
  67. preload = (HAL_TIMER_RATE) / (prescaler + 1) / f_desired - 1;
  68. }
  69. if (prescaler < 0) return; // Too high frequency
  70. timer_set_reload(timer, preload);
  71. timer_set_prescaler(timer, prescaler);
  72. }
  73. #endif // __STM32F1__