Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HD44780_SPI display fix #6148

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/spi_software.c
Expand Up @@ -9,13 +9,37 @@
#include "command.h" // DECL_COMMAND
#include "sched.h" // sched_shutdown
#include "spicmds.h" // spidev_set_software_bus
#include "board/irq.h" // irq_disable
#include "board/misc.h" // timer_from_us
#include "autoconf.h" // CONFIG_MACH_AVR

const uint32_t NSECS_IN_HZ = 1000000000;

struct spi_software {
struct gpio_in miso;
struct gpio_out mosi, sclk;
uint8_t mode;
uint32_t delay;
};


static uint32_t
nsecs_to_ticks(uint32_t ns)
{
return timer_from_us(ns * 1000) / 1000000;
}

static inline void
ndelay(uint32_t nsecs)
{
if (CONFIG_MACH_AVR)
// Slower MCUs don't require a delay
return;
uint32_t end = timer_read_time() + nsecs_to_ticks(nsecs);
while (timer_is_before(timer_read_time(), end))
irq_poll();
}

void
command_spi_set_software_bus(uint32_t *args)
{
Expand All @@ -29,6 +53,7 @@ command_spi_set_software_bus(uint32_t *args)
ss->mosi = gpio_out_setup(args[2], 0);
ss->sclk = gpio_out_setup(args[3], 0);
ss->mode = mode;
ss->delay = NSECS_IN_HZ * 1/2 * args[5]; //convert to nsecs
spidev_set_software_bus(spi, ss);
}
DECL_COMMAND(command_spi_set_software_bus,
Expand All @@ -54,17 +79,21 @@ spi_software_transfer(struct spi_software *ss, uint8_t receive_data
gpio_out_toggle(ss->sclk);
gpio_out_write(ss->mosi, outbuf & 0x80);
outbuf <<= 1;
ndelay(ss->delay);
gpio_out_toggle(ss->sclk);
ndelay(ss->delay);
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);
ndelay(ss->delay);
inbuf <<= 1;
inbuf |= gpio_in_read(ss->miso);
gpio_out_toggle(ss->sclk);
ndelay(ss->delay);
}
}

Expand Down