Skip to content

Commit 1fdf0eb

Browse files
nefelim4agKevinOConnor
authored andcommitted
static_pwm_clock: define module for stm32
Signed-off-by: Timofey Titovets <[email protected]>
1 parent ec0eb4a commit 1fdf0eb

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

klippy/extras/static_pwm_clock.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Define GPIO as clock output
2+
#
3+
# Copyright (C) 2025 Timofey Titovets <[email protected]>
4+
#
5+
# This file may be distributed under the terms of the GNU GPLv3 license.
6+
7+
import logging
8+
9+
class PrinterClockOutputPin:
10+
def __init__(self, config):
11+
self.name = config.get_name()
12+
self.printer = config.get_printer()
13+
ppins = self.printer.lookup_object('pins')
14+
mcu_pin = ppins.lookup_pin(config.get('pin'), can_invert=True)
15+
self.mcu = mcu_pin["chip"]
16+
self.pin = mcu_pin["pin"]
17+
self.invert = mcu_pin["invert"]
18+
self.frequency = config.getfloat('frequency', 100, above=(1/0.3),
19+
maxval=520000000)
20+
self.mcu.register_config_callback(self._build_config)
21+
def _build_config(self):
22+
self.mcu.lookup_command(
23+
"stm32_timer_output pin=%u cycle_ticks=%u on_ticks=%hu")
24+
mcu_freq = self.mcu.seconds_to_clock(1.0)
25+
cycle_ticks = int(mcu_freq // self.frequency)
26+
# validate frequency
27+
mcu_freq_rev = int(cycle_ticks * self.frequency)
28+
if mcu_freq_rev != mcu_freq:
29+
msg = """
30+
# Frequency output must be without remainder, %i != %i
31+
[%s]
32+
frequency = %f
33+
""" % (mcu_freq, mcu_freq_rev, self.name, self.frequency)
34+
raise self.printer.config_error(msg)
35+
value = int(0.5 * cycle_ticks)
36+
if self.invert:
37+
value = cycle_ticks - value
38+
if value/cycle_ticks < 0.4:
39+
logging.warning("[%s] pulse width < 40%%" % (self.name))
40+
if value/cycle_ticks > 0.6:
41+
logging.warning("[%s] pulse width > 60%%" % (self.name))
42+
self.mcu.add_config_cmd(
43+
"stm32_timer_output pin=%s cycle_ticks=%d on_ticks=%d"
44+
% (self.pin, cycle_ticks, value))
45+
46+
def load_config_prefix(config):
47+
return PrinterClockOutputPin(config)

0 commit comments

Comments
 (0)