From 3093a80f2a987c5b85d0666e369270a1bb1852c8 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Sun, 29 Mar 2020 23:42:19 +0200 Subject: [PATCH] Spi: fix chip select handling --- src/application/spi/main.cc | 4 +++- src/application/st7789_lcd/main.cc | 9 +++++++-- src/platform/nrf52/spi.cc | 11 ++++++----- src/platform/nrf52/spi.h | 5 ++++- src/st7789.cc | 8 +------- src/st7789.h | 7 +++---- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/application/spi/main.cc b/src/application/spi/main.cc index 421af2b..952e271 100644 --- a/src/application/spi/main.cc +++ b/src/application/spi/main.cc @@ -9,7 +9,9 @@ const uint8_t buf[] = "Test"; int main(void) { Gpio led_1(17); - Spi spi_0(0, 2, 3, 4, 25); + Gpio lcd_chip_select(25); + + Spi spi_0(0, 2, 3, 4, lcd_chip_select); while(1) { delay_ms(200); diff --git a/src/application/st7789_lcd/main.cc b/src/application/st7789_lcd/main.cc index 5ddb003..9afbbab 100644 --- a/src/application/st7789_lcd/main.cc +++ b/src/application/st7789_lcd/main.cc @@ -3,17 +3,22 @@ #include "platform/hal.h" #include "st7789.h" +hal::Gpio led_1(17); + hal::Gpio lcd_reset(26); hal::Gpio lcd_data_command(18); hal::Gpio lcd_backlight(23); hal::Gpio lcd_chip_select(25); -hal::Spi lcd_spi(0, 2, 3, 4, 25); +hal::Spi lcd_spi(0, 2, 3, 4, lcd_chip_select); -St7789 lcd(lcd_spi, lcd_reset, lcd_data_command, lcd_backlight, lcd_chip_select); +St7789 lcd(lcd_spi, lcd_reset, lcd_data_command, lcd_backlight); int main(void) { + lcd.init(); + lcd.clear(0); while(true) { delay_ms(200); + led_1.toggle(); } } diff --git a/src/platform/nrf52/spi.cc b/src/platform/nrf52/spi.cc index 19cf22c..21a920d 100644 --- a/src/platform/nrf52/spi.cc +++ b/src/platform/nrf52/spi.cc @@ -12,7 +12,8 @@ extern "C" { using namespace platform::nrf52; -Spi::Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, uint32_t cs) +Spi::Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, interfaces::GpioInterface & cs) + : chip_select(cs) { assert(instance < 3); @@ -24,6 +25,8 @@ Spi::Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, uint32_t SPI_REGS = reinterpret_cast(NRF_SPI2_BASE); } + this->chip_select.set(); + SPI_REGS->ENABLE = 0; SPI_REGS->PSELSCK = sck; SPI_REGS->PSELMOSI = mosi; @@ -41,13 +44,11 @@ Spi::~Spi() void Spi::send(const uint8_t * buffer, uint32_t len) { - //FIXME: missing CS handling - + this->chip_select.clear(); for(unsigned int i = 0; i < len; i++) { this->transfer(buffer[i]); } - - //FIXME: missing CS handling + this->chip_select.set(); } void Spi::recv(uint8_t * buffer, uint32_t len) diff --git a/src/platform/nrf52/spi.h b/src/platform/nrf52/spi.h index d236f4e..51913c4 100644 --- a/src/platform/nrf52/spi.h +++ b/src/platform/nrf52/spi.h @@ -1,6 +1,7 @@ #ifndef __PLATFORM_NRF52_SPI_H__ #define __PLATFORM_NRF52_SPI_H__ +#include "gpio_interface.h" #include "spi_interface.h" namespace platform::nrf52 { @@ -8,13 +9,15 @@ namespace platform::nrf52 { class Spi : public interfaces::SpiInterface { public: - Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, uint32_t cs); + Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, interfaces::GpioInterface & cs); ~Spi(); void send(const uint8_t * buffer, uint32_t len) override; void recv(uint8_t * buffer, uint32_t len) override; private: uint32_t transfer(uint32_t); + + interfaces::GpioInterface & chip_select; }; } diff --git a/src/st7789.cc b/src/st7789.cc index 9468d14..d7f1059 100644 --- a/src/st7789.cc +++ b/src/st7789.cc @@ -6,27 +6,21 @@ St7789::St7789(interfaces::SpiInterface & spi_if, interfaces::GpioInterface & rst, interfaces::GpioInterface & dc, - interfaces::GpioInterface & bl, - interfaces::GpioInterface & cs) + interfaces::GpioInterface & bl) : spi(spi_if) , reset(rst) , data_command(dc) , backlight(bl) - , select(cs) { this->reset.set(); this->data_command.set(); this->backlight.set(); - this->select.clear(); // hard reset this->reset.clear(); delay_ms(100); this->reset.set(); delay_ms(100); - - this->init(); - this->clear(0x0000); } void St7789::init() diff --git a/src/st7789.h b/src/st7789.h index ba97bfd..7e9e94a 100644 --- a/src/st7789.h +++ b/src/st7789.h @@ -9,22 +9,21 @@ class St7789 { public: St7789(interfaces::SpiInterface &, - interfaces::GpioInterface &, interfaces::GpioInterface &, interfaces::GpioInterface &, interfaces::GpioInterface &); + void init(); + void clear(uint16_t Color); + private: interfaces::SpiInterface & spi; interfaces::GpioInterface & reset; interfaces::GpioInterface & data_command; interfaces::GpioInterface & backlight; - interfaces::GpioInterface & select; - void init(); void send_cmd(uint8_t); void send_data(uint8_t); - void clear(uint16_t Color); void set_windows(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end); };