gpiocmds: Merge digital output and soft pwm code

The digital output commands implement a subset of the software pwm
commands.  Change the host code to just use the software pwm commands
and simplify the micro-controller code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-12-10 11:27:37 -05:00
parent 41a41881f7
commit f3c5be0f00
3 changed files with 141 additions and 261 deletions

View File

@ -133,15 +133,6 @@ This section lists some commonly used config commands.
see the description of the 'set_pwm_out' and 'config_digital_out'
commands for parameter description.
* `config_soft_pwm_out oid=%c pin=%u value=%c
default_value=%c max_duration=%u` : This command creates an internal
micro-controller object for software implemented PWM. Unlike
hardware pwm pins, a software pwm object does not require any
special hardware support (other than the ability to configure the
pin as a digital output GPIO). See the description of
the 'set_pwm_out' and 'config_digital_out'
commands for parameter description.
* `config_analog_in oid=%c pin=%u` : This command is used to configure
a pin in analog input sampling mode. Once configured, the pin can be
sampled at regular interval using the query_analog_in command (see
@ -191,23 +182,26 @@ Common commands
This section lists some commonly used run-time commands. It is likely
only of interest to developers looking to gain insight into Klipper.
* `queue_digital_out oid=%c clock=%u value=%c` : This command will
* `set_digital_out_pwm_cycle oid=%c cycle_ticks=%u` : This command
configures a digital output pin (as created by config_digital_out)
to use "software PWM". The 'cycle_ticks' is the number of clock
ticks for the PWM cycle. Because the output switching is implemented
in the micro-controller software, it is recommended that
'cycle_ticks' correspond to a time of 10ms or greater.
* `queue_digital_out oid=%c clock=%u on_ticks=%u` : This command will
schedule a change to a digital output GPIO pin at the given clock
time. To use this command a 'config_digital_out' command with the
same 'oid' parameter must have been issued during micro-controller
configuration.
configuration. If 'set_digital_out_pwm_cycle' has been called then
'on_ticks' is the on duration (in clock ticks) for the pwm cycle.
Otherwise, 'on_ticks' should be either 0 (for low voltage) or 1 (for
high voltage).
* `queue_pwm_out oid=%c clock=%u value=%hu` : Schedules a change to a
hardware PWM output pin. See the 'queue_digital_out' and
'config_pwm_out' commands for more info.
* `queue_soft_pwm_out oid=%c clock=%u on_ticks=%u` : Schedules a
change to a software PWM output pin. Because the output switching is
implemented in the micro-controller software, it is recommended that
the sum of on_ticks and off_ticks parameters corresponds to a time
of 10ms or greater. See the 'queue_digital_out' and
'config_soft_pwm_out' commands for more info.
* `query_analog_in oid=%c clock=%u sample_ticks=%u sample_count=%c
rest_ticks=%u min_value=%hu max_value=%hu` : This command sets up a
recurring schedule of analog input samples. To use this command a

View File

@ -155,7 +155,7 @@ class MCU_digital_out:
on_restart=True)
cmd_queue = self._mcu.alloc_command_queue()
self._set_cmd = self._mcu.lookup_command(
"queue_digital_out oid=%c clock=%u value=%c", cq=cmd_queue)
"queue_digital_out oid=%c clock=%u on_ticks=%u", cq=cmd_queue)
def set_digital(self, print_time, value):
clock = self._mcu.print_time_to_clock(print_time)
self._set_cmd.send([self._oid, clock, (not not value) ^ self._invert],
@ -235,23 +235,23 @@ class MCU_pwm:
self._mcu.request_move_queue_slot()
self._oid = self._mcu.create_oid()
self._mcu.add_config_cmd(
"config_soft_pwm_out oid=%d pin=%s value=%d"
"config_digital_out oid=%d pin=%s value=%d"
" default_value=%d max_duration=%d"
% (self._oid, self._pin, self._start_value >= 1.0,
self._shutdown_value >= 0.5,
self._mcu.seconds_to_clock(self._max_duration)))
self._mcu.add_config_cmd(
"set_soft_pwm_cycle_ticks oid=%d cycle_ticks=%d"
"set_digital_out_pwm_cycle oid=%d cycle_ticks=%d"
% (self._oid, cycle_ticks))
self._last_cycle_ticks = cycle_ticks
svalue = int(self._start_value * cycle_ticks + 0.5)
self._mcu.add_config_cmd(
"queue_soft_pwm_out oid=%d clock=%d on_ticks=%d"
"queue_digital_out oid=%d clock=%d on_ticks=%d"
% (self._oid, self._last_clock, svalue), is_init=True)
self._set_cmd = self._mcu.lookup_command(
"queue_soft_pwm_out oid=%c clock=%u on_ticks=%u", cq=cmd_queue)
"queue_digital_out oid=%c clock=%u on_ticks=%u", cq=cmd_queue)
self._set_cycle_ticks = self._mcu.lookup_command(
"set_soft_pwm_cycle_ticks oid=%c cycle_ticks=%u", cq=cmd_queue)
"set_digital_out_pwm_cycle oid=%c cycle_ticks=%u", cq=cmd_queue)
def set_pwm(self, print_time, value, cycle_time=None):
clock = self._mcu.print_time_to_clock(print_time)
minclock = self._last_clock

View File

@ -1,4 +1,4 @@
// Commands for controlling GPIO pins
// Commands for controlling GPIO output pins
//
// Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
//
@ -11,104 +11,168 @@
#include "command.h" // DECL_COMMAND
#include "sched.h" // sched_add_timer
/****************************************************************
* Digital out pins
****************************************************************/
struct digital_out_s {
struct timer timer;
uint32_t on_duration, off_duration, end_time;
struct gpio_out pin;
uint32_t max_duration;
uint8_t default_value;
uint32_t max_duration, cycle_time;
struct move_queue_head mq;
uint8_t flags;
};
struct digital_move {
struct move_node node;
uint32_t waketime;
uint8_t value;
uint32_t waketime, on_duration;
};
enum {
DF_ON=1<<0, DF_TOGGLING=1<<1, DF_CHECK_END=1<<2, DF_DEFAULT_ON=1<<4
};
static uint_fast8_t digital_load_event(struct timer *timer);
// Software PWM toggle event
static uint_fast8_t
digital_end_event(struct timer *timer)
digital_toggle_event(struct timer *timer)
{
shutdown("Missed scheduling of next pin event");
struct digital_out_s *d = container_of(timer, struct digital_out_s, timer);
gpio_out_toggle_noirq(d->pin);
d->flags ^= DF_ON;
uint32_t waketime = d->timer.waketime;
if (d->flags & DF_ON)
waketime += d->on_duration;
else
waketime += d->off_duration;
if (d->flags & DF_CHECK_END && !timer_is_before(waketime, d->end_time)) {
// End of normal pulsing - next event loads new pwm settings
d->timer.func = digital_load_event;
waketime = d->end_time;
}
d->timer.waketime = waketime;
return SF_RESCHEDULE;
}
// Load next pin output setting
static uint_fast8_t
digital_out_event(struct timer *timer)
digital_load_event(struct timer *timer)
{
// Apply next update and remove it from queue
struct digital_out_s *d = container_of(timer, struct digital_out_s, timer);
if (move_queue_empty(&d->mq))
shutdown("Missed scheduling of next digital out event");
struct move_node *mn = move_queue_pop(&d->mq);
struct digital_move *m = container_of(mn, struct digital_move, node);
uint8_t value = m->value;
gpio_out_write(d->pin, value);
uint32_t on_duration = m->on_duration;
uint8_t flags = on_duration ? DF_ON : 0;
gpio_out_write(d->pin, flags);
move_free(m);
// Check if more updates queued
if (move_queue_empty(&d->mq)) {
if (value == d->default_value || !d->max_duration)
return SF_DONE;
// Calculate next end_time and flags
uint32_t end_time = 0;
if (!flags || on_duration >= d->cycle_time) {
// Pin is in an always on or always off state
if (!flags != !(d->flags & DF_DEFAULT_ON) && d->max_duration) {
end_time = d->timer.waketime + d->max_duration;
flags |= DF_CHECK_END;
}
} else {
flags |= DF_TOGGLING;
if (d->max_duration) {
end_time = d->timer.waketime + d->max_duration;
flags |= DF_CHECK_END;
}
}
if (!move_queue_empty(&d->mq)) {
struct move_node *nn = move_queue_first(&d->mq);
uint32_t wake = container_of(nn, struct digital_move, node)->waketime;
if (flags & DF_CHECK_END && timer_is_before(end_time, wake))
shutdown("Scheduled digital out event will exceed max_duration");
end_time = wake;
flags |= DF_CHECK_END;
}
d->end_time = end_time;
d->flags = flags | (d->flags & DF_DEFAULT_ON);
// Start the safety timeout
d->timer.waketime += d->max_duration;
d->timer.func = digital_end_event;
// Schedule next event
if (!(flags & DF_TOGGLING)) {
if (!(flags & DF_CHECK_END))
// Pin not toggling and nothing scheduled
return SF_DONE;
d->timer.waketime = end_time;
return SF_RESCHEDULE;
}
// Schedule next update
struct move_node *nn = move_queue_first(&d->mq);
uint32_t wake = container_of(nn, struct digital_move, node)->waketime;
if (value != d->default_value && d->max_duration
&& timer_is_before(d->timer.waketime + d->max_duration, wake))
shutdown("Scheduled digital out event will exceed max_duration");
d->timer.waketime = wake;
uint32_t waketime = d->timer.waketime + on_duration;
if (flags & DF_CHECK_END && !timer_is_before(waketime, end_time)) {
d->timer.waketime = end_time;
return SF_RESCHEDULE;
}
d->timer.func = digital_toggle_event;
d->timer.waketime = waketime;
d->on_duration = on_duration;
d->off_duration = d->cycle_time - on_duration;
return SF_RESCHEDULE;
}
void
command_config_digital_out(uint32_t *args)
{
struct gpio_out pin = gpio_out_setup(args[1], args[2]);
struct gpio_out pin = gpio_out_setup(args[1], !!args[2]);
struct digital_out_s *d = oid_alloc(args[0], command_config_digital_out
, sizeof(*d));
d->pin = pin;
d->default_value = args[3];
d->flags = (args[2] ? DF_ON : 0) | (args[3] ? DF_DEFAULT_ON : 0);
d->max_duration = args[4];
d->timer.func = digital_out_event;
move_queue_setup(&d->mq, sizeof(struct digital_move));
}
DECL_COMMAND(command_config_digital_out,
"config_digital_out oid=%c pin=%u value=%c default_value=%c"
" max_duration=%u");
"config_digital_out oid=%c pin=%u value=%c"
" default_value=%c max_duration=%u");
void
command_set_digital_out_pwm_cycle(uint32_t *args)
{
struct digital_out_s *d = oid_lookup(args[0], command_config_digital_out);
irq_disable();
if (!move_queue_empty(&d->mq))
shutdown("Can not set soft pwm cycle ticks while updates pending");
d->cycle_time = args[1];
irq_enable();
}
DECL_COMMAND(command_set_digital_out_pwm_cycle,
"set_digital_out_pwm_cycle oid=%c cycle_ticks=%u");
void
command_queue_digital_out(uint32_t *args)
{
struct digital_out_s *d = oid_lookup(args[0], command_config_digital_out);
struct digital_move *m = move_alloc();
m->waketime = args[1];
m->value = args[2];
uint32_t time = m->waketime = args[1];
m->on_duration = args[2];
irq_disable();
int need_add_timer = move_queue_push(&m->node, &d->mq);
irq_enable();
if (!need_add_timer)
int first_on_queue = move_queue_push(&m->node, &d->mq);
if (!first_on_queue) {
irq_enable();
return;
// queue was empty and a timer needs to be added
sched_del_timer(&d->timer);
if (d->timer.func == digital_end_event
&& timer_is_before(d->timer.waketime, m->waketime))
}
uint8_t flags = d->flags;
if (flags & DF_CHECK_END && timer_is_before(d->end_time, time))
shutdown("Scheduled digital out event will exceed max_duration");
d->timer.func = digital_out_event;
d->timer.waketime = m->waketime;
sched_add_timer(&d->timer);
d->end_time = time;
d->flags = flags | DF_CHECK_END;
if (flags & DF_TOGGLING && timer_is_before(d->timer.waketime, time)) {
// digital_toggle_event() will schedule a load event when ready
} else {
// Schedule the loading of the parameters at the requested time
sched_del_timer(&d->timer);
d->timer.waketime = time;
d->timer.func = digital_load_event;
sched_add_timer(&d->timer);
}
irq_enable();
}
DECL_COMMAND(command_queue_digital_out,
"queue_digital_out oid=%c clock=%u value=%c");
"queue_digital_out oid=%c clock=%u on_ticks=%u");
void
command_update_digital_out(uint32_t *args)
@ -117,12 +181,15 @@ command_update_digital_out(uint32_t *args)
sched_del_timer(&d->timer);
if (!move_queue_empty(&d->mq))
shutdown("update_digital_out not valid with active queue");
uint8_t value = args[1];
gpio_out_write(d->pin, value);
if (value != d->default_value && d->max_duration) {
d->timer.waketime = timer_read_time() + d->max_duration;
d->timer.func = digital_end_event;
uint8_t value = args[1], flags = d->flags, on_flag = value ? DF_ON : 0;
gpio_out_write(d->pin, on_flag);
if (!on_flag != !(flags & DF_DEFAULT_ON) && d->max_duration) {
d->timer.waketime = d->end_time = timer_read_time() + d->max_duration;
d->timer.func = digital_load_event;
d->flags = (flags & DF_DEFAULT_ON) | on_flag | DF_CHECK_END;
sched_add_timer(&d->timer);
} else {
d->flags = (flags & DF_DEFAULT_ON) | on_flag;
}
}
DECL_COMMAND(command_update_digital_out, "update_digital_out oid=%c value=%c");
@ -133,8 +200,8 @@ digital_out_shutdown(void)
uint8_t i;
struct digital_out_s *d;
foreach_oid(i, d, command_config_digital_out) {
gpio_out_write(d->pin, d->default_value);
d->timer.func = digital_out_event;
gpio_out_write(d->pin, d->flags & DF_DEFAULT_ON);
d->flags = d->flags & DF_DEFAULT_ON ? DF_ON | DF_DEFAULT_ON : 0;
move_queue_clear(&d->mq);
}
}
@ -146,184 +213,3 @@ command_set_digital_out(uint32_t *args)
gpio_out_setup(args[0], args[1]);
}
DECL_COMMAND(command_set_digital_out, "set_digital_out pin=%u value=%c");
/****************************************************************
* Soft PWM output pins
****************************************************************/
struct soft_pwm_s {
struct timer timer;
uint32_t on_duration, off_duration, end_time;
struct gpio_out pin;
uint32_t max_duration, cycle_time;
struct move_queue_head mq;
uint8_t flags;
};
struct soft_pwm_move {
struct move_node node;
uint32_t waketime, on_duration;
};
enum {
SPF_ON=1<<0, SPF_TOGGLING=1<<1, SPF_CHECK_END=1<<2, SPF_DEFAULT_ON=1<<4
};
static uint_fast8_t soft_pwm_load_event(struct timer *timer);
// Normal pulse change event
static uint_fast8_t
soft_pwm_toggle_event(struct timer *timer)
{
struct soft_pwm_s *s = container_of(timer, struct soft_pwm_s, timer);
gpio_out_toggle_noirq(s->pin);
s->flags ^= SPF_ON;
uint32_t waketime = s->timer.waketime;
if (s->flags & SPF_ON)
waketime += s->on_duration;
else
waketime += s->off_duration;
if (s->flags & SPF_CHECK_END && !timer_is_before(waketime, s->end_time)) {
// End of normal pulsing - next event loads new pwm settings
s->timer.func = soft_pwm_load_event;
waketime = s->end_time;
}
s->timer.waketime = waketime;
return SF_RESCHEDULE;
}
// Load next pwm settings
static uint_fast8_t
soft_pwm_load_event(struct timer *timer)
{
// Apply next update and remove it from queue
struct soft_pwm_s *s = container_of(timer, struct soft_pwm_s, timer);
if (move_queue_empty(&s->mq))
shutdown("Missed scheduling of next pwm event");
struct move_node *mn = move_queue_pop(&s->mq);
struct soft_pwm_move *m = container_of(mn, struct soft_pwm_move, node);
uint32_t on_duration = m->on_duration;
uint8_t flags = on_duration ? SPF_ON : 0;
gpio_out_write(s->pin, flags);
move_free(m);
// Calculate next end_time and flags
uint32_t end_time = 0;
if (!flags || on_duration >= s->cycle_time) {
// Pin is in an always on or always off state
if (!flags != !(s->flags & SPF_DEFAULT_ON) && s->max_duration) {
end_time = s->timer.waketime + s->max_duration;
flags |= SPF_CHECK_END;
}
} else {
flags |= SPF_TOGGLING;
if (s->max_duration) {
end_time = s->timer.waketime + s->max_duration;
flags |= SPF_CHECK_END;
}
}
if (!move_queue_empty(&s->mq)) {
struct move_node *nn = move_queue_first(&s->mq);
uint32_t wake = container_of(nn, struct soft_pwm_move, node)->waketime;
if (flags & SPF_CHECK_END && timer_is_before(end_time, wake))
shutdown("Scheduled soft pwm event will exceed max_duration");
end_time = wake;
flags |= SPF_CHECK_END;
}
s->end_time = end_time;
s->flags = flags | (s->flags & SPF_DEFAULT_ON);
// Schedule next event
if (!(flags & SPF_TOGGLING)) {
if (!(flags & SPF_CHECK_END))
// Pin not toggling and nothing scheduled
return SF_DONE;
s->timer.waketime = end_time;
return SF_RESCHEDULE;
}
uint32_t waketime = s->timer.waketime + on_duration;
if (flags & SPF_CHECK_END && !timer_is_before(waketime, end_time)) {
s->timer.waketime = end_time;
return SF_RESCHEDULE;
}
s->timer.func = soft_pwm_toggle_event;
s->timer.waketime = waketime;
s->on_duration = on_duration;
s->off_duration = s->cycle_time - on_duration;
return SF_RESCHEDULE;
}
void
command_config_soft_pwm_out(uint32_t *args)
{
struct gpio_out pin = gpio_out_setup(args[1], !!args[2]);
struct soft_pwm_s *s = oid_alloc(args[0], command_config_soft_pwm_out
, sizeof(*s));
s->pin = pin;
s->flags = (args[2] ? SPF_ON : 0) | (args[3] ? SPF_DEFAULT_ON : 0);
s->max_duration = args[4];
move_queue_setup(&s->mq, sizeof(struct soft_pwm_move));
}
DECL_COMMAND(command_config_soft_pwm_out,
"config_soft_pwm_out oid=%c pin=%u value=%c"
" default_value=%c max_duration=%u");
void
command_set_soft_pwm_cycle_ticks(uint32_t *args)
{
struct soft_pwm_s *s = oid_lookup(args[0], command_config_soft_pwm_out);
irq_disable();
if (!move_queue_empty(&s->mq))
shutdown("Can not set soft pwm cycle ticks while updates pending");
s->cycle_time = args[1];
irq_enable();
}
DECL_COMMAND(command_set_soft_pwm_cycle_ticks,
"set_soft_pwm_cycle_ticks oid=%c cycle_ticks=%u");
void
command_queue_soft_pwm_out(uint32_t *args)
{
struct soft_pwm_s *s = oid_lookup(args[0], command_config_soft_pwm_out);
struct soft_pwm_move *m = move_alloc();
uint32_t time = m->waketime = args[1];
m->on_duration = args[2];
irq_disable();
int first_on_queue = move_queue_push(&m->node, &s->mq);
if (!first_on_queue) {
irq_enable();
return;
}
uint8_t flags = s->flags;
if (flags & SPF_CHECK_END && timer_is_before(s->end_time, time))
shutdown("Scheduled soft pwm event will exceed max_duration");
s->end_time = time;
s->flags = flags | SPF_CHECK_END;
if (flags & SPF_TOGGLING && timer_is_before(s->timer.waketime, time)) {
// soft_pwm_toggle_event() will schedule a load event when ready
} else {
// Schedule the loading of the pwm parameters at the requested time
sched_del_timer(&s->timer);
s->timer.waketime = time;
s->timer.func = soft_pwm_load_event;
sched_add_timer(&s->timer);
}
irq_enable();
}
DECL_COMMAND(command_queue_soft_pwm_out,
"queue_soft_pwm_out oid=%c clock=%u on_ticks=%u");
void
soft_pwm_shutdown(void)
{
uint8_t i;
struct soft_pwm_s *s;
foreach_oid(i, s, command_config_soft_pwm_out) {
gpio_out_write(s->pin, s->flags & SPF_DEFAULT_ON);
s->flags = s->flags & SPF_DEFAULT_ON ? SPF_ON | SPF_DEFAULT_ON : 0;
move_queue_clear(&s->mq);
}
}
DECL_SHUTDOWN(soft_pwm_shutdown);