Spi: fix chip select handling

This commit is contained in:
Thomas Klaehn 2020-03-29 23:42:19 +02:00
parent f975a37bf3
commit 3093a80f2a
6 changed files with 24 additions and 20 deletions

View File

@ -9,7 +9,9 @@ const uint8_t buf[] = "Test";
int main(void) int main(void)
{ {
Gpio led_1(17); 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) { while(1) {
delay_ms(200); delay_ms(200);

View File

@ -3,17 +3,22 @@
#include "platform/hal.h" #include "platform/hal.h"
#include "st7789.h" #include "st7789.h"
hal::Gpio led_1(17);
hal::Gpio lcd_reset(26); hal::Gpio lcd_reset(26);
hal::Gpio lcd_data_command(18); hal::Gpio lcd_data_command(18);
hal::Gpio lcd_backlight(23); hal::Gpio lcd_backlight(23);
hal::Gpio lcd_chip_select(25); 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) int main(void)
{ {
lcd.init();
lcd.clear(0);
while(true) { while(true) {
delay_ms(200); delay_ms(200);
led_1.toggle();
} }
} }

View File

@ -12,7 +12,8 @@ extern "C" {
using namespace platform::nrf52; 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); 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_SPI_Type *>(NRF_SPI2_BASE); SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI2_BASE);
} }
this->chip_select.set();
SPI_REGS->ENABLE = 0; SPI_REGS->ENABLE = 0;
SPI_REGS->PSELSCK = sck; SPI_REGS->PSELSCK = sck;
SPI_REGS->PSELMOSI = mosi; SPI_REGS->PSELMOSI = mosi;
@ -41,13 +44,11 @@ Spi::~Spi()
void Spi::send(const uint8_t * buffer, uint32_t len) 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++) { for(unsigned int i = 0; i < len; i++) {
this->transfer(buffer[i]); this->transfer(buffer[i]);
} }
this->chip_select.set();
//FIXME: missing CS handling
} }
void Spi::recv(uint8_t * buffer, uint32_t len) void Spi::recv(uint8_t * buffer, uint32_t len)

View File

@ -1,6 +1,7 @@
#ifndef __PLATFORM_NRF52_SPI_H__ #ifndef __PLATFORM_NRF52_SPI_H__
#define __PLATFORM_NRF52_SPI_H__ #define __PLATFORM_NRF52_SPI_H__
#include "gpio_interface.h"
#include "spi_interface.h" #include "spi_interface.h"
namespace platform::nrf52 { namespace platform::nrf52 {
@ -8,13 +9,15 @@ namespace platform::nrf52 {
class Spi : public interfaces::SpiInterface class Spi : public interfaces::SpiInterface
{ {
public: 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(); ~Spi();
void send(const uint8_t * buffer, uint32_t len) override; void send(const uint8_t * buffer, uint32_t len) override;
void recv(uint8_t * buffer, uint32_t len) override; void recv(uint8_t * buffer, uint32_t len) override;
private: private:
uint32_t transfer(uint32_t); uint32_t transfer(uint32_t);
interfaces::GpioInterface & chip_select;
}; };
} }

View File

@ -6,27 +6,21 @@
St7789::St7789(interfaces::SpiInterface & spi_if, St7789::St7789(interfaces::SpiInterface & spi_if,
interfaces::GpioInterface & rst, interfaces::GpioInterface & rst,
interfaces::GpioInterface & dc, interfaces::GpioInterface & dc,
interfaces::GpioInterface & bl, interfaces::GpioInterface & bl)
interfaces::GpioInterface & cs)
: spi(spi_if) : spi(spi_if)
, reset(rst) , reset(rst)
, data_command(dc) , data_command(dc)
, backlight(bl) , backlight(bl)
, select(cs)
{ {
this->reset.set(); this->reset.set();
this->data_command.set(); this->data_command.set();
this->backlight.set(); this->backlight.set();
this->select.clear();
// hard reset // hard reset
this->reset.clear(); this->reset.clear();
delay_ms(100); delay_ms(100);
this->reset.set(); this->reset.set();
delay_ms(100); delay_ms(100);
this->init();
this->clear(0x0000);
} }
void St7789::init() void St7789::init()

View File

@ -9,22 +9,21 @@ class St7789
{ {
public: public:
St7789(interfaces::SpiInterface &, St7789(interfaces::SpiInterface &,
interfaces::GpioInterface &,
interfaces::GpioInterface &, interfaces::GpioInterface &,
interfaces::GpioInterface &, interfaces::GpioInterface &,
interfaces::GpioInterface &); interfaces::GpioInterface &);
void init();
void clear(uint16_t Color);
private: private:
interfaces::SpiInterface & spi; interfaces::SpiInterface & spi;
interfaces::GpioInterface & reset; interfaces::GpioInterface & reset;
interfaces::GpioInterface & data_command; interfaces::GpioInterface & data_command;
interfaces::GpioInterface & backlight; interfaces::GpioInterface & backlight;
interfaces::GpioInterface & select;
void init();
void send_cmd(uint8_t); void send_cmd(uint8_t);
void send_data(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); void set_windows(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end);
}; };