aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2020-11-21 15:04:39 -0500
committerKevin O'Connor <kevin@koconnor.net>2020-12-04 16:10:13 -0500
commit086f0fafde4920694ef1ddb4d2b44d819343e719 (patch)
tree326b9b85aca5803c115ec19436cccfcc277b1593
parent7b28cdbae57bec87a7c399415f36306030637852 (diff)
downloadkutter-086f0fafde4920694ef1ddb4d2b44d819343e719.tar.gz
kutter-086f0fafde4920694ef1ddb4d2b44d819343e719.tar.xz
kutter-086f0fafde4920694ef1ddb4d2b44d819343e719.zip
gpiocmds: Send soft pwm cycle_time separately from schedule_soft_pwm_out
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--docs/MCU_Commands.md11
-rw-r--r--klippy/mcu.py27
-rw-r--r--src/gpiocmds.c33
3 files changed, 44 insertions, 27 deletions
diff --git a/docs/MCU_Commands.md b/docs/MCU_Commands.md
index 79425a2e..133476d8 100644
--- a/docs/MCU_Commands.md
+++ b/docs/MCU_Commands.md
@@ -201,12 +201,11 @@ only of interest to developers looking to gain insight into Klipper.
a hardware PWM output pin. See the 'schedule_digital_out' and
'config_pwm_out' commands for more info.
-* `schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u off_ticks=%u` : Schedules a
- change to a software PWM output pin. The parameters 'on_ticks' and 'off_ticks'
- define for how many ticks the pin will be on and off, respectively.
- 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 'schedule_digital_out' and
+* `schedule_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 'schedule_digital_out' and
'config_soft_pwm_out' commands for more info.
* `query_analog_in oid=%c clock=%u sample_ticks=%u sample_count=%c
diff --git a/klippy/mcu.py b/klippy/mcu.py
index 1d661050..d6d754c2 100644
--- a/klippy/mcu.py
+++ b/klippy/mcu.py
@@ -175,9 +175,9 @@ class MCU_pwm:
self._invert = pin_params['invert']
self._start_value = self._shutdown_value = float(self._invert)
self._is_static = False
- self._last_clock = 0
+ self._last_clock = self._last_cycle_ticks = 0
self._pwm_max = 0.
- self._set_cmd = None
+ self._set_cmd = self._set_cycle_ticks = None
def get_mcu(self):
return self._mcu
def setup_max_duration(self, max_duration):
@@ -226,7 +226,6 @@ class MCU_pwm:
# Software PWM
if self._shutdown_value not in [0., 1.]:
raise pins.error("shutdown value must be 0.0 or 1.0 on soft pwm")
- self._pwm_max = float(cycle_ticks)
if self._is_static:
self._mcu.add_config_cmd("set_digital_out pin=%s value=%d"
% (self._pin, self._start_value >= 0.5))
@@ -238,14 +237,18 @@ class MCU_pwm:
% (self._oid, self._pin, self._start_value >= 1.0,
self._shutdown_value >= 0.5,
self._mcu.seconds_to_clock(self._max_duration)))
- svalue = int(self._start_value * self._pwm_max + 0.5)
self._mcu.add_config_cmd(
- "schedule_soft_pwm_out oid=%d clock=%d on_ticks=%d off_ticks=%d"
- % (self._oid, self._last_clock, svalue, cycle_ticks - svalue),
- is_init=True)
+ "set_soft_pwm_cycle_ticks 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(
+ "schedule_soft_pwm_out oid=%d clock=%d on_ticks=%d"
+ % (self._oid, self._last_clock, svalue), is_init=True)
self._set_cmd = self._mcu.lookup_command(
- "schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u off_ticks=%u",
- cq=cmd_queue)
+ "schedule_soft_pwm_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)
def set_pwm(self, print_time, value, cycle_time=None):
clock = self._mcu.print_time_to_clock(print_time)
minclock = self._last_clock
@@ -261,8 +264,12 @@ class MCU_pwm:
if cycle_time is None:
cycle_time = self._cycle_time
cycle_ticks = self._mcu.seconds_to_clock(cycle_time)
+ if cycle_ticks != self._last_cycle_ticks:
+ self._set_cycle_ticks.send([self._oid, cycle_ticks],
+ minclock=minclock, reqclock=clock)
+ self._last_cycle_ticks = cycle_ticks
on_ticks = int(max(0., min(1., value)) * float(cycle_ticks) + 0.5)
- self._set_cmd.send([self._oid, clock, on_ticks, cycle_ticks - on_ticks],
+ self._set_cmd.send([self._oid, clock, on_ticks],
minclock=minclock, reqclock=clock)
class MCU_adc:
diff --git a/src/gpiocmds.c b/src/gpiocmds.c
index e6bf4671..a9eabdac 100644
--- a/src/gpiocmds.c
+++ b/src/gpiocmds.c
@@ -1,6 +1,6 @@
// Commands for controlling GPIO pins
//
-// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@@ -109,8 +109,8 @@ DECL_COMMAND(command_set_digital_out, "set_digital_out pin=%u value=%c");
struct soft_pwm_s {
struct timer timer;
uint32_t on_duration, off_duration, end_time;
- uint32_t next_on_duration, next_off_duration;
- uint32_t max_duration;
+ uint32_t next_on_duration;
+ uint32_t max_duration, cycle_time;
struct gpio_out pin;
uint8_t default_value, flags;
};
@@ -154,7 +154,7 @@ soft_pwm_load_event(struct timer *timer)
s->flags = flags;
gpio_out_write(s->pin, flags & SPF_ON);
if (!(flags & SPF_TOGGLING)) {
- // Pin is in an always on (value=256) or always off (value=0) state
+ // Pin is in an always on or always off state
if (!(flags & SPF_CHECK_END))
return SF_DONE;
s->timer.waketime = s->end_time = s->end_time + s->max_duration;
@@ -162,8 +162,8 @@ soft_pwm_load_event(struct timer *timer)
}
// Schedule normal pin toggle timer events
s->timer.func = soft_pwm_toggle_event;
- s->off_duration = s->next_off_duration;
s->on_duration = s->next_on_duration;
+ s->off_duration = s->cycle_time - s->on_duration;
s->timer.waketime = s->end_time + s->on_duration;
s->end_time += s->max_duration;
return SF_RESCHEDULE;
@@ -185,17 +185,29 @@ DECL_COMMAND(command_config_soft_pwm_out,
" 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 (s->flags & SPF_HAVE_NEXT)
+ 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_schedule_soft_pwm_out(uint32_t *args)
{
struct soft_pwm_s *s = oid_lookup(args[0], command_config_soft_pwm_out);
- uint32_t time = args[1], next_on_duration = args[2],
- next_off_duration = args[3];
+ uint32_t time = args[1], next_on_duration = args[2];
uint8_t next_flags = SPF_CHECK_END | SPF_HAVE_NEXT;
- if (next_on_duration == 0 || next_off_duration == 0) {
+ if (next_on_duration == 0 || next_on_duration >= s->cycle_time) {
next_flags |= next_on_duration ? SPF_NEXT_ON : 0;
if (!!next_on_duration != s->default_value && s->max_duration)
next_flags |= SPF_NEXT_CHECK_END;
- next_on_duration = next_off_duration = 0;
+ next_on_duration = 0;
} else {
next_flags |= SPF_NEXT_ON | SPF_NEXT_TOGGLING;
if (s->max_duration)
@@ -206,7 +218,6 @@ command_schedule_soft_pwm_out(uint32_t *args)
shutdown("next soft pwm extends existing pwm");
s->end_time = time;
s->next_on_duration = next_on_duration;
- s->next_off_duration = next_off_duration;
s->flags = (s->flags & 0xf) | next_flags;
if (s->flags & SPF_TOGGLING && timer_is_before(s->timer.waketime, time)) {
// soft_pwm_toggle_event() will schedule a load event when ready
@@ -220,7 +231,7 @@ command_schedule_soft_pwm_out(uint32_t *args)
irq_enable();
}
DECL_COMMAND(command_schedule_soft_pwm_out,
- "schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u off_ticks=%u");
+ "schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u");
void
soft_pwm_shutdown(void)