fast_pwm.cpp 3.1 KB

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