From 118fd21cb815d1c27e7e5bd6b394369bdf095919 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 12 Jul 2017 22:16:16 -0400 Subject: [PATCH] irq: Support sleeping when mcu is idle Signed-off-by: Kevin O'Connor --- klippy/mcu.py | 6 ++++-- src/avr/irq.h | 4 ++++ src/avr/timer.c | 26 +++++++++++++++++++++++++- src/basecmd.c | 18 ++++++++++++++---- src/basecmd.h | 1 + src/generic/armcm_irq.c | 6 ++++++ src/generic/irq.h | 1 + src/generic/timer_irq.c | 17 ++++++++++++++++- src/generic/timer_irq.h | 1 + src/pru/main.c | 12 ++++++++++++ src/pru/pru0.c | 7 +++++-- src/sam3x8e/timer.c | 7 +++++++ src/simulator/main.c | 5 +++++ 13 files changed, 101 insertions(+), 10 deletions(-) diff --git a/klippy/mcu.py b/klippy/mcu.py index cf85b740..f4e3e861 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -420,6 +420,7 @@ class MCU: self._stats_sumsq_base = 0. self._mcu_tick_avg = 0. self._mcu_tick_stddev = 0. + self._mcu_tick_awake = 0. def handle_mcu_stats(self, params): count = params['count'] tick_sum = params['sum'] @@ -427,6 +428,7 @@ class MCU: self._mcu_tick_avg = tick_sum * c tick_sumsq = params['sumsq'] * self._stats_sumsq_base self._mcu_tick_stddev = c * math.sqrt(count*tick_sumsq - tick_sum**2) + self._mcu_tick_awake = tick_sum / self._mcu_freq def handle_shutdown(self, params): if self.is_shutdown: return @@ -493,9 +495,9 @@ class MCU: self._ffi_lib.steppersync_free(self._steppersync) self._steppersync = None def stats(self, eventtime): - return "%s mcu_task_avg=%.06f mcu_task_stddev=%.06f" % ( + return "%s mcu_awake=%.03f mcu_task_avg=%.06f mcu_task_stddev=%.06f" % ( self.serial.stats(eventtime), - self._mcu_tick_avg, self._mcu_tick_stddev) + self._mcu_tick_awake, self._mcu_tick_avg, self._mcu_tick_stddev) def force_shutdown(self): self.send(self._emergency_stop_cmd.encode()) def microcontroller_restart(self): diff --git a/src/avr/irq.h b/src/avr/irq.h index 33172c0f..349c4c43 100644 --- a/src/avr/irq.h +++ b/src/avr/irq.h @@ -28,6 +28,10 @@ static inline void irq_restore(irqstatus_t flag) { SREG = flag; } +static inline void irq_wait(void) { + asm("sei\n sleep\n cli" : : : "memory"); +} + static inline void irq_poll(void) { } diff --git a/src/avr/timer.c b/src/avr/timer.c index 92459af3..d9397187 100644 --- a/src/avr/timer.c +++ b/src/avr/timer.c @@ -6,6 +6,7 @@ #include // TCNT1 #include "autoconf.h" // CONFIG_AVR_CLKPR +#include "basecmd.h" // stats_note_sleep #include "board/misc.h" // timer_from_us #include "command.h" // shutdown #include "irq.h" // irq_save @@ -62,6 +63,12 @@ timer_set(uint16_t next) OCR1A = next; } +static inline uint16_t +timer_get_next(void) +{ + return OCR1A; +} + static inline void timer_repeat_set(uint16_t next) { @@ -103,6 +110,9 @@ timer_init(void) // enable interrupt TIMSK1 = 1<signal, SIGNAL_PRU0_WAITING); diff --git a/src/sam3x8e/timer.c b/src/sam3x8e/timer.c index 0bc21dae..ba1124cb 100644 --- a/src/sam3x8e/timer.c +++ b/src/sam3x8e/timer.c @@ -18,6 +18,13 @@ timer_set(uint32_t value) TC0->TC_CHANNEL[0].TC_RA = value; } +// Return the next scheduled wake up time +uint32_t +timer_get_next(void) +{ + return TC0->TC_CHANNEL[0].TC_RA; +} + // Return the current time (in absolute clock ticks). uint32_t timer_read_time(void) diff --git a/src/simulator/main.c b/src/simulator/main.c index f0e18737..ccd6dd93 100644 --- a/src/simulator/main.c +++ b/src/simulator/main.c @@ -47,6 +47,11 @@ irq_restore(irqstatus_t flag) Interrupt_off = flag; } +void +irq_wait(void) +{ +} + void irq_poll(void) {