From bbf855b054e0122bd550607565016010e32079d3 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Mon, 5 Sep 2022 17:21:38 +0300 Subject: [PATCH] Add USB_SPI --- F1:F103/USB_SPI/Makefile | 152 ++++++++++++++ F1:F103/USB_SPI/Readme | 11 + F1:F103/USB_SPI/hardware.c | 39 ++++ F1:F103/USB_SPI/hardware.h | 56 +++++ F1:F103/USB_SPI/main.c | 84 ++++++++ F1:F103/USB_SPI/proto.c | 324 +++++++++++++++++++++++++++++ F1:F103/USB_SPI/proto.h | 31 +++ F1:F103/USB_SPI/ringbuffer.c | 77 +++++++ F1:F103/USB_SPI/ringbuffer.h | 33 +++ F1:F103/USB_SPI/spi.c | 123 +++++++++++ F1:F103/USB_SPI/spi.h | 39 ++++ F1:F103/USB_SPI/usb.c | 116 +++++++++++ F1:F103/USB_SPI/usb.h | 34 +++ F1:F103/USB_SPI/usb_lib.c | 387 +++++++++++++++++++++++++++++++++++ F1:F103/USB_SPI/usb_lib.h | 186 +++++++++++++++++ F1:F103/USB_SPI/usbhw.c | 129 ++++++++++++ F1:F103/USB_SPI/usbhw.h | 105 ++++++++++ F1:F103/USB_SPI/usbspi.bin | Bin 0 -> 6504 bytes F1:F103/USB_SPI/version.inc | 2 + 19 files changed, 1928 insertions(+) create mode 100644 F1:F103/USB_SPI/Makefile create mode 100644 F1:F103/USB_SPI/Readme create mode 100644 F1:F103/USB_SPI/hardware.c create mode 100644 F1:F103/USB_SPI/hardware.h create mode 100644 F1:F103/USB_SPI/main.c create mode 100644 F1:F103/USB_SPI/proto.c create mode 100644 F1:F103/USB_SPI/proto.h create mode 100644 F1:F103/USB_SPI/ringbuffer.c create mode 100644 F1:F103/USB_SPI/ringbuffer.h create mode 100644 F1:F103/USB_SPI/spi.c create mode 100644 F1:F103/USB_SPI/spi.h create mode 100644 F1:F103/USB_SPI/usb.c create mode 100644 F1:F103/USB_SPI/usb.h create mode 100644 F1:F103/USB_SPI/usb_lib.c create mode 100644 F1:F103/USB_SPI/usb_lib.h create mode 100644 F1:F103/USB_SPI/usbhw.c create mode 100644 F1:F103/USB_SPI/usbhw.h create mode 100755 F1:F103/USB_SPI/usbspi.bin create mode 100644 F1:F103/USB_SPI/version.inc diff --git a/F1:F103/USB_SPI/Makefile b/F1:F103/USB_SPI/Makefile new file mode 100644 index 0000000..a85d4db --- /dev/null +++ b/F1:F103/USB_SPI/Makefile @@ -0,0 +1,152 @@ +BINARY = usbspi +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) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST) + @rmdir $(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/USB_SPI/Readme b/F1:F103/USB_SPI/Readme new file mode 100644 index 0000000..c9f2a8a --- /dev/null +++ b/F1:F103/USB_SPI/Readme @@ -0,0 +1,11 @@ +USB PL2303 emulation. Send data to SPI and read answer. + +Protocol: + "F - change SPI flags (F f val), f== l-LSBFIRST, b-BR [18MHz/2^(b+1)], p-CPOL, h-CPHA\n" + "G - get SPI status\n" + "I - reinit SPI\n" + "R - software reset\n" + "S - send data over SPI\n" + "T - show Tms value\n" + +APB2 clock is 18MHz diff --git a/F1:F103/USB_SPI/hardware.c b/F1:F103/USB_SPI/hardware.c new file mode 100644 index 0000000..e629703 --- /dev/null +++ b/F1:F103/USB_SPI/hardware.c @@ -0,0 +1,39 @@ +/* + * This file is part of the pl2303 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" + +static inline void gpio_setup(){ + // Set APB2 clock to 72/4=18MHz + RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_PPRE2) | RCC_CFGR_PPRE2_DIV4; + // Enable clocks to the GPIO subsystems, turn on AFIO clocking to disable SWD/JTAG + RCC->APB2ENR = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN; + AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE; // for PA15 - USB pullup + // Set led as opendrain output + GPIOC->CRH = CRH(13, CNF_ODOUTPUT | MODE_SLOW); + // setup SPI GPIO - alternate function PP (PA5 - SCK, PA6 - MISO, PA7 - MOSI) + GPIOA->CRL = CRL(5, CNF_AFPP|MODE_FAST) | CRL(6, CNF_FLINPUT) | CRL(7, CNF_AFPP|MODE_FAST); + // USB pullup (PA15) - pushpull output + USBPU_OFF(); + GPIOA->CRH = CRH(15, CNF_PPOUTPUT | MODE_SLOW); +} + +void hw_setup(){ + gpio_setup(); +} + diff --git a/F1:F103/USB_SPI/hardware.h b/F1:F103/USB_SPI/hardware.h new file mode 100644 index 0000000..d9d0354 --- /dev/null +++ b/F1:F103/USB_SPI/hardware.h @@ -0,0 +1,56 @@ +/* + * This file is part of the pl2303 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 __HARDWARE_H__ +#define __HARDWARE_H__ + +#include + +// LED0 - PC13 (bluepill), blinking each second +#define LED0_port GPIOC +#define LED0_pin (1<<13) + +#define LED_blink(x) pin_toggle(x ## _port, x ## _pin) +#define LED_on(x) pin_clear(x ## _port, x ## _pin) +#define LED_off(x) pin_set(x ## _port, x ## _pin) + +#define USBPU_port GPIOA +#define USBPU_pin (1<<15) +#define USBPU_ON() pin_set(USBPU_port, USBPU_pin) +#define USBPU_OFF() pin_clear(USBPU_port, USBPU_pin) + +extern volatile uint32_t Tms; + +void hw_setup(); + +// SPI RX/TX max len +#define SPIBUFSZ (128) + +#define DMA_SPI_Rx_IRQ DMA1_Channel2_IRQn +#define SPIx SPI1 +#define SPI_APB2 RCC_APB2ENR_SPI1EN +#define DMA_SPI DMA1 +#define DMA_SPI_AHBENR RCC_AHBENR_DMA1EN +#define DMA_SPI_TCIF DMA_ISR_TCIF2 +#define DMA_SPI_CTCIF DMA_IFCR_CTCIF2 +#define DMA_SPI_TxChannel DMA1_Channel3 +#define DMA_SPI_RxChannel DMA1_Channel2 +#define DMA_SPI_Rx_ISR dma1_channel2_isr + +#endif // __HARDWARE_H__ diff --git a/F1:F103/USB_SPI/main.c b/F1:F103/USB_SPI/main.c new file mode 100644 index 0000000..71934a7 --- /dev/null +++ b/F1:F103/USB_SPI/main.c @@ -0,0 +1,84 @@ +/* + * This file is part of the pl2303 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 "proto.h" +#include "spi.h" +#include "usb.h" + +#define USBBUFSZ 127 + +volatile uint32_t Tms = 0; + +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; +} + +uint8_t buf[SPIBUFSZ] = {1, 2, 3, 4, 5}; + +int main(void){ + sysreset(); + StartHSE(); + SysTick_Config(72000); + hw_setup(); + USB_setup(); + spi_setup(); + + uint32_t ctr = Tms; + while(1){ + if(Tms - ctr > 499){ + ctr = Tms; + LED_blink(LED0); + } + char *txt = NULL; + usb_proc(); + if((txt = get_USB())){ + const char *ans = parse_cmd(txt); + if(ans) USB_send(ans); + } + uint8_t x = SPI_receive(buf, SPIBUFSZ); + if(x){ + USB_send("SPI receive: "); + for(uint8_t i = 0; i < x; ++i){ + if(i) USB_send(", "); + USB_send(u2hexstr(buf[i])); + } + USB_send("\n"); + } + } +} diff --git a/F1:F103/USB_SPI/proto.c b/F1:F103/USB_SPI/proto.c new file mode 100644 index 0000000..89f49f4 --- /dev/null +++ b/F1:F103/USB_SPI/proto.c @@ -0,0 +1,324 @@ +/* + * This file is part of the pl2303 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 "proto.h" +#include "spi.h" +#include "usb.h" +#include "version.inc" + +char *omit_spaces(const char *buf){ + while(*buf){ + if(*buf > ' ') break; + ++buf; + } + return (char*)buf; +} + +// In case of overflow return `buf` and N==0xffffffff +// read decimal number & return pointer to next non-number symbol +static char *getdec(const char *buf, uint32_t *N){ + char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '9'){ + break; + } + if(num > 429496729 || (num == 429496729 && c > '5')){ // overflow + *N = 0xffffff; + return start; + } + num *= 10; + num += c - '0'; + ++buf; + } + *N = num; + return (char*)buf; +} +// read hexadecimal number (without 0x prefix!) +static char *gethex(const char *buf, uint32_t *N){ + char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + uint8_t M = 0; + if(c >= '0' && c <= '9'){ + M = '0'; + }else if(c >= 'A' && c <= 'F'){ + M = 'A' - 10; + }else if(c >= 'a' && c <= 'f'){ + M = 'a' - 10; + } + if(M){ + if(num & 0xf0000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 4; + num += c - M; + }else{ + break; + } + ++buf; + } + *N = num; + return (char*)buf; +} +// read octal number (without 0 prefix!) +static char *getoct(const char *buf, uint32_t *N){ + char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '7'){ + break; + } + if(num & 0xe0000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 3; + num += c - '0'; + ++buf; + } + *N = num; + return (char*)buf; +} +// read binary number (without b prefix!) +static char *getbin(const char *buf, uint32_t *N){ + char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '1'){ + break; + } + if(num & 0x80000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 1; + if(c == '1') num |= 1; + ++buf; + } + *N = num; + return (char*)buf; +} + +/** + * @brief getnum - read uint32_t from string (dec, hex or bin: 127, 0x7f, 0b1111111) + * @param buf - buffer with number and so on + * @param N - the number read + * @return pointer to first non-number symbol in buf + * (if it is == buf, there's no number or if *N==0xffffffff there was overflow) + */ +char *getnum(const char *txt, uint32_t *N){ + char *nxt = NULL; + char *s = omit_spaces(txt); + if(*s == '0'){ // hex, oct or 0 + if(s[1] == 'x' || s[1] == 'X'){ // hex + nxt = gethex(s+2, N); + if(nxt == s+2) nxt = (char*)txt; + }else if(s[1] > '0'-1 && s[1] < '8'){ // oct + nxt = getoct(s+1, N); + if(nxt == s+1) nxt = (char*)txt; + }else{ // 0 + nxt = s+1; + *N = 0; + } + }else if(*s == 'b' || *s == 'B'){ + nxt = getbin(s+1, N); + if(nxt == s+1) nxt = (char*)txt; + }else{ + nxt = getdec(s, N); + if(nxt == s) nxt = (char*)txt; + } + return nxt; +} + +const char* helpmsg = + "https://github.com/eddyem/stm32samples/tree/master/F1-nolib/USB_SPI build#" BUILD_NUMBER " @ " BUILD_DATE "\n" + "F - change SPI flags (F f val), f== l-LSBFIRST, b-BR [18MHz/2^(b+1)], p-CPOL, h-CPHA\n" + "G - get SPI status\n" + "I - reinit SPI\n" + "R - software reset\n" + "S - send data over SPI\n" + "T - show Tms value\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; +} + +extern uint8_t usbON; +const char *parse_cmd(const char *buf){ + //uint32_t u3; + initbuf(); + if(buf[1] == '\n' || !buf[1]){ // one symbol commands + switch(*buf){ + case 'F': // just watch SPI->CR1 value + add2buf("SPI1->CR1="); add2buf(u2hexstr(SPI_CR1)); + break; + case 'G': + add2buf("SPI "); + switch(SPI_status){ + case SPI_NOTREADY: + add2buf("not ready"); + break; + case SPI_READY: + add2buf("ready"); + break; + case SPI_BUSY: + add2buf("busy"); + break; + default: + add2buf("unknown"); + } + break; + case 'I': + add2buf("Reinit SPI"); + spi_setup(); + break; + case 'R': + USB_send("Soft reset\n"); + NVIC_SystemReset(); + break; + case 'T': + add2buf("Tms="); + add2buf(u2str(Tms)); + break; + default: + return helpmsg; + } + newline(); + return stbuf; + } + uint32_t D = 0, N = 0; + char *nxt; + switch(*buf){ // long messages + case 'F': // SPI flags + nxt = omit_spaces(buf+1); + char c = *nxt; + if(*nxt && *nxt != '\n'){ + buf = nxt + 1; + nxt = getnum(buf, &D); + if(buf == nxt || D > 7) return helpmsg; + } + switch(c){ + case 'b': + SPI_CR1 &= ~SPI_CR1_BR; + SPI_CR1 |= ((uint8_t)D) << 3; + break; + case 'h': + if(D) SPI_CR1 |= SPI_CR1_CPHA; + else SPI_CR1 &= ~SPI_CR1_CPHA; + break; + case 'l': + if(D) SPI_CR1 |= SPI_CR1_LSBFIRST; + else SPI_CR1 &= ~SPI_CR1_LSBFIRST; + break; + case 'p': + if(D) SPI_CR1 |= SPI_CR1_CPOL; + else SPI_CR1 &= ~SPI_CR1_CPOL; + break; + default: + return helpmsg; + } + add2buf("SPI_CR1="); add2buf(u2hexstr(SPI_CR1)); + break; + case 'S': // use stbuf here to store user data + ++buf; + do{ + nxt = getnum(buf, &D); + if(buf == nxt) break; + buf = nxt; + if(D > 0xff){ + USB_send("Number should be from 0 to 0xff\n"); + return NULL; + } + stbuf[N++] = (uint8_t)D; + if(N == STBUFSZ) break; + }while(1); + if(N == 0){ + USB_send("Enter data bytes\n"); + return NULL; + } + USB_send("Send: "); + for(uint32_t i = 0; i < N; ++i){ + if(i) USB_send(", "); + USB_send(u2hexstr(stbuf[i])); + } + USB_send("\n... "); + if(N == SPI_transmit((uint8_t*)stbuf, (uint8_t)N)) USB_send("OK\n"); + else USB_send("Failed\n"); + return NULL; + break; + default: + return buf; + } + newline(); + return stbuf; +} + + +// return string with number `val` +char *u2str(uint32_t val){ + static char strbuf[11]; + char *bufptr = &strbuf[10]; + *bufptr = 0; + if(!val){ + *(--bufptr) = '0'; + }else{ + while(val){ + *(--bufptr) = val % 10 + '0'; + val /= 10; + } + } + return bufptr; +} + +char *u2hexstr(uint32_t val){ + static char strbuf[11] = "0x"; + char *sptr = strbuf + 2; + uint8_t *ptr = (uint8_t*)&val + 3; + int8_t i, j, z=1; + for(i = 0; i < 4; ++i, --ptr){ + if(*ptr == 0){ // omit leading zeros + if(i == 3) z = 0; + if(z) continue; + } + else z = 0; + for(j = 1; j > -1; --j){ + uint8_t half = (*ptr >> (4*j)) & 0x0f; + if(half < 10) *sptr++ = half + '0'; + else *sptr++ = half - 10 + 'a'; + } + } + *sptr = 0; + return strbuf; +} diff --git a/F1:F103/USB_SPI/proto.h b/F1:F103/USB_SPI/proto.h new file mode 100644 index 0000000..e4ecd05 --- /dev/null +++ b/F1:F103/USB_SPI/proto.h @@ -0,0 +1,31 @@ +/* + * This file is part of the pl2303 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 PROTO_H__ +#define PROTO_H__ + +#include + +const char *parse_cmd(const char *buf); +char *omit_spaces(const char *buf); +char *getnum(const char *buf, uint32_t *N); +char *u2str(uint32_t val); +char *u2hexstr(uint32_t val); + +#endif // PROTO_H__ diff --git a/F1:F103/USB_SPI/ringbuffer.c b/F1:F103/USB_SPI/ringbuffer.c new file mode 100644 index 0000000..168185a --- /dev/null +++ b/F1:F103/USB_SPI/ringbuffer.c @@ -0,0 +1,77 @@ +/* + * This file is part of the pl2303 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 + +#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 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); + return l; +} + diff --git a/F1:F103/USB_SPI/ringbuffer.h b/F1:F103/USB_SPI/ringbuffer.h new file mode 100644 index 0000000..5c83f0d --- /dev/null +++ b/F1:F103/USB_SPI/ringbuffer.h @@ -0,0 +1,33 @@ +/* + * This file is part of the pl2303 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 RINGBUFFER_H__ +#define RINGBUFFER_H__ + +#include "usbhw.h" // for USB_TXBUFSZ + +// ring buffer size in bytes +#define RBSIZE (512) +// max reading portion size +#define BLOCKSIZE (USB_TXBUFSZ) + +int RB_read(char s[BLOCKSIZE]); +int RB_write(const char *str, int l); + +#endif // RINGBUFFER_H__ diff --git a/F1:F103/USB_SPI/spi.c b/F1:F103/USB_SPI/spi.c new file mode 100644 index 0000000..2748b0b --- /dev/null +++ b/F1:F103/USB_SPI/spi.c @@ -0,0 +1,123 @@ +/* + * This file is part of the LED_screen project. + * Copyright 2019 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 . + */ + +// memcpy hangs -> use my own + +#include "spi.h" +#include "hardware.h" +#include "usb.h" +#include "proto.h" + +static void mymemcpy(uint8_t *dest, uint8_t *src, int len){ + while(len--) *dest++ = *src++; +} + +// CR1 register default values, can be changed in 'proto.c' +uint32_t SPI_CR1 = SPI_CR1_MSTR | SPI_CR1_BR | SPI_CR1_SSM | SPI_CR1_SSI; + +spiStatus SPI_status = SPI_NOTREADY; +static uint8_t inbuff[SPIBUFSZ] = {0}, outbuff[SPIBUFSZ], lastlen = 0; + +void spi_setup(){ + // configure SPI (transmit only) + RCC->APB2ENR |= SPI_APB2; // Enable the peripheral clock SPI1 + RCC->AHBENR |= DMA_SPI_AHBENR; // and DMA1 + // master, no slave select, BR=F/16, CPOL/CPHA - polarity. + SPIx->CR1 = SPI_CR1; + // Rx/Tx with DMA + SPIx->CR2 = SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN; + // setup SPI1 DMA + // Tx + DMA_SPI_TxChannel->CPAR = (uint32_t)&(SPIx->DR); // hardware + // memory increment, mem->hw, TC irq + DMA_SPI_TxChannel->CCR |= DMA_CCR_MINC | DMA_CCR_DIR; + // Rx + DMA_SPI_RxChannel->CPAR = (uint32_t)&(SPIx->DR); // hardware + DMA_SPI_RxChannel->CCR |= DMA_CCR_MINC | DMA_CCR_TCIE; // mem inc, hw->mem, TC irq + NVIC_SetPriority(DMA_SPI_Rx_IRQ, 0); + NVIC_EnableIRQ(DMA_SPI_Rx_IRQ); + SPI_status = SPI_READY; + SPIx->CR1 |= SPI_CR1_SPE; // enable SPI +} + +/** + * @brief SPI_transmit - transmit data over SPI DMA + * @param buf - data to transmit + * @param len - its length + * @return amount of transmitted data + */ +uint8_t SPI_transmit(const uint8_t *buf, uint8_t len){ + if(!buf || !len) return 0; // bad data format + if(SPI_status != SPI_READY) return 0; // spi not ready to transmit data +#if 0 + for(uint8_t x = 0; x < len; ++x){ + while(!(SPI1->SR & SPI_SR_TXE)); + SPI1->DR = buf[x]; + USB_send(u2hexstr(buf[x])); USB_send(" -> "); + while(!(SPI1->SR & SPI_SR_BSY)); + while(!(SPI1->SR & SPI_SR_RXNE)); + inbuff[x] = SPI1->DR; + USB_send(u2hexstr(inbuff[x])); USB_send("\n"); + } + lastlen = len; + return len; +#endif + if(len > SPIBUFSZ) len = SPIBUFSZ; // buflen too much + mymemcpy(outbuff, (uint8_t*)buf, len); + DMA_SPI_TxChannel->CCR &=~ DMA_CCR_EN; + DMA_SPI_RxChannel->CCR &=~ DMA_CCR_EN; + // refresh broken CMAR + DMA_SPI_TxChannel->CMAR = (uint32_t)outbuff; + DMA_SPI_RxChannel->CMAR = (uint32_t)inbuff; + // set CNDTR + DMA_SPI_TxChannel->CNDTR = len; + DMA_SPI_RxChannel->CNDTR = len; + SPI_status = SPI_BUSY; + lastlen = len; + DMA_SPI_RxChannel->CCR |= DMA_CCR_EN; + DMA_SPI_TxChannel->CCR |= DMA_CCR_EN; + return len; + +} + +/** + * @brief SPI_receive - get received data + * @param buf - buffer with len >= maxlen + * @param maxlen (io) - `buf` length + * @return amount of received bytes + */ +uint8_t SPI_receive(uint8_t *buf, uint8_t maxlen){ + if(SPI_status != SPI_READY) return 0; + if(lastlen == 0) return 0; + /*if(DMA_SPI_RxChannel->CNDTR){ + USB_send("Not all received\n"); + return 0; // not all received yet + }*/ + if(lastlen < maxlen) maxlen = lastlen; + mymemcpy(buf, inbuff, maxlen); + lastlen = 0; + return maxlen; +} + +// SPI1 DMA Rx interrupt +void DMA_SPI_Rx_ISR(){ + if(DMA_SPI->ISR & DMA_SPI_TCIF){ + DMA_SPI->IFCR = DMA_SPI_CTCIF; /* Clear TC flag */ + SPI_status = SPI_READY; + } +} diff --git a/F1:F103/USB_SPI/spi.h b/F1:F103/USB_SPI/spi.h new file mode 100644 index 0000000..6fe357f --- /dev/null +++ b/F1:F103/USB_SPI/spi.h @@ -0,0 +1,39 @@ +/* + * This file is part of the LED_screen project. + * Copyright 2019 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 SPI_H__ +#define SPI_H__ + +#include "stm32f1.h" + +extern uint32_t SPI_CR1; + +typedef enum{ + SPI_NOTREADY, + SPI_READY, + SPI_BUSY +} spiStatus; + +extern spiStatus SPI_status; + +void spi_setup(); +uint8_t SPI_transmit(const uint8_t *buf, uint8_t len); +uint8_t SPI_receive(uint8_t *buf, uint8_t maxlen); + +#endif // SPI_H__ diff --git a/F1:F103/USB_SPI/usb.c b/F1:F103/USB_SPI/usb.c new file mode 100644 index 0000000..f39a217 --- /dev/null +++ b/F1:F103/USB_SPI/usb.c @@ -0,0 +1,116 @@ +/* + * 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(){ + 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/USB_SPI/usb.h b/F1:F103/USB_SPI/usb.h new file mode 100644 index 0000000..6e62b64 --- /dev/null +++ b/F1:F103/USB_SPI/usb.h @@ -0,0 +1,34 @@ +/* + * 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/USB_SPI/usb_lib.c b/F1:F103/USB_SPI/usb_lib.c new file mode 100644 index 0000000..ff84a18 --- /dev/null +++ b/F1:F103/USB_SPI/usb_lib.c @@ -0,0 +1,387 @@ +/* + * 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 +#include "usb_lib.h" + +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; + +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 inline void std_d2h_req(){ + uint16_t status = 0; // bus powered + switch(setup_packet.bRequest){ + case GET_DESCRIPTOR: + get_descriptor(); + break; + case GET_STATUS: + EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered + break; + case GET_CONFIGURATION: + EP_WriteIRQ(0, &configuration, 1); + break; + default: + break; + } +} + +static inline void std_h2d_req(){ + 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; + break; + case SET_CONFIGURATION: + // Now device configured + USB_Dev.USB_Status = USB_STATE_CONFIGURED; + configuration = setup_packet.wValue; + break; + default: + break; + } +} + +/* +bmRequestType: 76543210 +7 direction: 0 - host->device, 1 - device->host +65 type: 0 - standard, 1 - class, 2 - vendor +4..0 getter: 0 - device, 1 - interface, 2 - endpoint, 3 - other +*/ +/** + * 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; + 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); + } + } + } 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; + } + } + epstatus = KEEP_DTOG(USB->EPnR[0]); + if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/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; +} + +/** + * Write data to EP buffer (called from IRQ handler) + * @param number - EP number + * @param *buf - array with data + * @param size - its size + */ +void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){ + if(size > endpoints[number].txbufsz) size = endpoints[number].txbufsz; + 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; + uint32_t *out = (uint32_t *)endpoints[number].tx_buf; + for(int i = 0; i < N2; ++i, ++out){ + *out = buf16[i]; + } + USB_BTABLE->EP[number].USB_COUNT_TX = size; +} + +/** + * Write data to EP buffer (called outside IRQ handler) + * @param number - EP number + * @param *buf - array with data + * @param size - its 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; +} + +/* + * Copy data from EP buffer into user buffer area + * @param *buf - user array for data + * @return amount of data read + */ +int EP_Read(uint8_t number, uint16_t *buf){ + int sz = endpoints[number].rx_cnt; + if(!sz) return 0; + endpoints[number].rx_cnt = 0; + 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; + } + return sz; +} + diff --git a/F1:F103/USB_SPI/usb_lib.h b/F1:F103/USB_SPI/usb_lib.h new file mode 100644 index 0000000..739fe2d --- /dev/null +++ b/F1:F103/USB_SPI/usb_lib.h @@ -0,0 +1,186 @@ +/* + * 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_LIB_H__ +#define __USB_LIB_H__ + +#include +#include "usbhw.h" + +#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 +#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_ADDRESS 0x05 +#define SET_DESCRIPTOR 0x07 // unused +#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 + +// 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 + +// 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 + +#define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX) +#define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX) +#define SETUP_FLAG(epstat) (epstat & USB_EPnR_SETUP) + +// EPnR bits manipulation +#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) \ +static const struct name \ +{ \ + uint8_t bLength; \ + uint8_t bDescriptorType; \ + uint16_t bString[(sizeof(str) - 2) / 2]; \ + \ +} \ +name = {sizeof(name), 0x03, str} + +#define _USB_LANG_ID_(name, lng_id) \ + \ +static const struct name \ +{ \ + uint8_t bLength; \ + uint8_t bDescriptorType; \ + uint16_t bString; \ + \ +} \ +name = {0x04, 0x03, lng_id} +#define STRING_LANG_DESCRIPTOR_SIZE_BYTE (4) + +// EP0 configuration packet +typedef struct { + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} config_pack_t; + +// endpoints state +typedef struct{ + uint16_t *tx_buf; // transmission buffer address + uint16_t txbufsz; // transmission buffer size + uint16_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(); + +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(); + +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__ diff --git a/F1:F103/USB_SPI/usbhw.c b/F1:F103/USB_SPI/usbhw.c new file mode 100644 index 0000000..f586d0e --- /dev/null +++ b/F1:F103/USB_SPI/usbhw.c @@ -0,0 +1,129 @@ +/* + * 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/USB_SPI/usbhw.h b/F1:F103/USB_SPI/usbhw.h new file mode 100644 index 0000000..17990aa --- /dev/null +++ b/F1:F103/USB_SPI/usbhw.h @@ -0,0 +1,105 @@ +/* + * 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/USB_SPI/usbspi.bin b/F1:F103/USB_SPI/usbspi.b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