gpiocmds: Specify soft pwm duration in clock ticks

Improve the precision of soft pwm pulses by allowing the host to
directly specify the clock duration of the pulse.  This improvement in
precision may be noticeable when controller servos (and bltouch).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-05-20 17:41:42 -04:00
parent 8cd8cb492e
commit 8532e2123e
2 changed files with 9 additions and 14 deletions

View File

@ -341,7 +341,7 @@ class MCU_pwm:
or self._shutdown_value not in [0., 1.]): or self._shutdown_value not in [0., 1.]):
raise pins.error( raise pins.error(
"start and shutdown values must be 0.0 or 1.0 on soft pwm") "start and shutdown values must be 0.0 or 1.0 on soft pwm")
self._pwm_max = self._mcu.get_constant_float("SOFT_PWM_MAX") self._pwm_max = float(cycle_ticks)
if self._is_static: if self._is_static:
self._mcu.add_config_cmd("set_digital_out pin=%s value=%d" % ( self._mcu.add_config_cmd("set_digital_out pin=%s value=%d" % (
self._pin, self._start_value >= 0.5)) self._pin, self._start_value >= 0.5))
@ -354,7 +354,8 @@ class MCU_pwm:
self._start_value >= 0.5, self._shutdown_value >= 0.5, self._start_value >= 0.5, self._shutdown_value >= 0.5,
self._mcu.seconds_to_clock(self._max_duration))) self._mcu.seconds_to_clock(self._max_duration)))
self._set_cmd = self._mcu.lookup_command( self._set_cmd = self._mcu.lookup_command(
"schedule_soft_pwm_out oid=%c clock=%u value=%hu", cq=cmd_queue) "schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u",
cq=cmd_queue)
def set_pwm(self, print_time, value): def set_pwm(self, print_time, value):
clock = self._mcu.print_time_to_clock(print_time) clock = self._mcu.print_time_to_clock(print_time)
if self._invert: if self._invert:

View File

@ -106,9 +106,6 @@ DECL_COMMAND(command_set_digital_out, "set_digital_out pin=%u value=%c");
* Soft PWM output pins * Soft PWM output pins
****************************************************************/ ****************************************************************/
#define MAX_SOFT_PWM 256
DECL_CONSTANT("SOFT_PWM_MAX", MAX_SOFT_PWM);
struct soft_pwm_s { struct soft_pwm_s {
struct timer timer; struct timer timer;
uint32_t on_duration, off_duration, end_time; uint32_t on_duration, off_duration, end_time;
@ -192,17 +189,14 @@ void
command_schedule_soft_pwm_out(uint32_t *args) command_schedule_soft_pwm_out(uint32_t *args)
{ {
struct soft_pwm_s *s = oid_lookup(args[0], command_config_soft_pwm_out); struct soft_pwm_s *s = oid_lookup(args[0], command_config_soft_pwm_out);
uint32_t time = args[1]; uint32_t time = args[1], next_on_duration = args[2], next_off_duration;
uint16_t value = args[2];
uint8_t next_flags = SPF_CHECK_END | SPF_HAVE_NEXT; uint8_t next_flags = SPF_CHECK_END | SPF_HAVE_NEXT;
uint32_t next_on_duration, next_off_duration; if (next_on_duration == 0 || next_on_duration >= s->cycle_time) {
if (value == 0 || value >= MAX_SOFT_PWM) { next_flags |= next_on_duration ? SPF_NEXT_ON : 0;
next_on_duration = next_off_duration = 0; if (!!next_on_duration != s->default_value && s->max_duration)
next_flags |= value ? SPF_NEXT_ON : 0;
if (!!value != s->default_value && s->max_duration)
next_flags |= SPF_NEXT_CHECK_END; next_flags |= SPF_NEXT_CHECK_END;
next_on_duration = next_off_duration = 0;
} else { } else {
next_on_duration = (s->cycle_time / MAX_SOFT_PWM) * value;
next_off_duration = s->cycle_time - next_on_duration; next_off_duration = s->cycle_time - next_on_duration;
next_flags |= SPF_NEXT_ON | SPF_NEXT_TOGGLING; next_flags |= SPF_NEXT_ON | SPF_NEXT_TOGGLING;
if (s->max_duration) if (s->max_duration)
@ -227,7 +221,7 @@ command_schedule_soft_pwm_out(uint32_t *args)
irq_enable(); irq_enable();
} }
DECL_COMMAND(command_schedule_soft_pwm_out, DECL_COMMAND(command_schedule_soft_pwm_out,
"schedule_soft_pwm_out oid=%c clock=%u value=%hu"); "schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u");
void void
soft_pwm_shutdown(void) soft_pwm_shutdown(void)