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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. //#define PRESCALER 1
  30. // ------------------------
  31. // Private Variables
  32. // ------------------------
  33. stm32_timer_t TimerHandle[NUM_HARDWARE_TIMERS];
  34. // ------------------------
  35. // Public functions
  36. // ------------------------
  37. bool timers_initialized[NUM_HARDWARE_TIMERS] = { false };
  38. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  39. if (!timers_initialized[timer_num]) {
  40. uint32_t step_prescaler = STEPPER_TIMER_PRESCALE - 1,
  41. temp_prescaler = TEMP_TIMER_PRESCALE - 1;
  42. switch (timer_num) {
  43. case STEP_TIMER_NUM:
  44. // STEPPER TIMER - use a 32bit timer if possible
  45. TimerHandle[timer_num].timer = STEP_TIMER_DEV;
  46. TimerHandle[timer_num].irqHandle = Step_Handler;
  47. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / step_prescaler) / frequency) - 1, step_prescaler);
  48. HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, STEP_TIMER_IRQ_PRIO, 0);
  49. break;
  50. case TEMP_TIMER_NUM:
  51. // TEMP TIMER - any available 16bit Timer
  52. TimerHandle[timer_num].timer = TEMP_TIMER_DEV;
  53. TimerHandle[timer_num].irqHandle = Temp_Handler;
  54. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / temp_prescaler) / frequency) - 1, temp_prescaler);
  55. HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, TEMP_TIMER_IRQ_PRIO, 0);
  56. break;
  57. }
  58. timers_initialized[timer_num] = true;
  59. }
  60. }
  61. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  62. const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
  63. HAL_NVIC_EnableIRQ(IRQ_Id);
  64. }
  65. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  66. const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
  67. HAL_NVIC_DisableIRQ(IRQ_Id);
  68. // We NEED memory barriers to ensure Interrupts are actually disabled!
  69. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  70. __DSB();
  71. __ISB();
  72. }
  73. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  74. const uint32_t IRQ_Id = getTimerIrq(TimerHandle[timer_num].timer);
  75. return NVIC->ISER[IRQ_Id >> 5] & _BV32(IRQ_Id & 0x1F);
  76. }
  77. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC