From aa0ffe8a72c47ac891b8faab70523f1c29e3982e Mon Sep 17 00:00:00 2001 From: eddyem Date: Mon, 9 Nov 2020 21:26:25 +0300 Subject: [PATCH] Add SEVEN CDCs --- F1-nolib/SevenCDCs/Makefile | 159 +++++ F1-nolib/SevenCDCs/Readme | 1 + F1-nolib/SevenCDCs/cdcacmcore.bin | Bin 0 -> 5684 bytes F1-nolib/SevenCDCs/hardware.c | 70 +++ F1-nolib/SevenCDCs/hardware.h | 49 ++ F1-nolib/SevenCDCs/main.c | 103 ++++ F1-nolib/SevenCDCs/proto.c | 221 +++++++ F1-nolib/SevenCDCs/proto.h | 55 ++ F1-nolib/SevenCDCs/usb.c | 175 ++++++ F1-nolib/SevenCDCs/usb.h | 41 ++ F1-nolib/SevenCDCs/usb_defs.h | 107 ++++ F1-nolib/SevenCDCs/usb_lib.c | 988 ++++++++++++++++++++++++++++++ F1-nolib/SevenCDCs/usb_lib.h | 215 +++++++ 13 files changed, 2184 insertions(+) create mode 100644 F1-nolib/SevenCDCs/Makefile create mode 100644 F1-nolib/SevenCDCs/Readme create mode 100755 F1-nolib/SevenCDCs/cdcacmcore.bin create mode 100644 F1-nolib/SevenCDCs/hardware.c create mode 100644 F1-nolib/SevenCDCs/hardware.h create mode 100644 F1-nolib/SevenCDCs/main.c create mode 100644 F1-nolib/SevenCDCs/proto.c create mode 100644 F1-nolib/SevenCDCs/proto.h create mode 100644 F1-nolib/SevenCDCs/usb.c create mode 100644 F1-nolib/SevenCDCs/usb.h create mode 100644 F1-nolib/SevenCDCs/usb_defs.h create mode 100644 F1-nolib/SevenCDCs/usb_lib.c create mode 100644 F1-nolib/SevenCDCs/usb_lib.h diff --git a/F1-nolib/SevenCDCs/Makefile b/F1-nolib/SevenCDCs/Makefile new file mode 100644 index 0000000..e906fec --- /dev/null +++ b/F1-nolib/SevenCDCs/Makefile @@ -0,0 +1,159 @@ +# make debug adds -DEBUG -Werror +# make ADDEFS="additional defs" +BINARY = cdcacmcore +BOOTPORT ?= /dev/ttyUSB0 +BOOTSPEED ?= 115200 +# MCU FAMILY +FAMILY ?= F1 +# MCU code +MCU ?= F103x8 +# density (stm32f10x.h, lines 70-84) +DENSITY ?= MD +# change this linking script depending on particular MCU model, +LDSCRIPT ?= stm32f103x8.ld +DEFS = ${ADDEFS} -DVERSION=\"0.0.1\" +TARGET := RELEASE +# proxy GPS output over USART1 +#DEFS += -DUSART1PROXY + +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 +# don't replace ld with gcc: the binary size would be much greater!! +LD := $(PREFIX)-ld +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 +CFLAGS += -Wall -Wextra -Wshadow +CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector +CFLAGS += $(ARCH_FLAGS) + +############################################################################### +# Linker flags +LDFLAGS += --static -nostartfiles -nostdlibs +LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB) +LDFLAGS += -T$(LDSCRIPT) + +############################################################################### +# Used libraries +LDLIBS += -lc $(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: $(OBJDIR)/RELEASE bin list size +release: all + +debug: CFLAGS += -DEBUG -Werror +debug: $(OBJDIR)/DEBUG bin list size + +$(OBJDIR)/DEBUG: + @rm -rf $(OBJDIR) + @mkdir $(OBJDIR) + @> $(OBJDIR)/DEBUG + @echo "TARGET: DEBUG" + echo "CFLAGS += -DEBUG -Werror" > $(OBJDIR)/CFLAGS +$(OBJDIR)/RELEASE: + @rm -rf $(OBJDIR) + @mkdir $(OBJDIR) + @> $(OBJDIR)/RELEASE + @echo "TARGET: RELEASE" + echo "" > $(OBJDIR)/CFLAGS + +elf: $(ELF) +bin: $(BIN) +hex: $(HEX) +list: $(LIST) + +ifneq ($(MAKECMDGOALS),clean) +-include $(DEPS) +-include $(OBJDIR)/CFLAGS +endif + +$(OBJDIR): + mkdir $(OBJDIR) + +$(STARTUP): $(INC_DIR)/startup/vector.c + $(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $< + +$(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) $(HEX) + @$(RM) -rf $(OBJDIR) 2>/dev/null || true + + +flash: $(BIN) + @echo " FLASH $(BIN)" + $(STFLASH) --reset 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-nolib/SevenCDCs/Readme b/F1-nolib/SevenCDCs/Readme new file mode 100644 index 0000000..37ffa79 --- /dev/null +++ b/F1-nolib/SevenCDCs/Readme @@ -0,0 +1 @@ +SEVEN CDCs @ one device diff --git a/F1-nolib/SevenCDCs/cdcacmcore.bin b/F1-nolib/SevenCDCs/cdcacmcore.bin new file mode 100755 index 0000000000000000000000000000000000000000..9b4049b8f1babf48bd10789e4bee1b9a09fed8a3 GIT binary patch literal 5684 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 literal 0 HcmV?d00001 diff --git a/F1-nolib/SevenCDCs/hardware.c b/F1-nolib/SevenCDCs/hardware.c new file mode 100644 index 0000000..ce4475e --- /dev/null +++ b/F1-nolib/SevenCDCs/hardware.c @@ -0,0 +1,70 @@ +/* + * geany_encoding=koi8-r + * hardware.c - hardware-dependent macros & functions + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#include "hardware.h" + +// pause in milliseconds for some purposes +void pause_ms(uint32_t pause){ + uint32_t Tnxt = Tms + pause; + while(Tms < Tnxt) nop(); +} + +static inline void gpio_setup(){ + // Enable clocks to the GPIO subsystems (PB for ADC), turn on AFIO clocking to disable SWD/JTAG + RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN; + // turn off SWJ/JTAG +// AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE; + // turn off USB pullup + GPIOA->ODR = (1<<13); // turn off usb pullup & turn on pullups for buttons + // Set led as opendrain output + GPIOC->CRH |= CRH(13, CNF_ODOUTPUT|MODE_SLOW); + // USB pullup (PA13) - opendrain output + GPIOA->CRH = CRH(13, CNF_ODOUTPUT|MODE_SLOW); +} + +void hw_setup(){ + gpio_setup(); +} + +void iwdg_setup(){ + uint32_t tmout = 16000000; + /* Enable the peripheral clock RTC */ + /* (1) Enable the LSI (40kHz) */ + /* (2) Wait while it is not ready */ + RCC->CSR |= RCC_CSR_LSION; /* (1) */ + while((RCC->CSR & RCC_CSR_LSIRDY) != RCC_CSR_LSIRDY){if(--tmout == 0) break;} /* (2) */ + /* Configure IWDG */ + /* (1) Activate IWDG (not needed if done in option bytes) */ + /* (2) Enable write access to IWDG registers */ + /* (3) Set prescaler by 64 (1.6ms for each tick) */ + /* (4) Set reload value to have a rollover each 2s */ + /* (5) Check if flags are reset */ + /* (6) Refresh counter */ + IWDG->KR = IWDG_START; /* (1) */ + IWDG->KR = IWDG_WRITE_ACCESS; /* (2) */ + IWDG->PR = IWDG_PR_PR_1; /* (3) */ + IWDG->RLR = 1250; /* (4) */ + tmout = 16000000; + while(IWDG->SR){if(--tmout == 0) break;} /* (5) */ + IWDG->KR = IWDG_REFRESH; /* (6) */ +} diff --git a/F1-nolib/SevenCDCs/hardware.h b/F1-nolib/SevenCDCs/hardware.h new file mode 100644 index 0000000..51edec7 --- /dev/null +++ b/F1-nolib/SevenCDCs/hardware.h @@ -0,0 +1,49 @@ +/* + * geany_encoding=koi8-r + * hardware.h + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#pragma once +#ifndef __HARDWARE_H__ +#define __HARDWARE_H__ + +#include "stm32f1.h" + +// LED0 - PC13 (bluepill), blinking each second +#define LED0_port GPIOC +#define LED0_pin (1<<13) + +// USB pullup (not present in bluepill) - PA13 +#define USBPU_port GPIOA +#define USBPU_pin (1<<13) +#define USBPU_ON() pin_clear(USBPU_port, USBPU_pin) +#define USBPU_OFF() pin_set(USBPU_port, USBPU_pin) + +#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) + +extern volatile uint32_t Tms; + +void hw_setup(); +void iwdg_setup(); +void pause_ms(uint32_t pause); + +#endif // __HARDWARE_H__ diff --git a/F1-nolib/SevenCDCs/main.c b/F1-nolib/SevenCDCs/main.c new file mode 100644 index 0000000..8ab2072 --- /dev/null +++ b/F1-nolib/SevenCDCs/main.c @@ -0,0 +1,103 @@ +/* + * main.c + * + * Copyright 2017 Edward V. Emelianoff + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include "hardware.h" +#include "proto.h" +#include "usb.h" +#include "usb_lib.h" + +volatile uint32_t Tms = 0; + +/* Called when systick fires */ +void sys_tick_handler(void){ + ++Tms; +} + +#define USBBUF 63 +// usb getline +static char *get_USB(int idx){ + static char tmpbuf[8][USBBUF+1]; + static char *curptr[8] = {tmpbuf[0], tmpbuf[1], tmpbuf[2], tmpbuf[3], tmpbuf[4], tmpbuf[5], tmpbuf[6], tmpbuf[7]}; + static int rest[8] = {USBBUF, USBBUF, USBBUF, USBBUF, USBBUF, USBBUF, USBBUF, USBBUF}; + uint8_t x = USB_receive(idx, (uint8_t*)curptr[idx]); + if(!x) return NULL; + curptr[idx][x] = 0; + if(x == 1 && *curptr[idx] == 0x7f){ // backspace + if(curptr[idx] > tmpbuf[idx]){ + --curptr[idx]; + USND(idx, "\b \b"); + } + return NULL; + } + USB_sendstr(idx, curptr[idx]); // echo + if(curptr[idx][x-1] == '\n'){ // || curptr[x-1] == '\r'){ + curptr[idx] = tmpbuf[idx]; + rest[idx] = USBBUF; + // omit empty lines + if(tmpbuf[idx][0] == '\n') return NULL; + // and wrong empty lines + if(tmpbuf[idx][0] == '\r' && tmpbuf[idx][1] == '\n') return NULL; + return tmpbuf[idx]; + } + curptr[idx] += x; rest[idx] -= x; + if(rest[idx] <= 0){ // buffer overflow + curptr[idx] = tmpbuf[idx]; + rest[idx] = USBBUF; + } + return NULL; +} + +int main(void){ + uint32_t lastT = 0; + sysreset(); + StartHSE(); + SysTick_Config(72000); + hw_setup(); + USBPU_OFF(); + /*if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured + SEND("WDGRESET=1"); newline(); + } + if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured + SEND("SOFTRESET=1"); newline(); + }*/ + RCC->CSR |= RCC_CSR_RMVF; // remove reset flags + + USB_setup(); + iwdg_setup(); + USBPU_ON(); + while(1){ + IWDG->KR = IWDG_REFRESH; // refresh watchdog + if(Tms - lastT > 499){ + LED_blink(LED0); + lastT = Tms; + } + usb_proc(); + for(uint8_t idx = 1; idx < 8; ++idx){ + char *txt = get_USB(idx); + if(txt){ + IWDG->KR = IWDG_REFRESH; + cmd_parser(txt); + } + } + } + return 0; +} + diff --git a/F1-nolib/SevenCDCs/proto.c b/F1-nolib/SevenCDCs/proto.c new file mode 100644 index 0000000..2634214 --- /dev/null +++ b/F1-nolib/SevenCDCs/proto.c @@ -0,0 +1,221 @@ +/* + * geany_encoding=koi8-r + * proto.c + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#include "hardware.h" +#include "proto.h" +#include "usb.h" + +#include +#include // strlen + +static char buff[BUFSZ+1], *bptr = buff; +static uint8_t blen = 0; + +void sendbuf(){ + IWDG->KR = IWDG_REFRESH; + if(blen == 0) return; + *bptr = 0; + for(uint8_t i = 1; i < 8; ++i) USB_sendstr(i, buff); + bptr = buff; + blen = 0; +} + +void bufputchar(char ch){ + if(blen > BUFSZ-1){ + sendbuf(); + } + *bptr++ = ch; + ++blen; +} + +void addtobuf(const char *txt){ + IWDG->KR = IWDG_REFRESH; + while(*txt) bufputchar(*txt++); +} + +char *omit_spaces(char *buf){ + while(*buf){ + if(*buf > ' ') break; + ++buf; + } + return buf; +} + +// THERE'S NO OVERFLOW PROTECTION IN NUMBER READ PROCEDURES! +// read decimal number +static char *getdec(char *buf, uint32_t *N){ + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '9'){ + break; + } + num *= 10; + num += c - '0'; + ++buf; + } + *N = num; + return buf; +} +// read hexadecimal number (without 0x prefix!) +static char *gethex(char *buf, uint32_t *N){ + 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){ + num <<= 4; + num += c - M; + }else{ + break; + } + ++buf; + } + *N = num; + return buf; +} +// read binary number (without 0b prefix!) +static char *getbin(char *buf, uint32_t *N){ + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '1'){ + break; + } + num <<= 1; + if(c == '1') num |= 1; + ++buf; + } + *N = num; + return 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) + */ +char *getnum(char *txt, uint32_t *N){ + if(*txt == '0'){ + if(txt[1] == 'x' || txt[1] == 'X') return gethex(txt+2, N); + if(txt[1] == 'b' || txt[1] == 'B') return getbin(txt+2, N); + } + return getdec(txt, N); +} + +/** + * @brief cmd_parser - command parsing + * @param txt - buffer with commands & data + * @param isUSB - == 1 if data got from USB + */ +void cmd_parser(char *txt){ + char _1st = txt[0]; + /* + * parse long commands here + */ + /*switch(_1st){ + case 'a': + addIGN(txt + 1); + goto eof; + break; + }*/ + if(txt[1] != '\n') *txt = '?'; // help for wrong message length + switch(_1st){ + case 'I': + SEND("N ifaces: "); + bufputchar(ifacesno + '0'); + SEND(", errcode: "); + bufputchar(errcode + '0'); + SEND("\nStatus:\n"); + for(int i = 1; i < 8; ++i){ + bufputchar(i + '0'); SEND(": "); + if(dontans[i]) SEND("absent"); + else SEND("present"); + newline(); + } + newline(); + break; + case 'R': + SEND("Soft reset\n"); + sendbuf(); + pause_ms(5); // a little pause to transmit data + NVIC_SystemReset(); + break; + case 'T': + SEND("Time (ms): "); + printu(Tms); + newline(); + break; + default: // help + SEND( + "'I' - ifacesno\n" + "'R' - software reset\n" + "'T' - get time from start (ms)\n" + ); + break; + } + newline(); + sendbuf(); +} + +// print 32bit unsigned int +void printu(uint32_t val){ + char buf[11], *bufptr = &buf[10]; + *bufptr = 0; + if(!val){ + *(--bufptr) = '0'; + }else{ + while(val){ + *(--bufptr) = val % 10 + '0'; + val /= 10; + } + } + addtobuf(bufptr); +} + +// print 32bit unsigned int as hex +void printuhex(uint32_t val){ + addtobuf("0x"); + 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) bufputchar(half + '0'); + else bufputchar(half - 10 + 'a'); + } + } +} + diff --git a/F1-nolib/SevenCDCs/proto.h b/F1-nolib/SevenCDCs/proto.h new file mode 100644 index 0000000..e979633 --- /dev/null +++ b/F1-nolib/SevenCDCs/proto.h @@ -0,0 +1,55 @@ +/* + * geany_encoding=koi8-r + * proto.h + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#pragma once +#ifndef __PROTO_H__ +#define __PROTO_H__ + +#include "stm32f1.h" +#include "hardware.h" + +#define BUFSZ (64) + +// macro for static strings +#define SEND(str) do{addtobuf(str);}while(0) + +#ifdef EBUG +#define MSG(str) do{addtobuf(__FILE__ " (L" STR(__LINE__) "): " str);}while(0) +#else +#define MSG(str) +#endif + +#define newline() do{bufputchar('\n');}while(0) +// newline with buffer sending over USART +#define NL() do{bufputchar('\n'); sendbuf();}while(0) + +void cmd_parser(char *buf); +void addtobuf(const char *txt); +void bufputchar(char ch); +void printu(uint32_t val); +void printuhex(uint32_t val); +void sendbuf(); + +char *omit_spaces(char *buf); +char *getnum(char *buf, uint32_t *N); + +#endif // __PROTO_H__ diff --git a/F1-nolib/SevenCDCs/usb.c b/F1-nolib/SevenCDCs/usb.c new file mode 100644 index 0000000..e60ef60 --- /dev/null +++ b/F1-nolib/SevenCDCs/usb.c @@ -0,0 +1,175 @@ +/* + * geany_encoding=koi8-r + * usb.c - base functions for different USB types + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#include "usb.h" +#include "usb_lib.h" + +static volatile uint8_t tx_succesfull[8] = {1,1,1,1,1,1,1,1}; // zero's idx is omit +static uint8_t rxNE[8] = {0}; // zero's idx is omit + +uint8_t dontans[8] = {0}; + +static void rxtx_Handler(uint8_t epno){ + uint16_t epstatus = KEEP_DTOG(USB->EPnR[epno]); + if(RX_FLAG(epstatus)){ + rxNE[epno] = 1; + epstatus = (epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // keep stat Tx & set valid RX, clear CTR Rx + }else{ + tx_succesfull[epno] = 1; + epstatus = (epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX; // keep stat Tx & set valid RX, clear CTR Tx + } + USB->EPnR[epno] = epstatus; +} + +void USB_setup(){ + NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); + NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); + 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 | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts + NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); +} + +static int usbwr(uint8_t idx, const uint8_t *buf, uint16_t l){ + uint32_t ctra = 200000; + while(--ctra && tx_succesfull[idx] == 0){ + IWDG->KR = IWDG_REFRESH; + } + if(tx_succesfull[idx] == 0){ + dontans[idx] = 1; + return 1; + }else dontans[idx] = 0; + tx_succesfull[idx] = 0; + EP_Write(idx, buf, l); + /* + ctra = 1000000; + while(--ctra && tx_succesfull[idx] == 0){ + IWDG->KR = IWDG_REFRESH; + } + //if(tx_succesfull[idx] == 0){usbON = 0; return 1;} // usb is OFF? + if(tx_succesfull[idx] == 0){return 1;} // this channel is OFF? + */ + return 0; +} + +static uint8_t usbbuff[8][USB_TXBUFSZ-1]; // temporary buffer (63 - to prevent need of ZLP) +static uint8_t buflen[8] = {0}; // amount of symbols in usbbuff + +// send next up to 64 bytes of data in usbbuff +static void send_next(){ + for(uint8_t idx = 1; idx < 8; ++idx){ + if(!buflen[idx] || !tx_succesfull[idx]) continue; + tx_succesfull[idx] = 0; + EP_Write(idx, usbbuff[idx], buflen[idx]); + buflen[idx] = 0; + } +} + +// unblocking sending - just fill a buffer +void USB_send(uint8_t idx, const uint8_t *buf, uint16_t len){ + if(!usbON || !len) return; + if(len > USB_TXBUFSZ-1 - buflen[idx]){ + usbwr(idx, usbbuff[idx], buflen[idx]); + buflen[idx] = 0; + } + if(len > USB_TXBUFSZ-1){ + USB_send_blk(idx, buf, len); + return; + } + while(len--) usbbuff[idx][buflen[idx]++] = *buf++; +} + +// send zero-terminated string +void USB_sendstr(uint8_t idx, const char *str){ + uint16_t l = 0; + const char *ptr = str; + while(*ptr++) ++l; + USB_send(idx, (uint8_t*)str, l); +} + +// blocking sending +void USB_send_blk(uint8_t idx, const uint8_t *buf, uint16_t len){ + if(!usbON || !len) return; // USB disconnected + if(buflen[idx]){ + usbwr(idx, usbbuff[idx], buflen[idx]); + buflen[idx] = 0; + } + int needzlp = 0; + while(len){ + if(len == USB_TXBUFSZ) needzlp = 1; + uint16_t s = (len > USB_TXBUFSZ) ? USB_TXBUFSZ : len; + if(usbwr(idx, buf, s)) return; + len -= s; + buf += s; + } + if(needzlp){ + usbwr(idx, NULL, 0); + } +} + +uint8_t ifacesno = 0, errcode = 0; + +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 + ifacesno = 0; + for(uint8_t i = 1; i < 8; ++i){ + if((errcode = EP_Init(i, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_Handler))) break; + ++ifacesno; + } + 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; + send_next(); + } +} + +/** + * @brief USB_receive + * @param buf (i) - buffer[64] for received data + * @return amount of received bytes + */ +uint8_t USB_receive(uint8_t idx, uint8_t *buf){ + if(idx < 1 || idx > 7) return 0; + if(!usbON || !rxNE[idx]) return 0; + uint8_t sz = EP_Read(idx, (uint16_t*)buf); + uint16_t epstatus = KEEP_DTOG(USB->EPnR[idx]); + // keep stat_tx & set ACK rx + USB->EPnR[idx] = (epstatus & ~(USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX; + rxNE[idx] = 0; + return sz; +} diff --git a/F1-nolib/SevenCDCs/usb.h b/F1-nolib/SevenCDCs/usb.h new file mode 100644 index 0000000..89fc6e4 --- /dev/null +++ b/F1-nolib/SevenCDCs/usb.h @@ -0,0 +1,41 @@ +/* + * geany_encoding=koi8-r + * usb.h + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#pragma once +#ifndef __USB_H__ +#define __USB_H__ + +#include "hardware.h" + +// send string with constant length +#define USND(idx, str) do{USB_send(idx, (uint8_t*)str, sizeof(str)-1);}while(0) +extern uint8_t ifacesno, errcode; +extern uint8_t dontans[8]; + +void USB_setup(); +void usb_proc(); +void USB_send(uint8_t idx, const uint8_t *buf, uint16_t len); +void USB_sendstr(uint8_t idx, const char *str); +void USB_send_blk(uint8_t idx, const uint8_t *buf, uint16_t len); +uint8_t USB_receive(uint8_t idx, uint8_t *buf); + +#endif // __USB_H__ diff --git a/F1-nolib/SevenCDCs/usb_defs.h b/F1-nolib/SevenCDCs/usb_defs.h new file mode 100644 index 0000000..1f908de --- /dev/null +++ b/F1-nolib/SevenCDCs/usb_defs.h @@ -0,0 +1,107 @@ +/* + * geany_encoding=koi8-r + * usb_defs.h + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#pragma once +#ifndef __USB_DEFS_H__ +#define __USB_DEFS_H__ + +#include + +// 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 16 +// USB receive buffer size (64 for PL2303) +#define USB_RXBUFSZ 16 +// 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 + +#ifdef USB_TypeDef +#define USB_TypeDef USB_TypeDef_custom +#endif + +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; + +#endif // __USB_DEFS_H__ diff --git a/F1-nolib/SevenCDCs/usb_lib.c b/F1-nolib/SevenCDCs/usb_lib.c new file mode 100644 index 0000000..2c722e4 --- /dev/null +++ b/F1-nolib/SevenCDCs/usb_lib.c @@ -0,0 +1,988 @@ +/* + * geany_encoding=koi8-r + * usb_lib.c + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#include +#include "usb_lib.h" + +ep_t endpoints[STM32ENDPOINTS]; + +usb_dev_t USB_Dev; +static usb_LineCoding lineCoding = {115200, 0, 0, 8}; +static config_pack_t setup_packet; +static uint8_t ep0databuf[EP0DATABUF_SIZE]; +static 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 0x00 +#define bcdUSB_H 0x02 +// Class - Misc (EF), subclass - common (2), protocol - interface association descr (1) +#define bDeviceClass 0xEF +#define bDeviceSubClass 0x02 +#define bDeviceProtocol 0x01 +#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 + // 0483:5740 (VID:PID) - stm32 VCP + 0x83, 0x04, 0x40, 0x57, + 0x00, // bcdDevice_Ver_L + 0x02, // bcdDevice_Ver_H + 0x01, // iManufacturer + 0x02, // iProduct + 0x03, // 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 */ + 0xD7, /* wTotalLength:no of returned bytes (9+7*66=471, 0x1D7 */ + 0x01, + 14, /* bNumInterfaces: 14 interfaces (7*2) */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ + 0x80, /* bmAttributes - Bus powered */ + 0x32, /* MaxPower 100 mA */ + + /*---------------------------------------------------------------------------*/ + // IAD0 (66 bytes) + 0x08, // bLength: Interface Descriptor size + 0x0B, // bDescriptorType: IAD + 0, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass: CDC + 0x02, // bFunctionSubClass + 0x01, // bFunctionProtocol (0 or 1?) + 0x00, // Interface string index + /*---------------------------------------------------------------------------*/ + /* Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: Interface */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol */ + 0x00, /* iInterface: */ + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ + 0x01, /* bDataInterface: 1 */ + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ + 0x00, /* bMasterInterface: Communication class interface */ + 0x01, /* bSlaveInterface0: Data Class Interface */ + /*Endpoint 10 Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x88, /* bEndpointAddress IN8 - non-existant! */ + 0x03, /* bmAttributes: Interrupt */ + (USB_EP1BUFSZ & 0xff), /* wMaxPacketSize LO: */ + (USB_EP1BUFSZ >> 8), /* wMaxPacketSize HI: */ + 0x10, /* bInterval: */ + /*---------------------------------------------------------------------------*/ + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x01, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x02, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + /*Endpoint IN Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ + 0x81, /* bEndpointAddress IN1 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*Endpoint OUT Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ + 0x01, /* bEndpointAddress OUT1 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*---------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------*/ + // IAD1: interfaces 2&3, EP2 + 0x08, // bLength: Interface Descriptor size + 0x0B, // bDescriptorType: IAD +/**/ 2, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass: CDC + 0x02, // bFunctionSubClass + 0x01, // bFunctionProtocol (0 or 1?) + 0x02, // iFunction + /*---------------------------------------------------------------------------*/ + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: Interface */ +/**/ 2, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol */ + 0x00, /* iInterface: */ + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ +/**/ 3, /* bDataInterface: 3 */ + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ +/**/ 2, /* bMasterInterface: Communication class interface */ +/**/ 3, /* bSlaveInterface0: Data Class Interface */ + /*Endpoint 11 Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x89, /* bEndpointAddress IN9 - non-existant! */ + 0x03, /* bmAttributes: Interrupt */ + (USB_EP1BUFSZ & 0xff), /* wMaxPacketSize LO: */ + (USB_EP1BUFSZ >> 8), /* wMaxPacketSize HI: */ + 0x10, /* bInterval: */ + /*---------------------------------------------------------------------------*/ + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + 0x04, /* bDescriptorType: */ +/**/ 3, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x02, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + /*Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x82, /* bEndpointAddress IN2 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x02, /* bEndpointAddress OUT2 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*---------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------*/ + // IAD2: interfaces 4&5, EP3 + 0x08, // bLength: Interface Descriptor size + 0x0B, // bDescriptorType: IAD +/**/ 4, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass: CDC + 0x02, // bFunctionSubClass + 0x01, // bFunctionProtocol (0 or 1?) + 0x02, // iFunction + /*---------------------------------------------------------------------------*/ + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: Interface */ +/**/ 4, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol */ + 0x00, /* iInterface: */ + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ +/**/ 5, /* bDataInterface */ + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ +/**/ 4, /* bMasterInterface: Communication class interface */ +/**/ 5, /* bSlaveInterface0: Data Class Interface */ + /*Endpoint 11 Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x8a, /* bEndpointAddress IN10 - non-existant! */ + 0x03, /* bmAttributes: Interrupt */ + (USB_EP1BUFSZ & 0xff), /* wMaxPacketSize LO: */ + (USB_EP1BUFSZ >> 8), /* wMaxPacketSize HI: */ + 0x10, /* bInterval: */ + /*---------------------------------------------------------------------------*/ + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + 0x04, /* bDescriptorType: */ +/**/ 5, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x02, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + /*Endpoint IN 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 */ + /*Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x03, /* bEndpointAddress OUT3 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*---------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------*/ + // IAD3: interfaces 6&7, EP4 + 0x08, // bLength: Interface Descriptor size + 0x0B, // bDescriptorType: IAD +/**/ 6, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass: CDC + 0x02, // bFunctionSubClass + 0x01, // bFunctionProtocol (0 or 1?) + 0x02, // iFunction + /*---------------------------------------------------------------------------*/ + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: Interface */ +/**/ 6, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol */ + 0x00, /* iInterface: */ + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ +/**/ 7, /* bDataInterface */ + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ +/**/ 6, /* bMasterInterface: Communication class interface */ +/**/ 7, /* bSlaveInterface0: Data Class Interface */ + /* Interrupt Endpoint Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x8b, /* bEndpointAddress IN11 - non-existant! */ + 0x03, /* bmAttributes: Interrupt */ + (USB_EP1BUFSZ & 0xff), /* wMaxPacketSize LO: */ + (USB_EP1BUFSZ >> 8), /* wMaxPacketSize HI: */ + 0x10, /* bInterval: */ + /*---------------------------------------------------------------------------*/ + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + 0x04, /* bDescriptorType: */ +/**/ 7, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x02, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + /*Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x84, /* bEndpointAddress IN4 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x04, /* bEndpointAddress OUT4 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*---------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------*/ + // IAD4: interfaces 8&9, EP5 + 0x08, // bLength: Interface Descriptor size + 0x0B, // bDescriptorType: IAD +/**/ 8, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass: CDC + 0x02, // bFunctionSubClass + 0x01, // bFunctionProtocol (0 or 1?) + 0x02, // iFunction + /*---------------------------------------------------------------------------*/ + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: Interface */ +/**/ 8, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol */ + 0x00, /* iInterface: */ + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ +/**/ 9, /* bDataInterface */ + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ +/**/ 8, /* bMasterInterface: Communication class interface */ +/**/ 9, /* bSlaveInterface0: Data Class Interface */ + /* Interrupt Endpoint Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x8c, /* bEndpointAddress IN12 - non-existant! */ + 0x03, /* bmAttributes: Interrupt */ + (USB_EP1BUFSZ & 0xff), /* wMaxPacketSize LO: */ + (USB_EP1BUFSZ >> 8), /* wMaxPacketSize HI: */ + 0x10, /* bInterval: */ + /*---------------------------------------------------------------------------*/ + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + 0x04, /* bDescriptorType: */ +/**/ 9, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x02, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + /*Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x85, /* bEndpointAddress IN5 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x05, /* bEndpointAddress OUT5 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*---------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------*/ + // IAD5: interfaces 10&11, EP6 + 0x08, // bLength: Interface Descriptor size + 0x0B, // bDescriptorType: IAD +/**/ 10, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass: CDC + 0x02, // bFunctionSubClass + 0x01, // bFunctionProtocol (0 or 1?) + 0x02, // iFunction + /*---------------------------------------------------------------------------*/ + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: Interface */ +/**/ 10, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol */ + 0x00, /* iInterface: */ + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ +/**/ 11, /* bDataInterface */ + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ +/**/ 10, /* bMasterInterface: Communication class interface */ +/**/ 11, /* bSlaveInterface0: Data Class Interface */ + /* Interrupt Endpoint Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x8d, /* bEndpointAddress IN13 - non-existant! */ + 0x03, /* bmAttributes: Interrupt */ + (USB_EP1BUFSZ & 0xff), /* wMaxPacketSize LO: */ + (USB_EP1BUFSZ >> 8), /* wMaxPacketSize HI: */ + 0x10, /* bInterval: */ + /*---------------------------------------------------------------------------*/ + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + 0x04, /* bDescriptorType: */ +/**/ 11, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x02, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + /*Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x86, /* bEndpointAddress IN6 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x06, /* bEndpointAddress OUT6 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*---------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------*/ + // IAD6: interfaces 12&13, EP7 + 0x08, // bLength: Interface Descriptor size + 0x0B, // bDescriptorType: IAD +/**/ 12, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass: CDC + 0x02, // bFunctionSubClass + 0x01, // bFunctionProtocol (0 or 1?) + 0x02, // iFunction + /*---------------------------------------------------------------------------*/ + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: Interface */ +/**/ 12, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol */ + 0x00, /* iInterface: */ + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ +/**/ 13, /* bDataInterface */ + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ +/**/ 12, /* bMasterInterface: Communication class interface */ +/**/ 13, /* bSlaveInterface0: Data Class Interface */ + /* Interrupt Endpoint Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x8e, /* bEndpointAddress IN14 - non-existant! */ + 0x03, /* bmAttributes: Interrupt */ + (USB_EP1BUFSZ & 0xff), /* wMaxPacketSize LO: */ + (USB_EP1BUFSZ >> 8), /* wMaxPacketSize HI: */ + 0x10, /* bInterval: */ + /*---------------------------------------------------------------------------*/ + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + 0x04, /* bDescriptorType: */ +/**/ 13, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x02, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + /*Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x87, /* bEndpointAddress IN7 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + 0x05, /* bDescriptorType: Endpoint */ +/**/0x07, /* bEndpointAddress OUT7 */ + 0x02, /* bmAttributes: Bulk */ + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), + 0x00, /* bInterval: ignore for Bulk transfer */ + /*---------------------------------------------------------------------------*/ +}; + + +USB_LANG_ID(USB_StringLangDescriptor, LANG_US); + +USB_STRING(USB_StringSerialDescriptor, u"000001"); +USB_STRING(USB_StringManufacturingDescriptor, u"Eddy @ SAO RAS"); +USB_STRING(USB_StringProdDescriptor, u"USB-Serial Controller"); + +/* + * default handlers + */ +// SET_LINE_CODING +void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lcd){ + //MSG("linecoding_handler()"); +} + +// SET_CONTROL_LINE_STATE +void WEAK clstate_handler(uint16_t __attribute__((unused)) val){ + //MSG("clstate_handler()"); +} + +// SEND_BREAK +void WEAK break_handler(){ + //MSG("break_handler()"); +} + +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 state = 0; // bus powered + switch(setup_packet.bRequest){ + case GET_DESCRIPTOR: + get_descriptor(); + break; + case GET_STATUS: + EP_WriteIRQ(0, (uint8_t *)&state, 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 + */ +static void EP0_Handler(uint8_t epno){ + (void) epno; + uint8_t reqtype = setup_packet.bmRequestType & 0x7f; + uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0; + uint16_t epstatus = USB->EPnR[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); + }else{ + } + 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: + break_handler(); + break; + default: + break; + } + if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); + 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; +} + +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)(uint8_t epno) - 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)(uint8_t epno)){ + 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+1) 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; +} + +//extern int8_t dump; +// 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; + // 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; + // state is default - wait for enumeration + 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); + } + } + }else{ // IN interrupt - transmit data, only CTR_TX == 1 + // enumeration end could be MSG (if EP0) + } + // call EP handler + if(endpoints[n].func) endpoints[n].func(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; + } +} + +/** + * 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){ + uint8_t i; + 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(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-nolib/SevenCDCs/usb_lib.h b/F1-nolib/SevenCDCs/usb_lib.h new file mode 100644 index 0000000..2f083f0 --- /dev/null +++ b/F1-nolib/SevenCDCs/usb_lib.h @@ -0,0 +1,215 @@ +/* + * geany_encoding=koi8-r + * usb_lib.h + * + * Copyright 2018 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#pragma once +#ifndef __USB_LIB_H__ +#define __USB_LIB_H__ + +#include +#include "usb_defs.h" + +#define EP0DATABUF_SIZE (64) +#define LASTADDR_DEFAULT (STM32ENDPOINTS * 8) + +// Max EP amount (EP0 + other used) +//#define ENDPOINTS_NUM 4 +// 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) + +// keep all DTOGs and STATs +#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)) + +//#define RX_CNT(N) (USB_BTABLE->EP[N].USB_COUNT_RX & 0x3FF) + +/* +// EPnR bits manipulation +#define CLEAR_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? R : (R & (~USB_EPnR_DTOG_RX)) +#define SET_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? (R & (~USB_EPnR_DTOG_RX)) : R +#define TOGGLE_DTOG_RX(R) (R | USB_EPnR_DTOG_RX) +#define KEEP_DTOG_RX(R) (R & (~USB_EPnR_DTOG_RX)) +#define CLEAR_DTOG_TX(R) (R & USB_EPnR_DTOG_TX) ? R : (R & (~USB_EPnR_DTOG_TX)) +#define SET_DTOG_TX(R) (R & USB_EPnR_DTOG_TX) ? (R & (~USB_EPnR_DTOG_TX)) : R +#define TOGGLE_DTOG_TX(R) (R | USB_EPnR_DTOG_TX) +#define KEEP_DTOG_TX(R) (R & (~USB_EPnR_DTOG_TX)) +#define SET_VALID_RX(R) ((R & USB_EPnR_STAT_RX) ^ USB_EPnR_STAT_RX) | (R & (~USB_EPnR_STAT_RX)) +#define SET_NAK_RX(R) ((R & USB_EPnR_STAT_RX) ^ USB_EPnR_STAT_RX_1) | (R & (~USB_EPnR_STAT_RX)) +#define SET_STALL_RX(R) ((R & USB_EPnR_STAT_RX) ^ USB_EPnR_STAT_RX_0) | (R & (~USB_EPnR_STAT_RX)) +#define KEEP_STAT_RX(R) (R & (~USB_EPnR_STAT_RX)) +#define SET_VALID_TX(R) ((R & USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_TX) | (R & (~USB_EPnR_STAT_TX)) +#define SET_NAK_TX(R) ((R & USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_TX_1) | (R & (~USB_EPnR_STAT_TX)) +#define SET_STALL_TX(R) ((R & USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_TX_0) | (R & (~USB_EPnR_STAT_TX)) +#define KEEP_STAT_TX(R) (R & (~USB_EPnR_STAT_TX)) +#define CLEAR_CTR_RX(R) (R & (~USB_EPnR_CTR_RX)) +#define CLEAR_CTR_TX(R) (R & (~USB_EPnR_CTR_TX)) +#define CLEAR_CTR_RX_TX(R) (R & (~(USB_EPnR_CTR_TX | USB_EPnR_CTR_RX))) +*/ + +// 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 __ep_t{ + uint16_t *tx_buf; // transmission buffer address + uint16_t txbufsz; // transmission buffer size + uint16_t *rx_buf; // reception buffer address + void (*func)(uint8_t epno); // 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; + +void USB_Init(); +void USB_ResetState(); +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(uint8_t epno)); +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(); + +#endif // __USB_LIB_H__