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.

HAL_timers_STM32.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
  23. #include "HAL.h"
  24. #include "HAL_timers_STM32.h"
  25. // ------------------------
  26. // Local defines
  27. // ------------------------
  28. #define NUM_HARDWARE_TIMERS 2
  29. // ------------------------
  30. // Private Variables
  31. // ------------------------
  32. stm32_timer_t TimerHandle[NUM_HARDWARE_TIMERS];
  33. // ------------------------
  34. // Public functions
  35. // ------------------------
  36. bool timers_initialized[NUM_HARDWARE_TIMERS] = { false };
  37. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  38. if (!timers_initialized[timer_num]) {
  39. uint32_t step_prescaler = STEPPER_TIMER_PRESCALE - 1,
  40. temp_prescaler = TEMP_TIMER_PRESCALE - 1;
  41. switch (timer_num) {
  42. case STEP_TIMER_NUM:
  43. // STEPPER TIMER - use a 32bit timer if possible
  44. TimerHandle[timer_num].timer = STEP_TIMER_DEV;
  45. TimerHandle[timer_num].irqHandle = Step_Handler;
  46. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / step_prescaler) / frequency) - 1, step_prescaler);
  47. HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, STEP_TIMER_IRQ_PRIO, 0);
  48. break;
  49. case TEMP_TIMER_NUM:
  50. // TEMP TIMER - any available 16bit Timer
  51. TimerHandle[timer_num].timer = TEMP_TIMER_DEV;
  52. TimerHandle[timer_num].irqHandle = Temp_Handler;
  53. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / temp_prescaler) / frequency) - 1, temp_prescaler);
  54. HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, TEMP_TIMER_IRQ_PRIO, 0);
  55. break;
  56. }
  57. timers_initialized[timer_num] = true;
  58. }
  59. }
  60. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  61. const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
  62. HAL_NVIC_EnableIRQ(IRQ_Id);
  63. }
  64. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  65. const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
  66. HAL_NVIC_DisableIRQ(IRQ_Id);
  67. // We NEED memory barriers to ensure Interrupts are actually disabled!
  68. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  69. __DSB();
  70. __ISB();
  71. }
  72. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  73. const uint32_t IRQ_Id = getTimerIrq(TimerHandle[timer_num].timer);
  74. return NVIC->ISER[IRQ_Id >> 5] & _BV32(IRQ_Id & 0x1F);
  75. }
  76. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC