mirror of
https://github.com/Klipper3d/klipper.git
synced 2025-08-10 07:15:10 -06:00
spicmds: Rework spi config commands
Rework the spi_config commands so that bus configuration and shutdown message configuration is done separately from the main "config_spi" command. This makes the spi configuration more flexible. It's now possible to use software spi without a CS pin. It's now possible to define multiple SPI messages to send on a shutdown event. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
30bd7c0e47
commit
c2a08962b7
9 changed files with 115 additions and 116 deletions
|
@ -38,37 +38,33 @@ def resolve_bus_name(mcu, param, bus):
|
|||
|
||||
# Helper code for working with devices connected to an MCU via an SPI bus
|
||||
class MCU_SPI:
|
||||
def __init__(self, mcu, bus, pin, mode, speed, shutdown_seq, sw_pins=None):
|
||||
def __init__(self, mcu, bus, pin, mode, speed, sw_pins=None):
|
||||
self.mcu = mcu
|
||||
self.bus = bus
|
||||
shutdown_msg = "".join(["%02x" % (x,) for x in shutdown_seq])
|
||||
self.oid = self.mcu.create_oid()
|
||||
if pin is not None:
|
||||
# Set all CS pins high before first config_spi
|
||||
self.mcu.add_config_cmd("set_digital_out pin=%s value=1" % (pin,))
|
||||
self.config_sw_msg = None
|
||||
# Config SPI object (set all CS pins high before spi_set_bus commands)
|
||||
self.oid = mcu.create_oid()
|
||||
if pin is None:
|
||||
mcu.add_config_cmd("config_spi_without_cs oid=%d" % (self.oid,))
|
||||
else:
|
||||
mcu.add_config_cmd("config_spi oid=%d pin=%s" % (self.oid, pin))
|
||||
# Generate SPI bus config message
|
||||
if sw_pins is not None:
|
||||
software_spi_oid = self.mcu.create_oid()
|
||||
self.config_sw_msg = (
|
||||
"config_software_spi oid=%d sclk_pin=%s mosi_pin=%s miso_pin=%s"
|
||||
" mode=%d rate=%d" % (
|
||||
software_spi_oid, sw_pins[0], sw_pins[1], sw_pins[2],
|
||||
mode, speed))
|
||||
self.config_fmt = (
|
||||
"config_spi_from_software oid=%d sw_oid=%d pin=%s"
|
||||
" shutdown_msg=%s" % (
|
||||
self.oid, software_spi_oid, pin, shutdown_msg))
|
||||
elif pin is None:
|
||||
self.config_fmt = (
|
||||
"config_spi_without_cs oid=%d spi_bus=%%s mode=%d rate=%d"
|
||||
" shutdown_msg=%s" % (self.oid, mode, speed, shutdown_msg))
|
||||
"spi_set_software_bus oid=%d"
|
||||
" miso_pin=%s mosi_pin=%s sclk_pin=%s mode=%d rate=%d"
|
||||
% (self.oid, sw_pins[0], sw_pins[1], sw_pins[2], mode, speed))
|
||||
else:
|
||||
self.config_fmt = (
|
||||
"config_spi oid=%d spi_bus=%%s pin=%s mode=%d rate=%d"
|
||||
" shutdown_msg=%s" % (self.oid, pin, mode, speed, shutdown_msg))
|
||||
self.cmd_queue = self.mcu.alloc_command_queue()
|
||||
self.mcu.register_config_callback(self.build_config)
|
||||
"spi_set_bus oid=%d spi_bus=%%s mode=%d rate=%d"
|
||||
% (self.oid, mode, speed))
|
||||
self.cmd_queue = mcu.alloc_command_queue()
|
||||
mcu.register_config_callback(self.build_config)
|
||||
self.spi_send_cmd = self.spi_transfer_cmd = None
|
||||
def setup_shutdown_msg(self, shutdown_seq):
|
||||
shutdown_msg = "".join(["%02x" % (x,) for x in shutdown_seq])
|
||||
self.mcu.add_config_cmd(
|
||||
"config_spi_shutdown oid=%d spi_oid=%d shutdown_msg=%s"
|
||||
% (self.mcu.create_oid(), self.oid, shutdown_msg))
|
||||
def get_oid(self):
|
||||
return self.oid
|
||||
def get_mcu(self):
|
||||
|
@ -76,12 +72,10 @@ class MCU_SPI:
|
|||
def get_command_queue(self):
|
||||
return self.cmd_queue
|
||||
def build_config(self):
|
||||
if self.config_sw_msg is not None:
|
||||
self.mcu.add_config_cmd(self.config_sw_msg)
|
||||
self.mcu.add_config_cmd(self.config_fmt)
|
||||
else:
|
||||
if '%' in self.config_fmt:
|
||||
bus = resolve_bus_name(self.mcu, "spi_bus", self.bus)
|
||||
self.mcu.add_config_cmd(self.config_fmt % (bus,))
|
||||
self.config_fmt = self.config_fmt % (bus,)
|
||||
self.mcu.add_config_cmd(self.config_fmt)
|
||||
self.spi_send_cmd = self.mcu.lookup_command(
|
||||
"spi_send oid=%c data=%*s", cq=self.cmd_queue)
|
||||
self.spi_transfer_cmd = self.mcu.lookup_command(
|
||||
|
@ -101,7 +95,7 @@ class MCU_SPI:
|
|||
|
||||
# Helper to setup an spi bus from settings in a config section
|
||||
def MCU_SPI_from_config(config, mode, pin_option="cs_pin",
|
||||
default_speed=100000, shutdown_seq=()):
|
||||
default_speed=100000):
|
||||
# Determine pin from config
|
||||
ppins = config.get_printer().lookup_object("pins")
|
||||
cs_pin = config.get(pin_option)
|
||||
|
@ -115,7 +109,7 @@ def MCU_SPI_from_config(config, mode, pin_option="cs_pin",
|
|||
speed = config.getint('spi_speed', default_speed, minval=100000)
|
||||
if config.get('spi_software_sclk_pin', None) is not None:
|
||||
sw_pin_names = ['spi_software_%s_pin' % (name,)
|
||||
for name in ['sclk', 'mosi', 'miso']]
|
||||
for name in ['miso', 'mosi', 'sclk']]
|
||||
sw_pin_params = [ppins.lookup_pin(config.get(name), share_type=name)
|
||||
for name in sw_pin_names]
|
||||
for pin_params in sw_pin_params:
|
||||
|
@ -128,7 +122,7 @@ def MCU_SPI_from_config(config, mode, pin_option="cs_pin",
|
|||
bus = config.get('spi_bus', None)
|
||||
sw_pins = None
|
||||
# Create MCU_SPI object
|
||||
return MCU_SPI(mcu, bus, pin, mode, speed, shutdown_seq, sw_pins)
|
||||
return MCU_SPI(mcu, bus, pin, mode, speed, sw_pins)
|
||||
|
||||
|
||||
######################################################################
|
||||
|
|
|
@ -201,8 +201,8 @@ class Replicape:
|
|||
if not self.host_mcu.is_fileoutput() and os.path.exists(
|
||||
'/sys/devices/platform/ocp/481a0000.spi/spi_master/spi2'):
|
||||
sr_spi_bus = "spidev2.1"
|
||||
self.sr_spi = bus.MCU_SPI(self.host_mcu, sr_spi_bus,
|
||||
None, 0, 50000000, self.sr_disabled)
|
||||
self.sr_spi = bus.MCU_SPI(self.host_mcu, sr_spi_bus, None, 0, 50000000)
|
||||
self.sr_spi.setup_shutdown_msg(self.sr_disabled)
|
||||
self.sr_spi.spi_send(self.sr_disabled)
|
||||
def note_pwm_start_value(self, channel, start_value, shutdown_value):
|
||||
self.mcu_pwm_start_value |= not not start_value
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue