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.

84 lines
2.7 KiB

1 year ago
2 months ago
1 year ago
2 months ago
1 year ago
  1. #include "ztim.hpp"
  2. #include "irq_dispatcher/ztim_irq_dispatcher.hpp"
  3. using namespace iflytop;
  4. void ZTIM::initialize(zchip_tim_t *htim, mode_t mode) {
  5. m_htim = htim;
  6. m_mode = mode;
  7. if (m_mode == ktm_pwm) {
  8. ZEARLY_ASSERT(m_htim->Init.AutoReloadPreload == TIM_AUTORELOAD_PRELOAD_ENABLE);
  9. ZEARLY_ASSERT(m_htim->Init.CounterMode == TIM_COUNTERMODE_UP);
  10. ZEARLY_ASSERT(m_htim->Channel)
  11. }
  12. }
  13. ZTIM::mode_t ZTIM::getMode() { return m_mode; }
  14. /******************************************************
  15. * kTimMode_timer *
  16. ******************************************************/
  17. /**
  18. * @brief startTimer
  19. *
  20. * @param periodus
  21. * @param onirq null,使
  22. */
  23. void ZTIM::simpleTimer_startByPeriod(uint32_t periodus) { simpleTimer_startByFreq(1000000.0 / periodus); }
  24. void ZTIM::simpleTimer_startByFreq(float freq) {
  25. ZEARLY_ASSERT(m_htim->Init.AutoReloadPreload == TIM_AUTORELOAD_PRELOAD_ENABLE)
  26. m_timer_start = true;
  27. uint32_t prescaler = 0;
  28. uint32_t autoreload = 0;
  29. uint32_t timClkFreq = chip_get_timer_clock_sorce_freq(m_htim);
  30. ZEARLY_ASSERT(chip_calculate_prescaler_and_autoreload_by_expect_freq(timClkFreq, freq, &prescaler, &autoreload));
  31. __HAL_TIM_SET_AUTORELOAD(m_htim, autoreload);
  32. __HAL_TIM_SET_PRESCALER(m_htim, prescaler);
  33. HAL_TIM_Base_Start_IT(m_htim);
  34. ZIRQDispatcher::instance().regListener([this](zchip_tim_t *tim) {
  35. if (tim != m_htim) return;
  36. if (m_timer_start && m_simpleTimer_cb) m_simpleTimer_cb();
  37. });
  38. }
  39. void ZTIM::simpleTimer_stop() {
  40. m_timer_start = false;
  41. HAL_TIM_Base_Stop(m_htim);
  42. }
  43. void ZTIM::setPWMFreq(float freq) {
  44. uint32_t prescaler = 0;
  45. uint32_t autoreload = 0;
  46. uint32_t timClkFreq = chip_get_timer_clock_sorce_freq(m_htim);
  47. ZEARLY_ASSERT(chip_calculate_prescaler_and_autoreload_by_expect_freq(timClkFreq, freq, &prescaler, &autoreload));
  48. __HAL_TIM_SET_AUTORELOAD(m_htim, autoreload);
  49. __HAL_TIM_SET_PRESCALER(m_htim, prescaler);
  50. }
  51. void ZTIM::setPWMPolarity(uint32_t Channel, uint32_t Polarity) { m_timpwmPolarity[Channel] = Polarity; }
  52. void ZTIM::startPWM(uint32_t Channel, float duty) {
  53. /**
  54. * @brief
  55. */
  56. TIM_OC_InitTypeDef sConfigOC = {0};
  57. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  58. sConfigOC.Pulse = duty / 100.0 * __HAL_TIM_GET_AUTORELOAD(m_htim);
  59. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  60. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  61. sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  62. sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  63. if (HAL_TIM_PWM_ConfigChannel(m_htim, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) {
  64. Error_Handler();
  65. }
  66. HAL_TIM_PWM_Start(m_htim, Channel);
  67. }
  68. void ZTIM::stopPWM(uint32_t Channel) { startPWM(Channel, 100); }