Skip to content

Commit eb00f4b

Browse files
author
henrykotze
committed
pwm_out outputs expected waveforms
- however currently if the frequency is higher than what the pwm_out driver runs, there will be aliasing, based on how the registers gets resets
1 parent 54e4cb6 commit eb00f4b

File tree

1 file changed

+6
-14
lines changed
  • platforms/nuttx/src/px4/espressif/esp32/io_pins

1 file changed

+6
-14
lines changed

platforms/nuttx/src/px4/espressif/esp32/io_pins/pwm_servo.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@
122122
uint32_t reload = 0;
123123
uint32_t prescaler = 0;
124124
uint32_t shift = 0;
125-
double cnt_dt = 0;
126-
uint32_t timer_period = 0;
125+
uint32_t timer_rate = 0;
127126

128127
void get_optimal_timer_setup(uint32_t desired_freq){
129128

130129
uint32_t shifted = 1;
130+
timer_rate = desired_freq;
131131
reload = (20480000000 / desired_freq + LEDC_CLKDIV_MAX) / LEDC_CLKDIV_MAX;
132132

133133
if (reload == 0)
@@ -151,26 +151,18 @@ void get_optimal_timer_setup(uint32_t desired_freq){
151151
}
152152
shift = shifted;
153153
prescaler = (20480000000 / reload) / desired_freq;
154-
cnt_dt = ((prescaler >> 8) + ((prescaler & 0xff)/256))/80000000.0;
155-
timer_period = cnt_dt * reload * 1000000;
156-
157154
}
158155

159156
int up_pwm_servo_set(unsigned channel, uint16_t value)
160157
{
158+
uint32_t duty = (value*timer_rate)*0.065535;
159+
uint32_t regval = b16toi(duty * reload + b16HALF);
161160

162-
uint32_t duty = (value*reload)/timer_period;
163-
164-
// uint32_t regval = b16toi(duty + b16HALF);
165161
irqstate_t flags;
166-
flags = px4_enter_critical_section();
167162

168-
SET_CHAN_REG(channel, LEDC_LSCH0_DUTY_REG, duty << 4);
169-
/* Set pulse phase 0 */
170-
// SET_CHAN_REG(channel, LEDC_LSCH0_HPOINT_REG, 0);
171-
/* Update clock divide and reload to hardware */
163+
flags = px4_enter_critical_section();
164+
SET_CHAN_REG(channel, LEDC_LSCH0_DUTY_REG, regval << 4);
172165
SET_CHAN_BITS(channel, LEDC_LSCH0_CONF0_REG, LEDC_PARA_UP_LSCH0);
173-
// might be needed to update the LEDC_TIMERx_PARA_UP register
174166
px4_leave_critical_section(flags);
175167

176168
return OK;

0 commit comments

Comments
 (0)