linux: Convert linux SPI code to use the generic spicmds.c code
Use the generic spi send/receive code on Linux. Update the replicape code to use the updated command format. Also, update the replicape code to turn off the stepper motors on a shutdown event. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
31ae74c56c
commit
f70fefa06f
|
@ -7,8 +7,7 @@ import logging
|
|||
import pins, mcu
|
||||
|
||||
REPLICAPE_MAX_CURRENT = 3.84
|
||||
REPLICAPE_SHIFT_REGISTER_BUS = 1
|
||||
REPLICAPE_SHIFT_REGISTER_DEVICE = 1
|
||||
REPLICAPE_SHIFT_REGISTER_BUS = 0x0101
|
||||
REPLICAPE_PCA9685_BUS = 2
|
||||
REPLICAPE_PCA9685_ADDRESS = 0x70
|
||||
REPLICAPE_PCA9685_CYCLE_TIME = .001
|
||||
|
@ -137,7 +136,7 @@ class Replicape:
|
|||
"power_fan0": (pca9685_pwm, 7), "power_fan1": (pca9685_pwm, 8),
|
||||
"power_fan2": (pca9685_pwm, 9), "power_fan3": (pca9685_pwm, 10) }
|
||||
# Setup stepper config
|
||||
self.send_spi_cmd = None
|
||||
self.spi_send_cmd = None
|
||||
self.last_stepper_time = 0.
|
||||
self.stepper_dacs = {}
|
||||
shift_registers = [1, 0, 0, 1, 1]
|
||||
|
@ -177,13 +176,18 @@ class Replicape:
|
|||
shift_registers[4] &= ~1
|
||||
self.sr_enabled = tuple(reversed(shift_registers))
|
||||
self.host_mcu.add_config_object(self)
|
||||
self.host_mcu.add_config_cmd("send_spi bus=%d dev=%d msg=%s" % (
|
||||
REPLICAPE_SHIFT_REGISTER_BUS, REPLICAPE_SHIFT_REGISTER_DEVICE,
|
||||
"".join(["%02x" % (x,) for x in self.sr_disabled])))
|
||||
self.sr_oid = self.host_mcu.create_oid()
|
||||
str_sr_disabled = "".join(["%02x" % (x,) for x in self.sr_disabled])
|
||||
self.host_mcu.add_config_cmd(
|
||||
"config_spi_without_cs oid=%d bus=%d mode=0 rate=50000000"
|
||||
" shutdown_msg=%s" % (
|
||||
self.sr_oid, REPLICAPE_SHIFT_REGISTER_BUS, str_sr_disabled))
|
||||
self.host_mcu.add_config_cmd("spi_send oid=%d data=%s" % (
|
||||
self.sr_oid, str_sr_disabled), is_init=True)
|
||||
def build_config(self):
|
||||
cmd_queue = self.host_mcu.alloc_command_queue()
|
||||
self.send_spi_cmd = self.host_mcu.lookup_command(
|
||||
"send_spi bus=%u dev=%u msg=%*s", cq=cmd_queue)
|
||||
self.spi_send_cmd = self.host_mcu.lookup_command(
|
||||
"spi_send oid=%c data=%*s", cq=cmd_queue)
|
||||
def note_pwm_start_value(self, channel, start_value, shutdown_value):
|
||||
self.mcu_pwm_start_value |= not not start_value
|
||||
self.mcu_pwm_shutdown_value |= not not shutdown_value
|
||||
|
@ -215,10 +219,8 @@ class Replicape:
|
|||
return
|
||||
print_time = max(print_time, self.last_stepper_time + PIN_MIN_TIME)
|
||||
clock = self.host_mcu.print_time_to_clock(print_time)
|
||||
# XXX - the send_spi message should be scheduled
|
||||
self.send_spi_cmd.send([REPLICAPE_SHIFT_REGISTER_BUS,
|
||||
REPLICAPE_SHIFT_REGISTER_DEVICE, sr],
|
||||
minclock=clock, reqclock=clock)
|
||||
# XXX - the spi_send message should be scheduled
|
||||
self.spi_send_cmd.send([self.sr_oid, sr], minclock=clock, reqclock=clock)
|
||||
def setup_pin(self, pin_params):
|
||||
pin = pin_params['pin']
|
||||
if pin not in self.pins:
|
||||
|
|
|
@ -7,6 +7,7 @@ config LINUX_SELECT
|
|||
bool
|
||||
default y
|
||||
select HAVE_GPIO_ADC
|
||||
select HAVE_GPIO_SPI
|
||||
|
||||
config BOARD_DIRECTORY
|
||||
string
|
||||
|
|
|
@ -3,6 +3,13 @@
|
|||
|
||||
#include <stdint.h> // uint8_t
|
||||
|
||||
struct gpio_out {
|
||||
uint32_t pin;
|
||||
};
|
||||
struct gpio_out gpio_out_setup(uint8_t pin, uint8_t val);
|
||||
void gpio_out_toggle(struct gpio_out g);
|
||||
void gpio_out_write(struct gpio_out g, uint8_t val);
|
||||
|
||||
struct gpio_adc {
|
||||
int fd;
|
||||
};
|
||||
|
@ -11,4 +18,11 @@ uint32_t gpio_adc_sample(struct gpio_adc g);
|
|||
uint16_t gpio_adc_read(struct gpio_adc g);
|
||||
void gpio_adc_cancel_sample(struct gpio_adc g);
|
||||
|
||||
struct spi_config {
|
||||
int fd;
|
||||
};
|
||||
struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate);
|
||||
void spi_transfer(struct spi_config config, uint8_t receive_data
|
||||
, uint8_t len, uint8_t *data);
|
||||
|
||||
#endif // gpio.h
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// Communicating with an SPI device via linux spidev
|
||||
// Very basic shift-register support via a Linux SPI device
|
||||
//
|
||||
// Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
|
||||
// Copyright (C) 2017-2018 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
|||
#include <stdio.h> // snprintf
|
||||
#include <unistd.h> // write
|
||||
#include "command.h" // DECL_COMMAND
|
||||
#include "gpio.h" // spi_setup
|
||||
#include "internal.h" // report_errno
|
||||
#include "sched.h" // shutdown
|
||||
|
||||
|
@ -47,22 +48,33 @@ spi_open(uint32_t bus, uint32_t dev)
|
|||
return fd;
|
||||
}
|
||||
|
||||
static void
|
||||
spi_write(int fd, char *data, int len)
|
||||
struct spi_config
|
||||
spi_setup(uint32_t bus, uint8_t mode, uint32_t rate)
|
||||
{
|
||||
int ret = write(fd, data, len);
|
||||
int bus_id = (bus >> 8) & 0xff, dev_id = bus & 0xff;
|
||||
int fd = spi_open(bus_id, dev_id);
|
||||
return (struct spi_config) { fd };
|
||||
}
|
||||
|
||||
void
|
||||
spi_transfer(struct spi_config config, uint8_t receive_data
|
||||
, uint8_t len, uint8_t *data)
|
||||
{
|
||||
int ret = write(config.fd, data, len);
|
||||
if (ret < 0) {
|
||||
report_errno("write spi", ret);
|
||||
shutdown("Unable to write to spi");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
command_send_spi(uint32_t *args)
|
||||
// Dummy versions of gpio_out functions
|
||||
struct gpio_out
|
||||
gpio_out_setup(uint8_t pin, uint8_t val)
|
||||
{
|
||||
shutdown("gpio_out_setup not supported");
|
||||
}
|
||||
|
||||
void
|
||||
gpio_out_write(struct gpio_out g, uint8_t val)
|
||||
{
|
||||
int fd = spi_open(args[0], args[1]);
|
||||
uint8_t len = args[2];
|
||||
char *msg = (void*)(size_t)args[3];
|
||||
spi_write(fd, msg, len);
|
||||
}
|
||||
DECL_COMMAND(command_send_spi, "send_spi bus=%u dev=%u msg=%*s");
|
||||
|
|
Loading…
Reference in New Issue