From b345a725118a5815077b8871f2703bfee7178049 Mon Sep 17 00:00:00 2001 From: Kizarm Date: Mon, 14 Oct 2024 17:09:39 +0200 Subject: [PATCH] refactoring usb --- V203/usb/cdc/Makefile | 4 +- V203/usb/cdc/ch32v203 | 1 + V203/usb/cdc/common | 1 + V203/usb/{cdc => }/ch32v203/CH32V20xxx.h | 0 V203/usb/{cdc => }/ch32v203/cdc_class.cpp | 0 V203/usb/{cdc => }/ch32v203/cdc_class.h | 0 V203/usb/{cdc => }/ch32v203/ch32v20x_usb.h | 0 V203/usb/{cdc => }/ch32v203/clang.mk | 0 V203/usb/{cdc => }/ch32v203/gcc.mk | 0 V203/usb/{cdc => }/ch32v203/gpio.h | 0 V203/usb/{cdc => }/ch32v203/print.cpp | 0 V203/usb/{cdc => }/ch32v203/print.h | 0 V203/usb/{cdc => }/ch32v203/script.ld | 0 V203/usb/{cdc => }/ch32v203/startup.cpp | 0 V203/usb/{cdc => }/ch32v203/system.cpp | 0 V203/usb/{cdc => }/ch32v203/system.h | 0 V203/usb/{cdc/ch32v203 => common}/baselayer.h | 0 .../{cdc/ch32v203 => common}/ctrlinterface.h | 0 V203/usb/{cdc/ch32v203 => common}/fifo.h | 0 V203/usb/common/print.cpp | 85 +++++++++++++++++++ V203/usb/common/print.h | 73 ++++++++++++++++ 21 files changed, 162 insertions(+), 2 deletions(-) create mode 120000 V203/usb/cdc/ch32v203 create mode 120000 V203/usb/cdc/common rename V203/usb/{cdc => }/ch32v203/CH32V20xxx.h (100%) rename V203/usb/{cdc => }/ch32v203/cdc_class.cpp (100%) rename V203/usb/{cdc => }/ch32v203/cdc_class.h (100%) rename V203/usb/{cdc => }/ch32v203/ch32v20x_usb.h (100%) rename V203/usb/{cdc => }/ch32v203/clang.mk (100%) rename V203/usb/{cdc => }/ch32v203/gcc.mk (100%) rename V203/usb/{cdc => }/ch32v203/gpio.h (100%) rename V203/usb/{cdc => }/ch32v203/print.cpp (100%) rename V203/usb/{cdc => }/ch32v203/print.h (100%) rename V203/usb/{cdc => }/ch32v203/script.ld (100%) rename V203/usb/{cdc => }/ch32v203/startup.cpp (100%) rename V203/usb/{cdc => }/ch32v203/system.cpp (100%) rename V203/usb/{cdc => }/ch32v203/system.h (100%) rename V203/usb/{cdc/ch32v203 => common}/baselayer.h (100%) rename V203/usb/{cdc/ch32v203 => common}/ctrlinterface.h (100%) rename V203/usb/{cdc/ch32v203 => common}/fifo.h (100%) create mode 100644 V203/usb/common/print.cpp create mode 100644 V203/usb/common/print.h diff --git a/V203/usb/cdc/Makefile b/V203/usb/cdc/Makefile index c351225..ee3a31d 100644 --- a/V203/usb/cdc/Makefile +++ b/V203/usb/cdc/Makefile @@ -4,7 +4,7 @@ TOOL ?= gcc PRJ = example -VPATH = . ./$(TARGET) +VPATH = . ./$(TARGET) ./common BLD = ./build/ DFLAGS = -d LFLAGS = -g @@ -12,7 +12,7 @@ LDLIBS = BFLAGS = --strip-unneeded CFLAGS = -MMD -Wall -Wno-parentheses -ggdb -fno-exceptions -ffunction-sections -fdata-sections -CFLAGS+= -I. -I./$(TARGET) +CFLAGS+= -I. -I./$(TARGET) -I./common DEL = rm -f # zdrojaky diff --git a/V203/usb/cdc/ch32v203 b/V203/usb/cdc/ch32v203 new file mode 120000 index 0000000..7650c85 --- /dev/null +++ b/V203/usb/cdc/ch32v203 @@ -0,0 +1 @@ +../ch32v203/ \ No newline at end of file diff --git a/V203/usb/cdc/common b/V203/usb/cdc/common new file mode 120000 index 0000000..8332399 --- /dev/null +++ b/V203/usb/cdc/common @@ -0,0 +1 @@ +../common/ \ No newline at end of file diff --git a/V203/usb/cdc/ch32v203/CH32V20xxx.h b/V203/usb/ch32v203/CH32V20xxx.h similarity index 100% rename from V203/usb/cdc/ch32v203/CH32V20xxx.h rename to V203/usb/ch32v203/CH32V20xxx.h diff --git a/V203/usb/cdc/ch32v203/cdc_class.cpp b/V203/usb/ch32v203/cdc_class.cpp similarity index 100% rename from V203/usb/cdc/ch32v203/cdc_class.cpp rename to V203/usb/ch32v203/cdc_class.cpp diff --git a/V203/usb/cdc/ch32v203/cdc_class.h b/V203/usb/ch32v203/cdc_class.h similarity index 100% rename from V203/usb/cdc/ch32v203/cdc_class.h rename to V203/usb/ch32v203/cdc_class.h diff --git a/V203/usb/cdc/ch32v203/ch32v20x_usb.h b/V203/usb/ch32v203/ch32v20x_usb.h similarity index 100% rename from V203/usb/cdc/ch32v203/ch32v20x_usb.h rename to V203/usb/ch32v203/ch32v20x_usb.h diff --git a/V203/usb/cdc/ch32v203/clang.mk b/V203/usb/ch32v203/clang.mk similarity index 100% rename from V203/usb/cdc/ch32v203/clang.mk rename to V203/usb/ch32v203/clang.mk diff --git a/V203/usb/cdc/ch32v203/gcc.mk b/V203/usb/ch32v203/gcc.mk similarity index 100% rename from V203/usb/cdc/ch32v203/gcc.mk rename to V203/usb/ch32v203/gcc.mk diff --git a/V203/usb/cdc/ch32v203/gpio.h b/V203/usb/ch32v203/gpio.h similarity index 100% rename from V203/usb/cdc/ch32v203/gpio.h rename to V203/usb/ch32v203/gpio.h diff --git a/V203/usb/cdc/ch32v203/print.cpp b/V203/usb/ch32v203/print.cpp similarity index 100% rename from V203/usb/cdc/ch32v203/print.cpp rename to V203/usb/ch32v203/print.cpp diff --git a/V203/usb/cdc/ch32v203/print.h b/V203/usb/ch32v203/print.h similarity index 100% rename from V203/usb/cdc/ch32v203/print.h rename to V203/usb/ch32v203/print.h diff --git a/V203/usb/cdc/ch32v203/script.ld b/V203/usb/ch32v203/script.ld similarity index 100% rename from V203/usb/cdc/ch32v203/script.ld rename to V203/usb/ch32v203/script.ld diff --git a/V203/usb/cdc/ch32v203/startup.cpp b/V203/usb/ch32v203/startup.cpp similarity index 100% rename from V203/usb/cdc/ch32v203/startup.cpp rename to V203/usb/ch32v203/startup.cpp diff --git a/V203/usb/cdc/ch32v203/system.cpp b/V203/usb/ch32v203/system.cpp similarity index 100% rename from V203/usb/cdc/ch32v203/system.cpp rename to V203/usb/ch32v203/system.cpp diff --git a/V203/usb/cdc/ch32v203/system.h b/V203/usb/ch32v203/system.h similarity index 100% rename from V203/usb/cdc/ch32v203/system.h rename to V203/usb/ch32v203/system.h diff --git a/V203/usb/cdc/ch32v203/baselayer.h b/V203/usb/common/baselayer.h similarity index 100% rename from V203/usb/cdc/ch32v203/baselayer.h rename to V203/usb/common/baselayer.h diff --git a/V203/usb/cdc/ch32v203/ctrlinterface.h b/V203/usb/common/ctrlinterface.h similarity index 100% rename from V203/usb/cdc/ch32v203/ctrlinterface.h rename to V203/usb/common/ctrlinterface.h diff --git a/V203/usb/cdc/ch32v203/fifo.h b/V203/usb/common/fifo.h similarity index 100% rename from V203/usb/cdc/ch32v203/fifo.h rename to V203/usb/common/fifo.h diff --git a/V203/usb/common/print.cpp b/V203/usb/common/print.cpp new file mode 100644 index 0000000..aa785b1 --- /dev/null +++ b/V203/usb/common/print.cpp @@ -0,0 +1,85 @@ +#include "print.h" + +#define sleep() + +static const char * hexStr = "0123456789ABCDEF"; +static const uint16_t numLen[] = {1, 32, 1, 11, 8, 0}; + +Print::Print (PrintBases b) : BaseLayer () { + base = b; +} +// Výstup blokujeme podle toho, co se vrací ze spodní vrstvy +uint32_t Print::BlockDown (const char * buf, uint32_t len) { + uint32_t n, ofs = 0, req = len; + for (;;) { + // spodní vrstva může vrátit i nulu, pokud je FIFO plné + n = BaseLayer::Down (buf + ofs, req); + ofs += n; // Posuneme ukazatel + req -= n; // Zmenšíme další požadavek + if (!req) break; + sleep(); // A klidně můžeme spát + } + return ofs; +} + +Print& Print::operator<< (const char * str) { + uint32_t i = 0; + while (str[i++]); // strlen + BlockDown (str, --i); + return *this; +} + +Print& Print::operator<< (const int num) { + uint32_t i = BUFLEN; + + if (base == DEC) { + unsigned int u; + if (num < 0) u = -num; + else u = num; + do { + // Knihovní div() je nevhodné - dělí 2x. + // Přímočaré a funkční řešení + uint32_t rem; + rem = u % (unsigned) DEC; // 1.dělení + u = u / (unsigned) DEC; // 2.dělení + buf [--i] = hexStr [rem]; + } while (u); + if (num < 0) buf [--i] = '-'; + } else { + uint32_t m = (1U << (uint32_t) base) - 1U; + uint32_t l = (uint32_t) numLen [(int) base]; + uint32_t u = (uint32_t) num; + for (unsigned n=0; n>= (unsigned) base; + } + if (base == BIN) buf [--i] = 'b'; + if (base == HEX) buf [--i] = 'x'; + buf [--i] = '0'; + } + BlockDown (buf+i, BUFLEN-i); + return *this; +} + +Print& Print::operator<< (const PrintBases num) { + base = num; + return *this; +} +void Print::out (const void * p, uint32_t l) { + const unsigned char* q = (const unsigned char*) p; + unsigned char uc; + uint32_t k, n = 0; + for (uint32_t i=0; i> 4; + buf[n++] = hexStr [k]; + k = uc & 0x0f; + buf[n++] = hexStr [k]; + buf[n++] = '>'; + } + buf[n++] = '\r'; + buf[n++] = '\n'; + BlockDown (buf, n); +} + diff --git a/V203/usb/common/print.h b/V203/usb/common/print.h new file mode 100644 index 0000000..f4489eb --- /dev/null +++ b/V203/usb/common/print.h @@ -0,0 +1,73 @@ +#ifndef PRINT_H +#define PRINT_H + +#include "baselayer.h" + +#define EOL "\r\n" +#define BUFLEN 64 + +/** + * @file + * @brief Něco jako ostream. + * + */ + +/// Základy pro zobrazení čísla. +enum PrintBases { + BIN=1, OCT=3, DEC=10, HEX=4 +}; + +/** + * @class Print + * @brief Třída pro výpisy do Down(). + * + * + * V main pak přibude jen definice instance této třídy + * @code + static Print print; + * @endcode + * a ukázka, jak se s tím pracuje: + * @snippet main.cpp Main print example + * Nic na tom není - operátor << má přetížení pro string, číslo a volbu formátu čísla (enum PrintBases). + * Výstup je pak do bufferu a aby nám to "neutíkalo", tedy aby se vypsalo vše, + * zavedeme blokování, vycházející z toho, že spodní třída vrátí jen počet bytů, + * které skutečně odeslala. Při čekání spí, takže nepoužívat v přerušení. + * @snippet src/print.cpp Block example + * Toto blokování pak není použito ve vrchních třídách stacku, + * blokovaná metoda je BlockDown(). Pokud bychom použili přímo Down(), blokování by pak + * používaly všechny vrstvy nad tím. A protože mohou Down() používat v přerušení, byl by problém. + * + * Metody pro výpisy jsou sice dost zjednodušené, ale zase to nezabere + * moc místa - pro ladění se to použít dá. Délka vypisovaného stringu není omezena + * délkou použitého buferu. + * + */ + +class Print : public BaseLayer { + public: + /// Konstruktor @param b Default decimální výpisy. + Print (PrintBases b = DEC); + /// Blokování výstupu + /// @param buf Ukazatel na data + /// @param len Délka přenášených dat + /// @return Počet přenesených bytů (rovno len) + uint32_t BlockDown (const char * buf, uint32_t len); + /// Výstup řetězce bytů + /// @param str Ukazatel na řetězec + /// @return Odkaz na tuto třídu kvůli řetězení. + Print & operator << (const char * str); + /// Výstup celého čísla podle base + /// @param num Číslo + /// @return Odkaz na tuto třídu kvůli řetězení. + Print & operator << (const int num); + /// Změna základu pro výstup čísla + /// @param num enum PrintBases + /// @return Odkaz na tuto třídu kvůli řetězení. + Print & operator << (const PrintBases num); + void out (const void* p, uint32_t l); + private: + PrintBases base; //!< Základ pro výstup čísla. + char buf[BUFLEN]; //!< Buffer pro výstup čísla. +}; + +#endif // PRINT_H