bus: Support bus enumerations and add support for reserved pins

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-03-31 20:30:14 -04:00
parent 4c3d50693e
commit 1ab02e5225
5 changed files with 87 additions and 23 deletions

View File

@ -64,7 +64,7 @@ class KeyboardReader:
self.ser.register_callback(self.handle_output, '#output')
self.mcu_freq = msgparser.get_constant_float('CLOCK_FREQ')
mcu_type = msgparser.get_constant('MCU')
self.pins = pins.PinResolver(mcu_type, validate_aliases=False)
self.pins = pins.PinResolver(mcu_type, {}, validate_aliases=False)
self.output("="*20 + " connected " + "="*20)
return self.reactor.NEVER
def output(self, msg):

View File

@ -1,10 +1,35 @@
# Helper code for SPI and I2C bus communication
#
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2018,2019 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import mcu
def resolve_bus_name(mcu, param, bus):
# Find enumerations for the given bus
enumerations = mcu.get_enumerations()
enums = enumerations.get(param, enumerations.get('bus'))
if enums is None:
if bus is None:
return 0
return bus
# Verify bus is a valid enumeration
ppins = mcu.get_printer().lookup_object("pins")
mcu_name = mcu.get_name()
if bus is None:
rev_enums = {v: k for k, v in enums.items()}
if 0 not in rev_enums:
raise ppins.error("Must specify %s on mcu '%s'" % (param, mcu_name))
bus = rev_enums[0]
if bus not in enums:
raise ppins.error("Unknown %s '%s'" % (param, bus))
# Check for reserved bus pins
constants = mcu.get_constants()
reserve_pins = constants.get('BUS_PINS_%s' % (bus,), '')
for pin in reserve_pins.split(','):
ppins.reserve_pin(mcu_name, pin, bus)
return bus
######################################################################
# SPI
@ -14,31 +39,32 @@ import mcu
class MCU_SPI:
def __init__(self, mcu, bus, pin, mode, speed, shutdown_seq, 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
if sw_pins is not None:
software_spi_oid = self.mcu.create_oid()
self.config_msgs = [
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),
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)]
self.oid, software_spi_oid, pin, shutdown_msg))
elif pin is None:
self.config_msgs = [
"config_spi_without_cs oid=%d spi_bus=%d mode=%d rate=%d"
" shutdown_msg=%s" % (
self.oid, bus, mode, speed, shutdown_msg)]
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))
else:
self.config_msgs = [
"config_spi oid=%d spi_bus=%d pin=%s mode=%d rate=%d"
" shutdown_msg=%s" % (
self.oid, bus, pin, mode, speed, shutdown_msg)]
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)
self.spi_send_cmd = self.spi_transfer_cmd = None
@ -49,8 +75,12 @@ class MCU_SPI:
def get_command_queue(self):
return self.cmd_queue
def build_config(self):
for msg in self.config_msgs:
self.mcu.add_config_cmd(msg)
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:
bus = resolve_bus_name(self.mcu, "spi_bus", self.bus)
self.mcu.add_config_cmd(self.config_fmt % (bus,))
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(
@ -94,7 +124,7 @@ def MCU_SPI_from_config(config, mode, pin_option="cs_pin",
sw_pins = tuple([pin_params['pin'] for pin_params in sw_pin_params])
bus = None
else:
bus = config.getint('spi_bus', 0, minval=0)
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)
@ -108,11 +138,11 @@ def MCU_SPI_from_config(config, mode, pin_option="cs_pin",
class MCU_I2C:
def __init__(self, mcu, bus, addr, speed):
self.mcu = mcu
self.bus = bus
self.i2c_address = addr
self.oid = self.mcu.create_oid()
self.mcu.add_config_cmd(
"config_i2c oid=%d i2c_bus=%d rate=%d address=%d" % (
self.oid, bus, speed, addr))
self.config_fmt = "config_i2c oid=%d i2c_bus=%%s rate=%d address=%d" % (
self.oid, speed, addr)
self.cmd_queue = self.mcu.alloc_command_queue()
self.mcu.register_config_callback(self.build_config)
self.i2c_write_cmd = self.i2c_read_cmd = self.i2c_modify_bits_cmd = None
@ -125,6 +155,8 @@ class MCU_I2C:
def get_command_queue(self):
return self.cmd_queue
def build_config(self):
bus = resolve_bus_name(self.mcu, "i2c_bus", self.bus)
self.mcu.add_config_cmd(self.config_fmt % (bus,))
self.i2c_write_cmd = self.mcu.lookup_command(
"i2c_write oid=%c data=%*s", cq=self.cmd_queue)
self.i2c_read_cmd = self.mcu.lookup_command(
@ -163,7 +195,7 @@ def MCU_I2C_from_config(config, default_addr=None, default_speed=100000):
printer = config.get_printer()
i2c_mcu = mcu.get_printer_mcu(printer, config.get('i2c_mcu', 'mcu'))
speed = config.getint('i2c_speed', default_speed, minval=100000)
bus = config.getint('i2c_bus', 0, minval=0)
bus = config.get('i2c_bus', None)
if default_addr is None:
addr = config.getint('i2c_address', minval=0, maxval=127)
else:

View File

@ -539,7 +539,9 @@ class MCU:
self._oid_count,))
# Resolve pin names
mcu_type = self._serial.msgparser.get_constant('MCU')
pin_resolver = pins.PinResolver(mcu_type)
ppins = self._printer.lookup_object('pins')
reserved_pins = ppins.get_reserved_pins(self._name)
pin_resolver = pins.PinResolver(mcu_type, reserved_pins)
if self._pin_map is not None:
pin_resolver.update_aliases(self._pin_map)
for i, cmd in enumerate(self._config_cmds):
@ -615,8 +617,13 @@ class MCU:
name, len(msgparser.messages_by_id),
msgparser.version, msgparser.build_versions),
"MCU '%s' config: %s" % (name, " ".join(
["%s=%s" % (k, v) for k, v in msgparser.config.items()]))]
["%s=%s" % (k, v) for k, v in self.get_constants().items()]))]
logging.info("\n".join(log_info))
ppins = self._printer.lookup_object('pins')
for name, value in self.get_constants().items():
if name.startswith("RESERVE_PINS_"):
for pin in value.split(','):
ppins.reserve_pin(name, pin, name[14:])
self._mcu_freq = self.get_constant_float('CLOCK_FREQ')
self._stats_sumsq_base = self.get_constant_float('STATS_SUMSQ_BASE')
self._emergency_stop_cmd = self.lookup_command("emergency_stop")
@ -663,6 +670,10 @@ class MCU:
def get_max_stepper_error(self):
return self._max_stepper_error
# Wrapper functions
def get_printer(self):
return self._printer
def get_name(self):
return self._name
def register_msg(self, cb, msg, oid=None):
self._serial.register_callback(cb, msg, oid)
def alloc_command_queue(self):
@ -676,6 +687,10 @@ class MCU:
return None
def lookup_command_id(self, msgformat):
return self._serial.msgparser.lookup_command(msgformat).msgid
def get_enumerations(self):
return self._serial.msgparser.get_enumerations()
def get_constants(self):
return self._serial.msgparser.get_constants()
def get_constant_float(self, name):
return self._serial.msgparser.get_constant_float(name)
def print_time_to_clock(self, print_time):

View File

@ -384,6 +384,10 @@ class MessageParser:
except Exception as e:
logging.exception("process_identify error")
raise error("Error during identify: %s" % (str(e),))
def get_enumerations(self):
return dict(self.enumerations)
def get_constants(self):
return dict(self.config)
class sentinel: pass
def get_constant(self, name, default=sentinel, parser=str):
if name not in self.config:

View File

@ -129,8 +129,9 @@ def get_aliases_beaglebone(mcu):
re_pin = re.compile(r'(?P<prefix>[ _]pin=)(?P<name>[^ ]*)')
class PinResolver:
def __init__(self, mcu_type, validate_aliases=True):
def __init__(self, mcu_type, reserved_pins, validate_aliases=True):
self.mcu_type = mcu_type
self.reserved_pins = reserved_pins
self.validate_aliases = validate_aliases
self.aliases = {}
self.active_pins = {}
@ -149,6 +150,9 @@ class PinResolver:
and self.validate_aliases):
raise error("pin %s is an alias for %s" % (
name, self.active_pins[pin_id]))
if pin_id in self.reserved_pins:
raise error("pin %s is reserved for %s" % (
name, self.reserved_pins[pin_id]))
return m.group('prefix') + str(pin_id)
return re_pin.sub(pin_fixup, cmd)
@ -162,6 +166,7 @@ class PrinterPins:
def __init__(self):
self.chips = {}
self.active_pins = {}
self.reserved_pins = {}
def lookup_pin(self, pin_desc, can_invert=False, can_pullup=False,
share_type=None):
desc = pin_desc.strip()
@ -210,6 +215,14 @@ class PrinterPins:
def reset_pin_sharing(self, pin_params):
share_name = "%s:%s" % (pin_params['chip_name'], pin_params['pin'])
del self.active_pins[share_name]
def reserve_pin(self, chip_name, pin, reserve_name):
chip_reserve = self.reserved_pins.setdefault(chip_name, {})
if pin in chip_reserve and chip_reserve[pin] != reserve_name:
raise error("Pin %s:%s reserved for %s - can't reserve for %s" % (
chip_name, pin, chip_reserve[pin], reserve_name))
chip_reserve[pin] = reserve_name
def get_reserved_pins(self, chip_name):
return self.reserved_pins.get(chip_name, {})
def register_chip(self, chip_name, chip):
chip_name = chip_name.strip()
if chip_name in self.chips: