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:
Kevin O'Connor 2019-08-06 11:44:29 -04:00
parent 30bd7c0e47
commit c2a08962b7
9 changed files with 115 additions and 116 deletions

View File

@ -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 # Helper code for working with devices connected to an MCU via an SPI bus
class MCU_SPI: 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.mcu = mcu
self.bus = bus self.bus = bus
shutdown_msg = "".join(["%02x" % (x,) for x in shutdown_seq]) # Config SPI object (set all CS pins high before spi_set_bus commands)
self.oid = self.mcu.create_oid() self.oid = mcu.create_oid()
if pin is not None: if pin is None:
# Set all CS pins high before first config_spi mcu.add_config_cmd("config_spi_without_cs oid=%d" % (self.oid,))
self.mcu.add_config_cmd("set_digital_out pin=%s value=1" % (pin,)) else:
self.config_sw_msg = None mcu.add_config_cmd("config_spi oid=%d pin=%s" % (self.oid, pin))
# Generate SPI bus config message
if sw_pins is not None: 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 = ( self.config_fmt = (
"config_spi_from_software oid=%d sw_oid=%d pin=%s" "spi_set_software_bus oid=%d"
" shutdown_msg=%s" % ( " miso_pin=%s mosi_pin=%s sclk_pin=%s mode=%d rate=%d"
self.oid, software_spi_oid, pin, shutdown_msg)) % (self.oid, sw_pins[0], sw_pins[1], sw_pins[2], mode, speed))
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))
else: else:
self.config_fmt = ( self.config_fmt = (
"config_spi oid=%d spi_bus=%%s pin=%s mode=%d rate=%d" "spi_set_bus oid=%d spi_bus=%%s mode=%d rate=%d"
" shutdown_msg=%s" % (self.oid, pin, mode, speed, shutdown_msg)) % (self.oid, mode, speed))
self.cmd_queue = self.mcu.alloc_command_queue() self.cmd_queue = mcu.alloc_command_queue()
self.mcu.register_config_callback(self.build_config) mcu.register_config_callback(self.build_config)
self.spi_send_cmd = self.spi_transfer_cmd = None 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): def get_oid(self):
return self.oid return self.oid
def get_mcu(self): def get_mcu(self):
@ -76,12 +72,10 @@ class MCU_SPI:
def get_command_queue(self): def get_command_queue(self):
return self.cmd_queue return self.cmd_queue
def build_config(self): def build_config(self):
if self.config_sw_msg is not None: if '%' in self.config_fmt:
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) 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( self.spi_send_cmd = self.mcu.lookup_command(
"spi_send oid=%c data=%*s", cq=self.cmd_queue) "spi_send oid=%c data=%*s", cq=self.cmd_queue)
self.spi_transfer_cmd = self.mcu.lookup_command( 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 # Helper to setup an spi bus from settings in a config section
def MCU_SPI_from_config(config, mode, pin_option="cs_pin", def MCU_SPI_from_config(config, mode, pin_option="cs_pin",
default_speed=100000, shutdown_seq=()): default_speed=100000):
# Determine pin from config # Determine pin from config
ppins = config.get_printer().lookup_object("pins") ppins = config.get_printer().lookup_object("pins")
cs_pin = config.get(pin_option) 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) speed = config.getint('spi_speed', default_speed, minval=100000)
if config.get('spi_software_sclk_pin', None) is not None: if config.get('spi_software_sclk_pin', None) is not None:
sw_pin_names = ['spi_software_%s_pin' % (name,) 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) sw_pin_params = [ppins.lookup_pin(config.get(name), share_type=name)
for name in sw_pin_names] for name in sw_pin_names]
for pin_params in sw_pin_params: 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) bus = config.get('spi_bus', None)
sw_pins = None sw_pins = None
# Create MCU_SPI object # 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)
###################################################################### ######################################################################

View File

@ -201,8 +201,8 @@ class Replicape:
if not self.host_mcu.is_fileoutput() and os.path.exists( if not self.host_mcu.is_fileoutput() and os.path.exists(
'/sys/devices/platform/ocp/481a0000.spi/spi_master/spi2'): '/sys/devices/platform/ocp/481a0000.spi/spi_master/spi2'):
sr_spi_bus = "spidev2.1" sr_spi_bus = "spidev2.1"
self.sr_spi = bus.MCU_SPI(self.host_mcu, sr_spi_bus, self.sr_spi = bus.MCU_SPI(self.host_mcu, sr_spi_bus, None, 0, 50000000)
None, 0, 50000000, self.sr_disabled) self.sr_spi.setup_shutdown_msg(self.sr_disabled)
self.sr_spi.spi_send(self.sr_disabled) self.sr_spi.spi_send(self.sr_disabled)
def note_pwm_start_value(self, channel, start_value, shutdown_value): def note_pwm_start_value(self, channel, start_value, shutdown_value):
self.mcu_pwm_start_value |= not not start_value self.mcu_pwm_start_value |= not not start_value

View File

@ -6,9 +6,7 @@
// This file may be distributed under the terms of the GNU GPLv3 license. // This file may be distributed under the terms of the GNU GPLv3 license.
#include "internal.h" // enable_pclock #include "internal.h" // enable_pclock
#include "command.h" // shutdown
#include "gpio.h" // spi_setup #include "gpio.h" // spi_setup
#include "sched.h" // sched_shutdown
void void
spi_init(uint32_t bus, SercomSpi *ss, uint32_t ctrla, uint32_t baud) spi_init(uint32_t bus, SercomSpi *ss, uint32_t ctrla, uint32_t baud)

View File

@ -27,7 +27,7 @@ alloc_init(void)
DECL_INIT(alloc_init); DECL_INIT(alloc_init);
// Allocate an area of memory // Allocate an area of memory
static void * void *
alloc_chunk(size_t size) alloc_chunk(size_t size)
{ {
if (alloc_end + size > dynmem_end()) if (alloc_end + size > dynmem_end())

View File

@ -1,8 +1,10 @@
#ifndef __BASECMD_H #ifndef __BASECMD_H
#define __BASECMD_H #define __BASECMD_H
#include <stddef.h> // size_t
#include <stdint.h> // uint8_t #include <stdint.h> // uint8_t
void *alloc_chunk(size_t size);
void move_free(void *m); void move_free(void *m);
void *move_alloc(void); void *move_alloc(void);
void move_request_size(int size); void move_request_size(int size);

View File

@ -97,13 +97,13 @@ spi_transfer(struct spi_config config, uint8_t receive_data
int ret = ioctl(config.fd, SPI_IOC_MESSAGE(1), &transfer); int ret = ioctl(config.fd, SPI_IOC_MESSAGE(1), &transfer);
if (ret < 0) { if (ret < 0) {
report_errno("spi ioctl", ret); report_errno("spi ioctl", ret);
shutdown("Unable to issue spi ioctl"); try_shutdown("Unable to issue spi ioctl");
} }
} else { } else {
int ret = write(config.fd, data, len); int ret = write(config.fd, data, len);
if (ret < 0) { if (ret < 0) {
report_errno("write spi", ret); report_errno("write spi", ret);
shutdown("Unable to write to spi"); try_shutdown("Unable to write to spi");
} }
} }
} }

View File

@ -9,39 +9,33 @@
#include "basecmd.h" // oid_alloc #include "basecmd.h" // oid_alloc
#include "command.h" // DECL_COMMAND #include "command.h" // DECL_COMMAND
#include "sched.h" // sched_shutdown #include "sched.h" // sched_shutdown
#include "spicmds.h" // spidev_set_software_bus
struct spi_software { struct spi_software {
struct gpio_out sclk, mosi;
struct gpio_in miso; struct gpio_in miso;
struct gpio_out mosi, sclk;
uint8_t mode; uint8_t mode;
}; };
void void
command_config_software_spi(uint32_t *args) command_spi_set_software_bus(uint32_t *args)
{ {
uint8_t oid = args[0], sclk_pin = args[1], mosi_pin = args[2]; uint8_t mode = args[4];
uint8_t miso_pin = args[3], mode = args[4];
if (mode > 3) if (mode > 3)
shutdown("Invalid spi mode"); shutdown("Invalid spi config");
struct spi_software *spi = oid_alloc(oid, command_config_software_spi struct spidev_s *spi = spidev_oid_lookup(args[0]);
, sizeof(*spi)); struct spi_software *ss = alloc_chunk(sizeof(*ss));
ss->miso = gpio_in_setup(args[1], 1);
spi->sclk = gpio_out_setup(sclk_pin, 0); ss->mosi = gpio_out_setup(args[2], 0);
spi->mosi = gpio_out_setup(mosi_pin, 0); ss->sclk = gpio_out_setup(args[3], 0);
spi->miso = gpio_in_setup(miso_pin, 1); ss->mode = mode;
spi->mode = mode; spidev_set_software_bus(spi, ss);
} }
DECL_COMMAND(command_config_software_spi, DECL_COMMAND(command_spi_set_software_bus,
"config_software_spi oid=%c sclk_pin=%u mosi_pin=%u miso_pin=%u" "spi_set_software_bus oid=%c miso_pin=%u mosi_pin=%u sclk_pin=%u"
" mode=%u rate=%u"); " mode=%u rate=%u");
struct spi_software *
spi_software_oid_lookup(uint8_t oid)
{
return oid_lookup(oid, command_config_software_spi);
}
void void
spi_software_prepare(struct spi_software *ss) spi_software_prepare(struct spi_software *ss)
{ {

View File

@ -1,6 +1,6 @@
// Commands for sending messages on an SPI bus // Commands for sending messages on an SPI bus
// //
// Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> // Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // This file may be distributed under the terms of the GNU GPLv3 license.
@ -20,69 +20,27 @@ struct spidev_s {
}; };
struct gpio_out pin; struct gpio_out pin;
uint8_t flags; uint8_t flags;
uint8_t shutdown_msg_len;
uint8_t shutdown_msg[];
}; };
enum { enum {
SF_HAVE_PIN = 1, SF_SOFTWARE = 2, SF_HAVE_PIN = 1, SF_SOFTWARE = 2, SF_HARDWARE = 4,
}; };
void void
command_config_spi(uint32_t *args) command_config_spi(uint32_t *args)
{ {
uint8_t mode = args[3], shutdown_msg_len = args[5]; struct spidev_s *spi = oid_alloc(args[0], command_config_spi, sizeof(*spi));
if (mode > 3) spi->pin = gpio_out_setup(args[1], 1);
shutdown("Invalid spi mode"); spi->flags |= SF_HAVE_PIN;
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;
spi->spi_config = spi_setup(args[1], mode, args[4]);
spi->shutdown_msg_len = shutdown_msg_len;
uint8_t *shutdown_msg = (void*)(size_t)args[6];
memcpy(spi->shutdown_msg, shutdown_msg, shutdown_msg_len);
} }
DECL_COMMAND(command_config_spi, DECL_COMMAND(command_config_spi, "config_spi oid=%c pin=%u");
"config_spi oid=%c spi_bus=%u pin=%u mode=%u rate=%u"
" shutdown_msg=%*s");
void void
command_config_spi_without_cs(uint32_t *args) command_config_spi_without_cs(uint32_t *args)
{ {
uint8_t mode = args[2], shutdown_msg_len = args[4]; struct spidev_s *spi = oid_alloc(args[0], command_config_spi, sizeof(*spi));
if (mode > 3)
shutdown("Invalid spi mode");
struct spidev_s *spi = oid_alloc(args[0], command_config_spi
, sizeof(*spi) + shutdown_msg_len);
spi->spi_config = spi_setup(args[1], mode, args[3]);
spi->shutdown_msg_len = shutdown_msg_len;
uint8_t *shutdown_msg = (void*)(size_t)args[5];
memcpy(spi->shutdown_msg, shutdown_msg, shutdown_msg_len);
} }
DECL_COMMAND(command_config_spi_without_cs, DECL_COMMAND(command_config_spi_without_cs, "config_spi_without_cs oid=%c");
"config_spi_without_cs oid=%c spi_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 * struct spidev_s *
spidev_oid_lookup(uint8_t oid) spidev_oid_lookup(uint8_t oid)
@ -90,10 +48,36 @@ spidev_oid_lookup(uint8_t oid)
return oid_lookup(oid, command_config_spi); return oid_lookup(oid, command_config_spi);
} }
void
command_spi_set_bus(uint32_t *args)
{
struct spidev_s *spi = spidev_oid_lookup(args[0]);
uint8_t mode = args[2];
if (mode > 3 || spi->flags & (SF_SOFTWARE|SF_HARDWARE))
shutdown("Invalid spi config");
spi->spi_config = spi_setup(args[1], mode, args[3]);
spi->flags |= SF_HARDWARE;
}
DECL_COMMAND(command_spi_set_bus,
"spi_set_bus oid=%c spi_bus=%u mode=%u rate=%u");
void
spidev_set_software_bus(struct spidev_s *spi, struct spi_software *ss)
{
if (spi->flags & (SF_SOFTWARE|SF_HARDWARE))
shutdown("Invalid spi config");
spi->spi_software = ss;
spi->flags |= SF_SOFTWARE;
}
void void
spidev_transfer(struct spidev_s *spi, uint8_t receive_data spidev_transfer(struct spidev_s *spi, uint8_t receive_data
, uint8_t data_len, uint8_t *data) , uint8_t data_len, uint8_t *data)
{ {
if (!(spi->flags & (SF_SOFTWARE|SF_HARDWARE)))
// Not yet initialized
return;
if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE) if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE)
spi_software_prepare(spi->spi_software); spi_software_prepare(spi->spi_software);
else else
@ -115,7 +99,7 @@ void
command_spi_transfer(uint32_t *args) command_spi_transfer(uint32_t *args)
{ {
uint8_t oid = args[0]; uint8_t oid = args[0];
struct spidev_s *spi = oid_lookup(oid, command_config_spi); struct spidev_s *spi = spidev_oid_lookup(oid);
uint8_t data_len = args[1]; uint8_t data_len = args[1];
uint8_t *data = (void*)(size_t)args[2]; uint8_t *data = (void*)(size_t)args[2];
spidev_transfer(spi, 1, data_len, data); spidev_transfer(spi, 1, data_len, data);
@ -126,14 +110,39 @@ DECL_COMMAND(command_spi_transfer, "spi_transfer oid=%c data=%*s");
void void
command_spi_send(uint32_t *args) command_spi_send(uint32_t *args)
{ {
uint8_t oid = args[0]; struct spidev_s *spi = spidev_oid_lookup(args[0]);
struct spidev_s *spi = oid_lookup(oid, command_config_spi);
uint8_t data_len = args[1]; uint8_t data_len = args[1];
uint8_t *data = (void*)(size_t)args[2]; uint8_t *data = (void*)(size_t)args[2];
spidev_transfer(spi, 0, data_len, data); spidev_transfer(spi, 0, data_len, data);
} }
DECL_COMMAND(command_spi_send, "spi_send oid=%c data=%*s"); DECL_COMMAND(command_spi_send, "spi_send oid=%c data=%*s");
/****************************************************************
* Shutdown handling
****************************************************************/
struct spidev_shutdown_s {
struct spidev_s *spi;
uint8_t shutdown_msg_len;
uint8_t shutdown_msg[];
};
void
command_config_spi_shutdown(uint32_t *args)
{
struct spidev_s *spi = spidev_oid_lookup(args[1]);
uint8_t shutdown_msg_len = args[2];
struct spidev_shutdown_s *sd = oid_alloc(
args[0], command_config_spi_shutdown, sizeof(*sd) + shutdown_msg_len);
sd->spi = spi;
sd->shutdown_msg_len = shutdown_msg_len;
uint8_t *shutdown_msg = (void*)(size_t)args[3];
memcpy(sd->shutdown_msg, shutdown_msg, shutdown_msg_len);
}
DECL_COMMAND(command_config_spi_shutdown,
"config_spi_shutdown oid=%c spi_oid=%c shutdown_msg=%*s");
void void
spidev_shutdown(void) spidev_shutdown(void)
{ {
@ -146,9 +155,9 @@ spidev_shutdown(void)
} }
// Send shutdown messages // Send shutdown messages
foreach_oid(oid, spi, command_config_spi) { struct spidev_shutdown_s *sd;
if (spi->shutdown_msg_len) foreach_oid(oid, sd, command_config_spi_shutdown) {
spidev_transfer(spi, 0, spi->shutdown_msg_len, spi->shutdown_msg); spidev_transfer(sd->spi, 0, sd->shutdown_msg_len, sd->shutdown_msg);
} }
} }
DECL_SHUTDOWN(spidev_shutdown); DECL_SHUTDOWN(spidev_shutdown);

View File

@ -4,7 +4,9 @@
#include <stdint.h> // uint8_t #include <stdint.h> // uint8_t
struct spidev_s *spidev_oid_lookup(uint8_t oid); struct spidev_s *spidev_oid_lookup(uint8_t oid);
struct spi_software;
void spidev_set_software_bus(struct spidev_s *spi, struct spi_software *ss);
void spidev_transfer(struct spidev_s *spi, uint8_t receive_data void spidev_transfer(struct spidev_s *spi, uint8_t receive_data
, uint8_t data_len, uint8_t *data); , uint8_t data_len, uint8_t *data);
#endif // stepper.h #endif // spicmds.h