diff --git a/F3:F303/Readme.md b/F3:F303/Readme.md
index 23740e0..9008d48 100644
--- a/F3:F303/Readme.md
+++ b/F3:F303/Readme.md
@@ -3,11 +3,16 @@ Samples for STM32F303
Most of these samples works with [my non-solder devboard](https://github.com/eddyem/stm32samples/tree/master/F0_F1_F3-LQFP48_testboard)
+- **ADC** - work with ADC
- **blink** - simple LEDs blink on PB0 and PB1
+- **CANusb** - USB-CAN adapter
- **floatPrintf** - work with floats, convert floating point number into a string
+- **I2C_scan** - simple I2C scanner
- **Multistepper** - stepper motor controller (8 independend motors or up to 64 multiplexed)
- **NitrogenFlooding** - automatic nitrogen flooding
- **PL2303** - emulates PL2303
+- **Seven_CDCs** - 7 CDCs in one device
- **usart1** - read data from USART1 and send it back (blocking sending)
- **usart1fullDMA** - USART1 Rx and Tx DMA access (Rx terminated by character match of '\n' or buffer overflow).
- **usarts** - work with all three USARTs with Tx DMA, send to USART1 data read from other, you can send "2\n" or "3\n" to USART1 for tests of USART2/USART3
+- **USB_template** - old USB template
diff --git a/F3:F303/Seven_CDCs/7CDCs.cflags b/F3:F303/Seven_CDCs/7CDCs.cflags
new file mode 100644
index 0000000..68d5165
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.cflags
@@ -0,0 +1 @@
+-std=c17
\ No newline at end of file
diff --git a/F3:F303/Seven_CDCs/7CDCs.config b/F3:F303/Seven_CDCs/7CDCs.config
new file mode 100644
index 0000000..1aff3d9
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.config
@@ -0,0 +1,8 @@
+// Add predefined macros for your project here. For example:
+// #define THE_ANSWER 42
+#define EBUG
+#define STM32F3
+#define STM32F303xb
+#define __thumb2__ 1
+#define __ARM_ARCH_7M__
+#define USB1_16
diff --git a/F3:F303/Seven_CDCs/7CDCs.creator b/F3:F303/Seven_CDCs/7CDCs.creator
new file mode 100644
index 0000000..e94cbbd
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.creator
@@ -0,0 +1 @@
+[General]
diff --git a/F3:F303/Seven_CDCs/7CDCs.creator.user b/F3:F303/Seven_CDCs/7CDCs.creator.user
new file mode 100644
index 0000000..f16c1bd
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.creator.user
@@ -0,0 +1,171 @@
+
+
+
+
+
+ EnvironmentId
+ {cf63021e-ef53-49b0-b03b-2f2570cdf3b6}
+
+
+ ProjectExplorer.Project.ActiveTarget
+ 0
+
+
+ ProjectExplorer.Project.EditorSettings
+
+ true
+ false
+ true
+
+ Cpp
+
+ CppGlobal
+
+
+
+ QmlJS
+
+ QmlJSGlobal
+
+
+ 2
+ KOI8-R
+ false
+ 4
+ false
+ 80
+ true
+ true
+ 1
+ false
+ true
+ false
+ 1
+ true
+ true
+ 0
+ 8
+ true
+ false
+ 2
+ true
+ false
+ true
+ *.md, *.MD, Makefile
+ false
+ true
+
+
+
+ ProjectExplorer.Project.PluginSettings
+
+
+ true
+ false
+ true
+ true
+ true
+ true
+
+
+ 0
+ true
+
+ true
+ true
+ Builtin.DefaultTidyAndClazy
+ 1
+
+
+
+ true
+
+
+
+
+ ProjectExplorer.Project.Target.0
+
+ Desktop
+ Desktop
+ Desktop
+ {91347f2c-5221-46a7-80b1-0a054ca02f79}
+ 0
+ 0
+ 0
+
+ /home/eddy/C-files/stm32samples/F3:F303/PL2303
+
+
+
+ all
+
+ true
+ GenericProjectManager.GenericMakeStep
+
+ 1
+ Сборка
+ Сборка
+ ProjectExplorer.BuildSteps.Build
+
+
+
+
+ clean
+
+ true
+ GenericProjectManager.GenericMakeStep
+
+ 1
+ Очистка
+ Очистка
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ false
+
+ По умолчанию
+ GenericProjectManager.GenericBuildConfiguration
+
+ 1
+
+
+ 0
+ Развёртывание
+ Развёртывание
+ ProjectExplorer.BuildSteps.Deploy
+
+ 1
+
+ false
+ ProjectExplorer.DefaultDeployConfiguration
+
+ 1
+
+
+ 2
+
+ ProjectExplorer.CustomExecutableRunConfiguration
+
+ false
+ true
+ false
+ true
+
+ 1
+
+
+
+ ProjectExplorer.Project.TargetCount
+ 1
+
+
+ ProjectExplorer.Project.Updater.FileVersion
+ 22
+
+
+ Version
+ 22
+
+
diff --git a/F3:F303/Seven_CDCs/7CDCs.creator.user.7bd84e3 b/F3:F303/Seven_CDCs/7CDCs.creator.user.7bd84e3
new file mode 100644
index 0000000..aa79e75
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.creator.user.7bd84e3
@@ -0,0 +1,171 @@
+
+
+
+
+
+ EnvironmentId
+ {7bd84e39-ca37-46d3-be9d-99ebea85bc0d}
+
+
+ ProjectExplorer.Project.ActiveTarget
+ 0
+
+
+ ProjectExplorer.Project.EditorSettings
+
+ true
+ false
+ true
+
+ Cpp
+
+ CppGlobal
+
+
+
+ QmlJS
+
+ QmlJSGlobal
+
+
+ 2
+ KOI8-R
+ false
+ 4
+ false
+ 80
+ true
+ true
+ 1
+ false
+ true
+ false
+ 0
+ true
+ true
+ 0
+ 8
+ true
+ false
+ 1
+ true
+ false
+ true
+ *.md, *.MD, Makefile
+ false
+ true
+
+
+
+ ProjectExplorer.Project.PluginSettings
+
+
+ true
+ false
+ true
+ true
+ true
+ true
+
+
+ 0
+ true
+
+ true
+ true
+ Builtin.DefaultTidyAndClazy
+ 2
+
+
+
+ true
+
+
+
+
+ ProjectExplorer.Project.Target.0
+
+ Desktop
+ Desktop
+ Desktop
+ {65a14f9e-e008-4c1b-89df-4eaa4774b6e3}
+ 0
+ 0
+ 0
+
+ /Big/Data/00__Electronics/STM32/F303-nolib/blink
+
+
+
+ all
+
+ true
+ GenericProjectManager.GenericMakeStep
+
+ 1
+ Сборка
+ Сборка
+ ProjectExplorer.BuildSteps.Build
+
+
+
+
+ clean
+
+ true
+ GenericProjectManager.GenericMakeStep
+
+ 1
+ Очистка
+ Очистка
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ false
+
+ Default
+ GenericProjectManager.GenericBuildConfiguration
+
+ 1
+
+
+ 0
+ Развёртывание
+ Развёртывание
+ ProjectExplorer.BuildSteps.Deploy
+
+ 1
+
+ false
+ ProjectExplorer.DefaultDeployConfiguration
+
+ 1
+
+
+ 2
+
+ ProjectExplorer.CustomExecutableRunConfiguration
+
+ false
+ true
+ false
+ true
+
+ 1
+
+
+
+ ProjectExplorer.Project.TargetCount
+ 1
+
+
+ ProjectExplorer.Project.Updater.FileVersion
+ 22
+
+
+ Version
+ 22
+
+
diff --git a/F3:F303/Seven_CDCs/7CDCs.cxxflags b/F3:F303/Seven_CDCs/7CDCs.cxxflags
new file mode 100644
index 0000000..6435dfc
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.cxxflags
@@ -0,0 +1 @@
+-std=c++17
\ No newline at end of file
diff --git a/F3:F303/Seven_CDCs/7CDCs.files b/F3:F303/Seven_CDCs/7CDCs.files
new file mode 100644
index 0000000..3d78e2d
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.files
@@ -0,0 +1,15 @@
+cmdproto.c
+cmdproto.h
+hardware.c
+hardware.h
+main.c
+ringbuffer.c
+ringbuffer.h
+usart.c
+usart.h
+usb.c
+usb.h
+usb_lib.c
+usb_lib.h
+usbhw.c
+usbhw.h
diff --git a/F3:F303/Seven_CDCs/7CDCs.includes b/F3:F303/Seven_CDCs/7CDCs.includes
new file mode 100644
index 0000000..06d1130
--- /dev/null
+++ b/F3:F303/Seven_CDCs/7CDCs.includes
@@ -0,0 +1,6 @@
+.
+../inc
+../inc/Fx
+../inc/cm
+../inc/ld
+../inc/startup
diff --git a/F3:F303/Seven_CDCs/Makefile b/F3:F303/Seven_CDCs/Makefile
new file mode 100644
index 0000000..a597ea5
--- /dev/null
+++ b/F3:F303/Seven_CDCs/Makefile
@@ -0,0 +1,9 @@
+BINARY := sevenCDCs
+# MCU code
+MCU := F303xb
+# change this linking script depending on particular MCU model,
+LDSCRIPT := stm32f303xB.ld
+DEFINES := -DUSB1_16
+
+include ../makefile.f3
+include ../../makefile.stm32
diff --git a/F3:F303/Seven_CDCs/cmdproto.c b/F3:F303/Seven_CDCs/cmdproto.c
new file mode 100644
index 0000000..bcd5aba
--- /dev/null
+++ b/F3:F303/Seven_CDCs/cmdproto.c
@@ -0,0 +1,257 @@
+/*
+ * This file is part of the SevenCDCs 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 "cmdproto.h"
+#include "usb.h"
+#include "version.inc"
+
+#define SEND(str) do{USB_sendstr(CMD_IDX, str);}while(0)
+#define SENDN(str) do{USB_sendstr(CMD_IDX, str); USB_putbyte(CMD_IDX, '\n');}while(0)
+
+extern volatile uint32_t Tms;
+
+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/F3:F303/PL2303 build#" BUILD_NUMBER " @ " BUILD_DATE "\n"
+ "'i' - print USB->ISTR state\n"
+ "'N' - read number (dec, 0xhex, 0oct, bbin) and show it in decimal\n"
+ "'R' - software reset\n"
+ "'U' - get USB status\n"
+ "'W' - test watchdog\n"
+;
+
+
+extern uint8_t usbON;
+void parse_cmd(const char *buf){
+ if(buf[1] == '\n' || !buf[1]){ // one symbol commands
+ switch(*buf){
+ case 'i':
+ SEND("USB->ISTR=");
+ SEND(u2hexstr(USB->ISTR));
+ SEND(", USB->CNTR=");
+ SENDN(u2hexstr(USB->CNTR));
+ break;
+ case 'R':
+ SENDN("Soft reset");
+ USB_sendall(CMD_IDX);
+ NVIC_SystemReset();
+ break;
+ case 'U':
+ SEND("USB status: ");
+ if(usbON) SENDN("ON");
+ else SENDN("OFF");
+ break;
+ case 'W':
+ SENDN("Wait for reboot");
+ USB_sendall(CMD_IDX);
+ while(1){nop();};
+ break;
+ default:
+ SEND(helpmsg);
+ }
+ return;
+ }
+ uint32_t Num = 0;
+ char *nxt;
+ switch(*buf){ // long messages
+ case 'N':
+ ++buf;
+ nxt = getnum(buf, &Num);
+ if(buf == nxt){
+ if(Num == 0) SENDN("Wrong number");
+ SENDN("Integer32 overflow");
+ }
+ SEND("You give: ");
+ SEND(u2str(Num));
+ if(*nxt && *nxt != '\n'){
+ SEND(", the rest of string: ");
+ SEND(nxt);
+ }else SEND("\n");
+ break;
+ default:
+ SEND(buf);
+ return;
+ }
+}
+
+
+// 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/F3:F303/Seven_CDCs/cmdproto.h b/F3:F303/Seven_CDCs/cmdproto.h
new file mode 100644
index 0000000..f41a242
--- /dev/null
+++ b/F3:F303/Seven_CDCs/cmdproto.h
@@ -0,0 +1,33 @@
+/*
+ * This file is part of the SevenCDCs 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
+
+extern uint8_t starttest;
+
+void parse_cmd(const char *buf);
+char *omit_spaces(const char *buf);
+char *getnum(const char *buf, uint32_t *N);
+char *u2str(uint32_t val);
+char *u2hexstr(uint32_t val);
+
+#endif // PROTO_H__
diff --git a/F3:F303/Seven_CDCs/hardware.c b/F3:F303/Seven_CDCs/hardware.c
new file mode 100644
index 0000000..00e2f48
--- /dev/null
+++ b/F3:F303/Seven_CDCs/hardware.c
@@ -0,0 +1,37 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * 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 "usart.h"
+
+static inline void gpio_setup(){
+ RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
+ // LEDs on PB0 and PB1
+ GPIOB->MODER = GPIO_MODER_MODER0_O | GPIO_MODER_MODER1_O;
+ GPIOB->ODR = 1;
+ // USB - alternate function 14 @ pins PA11/PA12; USART1 = AF7 @PA9/10; SWD - AF0 @PA13/14
+ GPIOA->AFR[1] = AFRf(7, 9) | AFRf(7, 10) | AFRf(14, 11) | AFRf(14, 12);
+ // USART1: PA10(Rx), PA9(Tx); USB - PA11, PA12; SWDIO - PA13, PA14; Pullup - PA15
+ GPIOA->MODER = MODER_AF(9) | MODER_AF(10) | MODER_AF(11) | MODER_AF(12) | MODER_AF(13) | MODER_AF(14) | MODER_O(15);
+ GPIOA->OSPEEDR = OSPEED_HI(11) | OSPEED_HI(12) | OSPEED_HI(13) | OSPEED_HI(14);
+}
+
+void hw_setup(){
+ gpio_setup();
+}
+
diff --git a/F3:F303/Seven_CDCs/hardware.h b/F3:F303/Seven_CDCs/hardware.h
new file mode 100644
index 0000000..82cd0ce
--- /dev/null
+++ b/F3:F303/Seven_CDCs/hardware.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * 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
+
+#define USBPU_port GPIOA
+#define USBPU_pin (1<<15)
+#define USBPU_ON() pin_clear(USBPU_port, USBPU_pin)
+#define USBPU_OFF() pin_set(USBPU_port, USBPU_pin)
+
+extern volatile uint32_t Tms;
+
+void hw_setup();
+
+#endif // __HARDWARE_H__
diff --git a/F3:F303/Seven_CDCs/main.c b/F3:F303/Seven_CDCs/main.c
new file mode 100644
index 0000000..e9bdfb4
--- /dev/null
+++ b/F3:F303/Seven_CDCs/main.c
@@ -0,0 +1,58 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * 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 "cmdproto.h"
+#include "usart.h"
+#include "usb.h"
+
+#define MAXSTRLEN RBINSZ
+
+volatile uint32_t Tms = 0;
+
+void sys_tick_handler(void){
+ ++Tms;
+}
+
+int main(void){
+ char inbuff[MAXSTRLEN+1];
+ USBPU_OFF();
+ if(StartHSE()){
+ SysTick_Config((uint32_t)72000); // 1ms
+ }else{
+ StartHSI();
+ SysTick_Config((uint32_t)48000); // 1ms
+ }
+ hw_setup();
+ usarts_setup();
+ USB_setup();
+ USBPU_ON();
+
+ uint32_t ctr = Tms;
+ while(1){
+ if(Tms - ctr > 499){
+ ctr = Tms;
+ pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0
+ }
+ int l = USB_receivestr(CMD_IDX, inbuff, MAXSTRLEN);
+ if(l < 0) USB_sendstr(CMD_IDX, "ERROR: USB buffer overflow or string was too long\n");
+ else if(l){
+ parse_cmd(inbuff);
+ }
+ }
+}
diff --git a/F3:F303/Seven_CDCs/openocd.cfg b/F3:F303/Seven_CDCs/openocd.cfg
new file mode 100644
index 0000000..0210594
--- /dev/null
+++ b/F3:F303/Seven_CDCs/openocd.cfg
@@ -0,0 +1,4 @@
+set FLASH_SIZE 0x20000
+
+source [find interface/stlink-v2-1.cfg]
+source [find target/stm32f3x.cfg]
diff --git a/F3:F303/Seven_CDCs/ringbuffer.c b/F3:F303/Seven_CDCs/ringbuffer.c
new file mode 100644
index 0000000..d7b4ced
--- /dev/null
+++ b/F3:F303/Seven_CDCs/ringbuffer.c
@@ -0,0 +1,124 @@
+/*
+ * This file is part of the SevenCDCs 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"
+
+// stored data length
+int RB_datalen(ringbuffer *b){
+ if(b->tail >= b->head) return (b->tail - b->head);
+ else return (b->length - b->head + b->tail);
+}
+
+/**
+ * @brief RB_hasbyte - check if buffer has given byte stored
+ * @param b - buffer
+ * @param byte - byte to find
+ * @return index if found, -1 if none
+ */
+int RB_hasbyte(ringbuffer *b, uint8_t byte){
+ if(b->head == b->tail) return -1; // no data in buffer
+ int startidx = b->head;
+ if(b->head > b->tail){ //
+ for(int found = b->head; found < b->length; ++found)
+ if(b->data[found] == byte) return found;
+ startidx = 0;
+ }
+ for(int found = startidx; found < b->tail; ++found)
+ if(b->data[found] == byte) return found;
+ return -1;
+}
+
+// poor memcpy
+static void mcpy(uint8_t *targ, const uint8_t *src, int l){
+ while(l--) *targ++ = *src++;
+}
+
+// increment head or tail
+TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){
+ *what += n;
+ if(*what >= b->length) *what -= b->length;
+}
+
+/**
+ * @brief RB_read - read data from ringbuffer
+ * @param b - buffer
+ * @param s - array to write data
+ * @param len - max len of `s`
+ * @return bytes read
+ */
+int RB_read(ringbuffer *b, uint8_t *s, int len){
+ int l = RB_datalen(b);
+ if(!l) return 0;
+ if(l > len) l = len;
+ int _1st = b->length - b->head;
+ if(_1st > l) _1st = l;
+ if(_1st > len) _1st = len;
+ mcpy(s, b->data + b->head, _1st);
+ if(_1st < len && l > _1st){
+ mcpy(s+_1st, b->data, l - _1st);
+ incr(b, &b->head, l);
+ return l;
+ }
+ incr(b, &b->head, _1st);
+ return _1st;
+}
+
+/**
+ * @brief RB_readto fill array `s` with data until byte `byte` (with it)
+ * @param b - ringbuffer
+ * @param byte - check byte
+ * @param s - buffer to write data
+ * @param len - length of `s`
+ * @return amount of bytes written (negative, if lenhead;
+ // now calculate length of new data portion
+ if(idx < b->head) partlen += b->length;
+ if(partlen > len) return -RB_read(b, s, len);
+ return RB_read(b, s, partlen);
+}
+
+/**
+ * @brief RB_write - write some data to ringbuffer
+ * @param b - buffer
+ * @param str - data
+ * @param l - length
+ * @return amount of bytes written
+ */
+int RB_write(ringbuffer *b, const uint8_t *str, int l){
+ int r = b->length - 1 - RB_datalen(b); // rest length
+ if(l > r) l = r;
+ if(!l) return 0;
+ int _1st = b->length - b->tail;
+ if(_1st > l) _1st = l;
+ mcpy(b->data + b->tail, str, _1st);
+ if(_1st < l){ // add another piece from start
+ mcpy(b->data, str+_1st, l-_1st);
+ }
+ incr(b, &b->tail, l);
+ return l;
+}
+
+// just delete all information in buffer `b`
+void RB_clearbuf(ringbuffer *b){
+ b->head = 0;
+ b->tail = 0;
+}
diff --git a/F3:F303/Seven_CDCs/ringbuffer.h b/F3:F303/Seven_CDCs/ringbuffer.h
new file mode 100644
index 0000000..b5e08b8
--- /dev/null
+++ b/F3:F303/Seven_CDCs/ringbuffer.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of the SevenCDCs 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
+
+typedef struct{
+ uint8_t *data; // data buffer
+ const int length; // its length
+ int head; // head index
+ int tail; // tail index
+} ringbuffer;
+
+int RB_read(ringbuffer *b, uint8_t *s, int len);
+int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len);
+int RB_hasbyte(ringbuffer *b, uint8_t byte);
+int RB_write(ringbuffer *b, const uint8_t *str, int l);
+int RB_datalen(ringbuffer *b);
+void RB_clearbuf(ringbuffer *b);
+
+#endif // RINGBUFFER_H__
diff --git a/F3:F303/Seven_CDCs/sevenCDCs.bin b/F3:F303/Seven_CDCs/sevenCDCs.bin
new file mode 100755
index 0000000..d2324ac
Binary files /dev/null and b/F3:F303/Seven_CDCs/sevenCDCs.bin differ
diff --git a/F3:F303/Seven_CDCs/usart.c b/F3:F303/Seven_CDCs/usart.c
new file mode 100644
index 0000000..1da5114
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usart.c
@@ -0,0 +1,85 @@
+/*
+ * This file is part of the SevenCDCs 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 "stm32f3.h"
+#include "hardware.h"
+#include "usart.h"
+#include "usb.h"
+#include
+
+static volatile int idatalen = 0; // received data line length (including '\n')
+// USARTs registers
+static volatile USART_TypeDef *USARTx[USARTSNO+1] = {0, USART1, USART2, USART3};
+
+volatile int linerdy = 0, // received data ready
+ dlen = 0, // length of data (including '\n') in current buffer
+ bufovr = 0; // input buffer overfull
+// USARTs speeds
+static int speeds[USARTSNO+1] = {0};
+
+usb_LineCoding *getLineCoding(int usartNo){
+ static usb_LineCoding lineCoding; // `static` - to return pointer to it
+ if(usartNo < 1 || usartNo > USARTSNO) return NULL;
+// TODO: fixme
+ lineCoding.dwDTERate = speeds[usartNo];
+ lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
+ lineCoding.bParityType = USB_CDC_NO_PARITY;
+ lineCoding.bDataBits = 8;
+ return &lineCoding;
+}
+
+void usart_putchar(uint8_t ch){
+ while(!(USART1->ISR & USART_ISR_TXE));
+ USART1->TDR = ch;
+}
+void usart_sendn(const uint8_t *str, int L){
+ if(!str || L < 0) return;
+ for(int i = 0; i < L; ++i){
+ usart_putchar(str[i]);
+ }
+}
+
+// setup all USARTs
+void usarts_setup(){
+ // clock
+ RCC->APB1ENR |= RCC_APB1ENR_USART2EN | RCC_APB1ENR_USART3EN;
+ RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
+ NVIC_EnableIRQ(USART1_IRQn);
+ NVIC_EnableIRQ(USART2_IRQn);
+ NVIC_EnableIRQ(USART3_IRQn);
+}
+
+void usart_config(uint8_t usartNo, usb_LineCoding *lc){
+ if(!usartNo || usartNo > USARTSNO) return;
+ speeds[usartNo] = lc->dwDTERate;
+ volatile USART_TypeDef *U = USARTx[usartNo];
+ U->ICR = 0xffffffff; // clear all flags
+ U->BRR = SysFreq / lc->dwDTERate;
+ U->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE; // 1start,8data,nstop; enable Rx,Tx,USART
+ uint32_t tmout = 16000000;
+ while(!(U->ISR & USART_ISR_TC)){if(--tmout == 0) break;} // polling idle frame Transmission
+ U->ICR = 0xffffffff; // clear all flags again
+}
+
+void usart1_exti25_isr(){
+ if(USART1->ISR & USART_ISR_RXNE){ // RX not emty - receive next char
+ // read RDR clears flag
+ uint8_t rb = USART1->RDR;
+ USB_putbyte(1, rb);
+ }
+}
diff --git a/F3:F303/Seven_CDCs/usart.h b/F3:F303/Seven_CDCs/usart.h
new file mode 100644
index 0000000..64bacfd
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usart.h
@@ -0,0 +1,36 @@
+/*
+ * This file is part of the SevenCDCs 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 __USART_H__
+#define __USART_H__
+
+#include "hardware.h"
+#include "usb_lib.h"
+
+// amount of usarts
+#define USARTSNO 3
+
+extern volatile int linerdy, bufovr;
+
+void usarts_setup();
+void usart_config(uint8_t usartNo, usb_LineCoding *lc);
+void usart_sendn(const uint8_t *str, int L);
+usb_LineCoding *getLineCoding(int usartNo);
+
+#endif // __USART_H__
diff --git a/F3:F303/Seven_CDCs/usb.c b/F3:F303/Seven_CDCs/usb.c
new file mode 100644
index 0000000..a2bae45
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usb.c
@@ -0,0 +1,129 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * 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 "hardware.h"
+#include "usb.h"
+#include "usb_lib.h"
+
+static volatile uint8_t usbbuff[USB_TRBUFSZ]; // temporary buffer for sending data
+// ring buffers for incoming and outgoing data
+static uint8_t obuf[WORK_EPs][RBOUTSZ], ibuf[WORK_EPs][RBINSZ];
+#define OBUF(N) {.data = obuf[N], .length = RBOUTSZ, .head = 0, .tail = 0}
+volatile ringbuffer rbout[WORK_EPs] = {OBUF(0), OBUF(1), OBUF(2), OBUF(3), OBUF(4), OBUF(5), OBUF(6)};
+#define IBUF(N) {.data = ibuf[N], .length = RBOUTSZ, .head = 0, .tail = 0}
+volatile ringbuffer rbin[WORK_EPs] = {IBUF(0), IBUF(1), IBUF(2), IBUF(3), IBUF(4), IBUF(5), IBUF(6)};
+// transmission is succesfull
+volatile uint8_t bufisempty[WORK_EPs] = {1,1,1,1,1,1};
+volatile uint8_t bufovrfl[WORK_EPs] = {0};
+
+// here and later: ifNo is index of buffers, i.e. ifNo = epno-1 !!!
+void send_next(int ifNo){
+ if(bufisempty[ifNo]) return;
+ static int lastdsz = 0;
+ int buflen = RB_read((ringbuffer*)&rbout[ifNo], (uint8_t*)usbbuff, USB_TRBUFSZ);
+ if(!buflen){
+ if(lastdsz == 64) EP_Write(3, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
+ lastdsz = 0;
+ bufisempty[ifNo] = 1;
+ return;
+ }
+ EP_Write(3, (uint8_t*)usbbuff, buflen);
+ lastdsz = buflen;
+}
+
+// blocking send full content of ring buffer
+int USB_sendall(int ifNo){
+ while(!bufisempty[ifNo]){
+ if(!usbON) return 0;
+ }
+ return 1;
+}
+
+// put `buf` into queue to send
+int USB_send(int ifNo, const uint8_t *buf, int len){
+ if(!buf || !usbON || !len) return 0;
+ while(len){
+ int a = RB_write((ringbuffer*)&rbout[ifNo], buf, len);
+ len -= a;
+ buf += a;
+ if(bufisempty[ifNo]){
+ bufisempty[ifNo] = 0;
+ send_next(ifNo);
+ }
+ }
+ return 1;
+}
+
+int USB_putbyte(int ifNo, uint8_t byte){
+ if(!usbON) return 0;
+ while(0 == RB_write((ringbuffer*)&rbout[ifNo], &byte, 1)){
+ if(bufisempty[ifNo]){
+ bufisempty[ifNo] = 0;
+ send_next(ifNo);
+ }
+ }
+ return 1;
+}
+
+int USB_sendstr(int ifNo, const char *string){
+ if(!string || !usbON) return 0;
+ int len = 0;
+ const char *b = string;
+ while(*b++) ++len;
+ if(!len) return 0;
+ return USB_send(ifNo, (const uint8_t*)string, len);
+}
+
+/**
+ * @brief USB_receive - get binary data from receiving ring-buffer
+ * @param buf (i) - buffer for received data
+ * @param len - length of `buf`
+ * @return amount of received bytes (negative, if overfull happened)
+ */
+int USB_receive(int ifNo, uint8_t *buf, int len){
+ int sz = RB_read((ringbuffer*)&rbin[ifNo], buf, len);
+ if(bufovrfl[ifNo]){
+ RB_clearbuf((ringbuffer*)&rbin[ifNo]);
+ if(!sz) sz = -1;
+ else sz = -sz;
+ bufovrfl[ifNo] = 0;
+ }
+ return sz;
+}
+
+/**
+ * @brief USB_receivestr - get string up to '\n' and replace '\n' with 0
+ * @param buf - receiving buffer
+ * @param len - its length
+ * @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared)
+ */
+int USB_receivestr(int ifNo, char *buf, int len){
+ int l = RB_readto((ringbuffer*)&rbin[ifNo], '\n', (uint8_t*)buf, len);
+ if(l == 0) return 0;
+ if(--l < 0 || bufovrfl[ifNo]) RB_clearbuf((ringbuffer*)&rbin[ifNo]);
+ else buf[l] = 0; // replace '\n' with strend
+ if(bufovrfl[ifNo]){
+ if(l > 0) l = -l;
+ else l = -1;
+ bufovrfl[ifNo] = 0;
+ }
+ return l;
+}
+
diff --git a/F3:F303/Seven_CDCs/usb.h b/F3:F303/Seven_CDCs/usb.h
new file mode 100644
index 0000000..af1d699
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usb.h
@@ -0,0 +1,62 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include "ringbuffer.h"
+#include "usbhw.h"
+
+// sizes of ringbuffers for outgoing and incoming data
+#define RBOUTSZ (128)
+#define RBINSZ (128)
+
+#define newline(x) USB_putbyte(x, '\n')
+#define USND(x, s) do{USB_sendstr(x, s); USB_putbyte(x, '\n');}while(0)
+
+#define STR_HELPER(s) #s
+#define STR(s) STR_HELPER(s)
+
+#ifdef EBUG
+#define DBG(str) do{USB_sendstr(__FILE__ " (L" STR(__LINE__) "): " str); newline();}while(0)
+#else
+#define DBG(str)
+#endif
+
+// total amount of working EPs
+#define WORK_EPs 7
+// functional EPs
+#define CMD_EPNO 1
+#define USART1_EPNO 2
+#define USART2_EPNO 3
+#define USART3_EPNO 4
+#define CAN_EPNO 6
+#define USARTMAX_EPNO USART3_EPNO
+// functional indexes
+#define CMD_IDX (CMD_EPNO-1)
+#define CAN_IDX (CAN_EPNO-1)
+
+extern volatile ringbuffer rbout[], rbin[];
+extern volatile uint8_t bufisempty[], bufovrfl[];
+
+void send_next(int ifNo);
+int USB_sendall(int ifNo);
+int USB_send(int ifNo, const uint8_t *buf, int len);
+int USB_putbyte(int ifNo, uint8_t byte);
+int USB_sendstr(int ifNo, const char *string);
+int USB_receive(int ifNo, uint8_t *buf, int len);
+int USB_receivestr(int ifNo, char *buf, int len);
diff --git a/F3:F303/Seven_CDCs/usb_lib.c b/F3:F303/Seven_CDCs/usb_lib.c
new file mode 100644
index 0000000..ee41396
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usb_lib.c
@@ -0,0 +1,941 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * 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 "usart.h"
+#include "usb.h"
+#include "usb_lib.h"
+#include "usbhw.h"
+
+ep_t endpoints[STM32ENDPOINTS];
+
+static uint16_t USB_Addr = 0;
+uint8_t ep0databuf[EP0DATABUF_SIZE], setupdatabuf[EP0DATABUF_SIZE];
+config_pack_t *setup_packet = (config_pack_t*) setupdatabuf;
+
+volatile 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
+ iMANUFACTURER_DESCR, // iManufacturer
+ iPRODUCT_DESCR, // iProduct
+ iSERIAL_DESCR, // 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?)
+ 0x02, // 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 */
+ iINTERFACE_DESCR_CMD, /* 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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 */
+ iINTERFACE_DESCR_USART1, /* 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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 */
+ iINTERFACE_DESCR_USART2, /* 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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 */
+ iINTERFACE_DESCR_USART3, /* 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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 */
+ iINTERFACE_DESCR_USART4, /* 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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 */
+ iINTERFACE_DESCR_CAN, /* 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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 */
+ iINTERFACE_DESCR_DBG, /* 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 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_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */
+ (USB_TRBUFSZ >> 8),
+ 0x00, /* bInterval: ignore for Bulk transfer */
+ /*---------------------------------------------------------------------------*/
+};
+
+_USB_LANG_ID_(LD, LANG_US);
+_USB_STRING_(SD, u"0.0.1");
+_USB_STRING_(MD, u"Emelianov E.V.");
+_USB_STRING_(PD, u"USB multiserial controller");
+_USB_STRING_(ID0, u"serial-cmd");
+_USB_STRING_(ID1, u"serial-usart1_");
+_USB_STRING_(ID2, u"serial-usart2_");
+_USB_STRING_(ID3, u"serial-usart3_");
+_USB_STRING_(ID4, u"serial-usart4_");
+_USB_STRING_(ID5, u"serial-can");
+_USB_STRING_(ID6, u"serial-debug");
+
+static void const *StringDescriptor[iDESCR_AMOUNT] = {
+ [iLANGUAGE_DESCR] = &LD,
+ [iMANUFACTURER_DESCR] = &MD,
+ [iPRODUCT_DESCR] = &PD,
+ [iSERIAL_DESCR] = &SD,
+ [iINTERFACE_DESCR_CMD] = &ID0,
+ [iINTERFACE_DESCR_USART1] = &ID1,
+ [iINTERFACE_DESCR_USART2] = &ID2,
+ [iINTERFACE_DESCR_USART3] = &ID3,
+ [iINTERFACE_DESCR_USART4] = &ID4,
+ [iINTERFACE_DESCR_CAN] = &ID5,
+ [iINTERFACE_DESCR_DBG] = &ID6
+};
+
+
+/*
+ * default handlers
+ */
+// SET_LINE_CODING
+static void linecoding_handler(int ifNo, usb_LineCoding *lc){
+ if(ifNo < USART1_EPNO || ifNo > USARTMAX_EPNO) return;
+ usart_config(ifNo + 1 - USART1_EPNO, lc);
+}
+
+// SET_CONTROL_LINE_STATE
+static void clstate_handler(int __attribute__((unused)) ifNo, uint16_t __attribute__((unused)) val){
+}
+
+// SEND_BREAK
+static void break_handler(int __attribute__((unused)) ifNo){
+}
+
+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(){
+ uint8_t descrtype = setup_packet->wValue >> 8,
+ descridx = setup_packet->wValue & 0xff;
+ switch(descrtype){
+ case DEVICE_DESCRIPTOR:
+ wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
+ break;
+ case CONFIGURATION_DESCRIPTOR:
+ wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
+ break;
+ case STRING_DESCRIPTOR:
+ if(descridx < iDESCR_AMOUNT) wr0((const uint8_t *)StringDescriptor[descridx], *((uint8_t*)StringDescriptor[descridx]));
+ else EP_WriteIRQ(0, (uint8_t*)0, 0);
+ break;
+ case DEVICE_QUALIFIER_DESCRIPTOR:
+ wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
+ break;
+ default:
+ break;
+ }
+}
+
+static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
+static inline void std_d2h_req(){
+ uint16_t status = 0; // bus powered
+ switch(setup_packet->bRequest){
+ 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, (uint8_t*)&configuration, 1);
+ break;
+ default:
+ break;
+ }
+}
+
+// Rx and Tx handlers for EP1..EP7
+static void rxtx_Handler(uint8_t epno){
+ uint8_t buf[USB_TRBUFSZ];
+ int idx = epno - 1;
+ uint16_t epstatus = KEEP_DTOG(USB->EPnR[epno]);
+ if(RX_FLAG(epstatus)){
+ uint8_t sz = EP_Read(epno, (uint8_t*)buf);
+ if(sz){
+ if(RB_write((ringbuffer*)&rbin[idx], buf, sz) != sz) bufovrfl[idx] = 1;
+ }
+ epstatus = (epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // keep stat Tx & set valid RX, clear CTR Rx
+ USB->EPnR[epno] = epstatus;
+ }else{
+ USB->EPnR[epno] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr
+ send_next(idx);
+ }
+}
+
+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_Addr = setup_packet->wValue;
+ break;
+ case SET_CONFIGURATION:
+ // Now device configured
+ configuration = setup_packet->wValue;
+ for(uint8_t i = 1; i <= WORK_EPs; ++i){
+ EP_Init(i, EP_TYPE_BULK, USB_TRBUFSZ, USB_TRBUFSZ, rxtx_Handler);
+ }
+ 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(uint8_t __attribute__((unused)) epno){
+ 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);
+ usb_LineCoding *lc;
+ 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 CONTROL_REQUEST_TYPE:
+ switch(setup_packet->bRequest){
+ case GET_LINE_CODING:
+ lc = getLineCoding(1); // TODO: HOW can we change number of interface???
+ if(!lc) EP_WriteIRQ(0, (uint8_t *)0, 0);
+ else EP_WriteIRQ(0, (uint8_t*)&lc, sizeof(usb_LineCoding));
+ break;
+ case SET_LINE_CODING: // omit this for next stage, when data will come
+ break;
+ case SET_CONTROL_LINE_STATE:
+ clstate_handler(1, setup_packet->wValue); // TODO: how can we check iface number???
+ break;
+ case SEND_BREAK:
+ break_handler(1); // TODO: how can we check iface number???
+ 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){
+ // TODO: how can we check iface number???
+ linecoding_handler(1, (usb_LineCoding*)ep0databuf);
+ }
+ }
+ } else if(TX_FLAG(epstatus)){ // package transmitted
+ // now we can change address after enumeration
+ if ((USB->DADDR & USB_DADDR_ADD) != USB_Addr){
+ USB->DADDR = USB_DADDR_EF | USB_Addr;
+ usbON = 0;
+ }
+ }
+ epstatus = KEEP_DTOG(USB->EPnR[0]);
+ if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/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;
+#if defined USB1_16
+ // very bad: what if `size` is odd?
+ uint32_t *out = (uint32_t *)endpoints[number].tx_buf;
+ for(int i = 0; i < N2; ++i, ++out){
+ *out = buf16[i];
+ }
+#elif defined USB2_16
+ // use memcpy instead?
+ for(int i = 0; i < N2; i++){
+ endpoints[number].tx_buf[i] = buf16[i];
+ }
+#else
+#error "Define USB1_16 or USB2_16"
+#endif
+ USB_BTABLE->EP[number].USB_COUNT_TX = size;
+}
+
+/**
+ * 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, uint8_t *buf){
+ int sz = endpoints[number].rx_cnt;
+ if(!sz) return 0;
+ endpoints[number].rx_cnt = 0;
+#if defined USB1_16
+ int n = (sz + 1) >> 1;
+ uint32_t *in = (uint32_t*)endpoints[number].rx_buf;
+ uint16_t *out = (uint16_t*)buf;
+ for(int i = 0; i < n; ++i, ++in)
+ out[i] = *(uint16_t*)in;
+#elif defined USB2_16
+ // use memcpy instead?
+ for(int i = 0; i < sz; ++i)
+ buf[i] = endpoints[number].rx_buf[i];
+#else
+#error "Define USB1_16 or USB2_16"
+#endif
+ return sz;
+}
+
diff --git a/F3:F303/Seven_CDCs/usb_lib.h b/F3:F303/Seven_CDCs/usb_lib.h
new file mode 100644
index 0000000..3bead86
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usb_lib.h
@@ -0,0 +1,183 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include
+#include "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
+
+// string descriptors
+enum{
+ iLANGUAGE_DESCR,
+ iMANUFACTURER_DESCR,
+ iPRODUCT_DESCR,
+ iSERIAL_DESCR,
+ iINTERFACE_DESCR_CMD,
+ iINTERFACE_DESCR_USART1,
+ iINTERFACE_DESCR_USART2,
+ iINTERFACE_DESCR_USART3,
+ iINTERFACE_DESCR_USART4,
+ iINTERFACE_DESCR_CAN,
+ iINTERFACE_DESCR_DBG,
+ iDESCR_AMOUNT
+};
+
+// Types of descriptors
+#define DEVICE_DESCRIPTOR 0x01
+#define CONFIGURATION_DESCRIPTOR 0x02
+#define STRING_DESCRIPTOR 0x03
+#define DEVICE_QUALIFIER_DESCRIPTOR 0x06
+
+#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))
+
+// 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
+
+#if 0
+typedef struct{
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint16_t *bString;
+} string_descriptor_t;
+
+#define _USB_STRING_(name, str) string_descriptor_t name = {(sizeof(str) + 2), STRING_DESCRIPTOR, str}
+#endif
+
+#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}
+
+// 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
+ uint8_t *rx_buf; // reception buffer address
+ void (*func)(uint8_t epno); // endpoint action function
+ unsigned rx_cnt : 10; // received data counter
+} ep_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 volatile uint8_t usbON;
+extern config_pack_t *setup_packet;
+extern uint8_t ep0databuf[], setupdatabuf[];
+
+void EP0_Handler(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, uint8_t *buf);
+
diff --git a/F3:F303/Seven_CDCs/usbhw.c b/F3:F303/Seven_CDCs/usbhw.c
new file mode 100644
index 0000000..3414927
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usbhw.c
@@ -0,0 +1,123 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include "usb.h"
+#include "usb_lib.h"
+
+// here we suppose that all PIN settings done in hw_setup earlier
+void USB_setup(){
+ NVIC_DisableIRQ(USB_LP_IRQn);
+ // remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303
+ RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG
+ SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP;
+ RCC->APB1ENR |= RCC_APB1ENR_USBEN;
+ 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_IRQn);
+}
+
+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) 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 * ACCESSZ);
+ endpoints[number].txbufsz = txsz;
+ lastaddr += txsz;
+ USB_BTABLE->EP[number].USB_COUNT_TX = 0;
+ USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
+ endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
+ lastaddr += rxsz;
+ USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
+ endpoints[number].func = func;
+ return 0;
+}
+
+// standard IRQ handler
+void usb_lp_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;
+ if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){
+ return;
+ }
+ USB->ISTR = ~USB_ISTR_RESET;
+ }
+ 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, setupdatabuf);
+ // interrupt handler will be called later
+ }else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
+ EP_Read(0, ep0databuf);
+ }
+ }
+ }
+ // 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;
+ }
+}
+
diff --git a/F3:F303/Seven_CDCs/usbhw.h b/F3:F303/Seven_CDCs/usbhw.h
new file mode 100644
index 0000000..b1656aa
--- /dev/null
+++ b/F3:F303/Seven_CDCs/usbhw.h
@@ -0,0 +1,111 @@
+/*
+ * This file is part of the SevenCDCs project.
+ * Copyright 2023 Edward V. Emelianov .
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include
+
+// max endpoints number
+#define STM32ENDPOINTS 8
+/**
+ * Buffers size definition
+ **/
+#define USB_BTABLE_SIZE 768
+// 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
+#define USB_EP0_BUFSZ 64
+// USB transmit/receive buffer size
+#define USB_TRBUFSZ 64
+// EP1 - interrupt - buffer size
+#define USB_EP1BUFSZ 8
+
+#define USB_BTABLE_BASE 0x40006000
+#define USB ((USB_TypeDef *) USB_BASE)
+
+#ifdef USB_BTABLE
+#undef USB_BTABLE
+#endif
+#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE))
+#define USB_ISTR_EPID 0x0000000F
+#define USB_FNR_LSOF_0 0x00000800
+#define USB_FNR_lSOF_1 0x00001000
+#define USB_LPMCSR_BESL_0 0x00000010
+#define USB_LPMCSR_BESL_1 0x00000020
+#define USB_LPMCSR_BESL_2 0x00000040
+#define USB_LPMCSR_BESL_3 0x00000080
+#define USB_EPnR_CTR_RX 0x00008000
+#define USB_EPnR_DTOG_RX 0x00004000
+#define USB_EPnR_STAT_RX 0x00003000
+#define USB_EPnR_STAT_RX_0 0x00001000
+#define USB_EPnR_STAT_RX_1 0x00002000
+#define USB_EPnR_SETUP 0x00000800
+#define USB_EPnR_EP_TYPE 0x00000600
+#define USB_EPnR_EP_TYPE_0 0x00000200
+#define USB_EPnR_EP_TYPE_1 0x00000400
+#define USB_EPnR_EP_KIND 0x00000100
+#define USB_EPnR_CTR_TX 0x00000080
+#define USB_EPnR_DTOG_TX 0x00000040
+#define USB_EPnR_STAT_TX 0x00000030
+#define USB_EPnR_STAT_TX_0 0x00000010
+#define USB_EPnR_STAT_TX_1 0x00000020
+#define USB_EPnR_EA 0x0000000F
+#define USB_COUNTn_RX_BLSIZE 0x00008000
+#define USB_COUNTn_NUM_BLOCK 0x00007C00
+#define USB_COUNTn_RX 0x0000003F
+
+#define USB_TypeDef USB_TypeDef_custom
+
+typedef struct {
+ __IO uint32_t EPnR[STM32ENDPOINTS];
+ __IO uint32_t RESERVED[STM32ENDPOINTS];
+ __IO uint32_t CNTR;
+ __IO uint32_t ISTR;
+ __IO uint32_t FNR;
+ __IO uint32_t DADDR;
+ __IO uint32_t BTABLE;
+} USB_TypeDef;
+
+// F303 D/E have 2x16 access scheme
+typedef struct{
+#if defined USB2_16
+ __IO uint16_t USB_ADDR_TX;
+ __IO uint16_t USB_COUNT_TX;
+ __IO uint16_t USB_ADDR_RX;
+ __IO uint16_t USB_COUNT_RX;
+#define ACCESSZ (1)
+#define BUFTYPE uint8_t
+#elif defined USB1_16
+ __IO uint32_t USB_ADDR_TX;
+ __IO uint32_t USB_COUNT_TX;
+ __IO uint32_t USB_ADDR_RX;
+ __IO uint32_t USB_COUNT_RX;
+#define ACCESSZ (2)
+#define BUFTYPE uint16_t
+#else
+#error "Define USB1_16 or USB2_16"
+#endif
+} USB_EPDATA_TypeDef;
+
+
+typedef struct{
+ __IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS];
+} USB_BtableDef;
+
+void USB_setup();
+int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(uint8_t epno));
diff --git a/F3:F303/Seven_CDCs/version.inc b/F3:F303/Seven_CDCs/version.inc
new file mode 100644
index 0000000..bb6dca5
--- /dev/null
+++ b/F3:F303/Seven_CDCs/version.inc
@@ -0,0 +1,2 @@
+#define BUILD_NUMBER "47"
+#define BUILD_DATE "2023-04-18"