diff --git a/.gitignore b/.gitignore index 7226500..ab98bb0 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ V203/usb/scope/software/moc/* V203/usb/scope/software/obj/* V203/usb/scope/software/qrc_src.cpp V203/usb/scope/software/ui_mainwindow.h +V203/usb/spitest/* diff --git a/V203/usb/ch32v203/spiclass.cpp b/V203/usb/ch32v203/spiclass.cpp index 05885ec..e4fdcbf 100644 --- a/V203/usb/ch32v203/spiclass.cpp +++ b/V203/usb/ch32v203/spiclass.cpp @@ -82,14 +82,14 @@ void SpiClass::Init() { return r.R; }); SPI1.CTLR1.modify([](SPI1_Type::CTLR1_DEF & r) -> uint32_t { - r.B.CPHA = SET; - r.B.CPOL = SET; + r.B.CPHA = RESET; + r.B.CPOL = RESET; r.B.MSTR = SET; r.B.DFF = RESET; // 8 bit r.B.SSM = SET; r.B.SSI = SET; - r.B.LSBFIRST = RESET; - r.B.BR = FPCLK_64; // 4.5 MHz + r.B.LSBFIRST = SET; + r.B.BR = FPCLK_32; // 4.5 MHz return r.R; }); SPI1.CTLR2.modify([](SPI1_Type::CTLR2_DEF & r) -> uint32_t { @@ -106,6 +106,8 @@ bool SpiClass::send (const char * data, const unsigned int len) { complete = false; total = len > SPIBUFLEN ? SPIBUFLEN : len; memcpy (buffer, data, total); + DMA1.PADDR3.R = reinterpret_cast (& SPI1.DATAR); + DMA1.MADDR3.R = reinterpret_cast (buffer); DMA1.CNTR3.R = total; DMA1.CFGR3.B.EN = SET; SPI1.CTLR1.B.SPE = SET; diff --git a/V203/usb/ch32v203/spisim.cpp b/V203/usb/ch32v203/spisim.cpp new file mode 100644 index 0000000..b639b8f --- /dev/null +++ b/V203/usb/ch32v203/spisim.cpp @@ -0,0 +1,22 @@ +#include "spisim.h" +#include "system.h" + +SpiSim::SpiSim() noexcept : nss(GPIOA,4), sck(GPIOA,5), mosi(GPIOA,7) { + delay_init(); + nss << true; + sck << false; + mosi << false; +} +void SpiSim::outbyte(const uint8_t b) const { + nss << false; + for (unsigned n=0u; n<8; n++) { + const bool bit = (b & (1u << n)) ? true : false; + mosi << bit; + delay_us (2); + sck << true; + delay_us (4); + sck << false; + delay_us (2); + } + nss << true; +} diff --git a/V203/usb/ch32v203/spisim.h b/V203/usb/ch32v203/spisim.h new file mode 100644 index 0000000..ddb843b --- /dev/null +++ b/V203/usb/ch32v203/spisim.h @@ -0,0 +1,17 @@ +#ifndef SPISIM_H +#define SPISIM_H +#include "gpio.h" + +/** Původní třída SpiClass s DMA a hardware SPI se chová divně + * a je zbytečně složitá. Zde je potřeba odeslat 1 Byte na obvod + * 74595 a je jednodušší to udělat softwarově. Výstup je sice + * blokující, ale ten byte je dost krátká doba, aby to nerušilo. + */ +class SpiSim { + GpioClass nss, sck, mosi; + public: + explicit SpiSim () noexcept; + void outbyte (const uint8_t b) const; +}; + +#endif // SPISIM_H diff --git a/V203/usb/scope/firmware/Makefile b/V203/usb/scope/firmware/Makefile index 3b5399e..bd2c67c 100644 --- a/V203/usb/scope/firmware/Makefile +++ b/V203/usb/scope/firmware/Makefile @@ -17,7 +17,7 @@ DEL = rm -f # zdrojaky OBJS = main.o hack.o -OBJS += usb_desc.o cdc_class.o spiclass.o adcscope.o samplering.o +OBJS += usb_desc.o cdc_class.o spisim.o adcscope.o samplering.o include $(TARGET)/$(TOOL).mk BOBJS = $(addprefix $(BLD),$(OBJS)) diff --git a/V203/usb/scope/firmware/samplering.cpp b/V203/usb/scope/firmware/samplering.cpp index a8e490a..639d5bd 100644 --- a/V203/usb/scope/firmware/samplering.cpp +++ b/V203/usb/scope/firmware/samplering.cpp @@ -76,11 +76,11 @@ void SampleRing::CommandPass(const unsigned int cmd) { switch (dest) { case DEST_CHA: voltage.a = header.bits.cmd_value; - spi.send (voltage.common, 1); + spi.outbyte (voltage.common[0]); break; case DEST_CHB: voltage.b = header.bits.cmd_value; - spi.send (voltage.common, 1); + spi.outbyte (voltage.common[0]); break; case DEST_BASE: m_time_base_order = header.bits.cmd_value; diff --git a/V203/usb/scope/firmware/samplering.h b/V203/usb/scope/firmware/samplering.h index 53581bf..a92873a 100644 --- a/V203/usb/scope/firmware/samplering.h +++ b/V203/usb/scope/firmware/samplering.h @@ -3,7 +3,7 @@ #include #include "structures.h" #include "baselayer.h" -#include "spiclass.h" +#include "spisim.h" static constexpr unsigned RING_BIT = 10; static constexpr unsigned RING_LEN = 1u << RING_BIT; @@ -19,7 +19,7 @@ enum RCVD_STATUS { }; class SampleRing : public BaseLayer { - SpiClass spi; + SpiSim spi; ChannelVoltage voltage; [[gnu::aligned(4)]]DATA_BLOCK ring_buffer [RING_LEN]; TrigerSettings m_settings; @@ -38,7 +38,7 @@ class SampleRing : public BaseLayer { explicit SampleRing () noexcept : BaseLayer(), spi(), voltage(), m_settings(), m_head(0), m_tail(0), m_lenght(0), m_old_triger(false), m_trigered(false), m_finished(false), m_mode (TIME_BASE_TRIGERED), o_mode (TIME_BASE_TRIGERED), m_time_base_order(6u), - rcvd_counter(0u), rcvd_status (RCVD_IDLE) { spi.Init(); }; + rcvd_counter(0u), rcvd_status (RCVD_IDLE) { }; uint32_t Up (const char * data, const uint32_t len) override; void write (DATA_BLOCK const * data); void pass ();