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_STM32F7.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2016 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. #ifdef STM32F7
  23. // --------------------------------------------------------------------------
  24. // Includes
  25. // --------------------------------------------------------------------------
  26. #include "../HAL.h"
  27. #include "HAL_timers_STM32F7.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. tTimerConfig timerConfig[NUM_HARDWARE_TIMERS];
  46. // --------------------------------------------------------------------------
  47. // Function prototypes
  48. // --------------------------------------------------------------------------
  49. // --------------------------------------------------------------------------
  50. // Private functions
  51. // --------------------------------------------------------------------------
  52. // --------------------------------------------------------------------------
  53. // Public functions
  54. // --------------------------------------------------------------------------
  55. bool timers_initialised[NUM_HARDWARE_TIMERS] = {false};
  56. void HAL_timer_start(uint8_t timer_num, uint32_t frequency) {
  57. if(!timers_initialised[timer_num]) {
  58. switch (timer_num) {
  59. case STEP_TIMER_NUM:
  60. //STEPPER TIMER TIM5 //use a 32bit timer
  61. __HAL_RCC_TIM5_CLK_ENABLE();
  62. timerConfig[0].timerdef.Instance = TIM5;
  63. timerConfig[0].timerdef.Init.Prescaler = (STEPPER_TIMER_PRESCALE);
  64. timerConfig[0].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
  65. timerConfig[0].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  66. timerConfig[0].IRQ_Id = TIM5_IRQn;
  67. timerConfig[0].callback = (uint32_t)TC5_Handler;
  68. HAL_NVIC_SetPriority(timerConfig[0].IRQ_Id, 1, 0);
  69. pinMode(STEPPER_ENABLE_PIN,OUTPUT);
  70. digitalWrite(STEPPER_ENABLE_PIN,LOW);
  71. break;
  72. case TEMP_TIMER_NUM:
  73. //TEMP TIMER TIM7 // any available 16bit Timer (1 already used for PWM)
  74. __HAL_RCC_TIM7_CLK_ENABLE();
  75. timerConfig[1].timerdef.Instance = TIM7;
  76. timerConfig[1].timerdef.Init.Prescaler = (TEMP_TIMER_PRESCALE);
  77. timerConfig[1].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
  78. timerConfig[1].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  79. timerConfig[1].IRQ_Id = TIM7_IRQn;
  80. timerConfig[1].callback = (uint32_t)TC7_Handler;
  81. HAL_NVIC_SetPriority(timerConfig[1].IRQ_Id, 2, 0);
  82. break;
  83. }
  84. timers_initialised[timer_num] = true;
  85. }
  86. timerConfig[timer_num].timerdef.Init.Period = ((HAL_TIMER_RATE / timerConfig[timer_num].timerdef.Init.Prescaler) / (frequency)) - 1;
  87. if(HAL_TIM_Base_Init(&timerConfig[timer_num].timerdef) == HAL_OK ){
  88. HAL_TIM_Base_Start_IT(&timerConfig[timer_num].timerdef);
  89. }
  90. }
  91. //forward the interrupt
  92. extern "C" void TIM5_IRQHandler()
  93. {
  94. ((void(*)(void))timerConfig[0].callback)();
  95. }
  96. extern "C" void TIM7_IRQHandler()
  97. {
  98. ((void(*)(void))timerConfig[1].callback)();
  99. }
  100. void HAL_timer_set_count (uint8_t timer_num, uint32_t count) {
  101. __HAL_TIM_SetAutoreload(&timerConfig[timer_num].timerdef, count);
  102. }
  103. void HAL_timer_set_current_count (uint8_t timer_num, uint32_t count) {
  104. __HAL_TIM_SetAutoreload(&timerConfig[timer_num].timerdef, count);
  105. }
  106. void HAL_timer_enable_interrupt (uint8_t timer_num) {
  107. HAL_NVIC_EnableIRQ(timerConfig[timer_num].IRQ_Id);
  108. }
  109. void HAL_timer_disable_interrupt (uint8_t timer_num) {
  110. HAL_NVIC_DisableIRQ(timerConfig[timer_num].IRQ_Id);
  111. }
  112. hal_timer_t HAL_timer_get_count (uint8_t timer_num) {
  113. return __HAL_TIM_GetAutoreload(&timerConfig[timer_num].timerdef);
  114. }
  115. uint32_t HAL_timer_get_current_count(uint8_t timer_num) {
  116. return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
  117. }
  118. void HAL_timer_isr_prologue (uint8_t timer_num) {
  119. if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
  120. __HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);
  121. }
  122. }
  123. #endif