diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 987d5296..fc439f3a 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -680,6 +680,13 @@ # The pin corresponding to the AD5206 chip select line. This pin # will be set to low at the start of SPI messages and raised to high # after the message completes. This parameter must be provided. +#spi_bus: +#spi_speed: +#spi_software_sclk_pin: +#spi_software_mosi_pin: +#spi_software_miso_pin: +# These optional parameters allow one to customize the SPI settings +# used to communicate with the chip. #channel_1: #channel_2: #channel_3: @@ -787,6 +794,13 @@ # The pin corresponding to the TMC2130 chip select line. This pin # will be set to low at the start of SPI messages and raised to high # after the message completes. This parameter must be provided. +#spi_bus: +#spi_speed: +#spi_software_sclk_pin: +#spi_software_mosi_pin: +#spi_software_miso_pin: +# These optional parameters allow one to customize the SPI settings +# used to communicate with the chip. #microsteps: # The number of microsteps to configure the driver to use. Valid # values are 1, 2, 4, 8, 16, 32, 64, 128, 256. This parameter must @@ -908,6 +922,11 @@ #spi_speed: 2000000 # SPI bus frequency used to communicate with the TMC2660 stepper # driver. The default is 2000000. +#spi_software_sclk_pin: +#spi_software_mosi_pin: +#spi_software_miso_pin: +# These optional parameters allow one to customize the SPI settings +# used to communicate with the chip. #microsteps: # The number of microsteps to configure the driver to use. Valid # values are 1, 2, 4, 8, 16, 32, 64, 128, 256. This parameter must @@ -1043,8 +1062,15 @@ # provided when using an uc1701 display. #cs_pin: #dc_pin: +#spi_bus: +#spi_speed: +#spi_software_sclk_pin: +#spi_software_mosi_pin: +#spi_software_miso_pin: # The pins connected to an ssd1306 type lcd when in "4-wire" spi -# mode. The default is to use i2c mode for ssd1306 displays. +# mode. The parameters that start with "spi_" are optional and they +# control the spi settings used to communicate with the chip. The +# default is to use i2c mode for ssd1306 displays. #menu_root: # Entry point for menu, root menu container name. If this parameter # is not provided then default menu root is used. When provided @@ -1140,6 +1166,12 @@ #spi_speed: 4000000 # The SPI speed (in hz) to use when communicating with the chip. # The default is 4000000. +#spi_bus: +#spi_software_sclk_pin: +#spi_software_mosi_pin: +#spi_software_miso_pin: +# These optional parameters allow one to customize the SPI settings +# used to communicate with the chip. #sensor_pin: # The chip select line for the sensor chip. This parameter must be # provided. diff --git a/klippy/extras/bus.py b/klippy/extras/bus.py index 2808d6fe..7c7a4454 100644 --- a/klippy/extras/bus.py +++ b/klippy/extras/bus.py @@ -12,22 +12,33 @@ import mcu # 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): + def __init__(self, mcu, bus, pin, mode, speed, shutdown_seq, sw_pins=None): self.mcu = mcu shutdown_msg = "".join(["%02x" % (x,) for x in shutdown_seq]) self.oid = self.mcu.create_oid() - if pin is None: - self.config_msg = ( - "config_spi_without_cs oid=%d bus=%d mode=%d rate=%d" - " shutdown_msg=%s" % ( - self.oid, bus, mode, speed, shutdown_msg)) - else: + 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_msg = ( + if sw_pins is not None: + software_spi_oid = self.mcu.create_oid() + self.config_msgs = [ + "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), + "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_msgs = [ + "config_spi_without_cs oid=%d bus=%d mode=%d rate=%d" + " shutdown_msg=%s" % ( + self.oid, bus, mode, speed, shutdown_msg)] + else: + self.config_msgs = [ "config_spi oid=%d bus=%d pin=%s mode=%d rate=%d" " shutdown_msg=%s" % ( - self.oid, bus, pin, mode, speed, shutdown_msg)) + self.oid, bus, 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 @@ -38,7 +49,8 @@ class MCU_SPI: def get_command_queue(self): return self.cmd_queue def build_config(self): - self.mcu.add_config_cmd(self.config_msg) + for msg in self.config_msgs: + self.mcu.add_config_cmd(msg) 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( @@ -68,11 +80,24 @@ def MCU_SPI_from_config(config, mode, pin_option="cs_pin", ppins.reset_pin_sharing(cs_pin_params) pin = None # Load bus parameters - speed = config.getint('spi_speed', default_speed, minval=100000) - bus = config.getint('spi_bus', 0, minval=0) - # Create MCU_SPI object mcu = cs_pin_params['chip'] - return MCU_SPI(mcu, bus, pin, mode, speed, shutdown_seq) + 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']] + 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: + if pin_params['chip'] != mcu: + raise ppins.error("%s: spi pins must be on same mcu" % ( + config.get_name(),)) + sw_pins = tuple([pin_params['pin'] for pin_params in sw_pin_params]) + bus = None + else: + bus = config.getint('spi_bus', 0, minval=0) + sw_pins = None + # Create MCU_SPI object + return MCU_SPI(mcu, bus, pin, mode, speed, shutdown_seq, sw_pins) ###################################################################### diff --git a/src/Makefile b/src/Makefile index 02386d9c..546dc0a5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,4 +7,4 @@ src-$(CONFIG_HAVE_GPIO_SPI) += spicmds.c thermocouple.c src-$(CONFIG_HAVE_GPIO_I2C) += i2ccmds.c src-$(CONFIG_HAVE_GPIO_HARD_PWM) += pwmcmds.c src-$(CONFIG_HAVE_GPIO_BITBANGING) += lcd_st7920.c lcd_hd44780.c buttons.c \ - tmcuart.c + tmcuart.c spi_software.c diff --git a/src/spi_software.c b/src/spi_software.c new file mode 100644 index 00000000..385b4f87 --- /dev/null +++ b/src/spi_software.c @@ -0,0 +1,81 @@ +// Software SPI emulation +// +// Copyright (C) 2019 Kevin O'Connor +// +// This file may be distributed under the terms of the GNU GPLv3 license. + +#include "board/irq.h" // gpio_out_setup +#include "board/gpio.h" // gpio_out_setup +#include "basecmd.h" // oid_alloc +#include "command.h" // DECL_COMMAND +#include "sched.h" // sched_shutdown + +struct spi_software { + struct gpio_out sclk, mosi; + struct gpio_in miso; + uint8_t mode; +}; + +void +command_config_software_spi(uint32_t *args) +{ + uint8_t oid = args[0], sclk_pin = args[1], mosi_pin = args[2]; + uint8_t miso_pin = args[3], mode = args[4]; + if (mode > 3) + shutdown("Invalid spi mode"); + + struct spi_software *spi = oid_alloc(oid, command_config_software_spi + , sizeof(*spi)); + + spi->sclk = gpio_out_setup(sclk_pin, 0); + spi->mosi = gpio_out_setup(mosi_pin, 0); + spi->miso = gpio_in_setup(miso_pin, 1); +} +DECL_COMMAND(command_config_software_spi, + "config_software_spi oid=%c sclk_pin=%u mosi_pin=%u miso_pin=%u" + " mode=%u rate=%u"); + +struct spi_software * +spi_software_oid_lookup(uint8_t oid) +{ + return oid_lookup(oid, command_config_software_spi); +} + +void +spi_software_prepare(struct spi_software *ss) +{ + gpio_out_write(ss->sclk, ss->mode < 2 ? 0 : 1); +} + +void +spi_software_transfer(struct spi_software *ss, uint8_t receive_data + , uint8_t len, uint8_t *data) +{ + while (len--) { + uint8_t outbuf = *data; + uint8_t inbuf = 0; + for (uint_fast8_t i = 0; i < 8; i++) { + if (ss->mode & 0x01) { + // MODE 1 & 3 + gpio_out_toggle(ss->sclk); + gpio_out_write(ss->mosi, outbuf & 0x80); + outbuf <<= 1; + gpio_out_toggle(ss->sclk); + inbuf <<= 1; + inbuf |= gpio_in_read(ss->miso); + } else { + // MODE 0 & 2 + gpio_out_write(ss->mosi, outbuf & 0x80); + outbuf <<= 1; + gpio_out_toggle(ss->sclk); + inbuf <<= 1; + inbuf |= gpio_in_read(ss->miso); + gpio_out_toggle(ss->sclk); + } + } + + if (receive_data) + *data = inbuf; + data++; + } +} diff --git a/src/spi_software.h b/src/spi_software.h new file mode 100644 index 00000000..549866f2 --- /dev/null +++ b/src/spi_software.h @@ -0,0 +1,11 @@ +#ifndef __SPI_SOFTWARE_H +#define __SPI_SOFTWARE_H + +#include // uint8_t + +struct spi_software *spi_software_oid_lookup(uint8_t oid); +void spi_software_prepare(struct spi_software *ss); +void spi_software_transfer(struct spi_software *ss, uint8_t receive_data + , uint8_t len, uint8_t *data); + +#endif // spi_software.h diff --git a/src/spicmds.c b/src/spicmds.c index 0017c56b..99e3b3a6 100644 --- a/src/spicmds.c +++ b/src/spicmds.c @@ -5,14 +5,19 @@ // This file may be distributed under the terms of the GNU GPLv3 license. #include // memcpy +#include "autoconf.h" // CONFIG_HAVE_GPIO_BITBANGING #include "board/gpio.h" // gpio_out_write #include "basecmd.h" // oid_alloc #include "command.h" // DECL_COMMAND #include "sched.h" // DECL_SHUTDOWN +#include "spi_software.h" // spi_software_setup #include "spicmds.h" // spidev_transfer struct spidev_s { - struct spi_config spi_config; + union { + struct spi_config spi_config; + struct spi_software *spi_software; + }; struct gpio_out pin; uint8_t flags; uint8_t shutdown_msg_len; @@ -20,7 +25,7 @@ struct spidev_s { }; enum { - SF_HAVE_PIN = 1, + SF_HAVE_PIN = 1, SF_SOFTWARE = 2, }; void @@ -58,6 +63,26 @@ DECL_COMMAND(command_config_spi_without_cs, "config_spi_without_cs oid=%c bus=%u mode=%u rate=%u" " shutdown_msg=%*s"); +void +command_config_spi_from_software(uint32_t *args) +{ + uint8_t shutdown_msg_len = args[3]; + struct spi_software *sspi = spi_software_oid_lookup(args[1]); + struct spidev_s *spi = oid_alloc(args[0], command_config_spi + , sizeof(*spi) + shutdown_msg_len); + spi->pin = gpio_out_setup(args[2], 1); + spi->flags = SF_HAVE_PIN | SF_SOFTWARE; + spi->spi_software = sspi; + spi->shutdown_msg_len = shutdown_msg_len; + uint8_t *shutdown_msg = (void*)(size_t)args[4]; + memcpy(spi->shutdown_msg, shutdown_msg, shutdown_msg_len); +} +#if CONFIG_HAVE_GPIO_BITBANGING +DECL_COMMAND(command_config_spi_from_software, + "config_spi_from_software oid=%c sw_oid=%u pin=%u" + " shutdown_msg=%*s"); +#endif + struct spidev_s * spidev_oid_lookup(uint8_t oid) { @@ -68,12 +93,18 @@ void spidev_transfer(struct spidev_s *spi, uint8_t receive_data , uint8_t data_len, uint8_t *data) { - spi_prepare(spi->spi_config); + if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE) + spi_software_prepare(spi->spi_software); + else + spi_prepare(spi->spi_config); if (spi->flags & SF_HAVE_PIN) gpio_out_write(spi->pin, 0); - spi_transfer(spi->spi_config, receive_data, data_len, data); + if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE) + spi_software_transfer(spi->spi_software, receive_data, data_len, data); + else + spi_transfer(spi->spi_config, receive_data, data_len, data); if (spi->flags & SF_HAVE_PIN) gpio_out_write(spi->pin, 1);