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.

48 lines
1.4 KiB

2 years ago
2 years ago
2 years ago
  1. #include "ztim.hpp"
  2. #include "zirq_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. }
  8. ZTIM::mode_t ZTIM::getMode() { return m_mode; }
  9. /******************************************************
  10. * kTimMode_timer *
  11. ******************************************************/
  12. /**
  13. * @brief startTimer
  14. *
  15. * @param periodus ʱ
  16. * @param onirq صΪnull,ʹϴעĻص
  17. */
  18. void ZTIM::simpleTimer_startByPeriod(uint32_t periodus) { simpleTimer_startByFreq(1000000.0 / periodus); }
  19. void ZTIM::simpleTimer_startByFreq(float freq) {
  20. ZASSERT(m_htim->Init.AutoReloadPreload == TIM_AUTORELOAD_PRELOAD_ENABLE)
  21. m_timer_start = true;
  22. uint32_t prescaler = 0;
  23. uint32_t autoreload = 0;
  24. uint32_t timClkFreq = chip_get_timer_clock_sorce_freq(m_htim);
  25. ZASSERT(chip_calculate_prescaler_and_autoreload_by_expect_freq(timClkFreq, freq, &prescaler, &autoreload));
  26. __HAL_TIM_SET_AUTORELOAD(m_htim, autoreload);
  27. __HAL_TIM_SET_PRESCALER(m_htim, prescaler);
  28. HAL_TIM_Base_Start_IT(m_htim);
  29. ZIRQDispatcher::instance().regTimIrqListener([this](zchip_tim_t *tim) {
  30. if (tim != m_htim) return;
  31. if (m_timer_start && m_simpleTimer_cb) m_simpleTimer_cb();
  32. });
  33. }
  34. void ZTIM::simpleTimer_stop() {
  35. m_timer_start = false;
  36. HAL_TIM_Base_Stop(m_htim);
  37. }