Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions zephyr/modules/owntech_hrtim_driver/zephyr/public_api/hrtim.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ void hrtim_dt_set(hrtim_tu_number_t tu_number,
/**
* @brief Updates the duty cycle of a timing unit
*
* Nonzero compares below the prescaler-dependent hardware minimum produce
* CMP1 = 0. Zero uses the original compare path in both alignments, with normal
* preload/update timing. Near-full duty retains the existing zero-compare plus
* output-swap policy. Zero takes precedence over that policy at small periods.
*
* @param[in] tu_number Timing unit number:
* `MSTR`, `TIMA`, `TIMB`, `TIMC`, `TIMD`, `TIME`, `TIMF`
* @param[in] value The desired duty cycle value
Expand Down
31 changes: 26 additions & 5 deletions zephyr/modules/owntech_hrtim_driver/zephyr/src/hrtim.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,10 +1090,29 @@ void hrtim_dt_set(hrtim_tu_number_t tu_number, uint16_t rise_ns, uint16_t fall_n
rise_dt);
}

inline void hrtim_duty_cycle_set(hrtim_tu_number_t tu_number, uint16_t value)
void hrtim_duty_cycle_set(hrtim_tu_number_t tu_number, uint16_t value)
{
tu_channel[tu_number]->pwm_conf.duty_cycle = value;
HRTIM1->sTimerxRegs[tu_number].CMP1xR = value;
timer_hrtim_t *tu = tu_channel[tu_number];
uint16_t minimum = tu->pwm_conf.duty_min;
if (minimum == 0) {
/* Frequency changes also visit timing units not yet configured. */
minimum = HRTIM_MIN_PER_and_CMP_REG_VALUES[tu->pwm_conf.ckpsc];
}
/* Keep zero as a valid compare in both alignments. Suppress unsupported
* nonzero compares before applying the existing near-full swap policy.
* In particular, zero must stay zero even at the minimum legal period. */
if (value != 0 && value < minimum) {
value = 0;
}
bool full = value != 0 && value >= tu->pwm_conf.period - 3U;
uint16_t applied = full ? 0 : value;

/* Zero and ordinary PWM share the original compare/preload path. */
tu->pwm_conf.duty_cycle = applied;
HRTIM1->sTimerxRegs[tu_number].CMP1xR = applied;
if (tu->pwm_conf.duty_swap != full) {
hrtim_output_hot_swap(tu_number);
}
}


Expand Down Expand Up @@ -1549,17 +1568,19 @@ void hrtim_change_frequency(uint32_t new_frequency)
new_duty = duty_cycle_ratio*new_tu_period;

LL_HRTIM_TIM_SetPeriod(HRTIM1,timing_unit,new_tu_period);
LL_HRTIM_TIM_SetCompare1(HRTIM1,timing_unit,new_duty);

phase_shift_ratio = (float32_t)old_shift/(float32_t)old_period;
new_shift = phase_shift_ratio*new_tu_period;

tu_channel[channel]->pwm_conf.frequency = new_frequency;
hrtim_phase_shift_set(channel, new_shift);

tu_channel[channel]->pwm_conf.duty_cycle = new_duty;
tu_channel[channel]->phase_shift.value = new_shift;
tu_channel[channel]->pwm_conf.period = new_tu_period;
/* Rescaling can move a valid pulse below the hardware limit.
* Keep endpoints and hardware bounds on the common path. */
hrtim_duty_cycle_set(channel,
tu_channel[channel]->pwm_conf.duty_swap ? new_tu_period : new_duty);
}


Expand Down
26 changes: 3 additions & 23 deletions zephyr/modules/owntech_shield_api/zephyr/src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ void PowerAPI::setDutyCycle(leg_t leg, float32_t duty_value)

void PowerAPI::setDutyCycleRaw(leg_t leg, uint16_t duty_value)
{
uint16_t period;
uint8_t swap_state;
hrtim_tu_number_t leg_tu;
uint16_t duty_cycle_max_raw;
uint16_t duty_cycle_min_raw;
Expand Down Expand Up @@ -269,27 +267,9 @@ void PowerAPI::setDutyCycleRaw(leg_t leg, uint16_t duty_value)
duty_value = duty_cycle_min_raw;
}

period = tu_channel[leg_tu]->pwm_conf.period;
swap_state = tu_channel[leg_tu]->pwm_conf.duty_swap;

/* Implements a logic that allows for a duty cycle of 100% */
if (duty_value >= period-3){
duty_value = 0;
hrtim_duty_cycle_set(leg_tu, duty_value);

if(swap_state == false){
hrtim_output_hot_swap(leg_tu);
}
}
else
{
if(swap_state == true) {
hrtim_duty_cycle_set(leg_tu, duty_value);
hrtim_output_hot_swap(leg_tu);
}else{
hrtim_duty_cycle_set(leg_tu, duty_value);
}
}
/* User limits are applied above; silicon limits and endpoints are
* handled once in the shared driver, as in the SPIN API. */
hrtim_duty_cycle_set(leg_tu, duty_value);
}
}

Expand Down
29 changes: 2 additions & 27 deletions zephyr/modules/owntech_spin_api/zephyr/src/PwmHAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,33 +221,8 @@ void PwmHAL::setDutyCycle(hrtim_tu_number_t pwmX, float32_t duty_cycle)

void PwmHAL::setDutyCycleRaw(hrtim_tu_number_t pwmX, uint16_t duty_cycle)
{
timer_hrtim_t *tu = tu_channel[pwmX]; /* Get pointer to timer unit config */
uint16_t previous_duty_cycle = tu->pwm_conf.duty_cycle;
/* Store previously set duty cycle */

if (previous_duty_cycle == duty_cycle) {
return; /* Skip update if duty cycle has not changed */
}

uint16_t period = tu->pwm_conf.period; /* Get PWM period */
bool swap_state = tu->pwm_conf.duty_swap; /* Get output swap state */

/* True if near 100% duty */
bool over_limit = (duty_cycle >= period - 3);

/* Force 0% to avoid glitches near 100% */
duty_cycle = over_limit ? 0 : duty_cycle;

/* Flip outputs only if needed to preserve polarity */
bool force_swap = swap_state ^ over_limit;

/* Write new duty cycle to HRTIM registers */
hrtim_duty_cycle_set(pwmX, duty_cycle);

if (force_swap) {
hrtim_output_hot_swap(pwmX);
/* Flip the outputs (high ↔ low) */
}
/* The shared driver owns hardware limits and endpoint transitions. */
hrtim_duty_cycle_set(pwmX, duty_cycle);
}


Expand Down
Loading