2019-02-09 20:22:10 +03:00
|
|
|
// Software SPI emulation
|
|
|
|
//
|
|
|
|
// Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
|
|
|
#include "board/gpio.h" // gpio_out_setup
|
|
|
|
#include "basecmd.h" // oid_alloc
|
|
|
|
#include "command.h" // DECL_COMMAND
|
|
|
|
#include "sched.h" // sched_shutdown
|
2019-08-06 18:44:29 +03:00
|
|
|
#include "spicmds.h" // spidev_set_software_bus
|
2019-02-09 20:22:10 +03:00
|
|
|
|
|
|
|
struct spi_software {
|
|
|
|
struct gpio_in miso;
|
2019-08-06 18:44:29 +03:00
|
|
|
struct gpio_out mosi, sclk;
|
2019-02-09 20:22:10 +03:00
|
|
|
uint8_t mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2019-08-06 18:44:29 +03:00
|
|
|
command_spi_set_software_bus(uint32_t *args)
|
2019-02-09 20:22:10 +03:00
|
|
|
{
|
2019-08-06 18:44:29 +03:00
|
|
|
uint8_t mode = args[4];
|
2019-02-09 20:22:10 +03:00
|
|
|
if (mode > 3)
|
2019-08-06 18:44:29 +03:00
|
|
|
shutdown("Invalid spi config");
|
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]);
|
|
|
|
struct spi_software *ss = alloc_chunk(sizeof(*ss));
|
|
|
|
ss->miso = gpio_in_setup(args[1], 1);
|
|
|
|
ss->mosi = gpio_out_setup(args[2], 0);
|
|
|
|
ss->sclk = gpio_out_setup(args[3], 0);
|
|
|
|
ss->mode = mode;
|
|
|
|
spidev_set_software_bus(spi, ss);
|
2019-02-09 20:22:10 +03:00
|
|
|
}
|
2019-08-06 18:44:29 +03:00
|
|
|
DECL_COMMAND(command_spi_set_software_bus,
|
|
|
|
"spi_set_software_bus oid=%c miso_pin=%u mosi_pin=%u sclk_pin=%u"
|
2019-02-09 20:22:10 +03:00
|
|
|
" mode=%u rate=%u");
|
|
|
|
|
|
|
|
void
|
|
|
|
spi_software_prepare(struct spi_software *ss)
|
|
|
|
{
|
2019-05-22 18:40:30 +03:00
|
|
|
gpio_out_write(ss->sclk, ss->mode & 0x02);
|
2019-02-09 20:22:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|