27 uint32_t period = DL_Timer_getLoadValue(timer_);
28 uint32_t compare_value = 0;
29 DL_TIMER_COUNT_MODE count_mode = DL_Timer_getCounterMode(timer_);
33 case DL_TIMER_COUNT_MODE_DOWN:
34 compare_value =
static_cast<uint32_t
>(
static_cast<float>(period) * (1.0f - value));
37 case DL_TIMER_COUNT_MODE_UP:
38 case DL_TIMER_COUNT_MODE_UP_DOWN:
39 compare_value =
static_cast<uint32_t
>(
static_cast<float>(period) * value);
43 return ErrorCode::NOT_SUPPORT;
46 if (compare_value > period)
48 compare_value = period;
51 DL_Timer_setCaptureCompareValue(timer_, compare_value, channel_);
58 const uint32_t SOURCE_CLOCK = clock_freq_;
61 return ErrorCode::ARG_ERR;
64 const uint32_t TOTAL_CYCLES_NEEDED = SOURCE_CLOCK / config.
frequency;
65 uint32_t min_total_prescale = (TOTAL_CYCLES_NEEDED + MAX_TIMER_LOAD) >> 16;
66 if (min_total_prescale == 0)
68 min_total_prescale = 1;
71 uint32_t best_div = (min_total_prescale + MAX_PRESCALER - 1) >> 8;
72 if (best_div > MAX_DIVIDE_RATIO)
74 return ErrorCode::NOT_SUPPORT;
81 uint32_t best_prescaler = (min_total_prescale + best_div - 1) / best_div;
82 if (best_prescaler == 0)
87 uint32_t best_load = (SOURCE_CLOCK / (best_div * best_prescaler * config.
frequency));
93 DL_Timer_ClockConfig clock_config;
94 DL_Timer_getClockConfig(timer_, &clock_config);
96 clock_config.divideRatio =
static_cast<DL_TIMER_CLOCK_DIVIDE
>(best_div - 1);
97 clock_config.prescale =
static_cast<uint8_t
>(best_prescaler - 1);
99 DL_Timer_setClockConfig(timer_, &clock_config);
100 DL_Timer_setLoadValue(timer_, best_load);
102 return ErrorCode::OK;