2016-05-25 18:37:40 +03:00
|
|
|
// Commands for sending messages on an SPI bus
|
|
|
|
//
|
2019-08-06 18:44:29 +03:00
|
|
|
// Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 18:37:40 +03:00
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2018-05-04 19:27:52 +03:00
|
|
|
#include <string.h> // memcpy
|
2019-02-09 20:22:10 +03:00
|
|
|
#include "autoconf.h" // CONFIG_HAVE_GPIO_BITBANGING
|
2016-05-25 18:37:40 +03:00
|
|
|
#include "board/gpio.h" // gpio_out_write
|
2018-05-04 19:27:52 +03:00
|
|
|
#include "basecmd.h" // oid_alloc
|
2016-05-25 18:37:40 +03:00
|
|
|
#include "command.h" // DECL_COMMAND
|
2018-05-04 19:27:52 +03:00
|
|
|
#include "sched.h" // DECL_SHUTDOWN
|
2019-02-09 20:22:10 +03:00
|
|
|
#include "spi_software.h" // spi_software_setup
|
2018-05-04 21:16:21 +03:00
|
|
|
#include "spicmds.h" // spidev_transfer
|
2018-05-04 19:27:52 +03:00
|
|
|
|
|
|
|
struct spidev_s {
|
2019-02-09 20:22:10 +03:00
|
|
|
union {
|
|
|
|
struct spi_config spi_config;
|
|
|
|
struct spi_software *spi_software;
|
|
|
|
};
|
2018-05-04 19:27:52 +03:00
|
|
|
struct gpio_out pin;
|
|
|
|
uint8_t flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2019-08-06 18:44:29 +03:00
|
|
|
SF_HAVE_PIN = 1, SF_SOFTWARE = 2, SF_HARDWARE = 4,
|
2018-05-04 19:27:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
command_config_spi(uint32_t *args)
|
|
|
|
{
|
2019-08-06 18:44:29 +03:00
|
|
|
struct spidev_s *spi = oid_alloc(args[0], command_config_spi, sizeof(*spi));
|
|
|
|
spi->pin = gpio_out_setup(args[1], 1);
|
|
|
|
spi->flags |= SF_HAVE_PIN;
|
2018-05-04 19:27:52 +03:00
|
|
|
}
|
2019-08-06 18:44:29 +03:00
|
|
|
DECL_COMMAND(command_config_spi, "config_spi oid=%c pin=%u");
|
2018-05-04 19:27:52 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
command_config_spi_without_cs(uint32_t *args)
|
|
|
|
{
|
2019-08-06 18:44:29 +03:00
|
|
|
struct spidev_s *spi = oid_alloc(args[0], command_config_spi, sizeof(*spi));
|
|
|
|
}
|
|
|
|
DECL_COMMAND(command_config_spi_without_cs, "config_spi_without_cs oid=%c");
|
|
|
|
|
|
|
|
struct spidev_s *
|
|
|
|
spidev_oid_lookup(uint8_t oid)
|
|
|
|
{
|
|
|
|
return oid_lookup(oid, command_config_spi);
|
2018-05-04 19:27:52 +03:00
|
|
|
}
|
|
|
|
|
2019-02-09 20:22:10 +03:00
|
|
|
void
|
2019-08-06 18:44:29 +03:00
|
|
|
command_spi_set_bus(uint32_t *args)
|
2019-02-09 20:22:10 +03:00
|
|
|
{
|
2019-08-06 18:44:29 +03:00
|
|
|
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;
|
2019-02-09 20:22:10 +03:00
|
|
|
}
|
2019-08-06 18:44:29 +03:00
|
|
|
DECL_COMMAND(command_spi_set_bus,
|
|
|
|
"spi_set_bus oid=%c spi_bus=%u mode=%u rate=%u");
|
2019-02-09 20:22:10 +03:00
|
|
|
|
2019-08-06 18:44:29 +03:00
|
|
|
void
|
|
|
|
spidev_set_software_bus(struct spidev_s *spi, struct spi_software *ss)
|
2018-05-04 21:16:21 +03:00
|
|
|
{
|
2019-08-06 18:44:29 +03:00
|
|
|
if (spi->flags & (SF_SOFTWARE|SF_HARDWARE))
|
|
|
|
shutdown("Invalid spi config");
|
|
|
|
spi->spi_software = ss;
|
|
|
|
spi->flags |= SF_SOFTWARE;
|
2018-05-04 21:16:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-05-04 19:27:52 +03:00
|
|
|
spidev_transfer(struct spidev_s *spi, uint8_t receive_data
|
|
|
|
, uint8_t data_len, uint8_t *data)
|
|
|
|
{
|
2019-08-06 18:44:29 +03:00
|
|
|
if (!(spi->flags & (SF_SOFTWARE|SF_HARDWARE)))
|
|
|
|
// Not yet initialized
|
|
|
|
return;
|
|
|
|
|
2019-02-09 20:22:10 +03:00
|
|
|
if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE)
|
|
|
|
spi_software_prepare(spi->spi_software);
|
|
|
|
else
|
|
|
|
spi_prepare(spi->spi_config);
|
2018-07-12 02:43:20 +03:00
|
|
|
|
|
|
|
if (spi->flags & SF_HAVE_PIN)
|
2018-05-04 19:27:52 +03:00
|
|
|
gpio_out_write(spi->pin, 0);
|
2018-07-12 02:43:20 +03:00
|
|
|
|
2019-02-09 20:22:10 +03:00
|
|
|
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);
|
2018-07-12 02:43:20 +03:00
|
|
|
|
|
|
|
if (spi->flags & SF_HAVE_PIN)
|
2018-05-04 19:27:52 +03:00
|
|
|
gpio_out_write(spi->pin, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
command_spi_transfer(uint32_t *args)
|
|
|
|
{
|
|
|
|
uint8_t oid = args[0];
|
2019-08-06 18:44:29 +03:00
|
|
|
struct spidev_s *spi = spidev_oid_lookup(oid);
|
2018-05-04 19:27:52 +03:00
|
|
|
uint8_t data_len = args[1];
|
|
|
|
uint8_t *data = (void*)(size_t)args[2];
|
|
|
|
spidev_transfer(spi, 1, data_len, data);
|
|
|
|
sendf("spi_transfer_response oid=%c response=%*s", oid, data_len, data);
|
|
|
|
}
|
|
|
|
DECL_COMMAND(command_spi_transfer, "spi_transfer oid=%c data=%*s");
|
2016-05-25 18:37:40 +03:00
|
|
|
|
|
|
|
void
|
2018-05-04 19:27:52 +03:00
|
|
|
command_spi_send(uint32_t *args)
|
2016-05-25 18:37:40 +03:00
|
|
|
{
|
2019-08-06 18:44:29 +03:00
|
|
|
struct spidev_s *spi = spidev_oid_lookup(args[0]);
|
2018-05-04 19:27:52 +03:00
|
|
|
uint8_t data_len = args[1];
|
|
|
|
uint8_t *data = (void*)(size_t)args[2];
|
|
|
|
spidev_transfer(spi, 0, data_len, data);
|
|
|
|
}
|
|
|
|
DECL_COMMAND(command_spi_send, "spi_send oid=%c data=%*s");
|
|
|
|
|
2019-08-06 18:44:29 +03:00
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* 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");
|
|
|
|
|
2018-05-04 19:27:52 +03:00
|
|
|
void
|
|
|
|
spidev_shutdown(void)
|
|
|
|
{
|
|
|
|
// Cancel any transmissions that may be in progress
|
|
|
|
uint8_t oid;
|
|
|
|
struct spidev_s *spi;
|
|
|
|
foreach_oid(oid, spi, command_config_spi) {
|
|
|
|
if (spi->flags & SF_HAVE_PIN)
|
|
|
|
gpio_out_write(spi->pin, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send shutdown messages
|
2019-08-06 18:44:29 +03:00
|
|
|
struct spidev_shutdown_s *sd;
|
|
|
|
foreach_oid(oid, sd, command_config_spi_shutdown) {
|
|
|
|
spidev_transfer(sd->spi, 0, sd->shutdown_msg_len, sd->shutdown_msg);
|
2018-05-04 19:27:52 +03:00
|
|
|
}
|
2016-05-25 18:37:40 +03:00
|
|
|
}
|
2018-05-04 19:27:52 +03:00
|
|
|
DECL_SHUTDOWN(spidev_shutdown);
|