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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // --------------------------------------------------------------------------
  24. // Includes
  25. // --------------------------------------------------------------------------
  26. #include "HAL.h"
  27. #include "HAL_timers_STM32.h"
  28. // --------------------------------------------------------------------------
  29. // Externals
  30. // --------------------------------------------------------------------------
  31. // --------------------------------------------------------------------------
  32. // Local defines
  33. // --------------------------------------------------------------------------
  34. #define NUM_HARDWARE_TIMERS 2
  35. //#define PRESCALER 1
  36. // --------------------------------------------------------------------------
  37. // Types
  38. // --------------------------------------------------------------------------
  39. // --------------------------------------------------------------------------
  40. // Public Variables
  41. // --------------------------------------------------------------------------
  42. // --------------------------------------------------------------------------
  43. // Private Variables
  44. // --------------------------------------------------------------------------
  45. stm32f4_timer_t TimerHandle[NUM_HARDWARE_TIMERS];
  46. // --------------------------------------------------------------------------
  47. // Function prototypes
  48. // --------------------------------------------------------------------------
  49. // --------------------------------------------------------------------------
  50. // Private functions
  51. // --------------------------------------------------------------------------
  52. // --------------------------------------------------------------------------
  53. // Public functions
  54. // --------------------------------------------------------------------------
  55. bool timers_initialized[NUM_HARDWARE_TIMERS] = { false };
  56. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  57. if (!timers_initialized[timer_num]) {
  58. uint32_t step_prescaler = STEPPER_TIMER_PRESCALE - 1,
  59. temp_prescaler = TEMP_TIMER_PRESCALE - 1;
  60. switch (timer_num) {
  61. case STEP_TIMER_NUM:
  62. // STEPPER TIMER - use a 32bit timer if possible
  63. TimerHandle[timer_num].timer = STEP_TIMER_DEV;
  64. TimerHandle[timer_num].irqHandle = Step_Handler;
  65. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / step_prescaler) / frequency) - 1, step_prescaler);
  66. HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, STEP_TIMER_IRQ_PRIO, 0);
  67. break;
  68. case TEMP_TIMER_NUM:
  69. // TEMP TIMER - any available 16bit Timer
  70. TimerHandle[timer_num].timer = TEMP_TIMER_DEV;
  71. TimerHandle[timer_num].irqHandle = Temp_Handler;
  72. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / temp_prescaler) / frequency) - 1, temp_prescaler);
  73. HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, TEMP_TIMER_IRQ_PRIO, 0);
  74. break;
  75. }
  76. timers_initialized[timer_num] = true;
  77. }
  78. }
  79. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  80. const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
  81. HAL_NVIC_EnableIRQ(IRQ_Id);
  82. }
  83. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  84. const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
  85. HAL_NVIC_DisableIRQ(IRQ_Id);
  86. // We NEED memory barriers to ensure Interrupts are actually disabled!
  87. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  88. __DSB();
  89. __ISB();
  90. }
  91. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  92. const uint32_t IRQ_Id = getTimerIrq(TimerHandle[timer_num].timer);
  93. return NVIC->ISER[IRQ_Id >> 5] & _BV32(IRQ_Id & 0x1F);
  94. }
  95. #endif // ARDUINO_ARCH_STM32