From 40a22864df96599d76ffe7dd51453c4f1cf102bd Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Tue, 11 Mar 2025 16:17:43 +0300 Subject: [PATCH] fix Canon managing to new USB (CDC ACM) --- F1:F103/Canon_managing_device/Makefile.old | 151 ----- F1:F103/Canon_managing_device/canon.c | 20 +- .../canonmanage.creator.user | 52 +- .../Canon_managing_device/canonmanage.files | 4 + F1:F103/Canon_managing_device/canonusb.bin | Bin 12540 -> 10880 bytes F1:F103/Canon_managing_device/flash.c | 52 +- F1:F103/Canon_managing_device/main.c | 45 +- F1:F103/Canon_managing_device/proto.c | 143 +++-- F1:F103/Canon_managing_device/proto.h | 6 +- F1:F103/Canon_managing_device/ringbuffer.c | 197 +++++-- F1:F103/Canon_managing_device/ringbuffer.h | 34 +- F1:F103/Canon_managing_device/usb.c | 119 ---- F1:F103/Canon_managing_device/usb.h | 34 -- F1:F103/Canon_managing_device/usb_descr.c | 210 +++++++ F1:F103/Canon_managing_device/usb_descr.h | 68 +++ F1:F103/Canon_managing_device/usb_dev.c | 241 ++++++++ F1:F103/Canon_managing_device/usb_dev.h | 57 ++ F1:F103/Canon_managing_device/usb_lib.c | 551 +++++++++--------- F1:F103/Canon_managing_device/usb_lib.h | 348 +++++++---- F1:F103/Canon_managing_device/usbhw.c | 129 ---- F1:F103/Canon_managing_device/usbhw.h | 105 ---- F1:F103/Canon_managing_device/version.inc | 4 +- F3:F303/CANbus4BTA/kicad/stm32.kicad_prl | 1 - F3:F303/CANbus4BTA/kicad/stm32.kicad_pro | 1 + 24 files changed, 1412 insertions(+), 1160 deletions(-) delete mode 100644 F1:F103/Canon_managing_device/Makefile.old delete mode 100644 F1:F103/Canon_managing_device/usb.c delete mode 100644 F1:F103/Canon_managing_device/usb.h create mode 100644 F1:F103/Canon_managing_device/usb_descr.c create mode 100644 F1:F103/Canon_managing_device/usb_descr.h create mode 100644 F1:F103/Canon_managing_device/usb_dev.c create mode 100644 F1:F103/Canon_managing_device/usb_dev.h delete mode 100644 F1:F103/Canon_managing_device/usbhw.c delete mode 100644 F1:F103/Canon_managing_device/usbhw.h diff --git a/F1:F103/Canon_managing_device/Makefile.old b/F1:F103/Canon_managing_device/Makefile.old deleted file mode 100644 index 2fe3391..0000000 --- a/F1:F103/Canon_managing_device/Makefile.old +++ /dev/null @@ -1,151 +0,0 @@ -BINARY = canonusb -BOOTPORT ?= /dev/ttyUSB0 -BOOTSPEED ?= 115200 -# MCU FAMILY -FAMILY ?= F1 -# MCU code -MCU ?= F103x8 -# density (stm32f10x.h, lines 70-84) -DENSITY ?= LD -# change this linking script depending on particular MCU model, -LDSCRIPT ?= stm32f103x8.ld -# debug -#DEFS = -DEBUG - -# autoincremental version & build date -VERSION_FILE = version.inc -NEXTVER := $(shell expr $$(awk '/#define BUILD_NUMBER/' $(VERSION_FILE) | tr -cd "[0-9]") + 1) -BUILDDATE := $(shell date +%Y-%m-%d) - -INDEPENDENT_HEADERS= - -FP_FLAGS ?= -msoft-float -mfloat-abi=soft -ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd -ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS) - -############################################################################### -# Executables -#PREFIX ?= arm-none-eabi -# gcc from arm web site -PREFIX ?= /opt/bin/arm-none-eabi -TOOLCHLIB ?= /opt/arm-none-eabi/lib -RM := rm -f -RMDIR := rmdir -CC := $(PREFIX)-gcc -LD := $(PREFIX)-gcc -AR := $(PREFIX)-ar -AS := $(PREFIX)-as -SIZE := $(PREFIX)-size -OBJCOPY := $(PREFIX)-objcopy -OBJDUMP := $(PREFIX)-objdump -GDB := $(PREFIX)-gdb -STFLASH := $(shell which st-flash) -STBOOT := $(shell which stm32flash) -DFUUTIL := $(shell which dfu-util) - -############################################################################### -# Source files -OBJDIR := mk -SRC := $(wildcard *.c) -OBJS := $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o)) -STARTUP = $(OBJDIR)/startup.o -OBJS += $(STARTUP) -# dependencies: we need them to recompile files if their headers-dependencies changed -DEPS := $(OBJS:.o=.d) - -INC_DIR ?= ../inc - -INCLUDE := -I$(INC_DIR)/Fx -I$(INC_DIR)/cm -LIB_DIR := $(INC_DIR)/ld - -############################################################################### -# C flags -CFLAGS += -O2 -g -D__thumb2__=1 -MD -g -gdwarf-2 -CFLAGS += -Wall -Werror -Wextra -Wshadow -CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector -fshort-enums -CFLAGS += $(ARCH_FLAGS) - -############################################################################### -# Linker flags -LDFLAGS += -nostartfiles -nostdlib --static -Wl,--gc-sections -Wl,--print-memory-usage -LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map -#LDFLAGS += --static --gc-sections --print-memory-usage -LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB) -LDFLAGS += -T$(LDSCRIPT) - -############################################################################### -# Used libraries -LDLIBS += -lc -lgcc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) - -DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY) - -ELF := $(OBJDIR)/$(BINARY).elf -LIST := $(OBJDIR)/$(BINARY).list -BIN := $(BINARY).bin -HEX := $(BINARY).hex - -all: bin list size - -elf: $(ELF) -bin: $(BIN) -hex: $(HEX) -list: $(LIST) - -ifneq ($(MAKECMDGOALS),clean) --include $(DEPS) -endif - -$(OBJDIR): - mkdir $(OBJDIR) - -$(STARTUP): $(INC_DIR)/startup/vector.c - $(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $< - -$(VERSION_FILE): *.[ch] - @echo " Generate version: $(NEXTVER) for date $(BUILDDATE)" - @sed -i "s/#define BUILD_NUMBER.*/#define BUILD_NUMBER \"$(NEXTVER)\"/" $(VERSION_FILE) - @sed -i "s/#define BUILD_DATE.*/#define BUILD_DATE \"$(BUILDDATE)\"/" $(VERSION_FILE) - -$(OBJDIR)/proto.o: proto.c $(VERSION_FILE) - -$(OBJDIR)/%.o: %.c - @echo " CC $<" - $(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $< - -$(BIN): $(ELF) - @echo " OBJCOPY $(BIN)" - $(OBJCOPY) -Obinary $(ELF) $(BIN) - -$(HEX): $(ELF) - @echo " OBJCOPY $(HEX)" - $(OBJCOPY) -Oihex $(ELF) $(HEX) - -$(LIST): $(ELF) - @echo " OBJDUMP $(LIST)" - $(OBJDUMP) -S $(ELF) > $(LIST) - -$(ELF): $(OBJDIR) $(OBJS) - @echo " LD $(ELF)" - $(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF) - -size: $(ELF) - $(SIZE) $(ELF) - -clean: - @echo " CLEAN" - @rm -rf $(OBJDIR) 2>/dev/null || true - - -flash: $(BIN) - @echo " FLASH $(BIN)" - $(STFLASH) write $(BIN) 0x8000000 - -boot: $(BIN) - @echo " LOAD $(BIN) through bootloader" - $(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN) - -dfuboot: $(BIN) - @echo " LOAD $(BIN) THROUGH DFU" - $(DFUUTIL) -a0 -D $(BIN) -s 0x08000000 - -.PHONY: clean flash boot diff --git a/F1:F103/Canon_managing_device/canon.c b/F1:F103/Canon_managing_device/canon.c index b2c2e69..e94eaad 100644 --- a/F1:F103/Canon_managing_device/canon.c +++ b/F1:F103/Canon_managing_device/canon.c @@ -21,7 +21,7 @@ #include "hardware.h" #include "proto.h" #include "spi.h" -#include "usb.h" +#include "usb_dev.h" #define CU(a) ((const uint8_t*)a) @@ -99,7 +99,7 @@ typedef enum{ static f_movstate Fstopped(uint16_t *oldF){ uint16_t f = getfocval(); #ifdef EBUG - USB_send("Curpos="); USB_send(u2str(f)); USB_send("\n"); + USB_sendstr("Curpos="); USB_sendstr(u2str(f)); USB_sendstr("\n"); #endif if(BADFOCVAL == f) return F_ERR; if(*oldF == f){ @@ -355,10 +355,10 @@ int canon_diaphragm(char command){ int canon_focus(int16_t val){ if(!ready || inistate != INI_READY) return 1; if(val < 0){ - USB_send("Fsteps="); USB_send(u2str(getfocval())); USB_send("\n"); + USB_sendstr("Fsteps="); USB_sendstr(u2str(getfocval())); USB_sendstr("\n"); }else{ if(val > Fmax){ - USB_send("Fmax="); USB_send(u2str(Fmax)); USB_send("\n"); + USB_sendstr("Fmax="); USB_sendstr(u2str(Fmax)); USB_sendstr("\n"); return 2; } uint16_t curF = getfocval(); @@ -383,20 +383,20 @@ int canon_sendcmd(uint8_t cmd){ // acquire 16bit value int canon_asku16(uint8_t cmd){ if(!ready || !canon_read(cmd, 3)) return 1; - USB_send("par="); - USB_send(u2str((buf[1] << 8) | buf[2])); - USB_send("\n"); + USB_sendstr("par="); + USB_sendstr(u2str((buf[1] << 8) | buf[2])); + USB_sendstr("\n"); //canon_poll(); return 0; } int canon_getinfo(){ if(!ready || !canon_read(CANON_GETINFO, 6)) return 1; - USB_send("Info="); for(int i = 1; i < 7; ++i){ - USB_send(u2hexstr(buf[i])); USB_send(" "); + USB_sendstr("Info="); for(int i = 1; i < 7; ++i){ + USB_sendstr(u2hexstr(buf[i])); USB_sendstr(" "); } //canon_poll(); - USB_send("\n"); + USB_sendstr("\n"); return 0; } diff --git a/F1:F103/Canon_managing_device/canonmanage.creator.user b/F1:F103/Canon_managing_device/canonmanage.creator.user index 8b28a72..a20b5a3 100644 --- a/F1:F103/Canon_managing_device/canonmanage.creator.user +++ b/F1:F103/Canon_managing_device/canonmanage.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -8,7 +8,7 @@ ProjectExplorer.Project.ActiveTarget - 0 + 0 ProjectExplorer.Project.EditorSettings @@ -28,15 +28,17 @@ QmlJSGlobal - 2 + 2 KOI8-R false 4 false + 0 80 true true 1 + 0 false false false @@ -54,16 +56,31 @@ *.md, *.MD, Makefile true true + true ProjectExplorer.Project.PluginSettings + + true + false + true + true + true + true + + false + + + 0 + true true true Builtin.DefaultTidyAndClazy 4 + false @@ -78,9 +95,9 @@ Desktop Desktop {91347f2c-5221-46a7-80b1-0a054ca02f79} - 0 - 0 - 0 + 0 + 0 + 0 /home/eddy/Docs/SAO/ELECTRONICS/STM32/F1-srcs/USB_SPI @@ -91,7 +108,7 @@ true GenericProjectManager.GenericMakeStep - 1 + 1 Сборка Сборка ProjectExplorer.BuildSteps.Build @@ -104,7 +121,7 @@ true GenericProjectManager.GenericMakeStep - 1 + 1 Очистка Очистка ProjectExplorer.BuildSteps.Clean @@ -117,10 +134,10 @@ По умолчанию GenericProjectManager.GenericBuildConfiguration - 1 + 1 - 0 + 0 Развёртывание Развёртывание ProjectExplorer.BuildSteps.Deploy @@ -130,24 +147,29 @@ false ProjectExplorer.DefaultDeployConfiguration - 1 + 1 + true + true + true 2 + false + -e cpu-cycles --call-graph dwarf,4096 -F 250 + ProjectExplorer.CustomExecutableRunConfiguration - false + false true - false true - 1 + 1 ProjectExplorer.Project.TargetCount - 1 + 1 ProjectExplorer.Project.Updater.FileVersion diff --git a/F1:F103/Canon_managing_device/canonmanage.files b/F1:F103/Canon_managing_device/canonmanage.files index be314c9..9f67e3f 100644 --- a/F1:F103/Canon_managing_device/canonmanage.files +++ b/F1:F103/Canon_managing_device/canonmanage.files @@ -15,6 +15,10 @@ spi.c spi.h usb.c usb.h +usb_descr.c +usb_descr.h +usb_dev.c +usb_dev.h usb_lib.c usb_lib.h usbhw.c diff --git a/F1:F103/Canon_managing_device/canonusb.bin b/F1:F103/Canon_managing_device/canonusb.bin index 94352dc744f8c06ec840957a66db1ddb51f87710..a958adff1d3e1d65c0607a492d0e17a678e9cda8 100755 GIT binary patch delta 8021 zcmb_Bdt4OP)%VWKF3Uq%1Pw2BABe~*S;RC(^9#eWjIax0)FcK?GCWKcB^5Po)BKum z7FZ*Bn5c`5N|I_tlNcX7G*O9#Xws&!O?FjMA4#R3;Hu;?$ifVWJKwnrnl$bA>px|G zXYT9VbMHC#yzXVV*!hrvQjo4Y3Tg6aq{;aSpr8B^90H^}h(GpU=lqn$e`pK;vSsPKG0W85*)n5#!!qd2 z9mrg(&Q-_QvWg#cXg>^RYe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q)pc2r4P_fVwN~#aJl#^OW1| zF5r;%)0u;?F%54G!mRj12+(={K$A#F@y7@KFJs^~L=pl7y*R;SL=b34W*q#dY%&7F zv7%a1f{0j=K%mFHdR!qP87Isn7a})uqHX~9HO4`I!Cb^}pc5#IginsEp&R{+jnqVHywHxopBUXh Od!UWKztmtD{QMs%vqoS5 diff --git a/F1:F103/Canon_managing_device/flash.c b/F1:F103/Canon_managing_device/flash.c index 94b2603..266af23 100644 --- a/F1:F103/Canon_managing_device/flash.c +++ b/F1:F103/Canon_managing_device/flash.c @@ -25,7 +25,7 @@ #include "flash.h" #include "proto.h" -#include "usb.h" // printout +#include "usb_dev.h" // printout #include // memcpy extern const uint32_t __varsstart, _BLOCKSIZE; @@ -63,11 +63,11 @@ static int binarySearch(int r, const uint8_t *start, int stor_size){ while(r >= l){ int mid = l + (r - l) / 2; #ifdef EBUG - USB_send("mid/l/r="); - USB_send(u2str(mid)); USB_send("/"); - USB_send(u2str(l)); USB_send("/"); - USB_send(u2str(r)); USB_send("/"); - USB_send("\n"); + USB_sendstr("mid/l/r="); + USB_sendstr(u2str(mid)); USB_sendstr("/"); + USB_sendstr(u2str(l)); USB_sendstr("/"); + USB_sendstr(u2str(r)); USB_sendstr("/"); + USB_sendstr("\n"); #endif const uint8_t *s = start + mid * stor_size; if(*((const uint16_t*)s) == stor_size){ @@ -94,7 +94,7 @@ void flashstorage_init(){ maxCnum = flsz / sizeof(user_conf); } #ifdef EBUG - USB_send("INIT\n"); + USB_sendstr("INIT\n"); #endif // -1 if there's no data at all & flash is clear; maxnum-1 if flash is full currentconfidx = binarySearch((int)maxCnum-2, (const uint8_t*)Flash_Data, sizeof(user_conf)); @@ -102,7 +102,7 @@ void flashstorage_init(){ memcpy(&the_conf, &Flash_Data[currentconfidx], sizeof(user_conf)); } #ifdef EBUG - USB_send("currentconfidx="); USB_send(u2str(currentconfidx)); USB_send("\n"); + USB_sendstr("currentconfidx="); USB_sendstr(u2str(currentconfidx)); USB_sendstr("\n"); #endif } @@ -134,18 +134,18 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size *(volatile uint16_t*)(address + i) = data[i]; while(FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH; if(*(volatile uint16_t*)(address + i) != data[i]){ - USB_send("DON'T MATCH!\n"); + USB_sendstr("DON'T MATCH!\n"); ret = 1; break; } if(FLASH->SR & FLASH_SR_PGERR){ - USB_send("Prog err\n"); + USB_sendstr("Prog err\n"); ret = 1; // program error - meet not 0xffff break; } #ifdef EBUG - USB_send(u2str(stor_size)); USB_send("bytes stored @0x"); - USB_send(u2hexstr((uint32_t)(address + i))); USB_send("\n"); + USB_sendstr(u2str(stor_size)); USB_sendstr("bytes stored @0x"); + USB_sendstr(u2hexstr((uint32_t)(address + i))); USB_sendstr("\n"); #endif FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; } @@ -157,7 +157,7 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size static int erase_pageN(int N){ int ret = 0; #ifdef EBUG - USB_send("Erase page #"); USB_send(u2str(N)); USB_send("\n"); + USB_sendstr("Erase page #"); USB_sendstr(u2str(N)); USB_sendstr("\n"); #endif FLASH->AR = (uint32_t)Flash_Data + N*FLASH_blocksize; FLASH->CR |= FLASH_CR_STRT; @@ -180,12 +180,12 @@ int erase_storage(int npage){ } end = flsz / FLASH_blocksize; #ifdef EBUG - USB_send("FLASH_SIZE="); USB_send(u2str(FLASH_SIZE)); - USB_send("\nflsz="); USB_send(u2str(flsz)); - USB_send("\nend="); USB_send(u2str(end)); - USB_send("\ncurrentconfidx="); USB_send(u2str(currentconfidx)); - USB_send("\nmaxCnum="); USB_send(u2str(maxCnum)); - USB_send("\n"); + USB_sendstr("FLASH_SIZE="); USB_sendstr(u2str(FLASH_SIZE)); + USB_sendstr("\nflsz="); USB_sendstr(u2str(flsz)); + USB_sendstr("\nend="); USB_sendstr(u2str(end)); + USB_sendstr("\ncurrentconfidx="); USB_sendstr(u2str(currentconfidx)); + USB_sendstr("\nmaxCnum="); USB_sendstr(u2str(maxCnum)); + USB_sendstr("\n"); #endif if(end == 0 || end >= FLASH_SIZE) return 1; if(npage > -1){ // erase only one page @@ -211,10 +211,12 @@ int erase_storage(int npage){ } void dump_userconf(){ - USB_send("userconf_sz="); printu(the_conf.userconf_sz); - USB_send("\ncurrentconfidx="); USB_send(u2str(currentconfidx)); - USB_send("\nCAN_speed="); printu(the_conf.canspeed); - USB_send("\nCAN_ID="); printu(the_conf.canID); - USB_send("\nautoinit="); printu(the_conf.autoinit); - USB_send("\n"); + USB_sendstr("userconf_sz="); printu(the_conf.userconf_sz); + USB_sendstr("\ncurrentconfidx="); + if(currentconfidx == -1) USB_sendstr("-1"); + else USB_sendstr(u2str(currentconfidx)); + USB_sendstr("\nCAN_speed="); printu(the_conf.canspeed); + USB_sendstr("\nCAN_ID="); printu(the_conf.canID); + USB_sendstr("\nautoinit="); printu(the_conf.autoinit); + USB_sendstr("\n"); } diff --git a/F1:F103/Canon_managing_device/main.c b/F1:F103/Canon_managing_device/main.c index b2b84b1..f7ff278 100644 --- a/F1:F103/Canon_managing_device/main.c +++ b/F1:F103/Canon_managing_device/main.c @@ -22,7 +22,7 @@ #include "hardware.h" #include "proto.h" #include "spi.h" -#include "usb.h" +#include "usb_dev.h" #define USBBUFSZ 127 @@ -32,47 +32,30 @@ void sys_tick_handler(void){ ++Tms; } -// usb getline -char *get_USB(){ - static char tmpbuf[USBBUFSZ+1], *curptr = tmpbuf; - static int rest = USBBUFSZ; - uint8_t x = USB_receive(curptr); - if(!x) return NULL; - curptr[x] = 0; - if(curptr[x-1] == '\n'){ - curptr = tmpbuf; - rest = USBBUFSZ; - return tmpbuf; - } - curptr += x; rest -= x; - if(rest <= 0){ // buffer overflow - curptr = tmpbuf; - rest = USBBUFSZ; - } - return NULL; -} - int main(void){ + char inbuff[256]; sysreset(); StartHSE(); SysTick_Config(72000); flashstorage_init(); hw_setup(); - if(ISUSB()) USB_setup(); - else CAN_setup(the_conf.canspeed, the_conf.canID); + if(ISUSB()){ + USBPU_OFF(); + USB_setup(); + USBPU_ON(); + }else CAN_setup(the_conf.canspeed, the_conf.canID); spi_setup(); uint32_t SPIctr = Tms; while(1){ IWDG->KR = IWDG_REFRESH; - char *txt = NULL; - usb_proc(); - txt = get_USB(); - const char *ans = parse_cmd(txt); // call it even for NULL (if `flood` is running) - if(ans) USB_send(ans); - if(Tms - SPIctr > CANONPROC_INTERVAL){ // not more than once per 10ms - SPIctr = Tms; - canon_proc(); + int l = USB_receivestr(inbuff, 255); + if(l > 0){ + parse_cmd(inbuff); // call it even for NULL (if `flood` is running) + if(Tms - SPIctr > CANONPROC_INTERVAL){ // not more than once per 10ms + SPIctr = Tms; + canon_proc(); + } } } } diff --git a/F1:F103/Canon_managing_device/proto.c b/F1:F103/Canon_managing_device/proto.c index a84ad74..9559481 100644 --- a/F1:F103/Canon_managing_device/proto.c +++ b/F1:F103/Canon_managing_device/proto.c @@ -23,7 +23,7 @@ #include "hardware.h" #include "proto.h" #include "spi.h" -#include "usb.h" +#include "usb_dev.h" #include "version.inc" static const char *OK = "OK", *FAIL = "FAIL"; @@ -185,20 +185,6 @@ const char* helpmsg = "X - save current config to flash\n" ; -#define STBUFSZ 255 -static char stbuf[STBUFSZ+1], *bptr = NULL; -static int blen = 0; -static void initbuf(){bptr = stbuf; blen = STBUFSZ; *bptr = 0;} -#define newline() do{if(blen){ *bptr++ = '\n'; *bptr = 0; --blen; }}while(0) -static void add2buf(const char *s){ - while(blen && *s){ - *bptr++ = *s++; - --blen; - } - *bptr = 0; -} - - #define SPIBUFSZ (64) // buffer for SPI sending static uint8_t spibuf[SPIBUFSZ]; @@ -212,7 +198,7 @@ static int initspibuf(const char *buf){ if(buf == nxt) break; buf = nxt; if(D > 0xff){ - USB_send("Number should be from 0 to 0xff\n"); + USB_sendstr("Number should be from 0 to 0xff\n"); return 0; } spibuf[spibuflen++] = (uint8_t)D; @@ -224,24 +210,23 @@ static void sendspibuf(){ uint8_t buf[SPIBUFSZ]; memcpy(buf, spibuf, spibuflen); if(spibuflen == SPI_transmit((uint8_t*)buf, (uint8_t)spibuflen)){ - USB_send("Got SPI answer: "); + USB_sendstr("Got SPI answer: "); for(int i = 0; i < spibuflen; ++i){ - if(i) USB_send(", "); - USB_send(u2hexstr(buf[i])); + if(i) USB_sendstr(", "); + USB_sendstr(u2hexstr(buf[i])); } - USB_send("\n"); - }else USB_send("Failed to send SPI buffer\n"); + USB_sendstr("\n"); + }else USB_sendstr("Failed to send SPI buffer\n"); } static void errw(int e){ if(e){ - add2buf("Error with code "); - add2buf(u2str(e)); - if(e == 1) add2buf(" (busy or need initialization)"); - }else add2buf(OK); + USB_sendstr("Error with code "); + USB_sendstr(u2str(e)); + if(e == 1) USB_sendstr(" (busy or need initialization)"); + }else USB_sendstr(OK); } -extern uint8_t usbON; const char *connmsgs[LENS_S_AMOUNT+1] = { [LENS_DISCONNECTED] = "disconnected", [LENS_SLEEPING] = "sleeping, need init", @@ -262,15 +247,14 @@ const char *inimsgs[INI_S_AMOUNT+1] = { [INI_ERR] = "error in init procedure", [INI_S_AMOUNT] = "wrong state" }; -const char *parse_cmd(const char *buf){ +void parse_cmd(const char *buf){ static uint32_t lastFloodTime = 0; if(lastFloodTime && (Tms - lastFloodTime > FLOODING_INTERVAL)){ sendspibuf(); lastFloodTime = Tms ? Tms : 1; } - if(!buf || *buf == 0) return NULL; + if(!buf || *buf == 0) return; lastFloodTime= FALSE; - initbuf(); if(buf[1] == '\n' || !buf[1]){ // one symbol commands switch(*buf){ case 'a': @@ -296,67 +280,67 @@ const char *parse_cmd(const char *buf){ errw(canon_asku16(CANON_GETREG)); break; case 'E': - if(erase_storage(-1)) add2buf(FAIL); - add2buf(OK); + if(erase_storage(-1)) USB_sendstr(FAIL); + USB_sendstr(OK); break; case 'F': // just watch SPI->CR1 value - add2buf("SPI1->CR1="); add2buf(u2hexstr(SPI_CR1)); + USB_sendstr("SPI1->CR1="); USB_sendstr(u2hexstr(SPI_CR1)); break; case 'G': - add2buf("SPI "); + USB_sendstr("SPI "); switch(SPI_status){ case SPI_NOTREADY: - add2buf("not ready"); + USB_sendstr("not ready"); break; case SPI_READY: - add2buf("ready"); + USB_sendstr("ready"); break; case SPI_BUSY: - add2buf("busy"); + USB_sendstr("busy"); break; default: - add2buf("unknown"); + USB_sendstr("unknown"); } - add2buf("\nstate="); + USB_sendstr("\nstate="); uint16_t s = canon_getstate(); uint8_t idx = s & 0xff; if(idx > LENS_S_AMOUNT) idx = LENS_S_AMOUNT; - add2buf(connmsgs[idx]); + USB_sendstr(connmsgs[idx]); idx = s >> 8; - add2buf("\ninistate="); + USB_sendstr("\ninistate="); if(idx > INI_S_AMOUNT) idx = INI_S_AMOUNT; - add2buf(inimsgs[idx]); + USB_sendstr(inimsgs[idx]); break; case 'h': errw(canon_sendcmd(CANON_FOCBYHANDS)); break; case 'I': - USB_send("Reinit SPI\n"); + USB_sendstr("Reinit SPI\n"); spi_setup(); canon_init(); - return NULL; + return; break; case 'P': dump_userconf(); - return NULL; + return; break; case 'R': - USB_send("Soft reset\n"); + USB_sendstr("Soft reset\n"); NVIC_SystemReset(); break; case 'T': - add2buf("Tms="); - add2buf(u2str(Tms)); + USB_sendstr("Tms="); + USB_sendstr(u2str(Tms)); break; case 'X': - if(store_userconf()) add2buf(FAIL); - else add2buf(OK); + if(store_userconf()) USB_sendstr(FAIL); + else USB_sendstr(OK); break; default: - return helpmsg; + USB_sendstr(helpmsg); } newline(); - return stbuf; + return; } uint32_t D = 0; int16_t neg; @@ -367,8 +351,8 @@ const char *parse_cmd(const char *buf){ neg = 1; if(*buf == '-'){ ++buf; neg = -1; } nxt = getnum(buf, &D); - if(nxt == buf) add2buf("Need number"); - else if(D > 0x7fff) add2buf("From -0x7fff to 0x7fff"); + if(nxt == buf) USB_sendstr("Need number"); + else if(D > 0x7fff) USB_sendstr("From -0x7fff to 0x7fff"); else errw(canon_focus(neg * (int16_t)D)); break; case 'A': @@ -377,7 +361,7 @@ const char *parse_cmd(const char *buf){ if(D) the_conf.autoinit = 1; else the_conf.autoinit = 0; } - USB_send("autoinit="); USB_send(u2str(the_conf.autoinit)); USB_send("\n"); + USB_sendstr("autoinit="); USB_sendstr(u2str(the_conf.autoinit)); USB_sendstr("\n"); break; case 'd': nxt = omit_spaces(buf); @@ -388,26 +372,26 @@ const char *parse_cmd(const char *buf){ neg = 1; if(*buf == '-'){ ++buf; neg = -1; } nxt = getnum(buf, &D); - if(nxt == buf) add2buf("Need number"); - else if(D > 0x7fff) add2buf("From -0x7fff to 0x7fff"); + if(nxt == buf) USB_sendstr("Need number"); + else if(D > 0x7fff) USB_sendstr("From -0x7fff to 0x7fff"); else{ - if(canon_writeu16(CANON_FOCMOVE, neg * (int16_t)D)) add2buf(OK); - else add2buf(FAIL); + if(canon_writeu16(CANON_FOCMOVE, neg * (int16_t)D)) USB_sendstr(OK); + else USB_sendstr(FAIL); } break; case 'C': nxt = getnum(buf, &D); if(nxt != buf && D >= 25 && D <= 3000){ the_conf.canspeed = D; - add2buf("CAN_speed="); add2buf(u2str(the_conf.canspeed)); - }else add2buf(FAIL); + USB_sendstr("CAN_speed="); USB_sendstr(u2str(the_conf.canspeed)); + }else USB_sendstr(FAIL); break; case 'D': nxt = getnum(buf, &D); if(nxt != buf && D < 0x800){ the_conf.canID = D; - add2buf("CAN_ID="); add2buf(u2str(the_conf.canID)); - }else add2buf(FAIL); + USB_sendstr("CAN_ID="); USB_sendstr(u2str(the_conf.canID)); + }else USB_sendstr(FAIL); break; case 'F': // SPI flags nxt = omit_spaces(buf); @@ -415,7 +399,10 @@ const char *parse_cmd(const char *buf){ if(*nxt && *nxt != '\n'){ buf = nxt + 1; nxt = getnum(buf, &D); - if(buf == nxt || D > 7) return helpmsg; + if(buf == nxt || D > 7){ + USB_sendstr(helpmsg); + return; + } } switch(c){ case 'b': @@ -435,39 +422,39 @@ const char *parse_cmd(const char *buf){ else SPI_CR1 &= ~SPI_CR1_CPOL; break; default: - return helpmsg; + USB_sendstr(helpmsg); + return; } - add2buf("SPI_CR1="); add2buf(u2hexstr(SPI_CR1)); + USB_sendstr("SPI_CR1="); USB_sendstr(u2hexstr(SPI_CR1)); break; case 'L': if(0 == initspibuf(buf)){ - USB_send("Enter data bytes\n"); - return NULL; + USB_sendstr("Enter data bytes\n"); + return; } - USB_send("OK, activated\n"); + USB_sendstr("OK, activated\n"); sendspibuf(); lastFloodTime = Tms ? Tms : 1; - return NULL; + return; break; case 'S': // use stbuf here to store user data if(0 == initspibuf(buf)){ - USB_send("Enter data bytes\n"); - return NULL; + USB_sendstr("Enter data bytes\n"); + return; } - USB_send("Send: "); + USB_sendstr("Send: "); for(int i = 0; i < spibuflen; ++i){ - if(i) USB_send(", "); - USB_send(u2hexstr(spibuf[i])); + if(i) USB_sendstr(", "); + USB_sendstr(u2hexstr(spibuf[i])); } - USB_send("\n"); + USB_sendstr("\n"); sendspibuf(); - return NULL; + return; break; default: - return --buf; + return; } newline(); - return stbuf; } diff --git a/F1:F103/Canon_managing_device/proto.h b/F1:F103/Canon_managing_device/proto.h index 9991b63..efd66f0 100644 --- a/F1:F103/Canon_managing_device/proto.h +++ b/F1:F103/Canon_managing_device/proto.h @@ -22,8 +22,8 @@ #include -#define printu(x) do{USB_send(u2str(x));}while(0) -#define printuhex(x) do{USB_send(uhex2str(x));}while(0) +#define printu(x) do{USB_sendstr(u2str(x));}while(0) +#define printuhex(x) do{USB_sendstr(uhex2str(x));}while(0) #ifdef EBUG #define DBG(x) do{USB_send(x); USB_send("\n");}while(0) @@ -31,7 +31,7 @@ #define DBG(x) #endif -const char *parse_cmd(const char *buf); +void parse_cmd(const char *buf); char *omit_spaces(const char *buf); char *getnum(const char *buf, uint32_t *N); char *u2str(uint32_t val); diff --git a/F1:F103/Canon_managing_device/ringbuffer.c b/F1:F103/Canon_managing_device/ringbuffer.c index 972d6a0..b0ffe8e 100644 --- a/F1:F103/Canon_managing_device/ringbuffer.c +++ b/F1:F103/Canon_managing_device/ringbuffer.c @@ -1,6 +1,5 @@ /* - * This file is part of the canonmanage project. - * Copyright 2022 Edward V. Emelianov . + * Copyright 2023 Edward V. Emelianov . * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,62 +15,152 @@ * along with this program. If not, see . */ -#include - #include "ringbuffer.h" -// ring buffer -static char ringbuffer[RBSIZE]; -// head - position of first data byte -// tail - position of last data byte + 1 -// head == tail - empty! So, buffer can't store more than RBSIZE-1 bytes of data! -static volatile int head = 0, tail = 0; - -static int datalen(){ - if(tail >= head) return (tail - head); - else return (RBSIZE - head + tail); -} -static int restlen(){ - return (RBSIZE - 1 - datalen()); +static int datalen(ringbuffer *b){ + if(b->tail >= b->head) return (b->tail - b->head); + else return (b->length - b->head + b->tail); } -static void mcpy(char *targ, const char *src, int l){ - while(l--) *targ++ = *src++; -} - -TRUE_INLINE void incr(volatile int *what, int n){ - *what += n; - if(*what >= RBSIZE) *what -= RBSIZE; -} - -int RB_read(char s[BLOCKSIZE]){ - int l = datalen(); - if(!l) return 0; - if(l > BLOCKSIZE) l = BLOCKSIZE; - int _1st = RBSIZE - head; - if(_1st > l) _1st = l; - if(_1st > BLOCKSIZE) _1st = BLOCKSIZE; - mcpy(s, ringbuffer+head, _1st); - if(_1st < BLOCKSIZE && l > _1st){ - mcpy(s+_1st, ringbuffer, l-_1st); - incr(&head, l); - return l; - } - incr(&head ,_1st); - return _1st; -} - -int RB_write(const char *str, int l){ - int r = restlen(); - if(l > r) l = r; - if(!l) return 0; - int _1st = RBSIZE - tail; - if(_1st > l) _1st = l; - mcpy(ringbuffer+tail, str, _1st); - if(_1st < l){ // add another piece from start - mcpy(ringbuffer, str+_1st, l-_1st); - } - incr(&tail, l); +// stored data length +int RB_datalen(ringbuffer *b){ + if(b->busy) return -1; + b->busy = 1; + int l = datalen(b); + b->busy = 0; return l; } +static int hasbyte(ringbuffer *b, uint8_t byte){ + if(b->head == b->tail) return -1; // no data in buffer + int startidx = b->head; + if(b->head > b->tail){ // + for(int found = b->head; found < b->length; ++found) + if(b->data[found] == byte) return found; + startidx = 0; + } + for(int found = startidx; found < b->tail; ++found) + if(b->data[found] == byte) return found; + return -1; +} + +/** + * @brief RB_hasbyte - check if buffer has given byte stored + * @param b - buffer + * @param byte - byte to find + * @return index if found, -1 if none or busy + */ +int RB_hasbyte(ringbuffer *b, uint8_t byte){ + if(b->busy) return -1; + b->busy = 1; + int ret = hasbyte(b, byte); + b->busy = 0; + return ret; +} + +// poor memcpy +static void mcpy(uint8_t *targ, const uint8_t *src, int l){ + while(l--) *targ++ = *src++; +} + +// increment head or tail +TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){ + *what += n; + if(*what >= b->length) *what -= b->length; +} + +static int read(ringbuffer *b, uint8_t *s, int len){ + int l = datalen(b); + if(!l) return 0; + if(l > len) l = len; + int _1st = b->length - b->head; + if(_1st > l) _1st = l; + if(_1st > len) _1st = len; + mcpy(s, b->data + b->head, _1st); + if(_1st < len && l > _1st){ + mcpy(s+_1st, b->data, l - _1st); + incr(b, &b->head, l); + return l; + } + incr(b, &b->head, _1st); + return _1st; +} + +/** + * @brief RB_read - read data from ringbuffer + * @param b - buffer + * @param s - array to write data + * @param len - max len of `s` + * @return bytes read or -1 if busy + */ +int RB_read(ringbuffer *b, uint8_t *s, int len){ + if(b->busy) return -1; + b->busy = 1; + int r = read(b, s, len); + b->busy = 0; + return r; +} + +static int readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){ + int idx = hasbyte(b, byte); + if(idx < 0) return 0; + int partlen = idx + 1 - b->head; + // now calculate length of new data portion + if(idx < b->head) partlen += b->length; + if(partlen > len) return -read(b, s, len); + return read(b, s, partlen); +} + +/** + * @brief RB_readto fill array `s` with data until byte `byte` (with it) + * @param b - ringbuffer + * @param byte - check byte + * @param s - buffer to write data + * @param len - length of `s` + * @return amount of bytes written (negative, if lenbusy) return -1; + b->busy = 1; + int n = readto(b, byte, s, len); + b->busy = 0; + return n; +} + +static int write(ringbuffer *b, const uint8_t *str, int l){ + int r = b->length - 1 - datalen(b); // rest length + if(l > r || !l) return 0; + int _1st = b->length - b->tail; + if(_1st > l) _1st = l; + mcpy(b->data + b->tail, str, _1st); + if(_1st < l){ // add another piece from start + mcpy(b->data, str+_1st, l-_1st); + } + incr(b, &b->tail, l); + return l; +} + +/** + * @brief RB_write - write some data to ringbuffer + * @param b - buffer + * @param str - data + * @param l - length + * @return amount of bytes written or -1 if busy + */ +int RB_write(ringbuffer *b, const uint8_t *str, int l){ + if(b->busy) return -1; + b->busy = 1; + int w = write(b, str, l); + b->busy = 0; + return w; +} + +// just delete all information in buffer `b` +int RB_clearbuf(ringbuffer *b){ + if(b->busy) return -1; + b->busy = 1; + b->head = 0; + b->tail = 0; + b->busy = 0; + return 1; +} diff --git a/F1:F103/Canon_managing_device/ringbuffer.h b/F1:F103/Canon_managing_device/ringbuffer.h index 5acc33b..ed2cf95 100644 --- a/F1:F103/Canon_managing_device/ringbuffer.h +++ b/F1:F103/Canon_managing_device/ringbuffer.h @@ -1,6 +1,5 @@ /* - * This file is part of the canonmanage project. - * Copyright 2022 Edward V. Emelianov . + * Copyright 2023 Edward V. Emelianov . * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,17 +16,26 @@ */ #pragma once -#ifndef RINGBUFFER_H__ -#define RINGBUFFER_H__ -#include "usbhw.h" // for USB_TXBUFSZ +#if defined STM32F0 +#include +#elif defined STM32F1 +#include +#elif defined STM32F3 +#include +#endif -// ring buffer size in bytes -#define RBSIZE (512) -// max reading portion size -#define BLOCKSIZE (USB_TXBUFSZ) +typedef struct{ + uint8_t *data; // data buffer + const int length; // its length + int head; // head index + int tail; // tail index + volatile int busy; // == TRUE if buffer is busy now +} ringbuffer; -int RB_read(char s[BLOCKSIZE]); -int RB_write(const char *str, int l); - -#endif // RINGBUFFER_H__ +int RB_read(ringbuffer *b, uint8_t *s, int len); +int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len); +int RB_hasbyte(ringbuffer *b, uint8_t byte); +int RB_write(ringbuffer *b, const uint8_t *str, int l); +int RB_datalen(ringbuffer *b); +int RB_clearbuf(ringbuffer *b); diff --git a/F1:F103/Canon_managing_device/usb.c b/F1:F103/Canon_managing_device/usb.c deleted file mode 100644 index d474a84..0000000 --- a/F1:F103/Canon_managing_device/usb.c +++ /dev/null @@ -1,119 +0,0 @@ -/* - * This file is part of the MLX90640 project. - * Copyright 2022 Edward V. Emelianov . - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "ringbuffer.h" -#include "usb.h" -#include "usb_lib.h" - -static char usbbuff[USB_TXBUFSZ]; // temporary buffer for sending data -volatile uint8_t tx_succesfull = 1; -static volatile uint8_t rxNE = 0; - -void send_next(){ - //if(!tx_succesfull) return; - static int lastdsz = 0; - int buflen = RB_read(usbbuff); - if(!buflen){ - if(lastdsz == 64) EP_Write(3, NULL, 0); // send ZLP after 64 bits packet when nothing more to send - lastdsz = 0; - return; - } - tx_succesfull = 0; - EP_Write(3, (uint8_t*)usbbuff, buflen); - lastdsz = buflen; -} - -// put `buf` into queue to send -void USB_send(const char *buf){ - if(!buf || !usbON) return; - int len = 0; - const char *b = buf; - while(*b++) ++len; - if(!usbON || !len) return; - int l = len; - while(l){ - if(tx_succesfull) send_next(); - int a = RB_write(buf, l); - l -= a; - buf += a; - } -} - -// interrupt IN handler (never used?) -static void EP1_Handler(){ - uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]); - if(RX_FLAG(epstatus)) epstatus = (epstatus & ~USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_RX; // set valid RX - else epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX); - // clear CTR - epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)); - USB->EPnR[1] = epstatus; -} - -// data IN/OUT handlers -static void transmit_Handler(){ // EP3IN - tx_succesfull = 1; - uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[3]); - // clear CTR keep DTOGs & STATs - USB->EPnR[3] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr -} - -static void receive_Handler(){ // EP2OUT - rxNE = 1; - uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[2]); - USB->EPnR[2] = (epstatus & ~(USB_EPnR_CTR_RX)); // clear RX ctr -} - -void usb_proc(){ - if(CAN1->RF0R & CAN_RF0R_FOVR0){ // FIFO overrun - CAN1->RF0R &= ~CAN_RF0R_FOVR0; - } - switch(USB_Dev.USB_Status){ - case USB_STATE_CONFIGURED: - // make new BULK endpoint - // Buffer have 1024 bytes, but last 256 we use for CAN bus (30.2 of RM: USB main features) - EP_Init(1, EP_TYPE_INTERRUPT, USB_EP1BUFSZ, 0, EP1_Handler); // IN1 - transmit - EP_Init(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, receive_Handler); // OUT2 - receive data - EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, transmit_Handler); // IN3 - transmit data - USB_Dev.USB_Status = USB_STATE_CONNECTED; - break; - case USB_STATE_DEFAULT: - case USB_STATE_ADDRESSED: - if(usbON){ - usbON = 0; - } - break; - default: // USB_STATE_CONNECTED - send next data portion - if(!usbON) return; - if(tx_succesfull) send_next(); - } -} - -/** - * @brief USB_receive - * @param buf (i) - buffer[64] for received data - * @return amount of received bytes - */ -uint8_t USB_receive(char *buf){ - if(!usbON || !rxNE) return 0; - uint8_t sz = EP_Read(2, (uint16_t*)buf); - uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]); - // keep stat_tx & set ACK rx - USB->EPnR[2] = (epstatus & ~(USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX; - rxNE = 0; - return sz; -} diff --git a/F1:F103/Canon_managing_device/usb.h b/F1:F103/Canon_managing_device/usb.h deleted file mode 100644 index 6e62b64..0000000 --- a/F1:F103/Canon_managing_device/usb.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the MLX90640 project. - * Copyright 2022 Edward V. Emelianov . - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once -#ifndef __USB_H__ -#define __USB_H__ - -#include "usbhw.h" - -#define BUFFSIZE (64) - -extern volatile uint8_t tx_succesfull; - -void usb_proc(); -void send_next(); -void USB_send(const char *buf); -uint8_t USB_receive(char *buf); - -#endif // __USB_H__ diff --git a/F1:F103/Canon_managing_device/usb_descr.c b/F1:F103/Canon_managing_device/usb_descr.c new file mode 100644 index 0000000..08c05ce --- /dev/null +++ b/F1:F103/Canon_managing_device/usb_descr.c @@ -0,0 +1,210 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "usb_descr.h" + +// low/high for uint16_t +#define L16(x) (x & 0xff) +#define H16(x) (x >> 8) + +static const uint8_t USB_DeviceDescriptor[] = { + USB_DT_DEVICE_SIZE, // bLength + USB_DT_DEVICE, // bDescriptorType + L16(bcdUSB), // bcdUSB_L + H16(bcdUSB), // bcdUSB_H + USB_CLASS_MISC, // bDeviceClass + bDeviceSubClass, // bDeviceSubClass + bDeviceProtocol, // bDeviceProtocol + USB_EP0BUFSZ, // bMaxPacketSize + L16(idVendor), // idVendor_L + H16(idVendor), // idVendor_H + L16(idProduct), // idProduct_L + H16(idProduct), // idProduct_H + L16(bcdDevice_Ver), // bcdDevice_Ver_L + H16(bcdDevice_Ver), // bcdDevice_Ver_H + iMANUFACTURER_DESCR, // iManufacturer - indexes of string descriptors in array + iPRODUCT_DESCR, // iProduct + iSERIAL_DESCR, // iSerial + bNumConfigurations // bNumConfigurations +}; + +static const uint8_t USB_DeviceQualifierDescriptor[] = { + USB_DT_QUALIFIER_SIZE, //bLength + USB_DT_QUALIFIER, // bDescriptorType + L16(bcdUSB), // bcdUSB_L + H16(bcdUSB), // bcdUSB_H + USB_CLASS_PER_INTERFACE, // bDeviceClass + bDeviceSubClass, // bDeviceSubClass + bDeviceProtocol, // bDeviceProtocol + USB_EP0BUFSZ, // bMaxPacketSize0 + bNumConfigurations, // bNumConfigurations + 0 // Reserved +}; + +#define wTotalLength (USB_DT_CONFIG_SIZE + (bNumInterfaces * USB_DT_INTERFACE_SIZE) + (bTotNumEndpoints * USB_DT_ENDPOINT_SIZE) + (bNumCsInterfaces * USB_DT_CS_INTERFACE_SIZE) - 1) + +static const uint8_t USB_ConfigDescriptor[] = { + // Configuration Descriptor + USB_DT_CONFIG_SIZE, // bLength: Configuration Descriptor size + USB_DT_CONFIG, // bDescriptorType: Configuration + L16(wTotalLength), // wTotalLength.L :no of returned bytes + H16(wTotalLength), // wTotalLength.H + bNumInterfaces, // bNumInterfaces + 1, // bConfigurationValue: Current configuration value + 0, // iConfiguration: Index of string descriptor describing the configuration or 0 + BusPowered, // bmAttributes - Bus powered + 50, // MaxPower in 2mA units + //--------------------------------------------------------------------------- + // Virtual command Interface Descriptor + USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size + USB_DT_INTERFACE, // bDescriptorType: Interface + 0, // bInterfaceNumber: Number of Interface + 0, // bAlternateSetting: Alternate setting + 1, // bNumEndpoints: one for this + USB_CLASS_COMM, // bInterfaceClass + 2, // bInterfaceSubClass: ACM + 1, // bInterfaceProtocol: Common AT commands + iINTERFACE_DESCR1, // iInterface + // ---- CS Interfaces + USB_DT_CS_INTERFACE_SIZE, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 0, // bDescriptorSubtype: Header Func Desc + 0x10, // bcdCDC: spec release number + 1, // bDataInterface + USB_DT_CS_INTERFACE_SIZE, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 1, // bDescriptorSubtype: Call Management Func Desc + 0, // bmCapabilities: D0+D1 + 1, // bDataInterface + USB_DT_CS_INTERFACE_SIZE-1, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 2, // bDescriptorSubtype: Abstract Control Management desc + 2, // bmCapabilities + USB_DT_CS_INTERFACE_SIZE, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 6, // bDescriptorSubtype: Union func desc + 0, // bMasterInterface: Communication class interface + 1, // bSlaveInterface0: Data Class Interface + // Virtual endpoint 1 Descriptor + USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size + USB_DT_ENDPOINT, // bDescriptorType: Endpoint + 0x8A, // bEndpointAddress IN10 + USB_BM_ATTR_INTERRUPT, // bmAttributes: Interrupt + L16(USB_EP1BUFSZ), // wMaxPacketSize LO + H16(USB_EP1BUFSZ), // wMaxPacketSize HI + 0x10, // bInterval: 16ms + //--------------------------------------------------------------------------- + // Data interface + USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size + USB_DT_INTERFACE, // bDescriptorType: Interface + 1, // bInterfaceNumber: Number of Interface + 0, // bAlternateSetting: Alternate setting + 2, // bNumEndpoints: in and out + USB_CLASS_DATA, // bInterfaceClass + 2, // bInterfaceSubClass: ACM + 0, // bInterfaceProtocol + 0, // iInterface + //Endpoint IN1 Descriptor + USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size + USB_DT_ENDPOINT, // bDescriptorType: Endpoint + 0x81, // bEndpointAddress: IN1 + USB_BM_ATTR_BULK, // bmAttributes: Bulk + L16(USB_TXBUFSZ), // wMaxPacketSize LO + H16(USB_TXBUFSZ), // wMaxPacketSize HI + 0, // bInterval: ignore for Bulk transfer + // Endpoint OUT1 Descriptor + USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size + USB_DT_ENDPOINT, // bDescriptorType: Endpoint + 0x01, // bEndpointAddress: OUT1 + USB_BM_ATTR_BULK, // bmAttributes: Bulk + L16(USB_RXBUFSZ), // wMaxPacketSize LO + H16(USB_RXBUFSZ), // wMaxPacketSize HI + 0, // bInterval: ignore for Bulk transfer + +}; + +//const uint8_t HID_ReportDescriptor[]; + +_USB_LANG_ID_(LD, LANG_US); +_USB_STRING_(SD, u"0.1.0"); +_USB_STRING_(MD, u"eddy@sao.ru"); +_USB_STRING_(PD, u"USB/CAN Canon lens controller"); +_USB_STRING_(ID, u"canonlens"); + +static const void* const StringDescriptor[iDESCR_AMOUNT] = { + [iLANGUAGE_DESCR] = &LD, + [iMANUFACTURER_DESCR] = &MD, + [iPRODUCT_DESCR] = &PD, + [iSERIAL_DESCR] = &SD, + [iINTERFACE_DESCR1] = &ID +}; + +static void wr0(const uint8_t *buf, uint16_t size, uint16_t askedsize){ + if(askedsize < size) size = askedsize; // shortened request + if(size < USB_EP0BUFSZ){ + EP_WriteIRQ(0, buf, size); + return; + } + while(size){ + uint16_t l = size; + if(l > USB_EP0BUFSZ) l = USB_EP0BUFSZ; + EP_WriteIRQ(0, buf, l); + buf += l; + size -= l; + uint8_t needzlp = (l == USB_EP0BUFSZ) ? 1 : 0; + if(size || needzlp){ // send last data buffer + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); + // keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx + USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX)) + ^ USB_EPnR_STAT_TX; + uint32_t ctr = 1000000; + while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;}; + if((USB->ISTR & USB_ISTR_CTR) == 0){ + return; + } + if(needzlp) EP_WriteIRQ(0, NULL, 0); + } + } +} + +void get_descriptor(config_pack_t *pack){ + uint8_t descrtype = pack->wValue >> 8, + descridx = pack->wValue & 0xff; + switch(descrtype){ + case DEVICE_DESCRIPTOR: + wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor), pack->wLength); + break; + case CONFIGURATION_DESCRIPTOR: + wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor), pack->wLength); + break; + case STRING_DESCRIPTOR: + if(descridx < iDESCR_AMOUNT){ + wr0((const uint8_t *)StringDescriptor[descridx], *((uint8_t*)StringDescriptor[descridx]), pack->wLength); + }else{ + EP_WriteIRQ(0, NULL, 0); + } + break; + case DEVICE_QUALIFIER_DESCRIPTOR: + wr0(USB_DeviceQualifierDescriptor, sizeof(USB_DeviceQualifierDescriptor), pack->wLength); + break; + /* case HID_REPORT_DESCRIPTOR: + wr0(HID_ReportDescriptor, sizeof(HID_ReportDescriptor), pack->wLength); + break;*/ + default: + break; + } +} diff --git a/F1:F103/Canon_managing_device/usb_descr.h b/F1:F103/Canon_managing_device/usb_descr.h new file mode 100644 index 0000000..0874763 --- /dev/null +++ b/F1:F103/Canon_managing_device/usb_descr.h @@ -0,0 +1,68 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +#include "usb_lib.h" + +// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor +// bcdUSB: 1.10 +#define bcdUSB 0x0110 +// Class - Misc (EF), subclass - common (2), protocol - interface association descr (1) +#define bDeviceSubClass 0x02 +#define bDeviceProtocol 0x01 +#define idVendor 0x0483 +#define idProduct 0x5740 +#define bcdDevice_Ver 0x0200 +#define bNumConfigurations 1 + +// amount of interfaces and endpoints (except 0) used +#define bNumInterfaces 2 +#define bTotNumEndpoints 3 +#define bNumCsInterfaces 4 + +// powered +#define BusPowered (1<<7) +#define SelfPowered (1<<6) +#define RemoteWakeup (1<<5) + +// buffer sizes +// for USB FS EP0 buffers are from 8 to 64 bytes long +#define USB_EP0BUFSZ 64 +#define USB_EP1BUFSZ 10 +// Rx/Tx EPs +#define USB_RXBUFSZ 64 +#define USB_TXBUFSZ 64 + +// string descriptors +enum{ + iLANGUAGE_DESCR, + iMANUFACTURER_DESCR, + iPRODUCT_DESCR, + iSERIAL_DESCR, + iINTERFACE_DESCR1, + /* iINTERFACE_DESCR2, + iINTERFACE_DESCR3, + iINTERFACE_DESCR4, + iINTERFACE_DESCR5, + iINTERFACE_DESCR6, + iINTERFACE_DESCR7,*/ + iDESCR_AMOUNT +}; + +void get_descriptor(config_pack_t *pack); diff --git a/F1:F103/Canon_managing_device/usb_dev.c b/F1:F103/Canon_managing_device/usb_dev.c new file mode 100644 index 0000000..f6af768 --- /dev/null +++ b/F1:F103/Canon_managing_device/usb_dev.c @@ -0,0 +1,241 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "ringbuffer.h" +#include "usb_descr.h" +#include "usb_dev.h" + +// Class-Specific Control Requests +#define SEND_ENCAPSULATED_COMMAND 0x00 // unused +#define GET_ENCAPSULATED_RESPONSE 0x01 // unused +#define SET_COMM_FEATURE 0x02 // unused +#define GET_COMM_FEATURE 0x03 // unused +#define CLEAR_COMM_FEATURE 0x04 // unused +#define SET_LINE_CODING 0x20 +#define GET_LINE_CODING 0x21 +#define SET_CONTROL_LINE_STATE 0x22 +#define SEND_BREAK 0x23 + +// control line states +#define CONTROL_DTR 0x01 +#define CONTROL_RTS 0x02 + +// inbuf overflow when receiving +static volatile uint8_t bufovrfl = 0; + +// receive buffer: hold data until chkin() call +static uint8_t volatile rcvbuf[USB_RXBUFSZ]; +static uint8_t volatile rcvbuflen = 0; +// line coding +usb_LineCoding WEAK lineCoding = {115200, 0, 0, 8}; +// CDC configured and ready to use +volatile uint8_t CDCready = 0; + +// ring buffers for incoming and outgoing data +static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ]; +static volatile ringbuffer rbout = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0}; +static volatile ringbuffer rbin = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0}; +// last send data size +static volatile int lastdsz = 0; + +static void chkin(){ + if(bufovrfl) return; // allow user to know that previous buffer was overflowed and cleared + if(!rcvbuflen) return; + int w = RB_write((ringbuffer*)&rbin, (uint8_t*)rcvbuf, rcvbuflen); + if(w < 0){ + return; + } + if(w != rcvbuflen) bufovrfl = 1; + rcvbuflen = 0; + uint16_t status = KEEP_DTOG(USB->EPnR[1]); // don't change DTOG + USB->EPnR[1] = (status & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // prepare to get next data portion +} + +// called from transmit EP to send next data portion or by user - when new transmission starts +static void send_next(){ + uint8_t usbbuff[USB_TXBUFSZ]; + int buflen = RB_read((ringbuffer*)&rbout, (uint8_t*)usbbuff, USB_TXBUFSZ); + if(buflen == 0){ + if(lastdsz == 64) EP_Write(1, NULL, 0); // send ZLP after 64 bits packet when nothing more to send + lastdsz = 0; + return; + }else if(buflen < 0){ + lastdsz = 0; + return; + } + EP_Write(1, (uint8_t*)usbbuff, buflen); + lastdsz = buflen; +} + +// data IN/OUT handler +static void rxtx_handler(){ + uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]); + if(RX_FLAG(epstatus)){ // receive data + if(rcvbuflen){ + bufovrfl = 1; // lost last data + rcvbuflen = 0; + } + rcvbuflen = EP_Read(1, (uint8_t*)rcvbuf); + USB->EPnR[1] = epstatus & ~(USB_EPnR_CTR_RX | USB_EPnR_STAT_RX | USB_EPnR_STAT_TX); // keep RX in STALL state until read data + chkin(); // try to write current data into RXbuf if it's not busy + }else{ // tx successfull + USB->EPnR[1] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX; + send_next(); + } +} + +// weak handlers: change them somewhere else if you want to setup USART +// SET_LINE_CODING +void WEAK linecoding_handler(usb_LineCoding *lc){ + lineCoding = *lc; +} + +// SET_CONTROL_LINE_STATE +void WEAK clstate_handler(uint16_t val){ + CDCready = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected +} + +// SEND_BREAK +void WEAK break_handler(){ + CDCready = 0; +} + + +// USB is configured: setup endpoints +void set_configuration(){ + EP_Init(1, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_handler); // IN1 and OUT1 +} + +// PL2303 CLASS request +void usb_class_request(config_pack_t *req, uint8_t *data, uint16_t datalen){ + uint8_t recipient = REQUEST_RECIPIENT(req->bmRequestType); + uint8_t dev2host = (req->bmRequestType & 0x80) ? 1 : 0; + switch(recipient){ + case REQ_RECIPIENT_INTERFACE: + switch(req->bRequest){ + case SET_LINE_CODING: + if(!data || !datalen) break; // wait for data + if(datalen == sizeof(usb_LineCoding)) + linecoding_handler((usb_LineCoding*)data); + break; + case GET_LINE_CODING: + EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding)); + break; + case SET_CONTROL_LINE_STATE: + clstate_handler(req->wValue); + break; + case SEND_BREAK: + break_handler(); + break; + default: + break; + } + break; + default: + if(dev2host) EP_WriteIRQ(0, NULL, 0); + } + if(!dev2host) EP_WriteIRQ(0, NULL, 0); +} + +// blocking send full content of ring buffer +int USB_sendall(){ + while(lastdsz > 0){ + if(!CDCready) return FALSE; + } + return TRUE; +} + +// put `buf` into queue to send +int USB_send(const uint8_t *buf, int len){ + if(!buf || !CDCready || !len) return FALSE; + while(len){ + int a = RB_write((ringbuffer*)&rbout, buf, len); + if(a > 0){ + len -= a; + buf += a; + } else if (a < 0) continue; // do nothing if buffer is in reading state + if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN + } + return TRUE; +} + +int USB_putbyte(uint8_t byte){ + if(!CDCready) return FALSE; + int l = 0; + while((l = RB_write((ringbuffer*)&rbout, &byte, 1)) != 1){ + if(l < 0) continue; + } + if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN + return TRUE; +} + +int USB_sendstr(const char *string){ + if(!string || !CDCready) return FALSE; + int len = 0; + const char *b = string; + while(*b++) ++len; + if(!len) return FALSE; + return USB_send((const uint8_t*)string, len); +} + +/** + * @brief USB_receive - get binary data from receiving ring-buffer + * @param buf (i) - buffer for received data + * @param len - length of `buf` + * @return amount of received bytes (negative, if overfull happened) + */ +int USB_receive(uint8_t *buf, int len){ + chkin(); + if(bufovrfl){ + while(1 != RB_clearbuf((ringbuffer*)&rbin)); + bufovrfl = 0; + return -1; + } + int sz = RB_read((ringbuffer*)&rbin, buf, len); + if(sz < 0) return 0; // buffer in writting state + return sz; +} + +/** + * @brief USB_receivestr - get string up to '\n' and replace '\n' with 0 + * @param buf - receiving buffer + * @param len - its length + * @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared) + */ +int USB_receivestr(char *buf, int len){ + chkin(); + if(bufovrfl){ + while(1 != RB_clearbuf((ringbuffer*)&rbin)); + bufovrfl = 0; + return -1; + } + int l = RB_readto((ringbuffer*)&rbin, '\n', (uint8_t*)buf, len); + if(l < 1){ + if(rbin.length == RB_datalen((ringbuffer*)&rbin)){ // buffer is full but no '\n' found + while(1 != RB_clearbuf((ringbuffer*)&rbin)); + return -1; + } + return 0; + } + if(l == 0) return 0; + buf[l-1] = 0; // replace '\n' with strend + return l; +} + diff --git a/F1:F103/Canon_managing_device/usb_dev.h b/F1:F103/Canon_managing_device/usb_dev.h new file mode 100644 index 0000000..ea2263e --- /dev/null +++ b/F1:F103/Canon_managing_device/usb_dev.h @@ -0,0 +1,57 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include "usb_lib.h" + +typedef struct { + uint32_t dwDTERate; + uint8_t bCharFormat; +#define USB_CDC_1_STOP_BITS 0 +#define USB_CDC_1_5_STOP_BITS 1 +#define USB_CDC_2_STOP_BITS 2 + uint8_t bParityType; +#define USB_CDC_NO_PARITY 0 +#define USB_CDC_ODD_PARITY 1 +#define USB_CDC_EVEN_PARITY 2 +#define USB_CDC_MARK_PARITY 3 +#define USB_CDC_SPACE_PARITY 4 + uint8_t bDataBits; +} __attribute__ ((packed)) usb_LineCoding; + +extern usb_LineCoding lineCoding; +extern volatile uint8_t CDCready; + +void break_handler(); +void clstate_handler(uint16_t val); +void linecoding_handler(usb_LineCoding *lc); + + +// sizes of ringbuffers for outgoing and incoming data +#define RBOUTSZ (1024) +#define RBINSZ (1024) + +#define newline() USB_putbyte('\n') +#define USND(s) do{USB_sendstr(s); USB_putbyte('\n');}while(0) + +int USB_sendall(); +int USB_send(const uint8_t *buf, int len); +int USB_putbyte(uint8_t byte); +int USB_sendstr(const char *string); +int USB_receive(uint8_t *buf, int len); +int USB_receivestr(char *buf, int len); diff --git a/F1:F103/Canon_managing_device/usb_lib.c b/F1:F103/Canon_managing_device/usb_lib.c index ff84a18..6c16545 100644 --- a/F1:F103/Canon_managing_device/usb_lib.c +++ b/F1:F103/Canon_managing_device/usb_lib.c @@ -1,6 +1,5 @@ /* - * This file is part of the MLX90640 project. - * Copyright 2022 Edward V. Emelianov . + * Copyright 2024 Edward V. Emelianov . * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,251 +14,100 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - #include + #include "usb_lib.h" +#include "usb_descr.h" +#include "usb_dev.h" -ep_t endpoints[STM32ENDPOINTS]; +static ep_t endpoints[STM32ENDPOINTS]; -usb_dev_t USB_Dev; -static usb_LineCoding lineCoding = {115200, 0, 0, 8}; -config_pack_t setup_packet; -uint8_t ep0databuf[EP0DATABUF_SIZE]; -uint8_t ep0dbuflen = 0; +static uint16_t USB_Addr = 0; +static uint8_t setupdatabuf[EP0DATABUF_SIZE]; +static config_pack_t *setup_packet = (config_pack_t*) setupdatabuf; +volatile uint8_t usbON = 0; // device is configured and active -usb_LineCoding getLineCoding(){return lineCoding;} - -uint8_t usbON = 0; // device disconnected from terminal - -// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor -#define bcdUSB_L 0x10 -#define bcdUSB_H 0x01 -#define bDeviceClass 0 -#define bDeviceSubClass 0 -#define bDeviceProtocol 0 -#define bNumConfigurations 1 - -static const uint8_t USB_DeviceDescriptor[] = { - 18, // bLength - 0x01, // bDescriptorType - Device descriptor - bcdUSB_L, // bcdUSB_L - 1.10 - bcdUSB_H, // bcdUSB_H - bDeviceClass, // bDeviceClass - USB_COMM - bDeviceSubClass, // bDeviceSubClass - bDeviceProtocol, // bDeviceProtocol - USB_EP0_BUFSZ, // bMaxPacketSize - 0x7b, // idVendor_L PL2303: VID=0x067b, PID=0x2303 - 0x06, // idVendor_H - 0x03, // idProduct_L - 0x23, // idProduct_H - 0x00, // bcdDevice_Ver_L - 0x03, // bcdDevice_Ver_H - 0x01, // iManufacturer - 0x02, // iProduct - 0x00, // iSerialNumber - bNumConfigurations // bNumConfigurations -}; - -static const uint8_t USB_DeviceQualifierDescriptor[] = { - 10, //bLength - 0x06, // bDescriptorType - Device qualifier - bcdUSB_L, // bcdUSB_L - bcdUSB_H, // bcdUSB_H - bDeviceClass, // bDeviceClass - bDeviceSubClass, // bDeviceSubClass - bDeviceProtocol, // bDeviceProtocol - USB_EP0_BUFSZ, // bMaxPacketSize0 - bNumConfigurations, // bNumConfigurations - 0x00 // Reserved -}; - -static const uint8_t USB_ConfigDescriptor[] = { - /*Configuration Descriptor*/ - 0x09, /* bLength: Configuration Descriptor size */ - 0x02, /* bDescriptorType: Configuration */ - 39, /* wTotalLength:no of returned bytes */ - 0x00, - 0x01, /* bNumInterfaces: 1 interface */ - 0x01, /* bConfigurationValue: Configuration value */ - 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ - 0xa0, /* bmAttributes - Bus powered, Remote wakeup */ - 0x32, /* MaxPower 100 mA */ - - /*---------------------------------------------------------------------------*/ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - 0x04, /* bDescriptorType: Interface */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x03, /* bNumEndpoints: 3 endpoints used */ - 0xff, /* bInterfaceClass */ - 0x00, /* bInterfaceSubClass */ - 0x00, /* bInterfaceProtocol */ - 0x00, /* iInterface: */ -/////////////////////////////////////////////////// - /*Endpoint 1 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - 0x05, /* bDescriptorType: Endpoint */ - 0x81, /* bEndpointAddress IN1 */ - 0x03, /* bmAttributes: Interrupt */ - 0x0a, /* wMaxPacketSize LO: */ - 0x00, /* wMaxPacketSize HI: */ - 0x01, /* bInterval: */ - - /*Endpoint OUT2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - 0x05, /* bDescriptorType: Endpoint */ - 0x02, /* bEndpointAddress: OUT2 */ - 0x02, /* bmAttributes: Bulk */ - (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_RXBUFSZ >> 8), - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN3 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - 0x05, /* bDescriptorType: Endpoint */ - 0x83, /* bEndpointAddress IN3 */ - 0x02, /* bmAttributes: Bulk */ - (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TXBUFSZ >> 8), - 0x00, /* bInterval: ignore for Bulk transfer */ -}; - -_USB_LANG_ID_(USB_StringLangDescriptor, LANG_US); -// these descriptors are not used in PL2303 emulator! -_USB_STRING_(USB_StringSerialDescriptor, u"0"); -_USB_STRING_(USB_StringManufacturingDescriptor, u"Prolific Technology Inc."); -_USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller"); - -/* - * default handlers - */ -// SET_LINE_CODING -void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){ -} - -// SET_CONTROL_LINE_STATE -void WEAK clstate_handler(uint16_t __attribute__((unused)) val){ -} - -// SEND_BREAK -void WEAK break_handler(){ -} - -// handler of vendor requests -void WEAK vendor_handler(config_pack_t *packet){ - if(packet->bmRequestType & 0x80){ // read - uint8_t c; - switch(packet->wValue){ - case 0x8484: - c = 2; - break; - case 0x0080: - c = 1; - break; - case 0x8686: - c = 0xaa; - break; - default: - c = 0; - } - EP_WriteIRQ(0, &c, 1); - }else{ // write ZLP - EP_WriteIRQ(0, (uint8_t *)0, 0); - } -} - -static void wr0(const uint8_t *buf, uint16_t size){ - if(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request - if(size < endpoints[0].txbufsz){ - EP_WriteIRQ(0, buf, size); - return; - } - while(size){ - uint16_t l = size; - if(l > endpoints[0].txbufsz) l = endpoints[0].txbufsz; - EP_WriteIRQ(0, buf, l); - buf += l; - size -= l; - uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0; - if(size || needzlp){ // send last data buffer - uint16_t status = KEEP_DTOG(USB->EPnR[0]); - // keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx - USB->EPnR[0] = (status & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX)) - ^ USB_EPnR_STAT_TX; - uint32_t ctr = 1000000; - while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;}; - if((USB->ISTR & USB_ISTR_CTR) == 0){ - return; - } - if(needzlp) EP_WriteIRQ(0, (uint8_t*)0, 0); - } - } -} - -static inline void get_descriptor(){ - switch(setup_packet.wValue){ - case DEVICE_DESCRIPTOR: - wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor)); - break; - case CONFIGURATION_DESCRIPTOR: - wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor)); - break; - case STRING_LANG_DESCRIPTOR: - wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE); - break; - case STRING_MAN_DESCRIPTOR: - wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength); - break; - case STRING_PROD_DESCRIPTOR: - wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength); - break; - case STRING_SN_DESCRIPTOR: - wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength); - break; - case DEVICE_QUALIFIER_DESCRIPTOR: - wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]); - break; - default: - break; - } -} - -static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured) +static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured) static inline void std_d2h_req(){ - uint16_t status = 0; // bus powered - switch(setup_packet.bRequest){ + uint16_t st = 0; + switch(setup_packet->bRequest){ case GET_DESCRIPTOR: - get_descriptor(); + get_descriptor(setup_packet); break; case GET_STATUS: - EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered + EP_WriteIRQ(0, (uint8_t *)&st, 2); // send status: Bus Powered break; case GET_CONFIGURATION: - EP_WriteIRQ(0, &configuration, 1); + EP_WriteIRQ(0, (uint8_t*)&configuration, 1); break; default: + EP_WriteIRQ(0, NULL, 0); break; } } static inline void std_h2d_req(){ - switch(setup_packet.bRequest){ + switch(setup_packet->bRequest){ case SET_ADDRESS: // new address will be assigned later - after acknowlegement or request to host - USB_Dev.USB_Addr = setup_packet.wValue; + USB_Addr = setup_packet->wValue; break; case SET_CONFIGURATION: // Now device configured - USB_Dev.USB_Status = USB_STATE_CONFIGURED; - configuration = setup_packet.wValue; + configuration = setup_packet->wValue; + set_configuration(); + usbON = 1; break; default: break; } } +void WEAK usb_standard_request(){ + uint8_t recipient = REQUEST_RECIPIENT(setup_packet->bmRequestType); + uint8_t dev2host = (setup_packet->bmRequestType & 0x80) ? 1 : 0; + switch(recipient){ + case REQ_RECIPIENT_DEVICE: + if(dev2host){ + std_d2h_req(); + }else{ + std_h2d_req(); + } + break; + case REQ_RECIPIENT_INTERFACE: + if(dev2host && setup_packet->bRequest == GET_DESCRIPTOR){ + get_descriptor(setup_packet); + } + break; + case REQ_RECIPIENT_ENDPOINT: + if(setup_packet->bRequest == CLEAR_FEATURE){ + }else{ + } + break; + default: + break; + } + if(!dev2host) EP_WriteIRQ(0, NULL, 0); +} + +void WEAK usb_class_request(config_pack_t *req, uint8_t _U_ *data, uint16_t _U_ datalen){ + switch(req->bRequest){ + case GET_INTERFACE: + break; + case SET_CONFIGURATION: // set featuring by req->wValue + break; + default: + break; + } + if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev + EP_WriteIRQ(0, NULL, 0); +} + +void WEAK usb_vendor_request(config_pack_t _U_ *packet, uint8_t _U_ *data, uint16_t _U_ datalen){ + if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev + EP_WriteIRQ(0, NULL, 0); +} + /* bmRequestType: 76543210 7 direction: 0 - host->device, 1 - device->host @@ -269,68 +117,49 @@ bmRequestType: 76543210 /** * Endpoint0 (control) handler */ -void EP0_Handler(){ - uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications - uint8_t reqtype = setup_packet.bmRequestType & 0x7f; - uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0; +static void EP0_Handler(){ + uint8_t ep0dbuflen = 0; + uint8_t ep0databuf[EP0DATABUF_SIZE]; + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications int rxflag = RX_FLAG(epstatus); - if(rxflag && SETUP_FLAG(epstatus)){ - switch(reqtype){ - case STANDARD_DEVICE_REQUEST_TYPE: // standard device request - if(dev2host){ - std_d2h_req(); - }else{ - std_h2d_req(); - EP_WriteIRQ(0, (uint8_t *)0, 0); - } - break; - case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request - if(setup_packet.bRequest == CLEAR_FEATURE){ - EP_WriteIRQ(0, (uint8_t *)0, 0); - } - break; - case VENDOR_REQUEST_TYPE: - vendor_handler(&setup_packet); - break; - case CONTROL_REQUEST_TYPE: - switch(setup_packet.bRequest){ - case GET_LINE_CODING: - EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding)); - break; - case SET_LINE_CODING: // omit this for next stage, when data will come - break; - case SET_CONTROL_LINE_STATE: - usbON = 1; - clstate_handler(setup_packet.wValue); - break; - case SEND_BREAK: - usbON = 0; - break_handler(); - break; - default: - break; - } - if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement - break; - default: - EP_WriteIRQ(0, (uint8_t *)0, 0); - } - }else if(rxflag){ // got data over EP0 or host acknowlegement - if(endpoints[0].rx_cnt){ - if(setup_packet.bRequest == SET_LINE_CODING){ - linecoding_handler((usb_LineCoding*)ep0databuf); + // check direction + if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit) + if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack + EP_Read(0, setupdatabuf); + // interrupt handler will be called later + }else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf + ep0dbuflen = EP_Read(0, ep0databuf); } - } - } else if(TX_FLAG(epstatus)){ // package transmitted - // now we can change address after enumeration - if ((USB->DADDR & USB_DADDR_ADD) != USB_Dev.USB_Addr){ - USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr; - // change state to ADRESSED - USB_Dev.USB_Status = USB_STATE_ADDRESSED; + } + if(rxflag){ + uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType); + switch(reqtype){ + case REQ_TYPE_STANDARD: + if(SETUP_FLAG(epstatus)){ + usb_standard_request(); + }else{ + } + break; + case REQ_TYPE_CLASS: + usb_class_request(setup_packet, ep0databuf, ep0dbuflen); + break; + case REQ_TYPE_VENDOR: + usb_vendor_request(setup_packet, ep0databuf, ep0dbuflen); + break; + default: + EP_WriteIRQ(0, NULL, 0); + break; } } - epstatus = KEEP_DTOG(USB->EPnR[0]); - if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/data transmission + if(TX_FLAG(epstatus)){ + // now we can change address after enumeration + if ((USB->DADDR & USB_DADDR_ADD) != USB_Addr){ + USB->DADDR = USB_DADDR_EF | USB_Addr; + usbON = 0; + } + } + //epstatus = KEEP_DTOG(USB->EPnR[0]); + if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged // keep DTOGs, clear CTR_RX,TX, set RX VALID USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX; @@ -347,10 +176,20 @@ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){ uint16_t N2 = (size + 1) >> 1; // the buffer is 16-bit, so we should copy data as it would be uint16_t uint16_t *buf16 = (uint16_t *)buf; +#if defined USB1_16 + // very bad: what if `size` is odd? uint32_t *out = (uint32_t *)endpoints[number].tx_buf; for(int i = 0; i < N2; ++i, ++out){ *out = buf16[i]; } +#elif defined USB2_16 + // use memcpy instead? + for(int i = 0; i < N2; i++){ + endpoints[number].tx_buf[i] = buf16[i]; + } +#else +#error "Define USB1_16 or USB2_16" +#endif USB_BTABLE->EP[number].USB_COUNT_TX = size; } @@ -362,9 +201,9 @@ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){ */ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){ EP_WriteIRQ(number, buf, size); - uint16_t status = KEEP_DTOG(USB->EPnR[number]); - // keep DTOGs, clear CTR_TX & set TX VALID to start transmission - USB->EPnR[number] = (status & ~(USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_TX; + uint16_t epstatus = KEEP_DTOG(USB->EPnR[number]); + // keep DTOGs and RX stat, clear CTR_TX & set TX VALID to start transmission + USB->EPnR[number] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_RX)) ^ USB_EPnR_STAT_TX; } /* @@ -372,16 +211,158 @@ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){ * @param *buf - user array for data * @return amount of data read */ -int EP_Read(uint8_t number, uint16_t *buf){ +int EP_Read(uint8_t number, uint8_t *buf){ int sz = endpoints[number].rx_cnt; if(!sz) return 0; endpoints[number].rx_cnt = 0; +#if defined USB1_16 int n = (sz + 1) >> 1; - uint32_t *in = (uint32_t *)endpoints[number].rx_buf; - if(n){ - for(int i = 0; i < n; ++i, ++in) - buf[i] = *(uint16_t*)in; - } + uint32_t *in = (uint32_t*)endpoints[number].rx_buf; + uint16_t *out = (uint16_t*)buf; + for(int i = 0; i < n; ++i, ++in) + out[i] = *(uint16_t*)in; +#elif defined USB2_16 + // use memcpy instead? + for(int i = 0; i < sz; ++i) + buf[i] = endpoints[number].rx_buf[i]; +#else +#error "Define USB1_16 or USB2_16" +#endif return sz; } + +static uint16_t lastaddr = LASTADDR_DEFAULT; +/** + * Endpoint initialisation + * @param number - EP num (0...7) + * @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT) + * @param txsz - transmission buffer size @ USB/CAN buffer + * @param rxsz - reception buffer size @ USB/CAN buffer + * @param uint16_t (*func)(ep_t *ep) - EP handler function + * @return 0 if all OK + */ +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ + if(number >= STM32ENDPOINTS) return 4; // out of configured amount + if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large + if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable + USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); + USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; + if(rxsz & 1) return 3; // wrong rx buffer size + uint16_t countrx = 0; + if(rxsz < 64) countrx = rxsz / 2; + else{ + if(rxsz & 0x1f) return 3; // should be multiple of 32 + countrx = 31 + rxsz / 32; + } + USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr; + endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ); + endpoints[number].txbufsz = txsz; + lastaddr += txsz; + USB_BTABLE->EP[number].USB_COUNT_TX = 0; + USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr; + endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ); + lastaddr += rxsz; + USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10; + endpoints[number].func = func; + return 0; +} + +// standard IRQ handler +void USB_IRQ(){ + uint32_t CNTR = USB->CNTR; + USB->CNTR = 0; + if(USB->ISTR & USB_ISTR_RESET){ + usbON = 0; + // Reinit registers + CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM; + // Endpoint 0 - CONTROL + // ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes! + lastaddr = LASTADDR_DEFAULT; + // clear address, leave only enable bit + USB->DADDR = USB_DADDR_EF; + USB->ISTR = ~USB_ISTR_RESET; + if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0BUFSZ, USB_EP0BUFSZ, EP0_Handler)){ + return; + }; + } + if(USB->ISTR & USB_ISTR_CTR){ + // EP number + uint8_t n = USB->ISTR & USB_ISTR_EPID; + // copy received bytes amount + endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter + // call EP handler + if(endpoints[n].func) endpoints[n].func(); + } + if(USB->ISTR & USB_ISTR_WKUP){ // wakeup +#ifndef STM32F0 + CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM); // clear suspend flags +#else + CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM); +#endif + USB->ISTR = ~USB_ISTR_WKUP; + } + if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep + usbON = 0; +#ifndef STM32F0 + CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM; +#else + CNTR |= USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM; +#endif + CNTR &= ~(USB_CNTR_SUSPM); + USB->ISTR = ~USB_ISTR_SUSP; + } + USB->CNTR = CNTR; // rewoke interrupts +} + +// here we suppose that all PIN settings done in hw_setup earlier +void USB_setup(){ +#if defined STM32F3 + NVIC_DisableIRQ(USB_LP_IRQn); + // remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303 + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG + SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP; +#elif defined STM32F1 + NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); + NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); +#elif defined STM32F0 + NVIC_DisableIRQ(USB_IRQn); + RCC->APB1ENR |= RCC_APB1ENR_CRSEN; + RCC->CFGR3 &= ~RCC_CFGR3_USBSW; // reset USB + RCC->CR2 |= RCC_CR2_HSI48ON; // turn ON HSI48 + uint32_t tmout = 16000000; + while(!(RCC->CR2 & RCC_CR2_HSI48RDY)){if(--tmout == 0) break;} + FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY; + CRS->CFGR &= ~CRS_CFGR_SYNCSRC; + CRS->CFGR |= CRS_CFGR_SYNCSRC_1; // USB SOF selected as sync source + CRS->CR |= CRS_CR_AUTOTRIMEN; // enable auto trim + CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only + RCC->CFGR |= RCC_CFGR_SW; +#endif + RCC->APB1ENR |= RCC_APB1ENR_USBEN; + //?? + USB->CNTR = USB_CNTR_FRES; // Force USB Reset + for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms + USB->CNTR = 0; + USB->BTABLE = 0; + USB->DADDR = 0; + USB->ISTR = 0; + USB->CNTR = USB_CNTR_RESETM; // allow only reset interrupts +#if defined STM32F3 + NVIC_EnableIRQ(USB_LP_IRQn); +#elif defined STM32F1 + NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); +#elif defined STM32F0 + USB->BCDR |= USB_BCDR_DPPU; + NVIC_EnableIRQ(USB_IRQn); +#endif +} + + +#if defined STM32F3 +void usb_lp_isr() __attribute__ ((alias ("USB_IRQ"))); +#elif defined STM32F1 +void usb_lp_can_rx0_isr() __attribute__ ((alias ("USB_IRQ"))); +#elif defined STM32F0 +void usb_isr() __attribute__ ((alias ("USB_IRQ"))); +#endif diff --git a/F1:F103/Canon_managing_device/usb_lib.h b/F1:F103/Canon_managing_device/usb_lib.h index 739fe2d..c6e70b0 100644 --- a/F1:F103/Canon_managing_device/usb_lib.h +++ b/F1:F103/Canon_managing_device/usb_lib.h @@ -1,6 +1,5 @@ /* - * This file is part of the MLX90640 project. - * Copyright 2022 Edward V. Emelianov . + * Copyright 2024 Edward V. Emelianov . * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,61 +14,253 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - #pragma once -#ifndef __USB_LIB_H__ -#define __USB_LIB_H__ +#include #include -#include "usbhw.h" + +/****************************************************************** + * Hardware registers etc * + *****************************************************************/ +#if defined STM32F0 +#include +#elif defined STM32F1 +#include +// there's no this define in standard header +#define USB_BASE ((uint32_t)0x40005C00) +#elif defined STM32F3 +#include +#endif + +// max endpoints number +#define STM32ENDPOINTS 8 +/** + * Buffers size definition + **/ + +// F0 - USB2_16; F1 - USB1_16; F3 - 1/2 depending on series +#if !defined USB1_16 && !defined USB2_16 +#if defined STM32F0 +#define USB2_16 +#elif defined STM32F1 +#define USB1_16 +#else +#error "Can't determine USB1_16 or USB2_16, define by hands" +#endif +#endif + +// BTABLE_SIZE FOR STM32F3: +// In STM32F303/302xB/C, 512 bytes SRAM is not shared with CAN. +// In STM32F302x6/x8 and STM32F30xxD/E, 726 bytes dedicated SRAM and 256 bytes shared SRAM with CAN i.e. +// 1Kbytes dedicated SRAM in case CAN is disabled. +// remember, that USB_BTABLE_SIZE will be divided by ACCESSZ, so don't divide it twice for 32-bit addressing + +#ifdef NOCAN +#if defined STM32F0 +#define USB_BTABLE_SIZE 1024 +#elif defined STM32F3 +#define USB_BTABLE_SIZE 512 +#warning "Please, check real buffer size due to docs" +#else +#error "define STM32F0 or STM32F3" +#endif +#else // !NOCAN: F0/F3 with CAN or F1 (can't simultaneously run CAN and USB) +#if defined STM32F0 +#define USB_BTABLE_SIZE 768 +#elif defined STM32F3 +#define USB_BTABLE_SIZE 512 +#warning "Please, check real buffer size due to docs" +#else // STM32F103: 1024 bytes but with 32-bit addressing +#define USB_BTABLE_SIZE 1024 +#endif +#endif // NOCAN + +// first 64 bytes of USB_BTABLE are registers! + +#define USB_BTABLE_BASE 0x40006000 +#define USB ((USB_TypeDef *) USB_BASE) + +#ifdef USB_BTABLE +#undef USB_BTABLE +#endif +#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE)) +#define USB_ISTR_EPID 0x0000000F +#define USB_FNR_LSOF_0 0x00000800 +#define USB_FNR_lSOF_1 0x00001000 +#define USB_LPMCSR_BESL_0 0x00000010 +#define USB_LPMCSR_BESL_1 0x00000020 +#define USB_LPMCSR_BESL_2 0x00000040 +#define USB_LPMCSR_BESL_3 0x00000080 +#define USB_EPnR_CTR_RX 0x00008000 +#define USB_EPnR_DTOG_RX 0x00004000 +#define USB_EPnR_STAT_RX 0x00003000 +#define USB_EPnR_STAT_RX_0 0x00001000 +#define USB_EPnR_STAT_RX_1 0x00002000 +#define USB_EPnR_SETUP 0x00000800 +#define USB_EPnR_EP_TYPE 0x00000600 +#define USB_EPnR_EP_TYPE_0 0x00000200 +#define USB_EPnR_EP_TYPE_1 0x00000400 +#define USB_EPnR_EP_KIND 0x00000100 +#define USB_EPnR_CTR_TX 0x00000080 +#define USB_EPnR_DTOG_TX 0x00000040 +#define USB_EPnR_STAT_TX 0x00000030 +#define USB_EPnR_STAT_TX_0 0x00000010 +#define USB_EPnR_STAT_TX_1 0x00000020 +#define USB_EPnR_EA 0x0000000F +#define USB_COUNTn_RX_BLSIZE 0x00008000 +#define USB_COUNTn_NUM_BLOCK 0x00007C00 +#define USB_COUNTn_RX 0x0000003F + +#define USB_TypeDef USB_TypeDef_custom + +typedef struct { + __IO uint32_t EPnR[STM32ENDPOINTS]; + __IO uint32_t RESERVED[STM32ENDPOINTS]; + __IO uint32_t CNTR; + __IO uint32_t ISTR; + __IO uint32_t FNR; + __IO uint32_t DADDR; + __IO uint32_t BTABLE; +#ifdef STM32F0 + __IO uint32_t LPMCSR; + __IO uint32_t BCDR; +#endif +} USB_TypeDef; + +// F303 D/E have 2x16 access scheme +typedef struct{ +#if defined USB2_16 + __IO uint16_t USB_ADDR_TX; + __IO uint16_t USB_COUNT_TX; + __IO uint16_t USB_ADDR_RX; + __IO uint16_t USB_COUNT_RX; +#define ACCESSZ (1) +#define BUFTYPE uint8_t +#elif defined USB1_16 + __IO uint32_t USB_ADDR_TX; + __IO uint32_t USB_COUNT_TX; + __IO uint32_t USB_ADDR_RX; + __IO uint32_t USB_COUNT_RX; +#define ACCESSZ (2) +#define BUFTYPE uint16_t +#else +#error "Define USB1_16 or USB2_16" +#endif +} USB_EPDATA_TypeDef; + + +typedef struct{ + __IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS]; +} USB_BtableDef; #define EP0DATABUF_SIZE (64) #define LASTADDR_DEFAULT (STM32ENDPOINTS * 8) -// bmRequestType & 0x7f -#define STANDARD_DEVICE_REQUEST_TYPE 0 -#define STANDARD_ENDPOINT_REQUEST_TYPE 2 -#define VENDOR_REQUEST_TYPE 0x40 -#define CONTROL_REQUEST_TYPE 0x21 -// bRequest, standard; for bmRequestType == 0x80 +/****************************************************************** + * Defines from usb.h * + *****************************************************************/ + +/* + * Device and/or Interface Class codes + */ +#define USB_CLASS_PER_INTERFACE 0 +#define USB_CLASS_AUDIO 1 +#define USB_CLASS_COMM 2 +#define USB_CLASS_HID 3 +#define USB_CLASS_PRINTER 7 +#define USB_CLASS_PTP 6 +#define USB_CLASS_MASS_STORAGE 8 +#define USB_CLASS_HUB 9 +#define USB_CLASS_DATA 10 +#define USB_CLASS_MISC 0xef +#define USB_CLASS_VENDOR_SPEC 0xff + +/* + * Descriptor types + */ +#define USB_DT_DEVICE 0x01 +#define USB_DT_CONFIG 0x02 +#define USB_DT_STRING 0x03 +#define USB_DT_INTERFACE 0x04 +#define USB_DT_ENDPOINT 0x05 +#define USB_DT_QUALIFIER 0x06 +#define USB_DT_IAD 0x0B + +#define USB_DT_HID 0x21 +#define USB_DT_REPORT 0x22 +#define USB_DT_PHYSICAL 0x23 +#define USB_DT_CS_INTERFACE 0x24 +#define USB_DT_HUB 0x29 + +/* + * Descriptor sizes per descriptor type + */ +#define USB_DT_DEVICE_SIZE 18 +#define USB_DT_CONFIG_SIZE 9 +#define USB_DT_INTERFACE_SIZE 9 +#define USB_DT_HID_SIZE 9 +#define USB_DT_ENDPOINT_SIZE 7 +#define USB_DT_QUALIFIER_SIZE 10 +#define USB_DT_CS_INTERFACE_SIZE 5 +#define USB_DT_IAD_SIZE 8 + + +// bmRequestType & 0x80 == dev2host (1) or host2dev (0) +// recipient: bmRequestType & 0x1f +#define REQUEST_RECIPIENT(b) (b & 0x1f) +#define REQ_RECIPIENT_DEVICE 0 +#define REQ_RECIPIENT_INTERFACE 1 +#define REQ_RECIPIENT_ENDPOINT 2 +#define REQ_RECIPIENT_OTHER 3 +// type: [bmRequestType & 0x60 >> 5] +#define REQUEST_TYPE(b) ((b&0x60)>>5) +#define REQ_TYPE_STANDARD 0 +#define REQ_TYPE_CLASS 1 +#define REQ_TYPE_VENDOR 2 +#define REQ_TYPE_RESERVED 3 + + +//#define VENDOR_REQUEST 0x01 + +// standard device requests #define GET_STATUS 0x00 -#define GET_DESCRIPTOR 0x06 -#define GET_CONFIGURATION 0x08 -// for bmRequestType == 0 #define CLEAR_FEATURE 0x01 -#define SET_FEATURE 0x03 // unused +#define SET_FEATURE 0x03 #define SET_ADDRESS 0x05 -#define SET_DESCRIPTOR 0x07 // unused +#define GET_DESCRIPTOR 0x06 +#define SET_DESCRIPTOR 0x07 +#define GET_CONFIGURATION 0x08 #define SET_CONFIGURATION 0x09 -// for bmRequestType == 0x81, 1 or 0xB2 -#define GET_INTERFACE 0x0A // unused -#define SET_INTERFACE 0x0B // unused -#define SYNC_FRAME 0x0C // unused -#define VENDOR_REQUEST 0x01 // unused +// and some standard interface requests +#define GET_INTERFACE 0x0A +#define SET_INTERFACE 0x0B +// and some standard endpoint requests +#define SYNC_FRAME 0x0C -// Class-Specific Control Requests -#define SEND_ENCAPSULATED_COMMAND 0x00 // unused -#define GET_ENCAPSULATED_RESPONSE 0x01 // unused -#define SET_COMM_FEATURE 0x02 // unused -#define GET_COMM_FEATURE 0x03 // unused -#define CLEAR_COMM_FEATURE 0x04 // unused -#define SET_LINE_CODING 0x20 -#define GET_LINE_CODING 0x21 -#define SET_CONTROL_LINE_STATE 0x22 -#define SEND_BREAK 0x23 +// Types of descriptors +#define DEVICE_DESCRIPTOR 0x01 +#define CONFIGURATION_DESCRIPTOR 0x02 +#define STRING_DESCRIPTOR 0x03 +#define DEVICE_QUALIFIER_DESCRIPTOR 0x06 +#define DEBUG_DESCRIPTOR 0x0a +#define HID_REPORT_DESCRIPTOR 0x22 -// control line states -#define CONTROL_DTR 0x01 -#define CONTROL_RTS 0x02 +// EP types for EP_init +#define EP_TYPE_BULK 0x00 +#define EP_TYPE_CONTROL 0x01 +#define EP_TYPE_ISO 0x02 +#define EP_TYPE_INTERRUPT 0x03 -// wValue -#define DEVICE_DESCRIPTOR 0x100 -#define CONFIGURATION_DESCRIPTOR 0x200 -#define STRING_LANG_DESCRIPTOR 0x300 -#define STRING_MAN_DESCRIPTOR 0x301 -#define STRING_PROD_DESCRIPTOR 0x302 -#define STRING_SN_DESCRIPTOR 0x303 -#define DEVICE_QUALIFIER_DESCRIPTOR 0x600 +// EP types for descriptors +#define USB_BM_ATTR_CONTROL 0x00 +#define USB_BM_ATTR_ISO 0x01 +#define USB_BM_ATTR_BULK 0x02 +#define USB_BM_ATTR_INTERRUPT 0x03 + + +/****************************************************************** + * Other stuff * + *****************************************************************/ #define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX) #define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX) @@ -79,20 +270,6 @@ #define KEEP_DTOG_STAT(EPnR) (EPnR & ~(USB_EPnR_STAT_RX|USB_EPnR_STAT_TX|USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) #define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) -// USB state: uninitialized, addressed, ready for use -typedef enum{ - USB_STATE_DEFAULT, - USB_STATE_ADDRESSED, - USB_STATE_CONFIGURED, - USB_STATE_CONNECTED -} USB_state; - -// EP types -#define EP_TYPE_BULK 0x00 -#define EP_TYPE_CONTROL 0x01 -#define EP_TYPE_ISO 0x02 -#define EP_TYPE_INTERRUPT 0x03 - #define LANG_US (uint16_t)0x0409 #define _USB_STRING_(name, str) \ @@ -106,7 +283,6 @@ static const struct name \ name = {sizeof(name), 0x03, str} #define _USB_LANG_ID_(name, lng_id) \ - \ static const struct name \ { \ uint8_t bLength; \ @@ -115,7 +291,6 @@ static const struct name \ \ } \ name = {0x04, 0x03, lng_id} -#define STRING_LANG_DESCRIPTOR_SIZE_BYTE (4) // EP0 configuration packet typedef struct { @@ -130,57 +305,20 @@ typedef struct { typedef struct{ uint16_t *tx_buf; // transmission buffer address uint16_t txbufsz; // transmission buffer size - uint16_t *rx_buf; // reception buffer address + uint8_t *rx_buf; // reception buffer address void (*func)(); // endpoint action function unsigned rx_cnt : 10; // received data counter } ep_t; -// USB status & its address -typedef struct { - uint8_t USB_Status; - uint16_t USB_Addr; -}usb_dev_t; - -typedef struct { - uint32_t dwDTERate; - uint8_t bCharFormat; - #define USB_CDC_1_STOP_BITS 0 - #define USB_CDC_1_5_STOP_BITS 1 - #define USB_CDC_2_STOP_BITS 2 - uint8_t bParityType; - #define USB_CDC_NO_PARITY 0 - #define USB_CDC_ODD_PARITY 1 - #define USB_CDC_EVEN_PARITY 2 - #define USB_CDC_MARK_PARITY 3 - #define USB_CDC_SPACE_PARITY 4 - uint8_t bDataBits; -} __attribute__ ((packed)) usb_LineCoding; - -typedef struct { - uint8_t bmRequestType; - uint8_t bNotificationType; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; -} __attribute__ ((packed)) usb_cdc_notification; - -extern ep_t endpoints[]; -extern usb_dev_t USB_Dev; -extern uint8_t usbON; -extern config_pack_t setup_packet; -extern uint8_t ep0databuf[]; -extern uint8_t ep0dbuflen; - -void EP0_Handler(); +extern volatile uint8_t usbON; +void USB_setup(); +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)()); void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size); void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size); -int EP_Read(uint8_t number, uint16_t *buf); -usb_LineCoding getLineCoding(); +int EP_Read(uint8_t number, uint8_t *buf); -void linecoding_handler(usb_LineCoding *lc); -void clstate_handler(uint16_t val); -void break_handler(); -void vendor_handler(config_pack_t *packet); - -#endif // __USB_LIB_H__ +// could be [re]defined in usb_dev.c +extern void usb_class_request(config_pack_t *packet, uint8_t *data, uint16_t datalen); +extern void usb_vendor_request(config_pack_t *packet, uint8_t *data, uint16_t datalen); +extern void set_configuration(); diff --git a/F1:F103/Canon_managing_device/usbhw.c b/F1:F103/Canon_managing_device/usbhw.c deleted file mode 100644 index f586d0e..0000000 --- a/F1:F103/Canon_managing_device/usbhw.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * This file is part of the MLX90640 project. - * Copyright 2022 Edward V. Emelianov . - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "hardware.h" -#include "usb.h" -#include "usbhw.h" -#include "usb_lib.h" - -void USB_setup(){ - NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); - NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); - USBPU_OFF(); - RCC->APB1ENR |= RCC_APB1ENR_USBEN; - RCC->APB2ENR |= USB_RCC; - USB->CNTR = USB_CNTR_FRES; // Force USB Reset - for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms - //uint32_t ctr = 0; - USB->CNTR = 0; - USB->BTABLE = 0; - USB->DADDR = 0; - USB->ISTR = 0; - USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts - NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); - USBPU_ON(); -} - -static uint16_t lastaddr = LASTADDR_DEFAULT; -/** - * Endpoint initialisation - * @param number - EP num (0...7) - * @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT) - * @param txsz - transmission buffer size @ USB/CAN buffer - * @param rxsz - reception buffer size @ USB/CAN buffer - * @param uint16_t (*func)(ep_t *ep) - EP handler function - * @return 0 if all OK - */ -int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ - if(number >= STM32ENDPOINTS) return 4; // out of configured amount - if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large - if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) return 2; // out of btable - USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); - USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; - if(rxsz & 1 || rxsz > 512) return 3; // wrong rx buffer size - uint16_t countrx = 0; - if(rxsz < 64) countrx = rxsz / 2; - else{ - if(rxsz & 0x1f) return 3; // should be multiple of 32 - countrx = 31 + rxsz / 32; - } - USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr; - endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr*2); - endpoints[number].txbufsz = txsz; - lastaddr += txsz; - USB_BTABLE->EP[number].USB_COUNT_TX = 0; - USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr; - endpoints[number].rx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr*2); - lastaddr += rxsz; - USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10; - endpoints[number].func = func; - return 0; -} - -// standard IRQ handler -void usb_lp_can_rx0_isr(){ - if(USB->ISTR & USB_ISTR_RESET){ - usbON = 0; - // Reinit registers - USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM; - USB->ISTR = 0; - // Endpoint 0 - CONTROL - // ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes! - lastaddr = LASTADDR_DEFAULT; - // clear address, leave only enable bit - USB->DADDR = USB_DADDR_EF; - USB_Dev.USB_Status = USB_STATE_DEFAULT; - USB->ISTR = ~USB_ISTR_RESET; - if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){ - return; - } - } - if(USB->ISTR & USB_ISTR_CTR){ - // EP number - uint8_t n = USB->ISTR & USB_ISTR_EPID; - // copy status register - uint16_t epstatus = USB->EPnR[n]; - // copy received bytes amount - endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter - // check direction - if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit) - if(n == 0){ // control endpoint - if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack - EP_Read(0, (uint16_t*)&setup_packet); - ep0dbuflen = 0; - // interrupt handler will be called later - }else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf - ep0dbuflen = endpoints[0].rx_cnt; - EP_Read(0, (uint16_t*)&ep0databuf); - } - } - } - // call EP handler - if(endpoints[n].func) endpoints[n].func(endpoints[n]); - } - if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep - usbON = 0; - USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE; - USB->ISTR = ~USB_ISTR_SUSP; - } - if(USB->ISTR & USB_ISTR_WKUP){ // wakeup - USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE); // clear suspend flags - USB->ISTR = ~USB_ISTR_WKUP; - } -} - diff --git a/F1:F103/Canon_managing_device/usbhw.h b/F1:F103/Canon_managing_device/usbhw.h deleted file mode 100644 index 17990aa..0000000 --- a/F1:F103/Canon_managing_device/usbhw.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the MLX90640 project. - * Copyright 2022 Edward V. Emelianov . - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once -#ifndef USBHW_H__ -#define USBHW_H__ - -#include - -#define USB_RCC RCC_APB2ENR_IOPAEN - -// max endpoints number -#define STM32ENDPOINTS 8 -/** - * Buffers size definition - **/ -#define USB_BTABLE_SIZE 512 -// first 64 bytes of USB_BTABLE are registers! -//#define USB_EP0_BASEADDR 64 -// for USB FS EP0 buffers are from 8 to 64 bytes long (64 for PL2303) -#define USB_EP0_BUFSZ 64 -// USB transmit buffer size (64 for PL2303) -#define USB_TXBUFSZ 64 -// USB receive buffer size (64 for PL2303) -#define USB_RXBUFSZ 64 -// EP1 - interrupt - buffer size -#define USB_EP1BUFSZ 8 - -#define USB_BTABLE_BASE 0x40006000 -#define USB_BASE ((uint32_t)0x40005C00) -#define USB ((USB_TypeDef *) USB_BASE) - -#ifdef USB_BTABLE -#undef USB_BTABLE -#endif -#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE)) -#define USB_ISTR_EPID 0x0000000F -#define USB_FNR_LSOF_0 0x00000800 -#define USB_FNR_lSOF_1 0x00001000 -#define USB_LPMCSR_BESL_0 0x00000010 -#define USB_LPMCSR_BESL_1 0x00000020 -#define USB_LPMCSR_BESL_2 0x00000040 -#define USB_LPMCSR_BESL_3 0x00000080 -#define USB_EPnR_CTR_RX 0x00008000 -#define USB_EPnR_DTOG_RX 0x00004000 -#define USB_EPnR_STAT_RX 0x00003000 -#define USB_EPnR_STAT_RX_0 0x00001000 -#define USB_EPnR_STAT_RX_1 0x00002000 -#define USB_EPnR_SETUP 0x00000800 -#define USB_EPnR_EP_TYPE 0x00000600 -#define USB_EPnR_EP_TYPE_0 0x00000200 -#define USB_EPnR_EP_TYPE_1 0x00000400 -#define USB_EPnR_EP_KIND 0x00000100 -#define USB_EPnR_CTR_TX 0x00000080 -#define USB_EPnR_DTOG_TX 0x00000040 -#define USB_EPnR_STAT_TX 0x00000030 -#define USB_EPnR_STAT_TX_0 0x00000010 -#define USB_EPnR_STAT_TX_1 0x00000020 -#define USB_EPnR_EA 0x0000000F -#define USB_COUNTn_RX_BLSIZE 0x00008000 -#define USB_COUNTn_NUM_BLOCK 0x00007C00 -#define USB_COUNTn_RX 0x0000003F - -#define USB_TypeDef USB_TypeDef_custom - -typedef struct { - __IO uint32_t EPnR[STM32ENDPOINTS]; - __IO uint32_t RESERVED[STM32ENDPOINTS]; - __IO uint32_t CNTR; - __IO uint32_t ISTR; - __IO uint32_t FNR; - __IO uint32_t DADDR; - __IO uint32_t BTABLE; -} USB_TypeDef; - -typedef struct{ - __IO uint32_t USB_ADDR_TX; - __IO uint32_t USB_COUNT_TX; - __IO uint32_t USB_ADDR_RX; - __IO uint32_t USB_COUNT_RX; -} USB_EPDATA_TypeDef; - -typedef struct{ - __IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS]; -} USB_BtableDef; - -void USB_setup(); -int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)()); - -#endif // USBHW_H__ diff --git a/F1:F103/Canon_managing_device/version.inc b/F1:F103/Canon_managing_device/version.inc index 3e517d1..c4a6cc0 100644 --- a/F1:F103/Canon_managing_device/version.inc +++ b/F1:F103/Canon_managing_device/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "103" -#define BUILD_DATE "2023-03-17" +#define BUILD_NUMBER "112" +#define BUILD_DATE "2025-03-11" diff --git a/F3:F303/CANbus4BTA/kicad/stm32.kicad_prl b/F3:F303/CANbus4BTA/kicad/stm32.kicad_prl index cd96303..d28e1dc 100644 --- a/F3:F303/CANbus4BTA/kicad/stm32.kicad_prl +++ b/F3:F303/CANbus4BTA/kicad/stm32.kicad_prl @@ -41,7 +41,6 @@ 11, 12, 13, - 14, 15, 16, 17, diff --git a/F3:F303/CANbus4BTA/kicad/stm32.kicad_pro b/F3:F303/CANbus4BTA/kicad/stm32.kicad_pro index 295edf1..abcb1d3 100644 --- a/F3:F303/CANbus4BTA/kicad/stm32.kicad_pro +++ b/F3:F303/CANbus4BTA/kicad/stm32.kicad_pro @@ -550,6 +550,7 @@ }, "schematic": { "annotate_start_num": 0, + "bom_export_filename": "", "bom_fmt_presets": [], "bom_fmt_settings": { "field_delimiter": ",",