From 207ab57e99db201756c42339999f0417a5f270e4 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Wed, 8 Jan 2025 18:00:07 +0300 Subject: [PATCH] seems like all works @ F103 --- .../USB_NEW_concept_7inone/Makefile | 9 + .../USB_NEW_concept_7inone/hardware.c | 50 +++ .../USB_NEW_concept_7inone/hardware.h | 37 ++ .../USB_NEW_concept_7inone/main.c | 93 ++++ .../USB_NEW_concept_7inone/openocd.cfg | 4 + .../USB_NEW_concept_7inone/ringbuffer.c | 166 +++++++ .../USB_NEW_concept_7inone/ringbuffer.h | 41 ++ .../USB_NEW_concept_7inone/someusb.bin | Bin 0 -> 5484 bytes .../USB_NEW_concept_7inone/someusb.cflags | 1 + .../USB_NEW_concept_7inone/someusb.config | 4 + .../USB_NEW_concept_7inone/someusb.creator | 1 + .../someusb.creator.user | 183 ++++++++ .../USB_NEW_concept_7inone/someusb.cxxflags | 1 + .../USB_NEW_concept_7inone/someusb.files | 13 + .../USB_NEW_concept_7inone/someusb.includes | 6 + .../USB_NEW_concept_7inone/usart.c | 123 ++++++ .../USB_NEW_concept_7inone/usart.h | 45 ++ .../USB_NEW_concept_7inone/usb_descr.c | 256 +++++++++++ .../USB_NEW_concept_7inone/usb_descr.h | 69 +++ .../USB_NEW_concept_7inone/usb_dev.c | 296 +++++++++++++ .../USB_NEW_concept_7inone/usb_dev.h | 56 +++ .../USB_NEW_concept_7inone/usb_lib.c | 414 ++++++++++++++++++ .../USB_NEW_concept_7inone/usb_lib.h | 324 ++++++++++++++ .../USB_NEW_concept_CDC/someusb.bin | Bin 4180 -> 6900 bytes .../USB_NEW_concept_CDC/someusb.creator.user | 2 +- .../USB_NEW_concept_CDC/usb_lib.c | 22 +- .../USB_NEW_concept_CDC/usb_lib.h | 3 +- .../USB_NEW_concept_HID/someusb.bin | Bin 3056 -> 3048 bytes .../USB_NEW_concept_HID/usb_lib.c | 35 +- .../USB_NEW_concept_HID/usb_lib.h | 6 +- .../USB_NEW_concept_PL2303/someusb.bin | Bin 4400 -> 4364 bytes .../USB_NEW_concept_PL2303/usb_lib.c | 22 +- .../USB_NEW_concept_PL2303/usb_lib.h | 3 +- 33 files changed, 2244 insertions(+), 41 deletions(-) create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/Makefile create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.c create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.h create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/main.c create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/openocd.cfg create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.c create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.h create mode 100755 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.bin create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cflags create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.config create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator.user create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cxxflags create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.files create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.includes create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.c create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.h create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.c create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.h create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.c create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.h create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.c create mode 100644 F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.h diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/Makefile b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/Makefile new file mode 100644 index 0000000..32e070d --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/Makefile @@ -0,0 +1,9 @@ +BINARY := someusb +# MCU code +MCU ?= F103x6 +# change this linking script depending on particular MCU model, +LDSCRIPT ?= stm32f103x6.ld +DEFINES := -DSTM32F10X_LD + +include ../makefile.f1 +include ../../makefile.stm32 diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.c b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.c new file mode 100644 index 0000000..f28ac75 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.c @@ -0,0 +1,50 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "hardware.h" + +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; + AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE; // for PA15 + // Set led as opendrain output + GPIOC->CRH |= CRH(13, CNF_ODOUTPUT|MODE_SLOW); + // USB pullup (PA15) - pushpull output + GPIOA->CRH = CRH(15, CNF_PPOUTPUT|MODE_SLOW); +} + +void hw_setup(){ + gpio_setup(); +} + +#ifndef EBUG +void iwdg_setup(){ + uint32_t tmout = 16000000; + RCC->CSR |= RCC_CSR_LSION; + while((RCC->CSR & RCC_CSR_LSIRDY) != RCC_CSR_LSIRDY){if(--tmout == 0) break;} /* (2) */ + IWDG->KR = IWDG_START; + IWDG->KR = IWDG_WRITE_ACCESS; + IWDG->PR = IWDG_PR_PR_1; + IWDG->RLR = 1250; + tmout = 16000000; + while(IWDG->SR){if(--tmout == 0) break;} + IWDG->KR = IWDG_REFRESH; +} +#endif + diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.h b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.h new file mode 100644 index 0000000..e7bfb32 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/hardware.h @@ -0,0 +1,37 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "stm32f1.h" + +// LED0 - PC13 (bluepill), blinking each second +#define LED0_port GPIOC +#define LED0_pin (1<<13) + +// USB pullup (not present in bluepill, should be soldered) - PA15 +#define USBPU_port GPIOA +#define USBPU_pin (1<<15) +#define USBPU_ON() pin_set(USBPU_port, USBPU_pin) +#define USBPU_OFF() pin_clear(USBPU_port, USBPU_pin) + +#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) + +void hw_setup(); +void iwdg_setup(); diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/main.c b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/main.c new file mode 100644 index 0000000..14d2ceb --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/main.c @@ -0,0 +1,93 @@ +/* + * This file is part of the I2Cscan project. + * Copyright 2020 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" +#include "usb_dev.h" + +volatile uint32_t Tms = 0; + +/* Called when systick fires */ +void sys_tick_handler(void){ + ++Tms; +} + +#define STRLEN (256) + +#define US(i, s, l) do{while(CDCready[i] && !USB_send(i, s, l)){IWDG->KR = IWDG_REFRESH;}}while(0) +#define USS(i, s) do{while(CDCready[i] && !USB_sendstr(i, s)){IWDG->KR = IWDG_REFRESH;}}while(0) + +int main(void){ + char inbuff[RBINSZ]; + uint32_t lastT = 0, lastS = 0; + StartHSE(); + hw_setup(); + USBPU_OFF(); + SysTick_Config(72000); +#ifdef EBUG + usart_setup(); + DBG("Start"); + uint32_t tt = 0; +#endif + USB_setup(); +#ifndef EBUG + iwdg_setup(); +#endif + USBPU_ON(); + while (1){ + IWDG->KR = IWDG_REFRESH; // refresh watchdog + if(Tms - lastT > 499){ + LED_blink(LED0); + lastT = Tms; + } +#ifdef EBUG + if(Tms != tt){ + __disable_irq(); + usart_transmit(); + tt = Tms; + __enable_irq(); + } +#endif + + if(Tms - lastS > 9999){ + for(int i = 0; i < bTotNumEndpoints; ++i){ + if(!CDCready[i]) continue; + USS(i, "Tms="); + USS(i, uhex2str(Tms)); + USS(i, "\n"); + /* for(int j = 0; j < 40; ++j) + US(i, (uint8_t*)"112345678921234567893123456789412345678951234567896123456789712345678981234567899123456789a123456789\n", 101); + USS(i, uhex2str(Tms)); + USS(i, "\n");*/ + } + lastS = Tms; + } + + for(int i = 0; i < bTotNumEndpoints; ++i){ + if(!CDCready[i]) continue; + int l = USB_receivestr(i, inbuff, RBINSZ); + if(l < 0) USS(i, "ERROR: USB buffer overflow or string was too long\n"); + else if(l){ + USS(i, "RECEIVED: _"); + USS(i, inbuff); + USS(i, "_\n"); + } + } + } + return 0; +} diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/openocd.cfg b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/openocd.cfg new file mode 100644 index 0000000..18ecde1 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/openocd.cfg @@ -0,0 +1,4 @@ +set FLASH_SIZE 0x20000 + +source [find interface/stlink-v2-1.cfg] +source [find target/stm32f1x.cfg] diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.c b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.c new file mode 100644 index 0000000..b0ffe8e --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.c @@ -0,0 +1,166 @@ +/* + * 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 "ringbuffer.h" + +static int datalen(ringbuffer *b){ + if(b->tail >= b->head) return (b->tail - b->head); + else return (b->length - b->head + b->tail); +} + +// stored data length +int RB_datalen(ringbuffer *b){ + if(b->busy) return -1; + b->busy = 1; + int l = datalen(b); + b->busy = 0; + return l; +} + +static int hasbyte(ringbuffer *b, uint8_t byte){ + if(b->head == b->tail) return -1; // no data in buffer + int startidx = b->head; + if(b->head > b->tail){ // + for(int found = b->head; found < b->length; ++found) + if(b->data[found] == byte) return found; + startidx = 0; + } + for(int found = startidx; found < b->tail; ++found) + if(b->data[found] == byte) return found; + return -1; +} + +/** + * @brief RB_hasbyte - check if buffer has given byte stored + * @param b - buffer + * @param byte - byte to find + * @return index if found, -1 if none or busy + */ +int RB_hasbyte(ringbuffer *b, uint8_t byte){ + if(b->busy) return -1; + b->busy = 1; + int ret = hasbyte(b, byte); + b->busy = 0; + return ret; +} + +// poor memcpy +static void mcpy(uint8_t *targ, const uint8_t *src, int l){ + while(l--) *targ++ = *src++; +} + +// increment head or tail +TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){ + *what += n; + if(*what >= b->length) *what -= b->length; +} + +static int read(ringbuffer *b, uint8_t *s, int len){ + int l = datalen(b); + if(!l) return 0; + if(l > len) l = len; + int _1st = b->length - b->head; + if(_1st > l) _1st = l; + if(_1st > len) _1st = len; + mcpy(s, b->data + b->head, _1st); + if(_1st < len && l > _1st){ + mcpy(s+_1st, b->data, l - _1st); + incr(b, &b->head, l); + return l; + } + incr(b, &b->head, _1st); + return _1st; +} + +/** + * @brief RB_read - read data from ringbuffer + * @param b - buffer + * @param s - array to write data + * @param len - max len of `s` + * @return bytes read or -1 if busy + */ +int RB_read(ringbuffer *b, uint8_t *s, int len){ + if(b->busy) return -1; + b->busy = 1; + int r = read(b, s, len); + b->busy = 0; + return r; +} + +static int readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){ + int idx = hasbyte(b, byte); + if(idx < 0) return 0; + int partlen = idx + 1 - b->head; + // now calculate length of new data portion + if(idx < b->head) partlen += b->length; + if(partlen > len) return -read(b, s, len); + return read(b, s, partlen); +} + +/** + * @brief RB_readto fill array `s` with data until byte `byte` (with it) + * @param b - ringbuffer + * @param byte - check byte + * @param s - buffer to write data + * @param len - length of `s` + * @return amount of bytes written (negative, if lenbusy) return -1; + b->busy = 1; + int n = readto(b, byte, s, len); + b->busy = 0; + return n; +} + +static int write(ringbuffer *b, const uint8_t *str, int l){ + int r = b->length - 1 - datalen(b); // rest length + if(l > r || !l) return 0; + int _1st = b->length - b->tail; + if(_1st > l) _1st = l; + mcpy(b->data + b->tail, str, _1st); + if(_1st < l){ // add another piece from start + mcpy(b->data, str+_1st, l-_1st); + } + incr(b, &b->tail, l); + return l; +} + +/** + * @brief RB_write - write some data to ringbuffer + * @param b - buffer + * @param str - data + * @param l - length + * @return amount of bytes written or -1 if busy + */ +int RB_write(ringbuffer *b, const uint8_t *str, int l){ + if(b->busy) return -1; + b->busy = 1; + int w = write(b, str, l); + b->busy = 0; + return w; +} + +// just delete all information in buffer `b` +int RB_clearbuf(ringbuffer *b){ + if(b->busy) return -1; + b->busy = 1; + b->head = 0; + b->tail = 0; + b->busy = 0; + return 1; +} diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.h b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.h new file mode 100644 index 0000000..ed2cf95 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/ringbuffer.h @@ -0,0 +1,41 @@ +/* + * 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 + +#if defined STM32F0 +#include +#elif defined STM32F1 +#include +#elif defined STM32F3 +#include +#endif + +typedef struct{ + uint8_t *data; // data buffer + const int length; // its length + int head; // head index + int tail; // tail index + volatile int busy; // == TRUE if buffer is busy now +} ringbuffer; + +int RB_read(ringbuffer *b, uint8_t *s, int len); +int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len); +int RB_hasbyte(ringbuffer *b, uint8_t byte); +int RB_write(ringbuffer *b, const uint8_t *str, int l); +int RB_datalen(ringbuffer *b); +int RB_clearbuf(ringbuffer *b); diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.bin b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.bin new file mode 100755 index 0000000000000000000000000000000000000000..aee4688ca06ccc8f50026113a36f63791d6b2aad GIT binary patch literal 5484 zcmd5=dw5e-wqN_4ljP(9q%BJN5KbQ>rBMm3b46f=!)bbGlVX&@h%k3NK+&iXYSGbq z_1*@GSOuk0Uw{u3M$zk7+A0GhPHQ`U*2mnNfW4M7+ImMDAag%YXi7IPd)H1<#&J4- z%zroiR`x#Yto_@2{npxR?-U9_`jrL5#|em!{y^XdA7;aZsagA#`ESSk5aaJXe`Oqh z?2|uO!`~bK|5b1G*xzny*r61*XR4Js)%Fa(7~*F0(M^Jjax+V*;Cx;=8|Jk6vZ`+hEHeD~GnaRHz%ESgSrM4&;q^&;CLyul0zO*L zb0}oEV?nn+>#<{gZOF`JZ6qxX3kz^WSTOXgiw74^mJ4*7u)KO2>e3BX)WIr{aw;av z?s#y?2D%&EiT6{yIo0&N;E#N9sKIe-sd5P1<5Y3`qeNr!-U(~49@1K2agF1E5G@o0Q zuPSVp3--2_tGVjv$n?>DQGwgu`m(#|(81XJNn9_#|Gg7>y^M1e`aNgfxRNB0)4Ve@ zZh*}Fsm6r@fn4Ei&s_nBaH5Be;d`OzPoYLN6oFh(CEG_wE__FJ#v^HJ0;@DWh;c;@ zzy-(+u-O5b=iooNo$Bj?!7fu<3 z%%00I>gAyWo{Y!3hRo2=GF5Hz6W6*Svr)DSC6{mHqe0kPTtbC_P*ShtQSL4p25F)o z^}K$=Gjy-iIkX^yIM;+YrxpTdRS5F-dY5ZO#tA(yRUDv$(kB64uj88(LkRK}J;;4r zy_xQIZPHx=Kj1VvKig4xm0Wse{`w|QzFZ%dt1`4S#__i4!Y4ibRPH=X?Ji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l1Imu5e;!H7y;?RM~mxnl&*-EN17I zZk#FY@?+Mhh1^`Sfi9V4iIItrdm!?X(|0N(?Zh!=ee+*gpXha!puG)px2Ei0ium-= z5-*-%-I$+;QAGS*vjlVa_t@uEc5{HD^I=C8nO(DR$=&OJg82*Ot2#`SF5(tJ%2z&~G<4uM~lL-76!p0;5mqZX! z2toqEcnQIfLJ$ll!(&MV!x%z+5`j-57*hziTwILl5`rm(U^JNwTapN-F@#M?1R;qa zrVzvgf_MqRoI()ICd1Ywf_V(#;Ut0~iC{?~SP}^4O9<8!g2iewJf1{A){{(bP9hkS z2(}c0ErDRUgfJn6V4GkvJds40Foy6*62Zh85b=JX<8S9~l*1_dP@X~wq6AQup}0|8 zC>Em~14?8}2T14dgZ21Nlv`mnRHG~$N2iXXD;r1W9!FO?j!qdzw*YlE-UZiSk-r9J z!Ik_{r~(BfC;X9VHMs6-_!p4TkCpiD8r0th0&f;%d=H(> zOYt9Qa68}#9q*cn4#P-&bbcJ2FpkbJj?Ora&NPlr97ks!M`syFXB|gp8%H-`9Nolm Pbd&y8=Xzj_4S@dxuNMhU literal 0 HcmV?d00001 diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cflags b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.config b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.config new file mode 100644 index 0000000..55bd3a6 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.config @@ -0,0 +1,4 @@ +#define EBUG +#define STM32F1 +#define STM32F103x6 +#define STM32F10X_LD diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator @@ -0,0 +1 @@ +[General] diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator.user b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator.user new file mode 100644 index 0000000..6d3b983 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.creator.user @@ -0,0 +1,183 @@ + + + + + + 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 + 0 + 80 + true + true + 1 + 0 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + false + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + false + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 8 + true + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Default + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + 0 + true + + + 2 + + false + -e cpu-cycles --call-graph dwarf,4096 -F 250 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cxxflags b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.files b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.files new file mode 100644 index 0000000..6f277ab --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.files @@ -0,0 +1,13 @@ +hardware.c +hardware.h +main.c +ringbuffer.c +ringbuffer.h +usart.c +usart.h +usb_descr.c +usb_descr.h +usb_lib.c +usb_lib.h +usb_dev.c +usb_dev.h diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.includes b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.includes new file mode 100644 index 0000000..06d1130 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/someusb.includes @@ -0,0 +1,6 @@ +. +../inc +../inc/Fx +../inc/cm +../inc/ld +../inc/startup diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.c b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.c new file mode 100644 index 0000000..4f31169 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.c @@ -0,0 +1,123 @@ +/* + * usart.c + * + * Copyright 2018 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 + +#include "stm32f1.h" +#include "usart.h" +#include "ringbuffer.h" + +extern volatile uint32_t Tms; +static uint8_t buf[UARTBUFSZO], txbuf[UARTBUFSZO]; +static ringbuffer ringbuf = {.data = buf, .length = UARTBUFSZO}; + +static volatile int usart_txrdy = 1; // transmission done + +// transmit current tbuf +void usart_transmit(){ + if(RB_hasbyte(&ringbuf, '\n') < 0 || !usart_txrdy) return; + int L = 0, l = 0; + do{ + l = RB_readto(&ringbuf, '\n', txbuf + L, UARTBUFSZO - L); + if(l > 0) L += l; + }while(l > 0 && L < UARTBUFSZO); + if(L < 1) return; + usart_txrdy = 0; + if(L < UARTBUFSZO-1){ + txbuf[L++] = '$'; txbuf[L++] = '\n'; + } + DMA1_Channel4->CCR &= ~DMA_CCR_EN; + DMA1_Channel4->CMAR = (uint32_t) txbuf; // mem + DMA1_Channel4->CNDTR = L; + DMA1_Channel4->CCR |= DMA_CCR_EN; +} + +void usart_putchar(const char ch){ + RB_write(&ringbuf, (const uint8_t*)&ch, 1); +} + +void usart_send(const char *str){ + int l = strlen(str); + if(RB_datalen(&ringbuf) > UARTBUFSZO/2) usart_transmit(); + RB_write(&ringbuf, (const uint8_t*)str, l); +} + +/* + * USART speed: baudrate = Fck/(USARTDIV) + * USARTDIV stored in USART->BRR + * + * for 72MHz USARTDIV=72000/f(kboud); so for 115200 USARTDIV=72000/115.2=625 -> BRR=0x271 + * 9600: BRR = 7500 (0x1D4C) + */ + +void usart_setup(){ + uint32_t tmout = 16000000; + // PA9 - Tx, PA10 - Rx + RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN; + RCC->AHBENR |= RCC_AHBENR_DMA1EN; + GPIOA->CRH |= CRH(9, CNF_AFPP|MODE_NORMAL) | CRH(10, CNF_FLINPUT|MODE_INPUT); + + // USART1 Tx DMA - Channel4 (Rx - channel 5) + DMA1_Channel4->CPAR = (uint32_t) &USART1->DR; // periph + DMA1_Channel4->CCR |= DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE; // 8bit, mem++, mem->per, transcompl irq + // Tx CNDTR set @ each transmission due to data size + NVIC_SetPriority(DMA1_Channel4_IRQn, 3); + NVIC_EnableIRQ(DMA1_Channel4_IRQn); + NVIC_SetPriority(USART1_IRQn, 0); + // setup usart1 + USART1->BRR = 72000000 / 4000000; + USART1->CR1 = USART_CR1_TE | USART_CR1_UE; // 1start,8data,nstop; enable Rx,Tx,USART + while(!(USART1->SR & USART_SR_TC)){if(--tmout == 0) break;} // polling idle frame Transmission + USART1->SR = 0; // clear flags + USART1->CR1 |= USART_CR1_RXNEIE; // allow Rx IRQ + USART1->CR3 = USART_CR3_DMAT; // enable DMA Tx +} + + +void dma1_channel4_isr(){ + if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx + DMA1->IFCR = DMA_IFCR_CTCIF4; // clear TC flag + usart_txrdy = 1; + } +} + + + +char *uhex2str(uint32_t val){ + static char abuf[12] = "0x"; + int npos = 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) abuf[npos++] = half + '0'; + else abuf[npos++] = half - 10 + 'a'; + } + } + abuf[npos] = 0; + return abuf; +} diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.h b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.h new file mode 100644 index 0000000..c362186 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usart.h @@ -0,0 +1,45 @@ +/* + * usart.h + * + * Copyright 2018 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. + */ + +#pragma once + +#include + +#define UARTBUFSZO (2048) + +// macro for static strings +#define USEND(str) usart_send(str) + +#ifdef EBUG +#define STR_HELPER(s) #s +#define STR(s) STR_HELPER(s) +#define DBG(str) do{usart_send(__FILE__ " (L" STR(__LINE__) "): " str); usart_putchar('\n');}while(0) +#define DBGs(str) do{usart_send(str); usart_putchar('\n');}while(0) +#else +#define DBG(s) +#define DBGs(s) +#endif + +void usart_transmit(); +void usart_setup(); +void usart_send(const char *str); +void usart_putchar(const char ch); +char *uhex2str(uint32_t val); diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.c b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.c new file mode 100644 index 0000000..dd1a78c --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.c @@ -0,0 +1,256 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "usart.h" +#include "usb_descr.h" + +// low/high for uint16_t +#define L16(x) (x & 0xff) +#define H16(x) (x >> 8) + +static const uint8_t USB_DeviceDescriptor[] = { + USB_DT_DEVICE_SIZE, // bLength + USB_DT_DEVICE, // bDescriptorType + L16(bcdUSB), // bcdUSB_L + H16(bcdUSB), // bcdUSB_H + USB_CLASS_MISC, // bDeviceClass + bDeviceSubClass, // bDeviceSubClass + bDeviceProtocol, // bDeviceProtocol + USB_EP0BUFSZ, // bMaxPacketSize + L16(idVendor), // idVendor_L + H16(idVendor), // idVendor_H + L16(idProduct), // idProduct_L + H16(idProduct), // idProduct_H + L16(bcdDevice_Ver), // bcdDevice_Ver_L + H16(bcdDevice_Ver), // bcdDevice_Ver_H + iMANUFACTURER_DESCR, // iManufacturer - indexes of string descriptors in array + iPRODUCT_DESCR, // iProduct + iSERIAL_DESCR, // iSerial + bNumConfigurations // bNumConfigurations +}; + +static const uint8_t USB_DeviceQualifierDescriptor[] = { + USB_DT_QUALIFIER_SIZE, //bLength + USB_DT_QUALIFIER, // bDescriptorType + L16(bcdUSB), // bcdUSB_L + H16(bcdUSB), // bcdUSB_H + USB_CLASS_PER_INTERFACE, // bDeviceClass + bDeviceSubClass, // bDeviceSubClass + bDeviceProtocol, // bDeviceProtocol + USB_EP0BUFSZ, // bMaxPacketSize0 + bNumConfigurations, // bNumConfigurations + 0 // Reserved +}; + +#define wTotalLength (USB_DT_CONFIG_SIZE + bTotNumEndpoints * 66) + +/* + * _1stI - number of first interface + * IDidx - 0 or index of string descriptor for given interface + * EPx - number of virtual interrupt EP + * EP - number of real EP in/out + */ +#define USB_IAD(_1stI, IDidx, EPx, EP) \ + USB_DT_IAD_SIZE, /* bLength: IAD size */ \ + USB_DT_IAD, /* bDescriptorType: IAD */ \ + _1stI, /* bFirstInterface */ \ + 2, /* bInterfaceCount */ \ + 2, /* bFunctionClass: CDC */ \ + 2, /* bFunctionSubClass */ \ + 0, /* bFunctionProtocol */ \ + 0, /* iFunction */ \ + /* Interface Descriptor */ \ + USB_DT_INTERFACE_SIZE, /* bLength: Interface Descriptor size */ \ + USB_DT_INTERFACE, /* bDescriptorType: Interface */ \ + _1stI, /* bInterfaceNumber: Number of Interface */ \ + 0, /* bAlternateSetting: Alternate setting */ \ + 1, /* bNumEndpoints: one for this */ \ + USB_CLASS_COMM, /* bInterfaceClass */ \ + 2, /* bInterfaceSubClass: ACM */ \ + 0, /* bInterfaceProtocol */ \ + IDidx, /* iInterface */ \ + /* CDC descriptor */ \ + USB_DT_CS_INTERFACE_SIZE, /* bLength */ \ + USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \ + 0, /* bDescriptorSubtype: Header Func Desc */ \ + 0x10, /* bcdCDC: spec release number */ \ + 1, /* bDataInterface */ \ + USB_DT_CS_INTERFACE_SIZE, /* bLength */ \ + USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \ + 1, /* bDescriptorSubtype: Call Management Func Desc */ \ + 0, /* bmCapabilities: D0+D1 */ \ + (_1stI+1), /* bDataInterface */ \ + USB_DT_CS_INTERFACE_SIZE-1, /* bLength */ \ + USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \ + 2, /* bDescriptorSubtype: Abstract Control Management desc */ \ + 2, /* bmCapabilities */ \ + USB_DT_CS_INTERFACE_SIZE, /* bLength */ \ + USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \ + 6, /* bDescriptorSubtype: Union func desc */ \ + _1stI, /* bMasterInterface: Communication class interface */ \ + (_1stI+1), /* bSlaveInterface0: Data Class Interface */ \ + /* Virtual endpoint 1 Descriptor */ \ + USB_DT_ENDPOINT_SIZE, /* bLength: Endpoint Descriptor size */ \ + USB_DT_ENDPOINT, /* bDescriptorType: Endpoint */ \ + (0x80+EPx), /* bEndpointAddress IN8 - non-existant */ \ + USB_BM_ATTR_INTERRUPT, /* bmAttributes: Interrupt */ \ + L16(USB_EP1BUFSZ), /* wMaxPacketSize LO */ \ + H16(USB_EP1BUFSZ), /* wMaxPacketSize HI */ \ + 0x10, /* bInterval: 16ms */ \ + /* DATA endpoint */ \ + USB_DT_INTERFACE_SIZE, /* bLength: Interface Descriptor size */ \ + USB_DT_INTERFACE, /* bDescriptorType: Interface */ \ + (_1stI+1), /* bInterfaceNumber: Number of Interface */ \ + 0, /* bAlternateSetting: Alternate setting */ \ + 2, /* bNumEndpoints: in and out */ \ + USB_CLASS_DATA, /* bInterfaceClass */ \ + 2, /* bInterfaceSubClass: ACM */ \ + 0, /* bInterfaceProtocol */ \ + 0, /* iInterface */ \ + /*Endpoint IN1 Descriptor */ \ + USB_DT_ENDPOINT_SIZE, /* bLength: Endpoint Descriptor size */ \ + USB_DT_ENDPOINT, /* bDescriptorType: Endpoint */ \ + (0x80+EP), /* bEndpointAddress: IN1 */ \ + USB_BM_ATTR_BULK, /* bmAttributes: Bulk */ \ + L16(USB_TXBUFSZ), /* wMaxPacketSize LO */ \ + H16(USB_TXBUFSZ), /* wMaxPacketSize HI */ \ + 0, /* bInterval: ignore for Bulk transfer */ \ + /* Endpoint OUT1 Descriptor */ \ + USB_DT_ENDPOINT_SIZE, /* bLength: Endpoint Descriptor size */ \ + USB_DT_ENDPOINT, /* bDescriptorType: Endpoint */ \ + EP, /* bEndpointAddress: OUT1 */ \ + USB_BM_ATTR_BULK, /* bmAttributes: Bulk */ \ + L16(USB_RXBUFSZ), /* wMaxPacketSize LO */ \ + H16(USB_RXBUFSZ), /* wMaxPacketSize HI */ \ + 0 /* bInterval: ignore for Bulk transfer */ + + +static const uint8_t USB_ConfigDescriptor[] = { + // Configuration Descriptor + USB_DT_CONFIG_SIZE, // bLength: Configuration Descriptor size + USB_DT_CONFIG, // bDescriptorType: Configuration + L16(wTotalLength), // wTotalLength.L :no of returned bytes + H16(wTotalLength), // wTotalLength.H + bNumInterfaces, // bNumInterfaces + 1, // bConfigurationValue: Current configuration value + 0, // iConfiguration: Index of string descriptor describing the configuration or 0 + BusPowered, // bmAttributes - Bus powered + 50, // MaxPower in 2mA units + //--IADs------------------------------------------------------------------------- + USB_IAD(0, iINTERFACE_DESCR1, 8, 1), + USB_IAD(2, iINTERFACE_DESCR2, 9, 2), + USB_IAD(4, iINTERFACE_DESCR3, 10, 3), + USB_IAD(6, iINTERFACE_DESCR4, 11, 4), + USB_IAD(8, iINTERFACE_DESCR5, 12, 5), + USB_IAD(10, iINTERFACE_DESCR6, 13, 6), + USB_IAD(12, iINTERFACE_DESCR7, 14, 7), +}; + +//const uint8_t HID_ReportDescriptor[]; + +_USB_LANG_ID_(LD, LANG_US); +_USB_STRING_(SD, u"0.0.1"); +_USB_STRING_(MD, u"eddy@sao.ru"); +_USB_STRING_(PD, u"USB CDC Controller"); +_USB_STRING_(ID1, u"usb_cdcA"); +_USB_STRING_(ID2, u"usb_cdcB"); +_USB_STRING_(ID3, u"usb_cdcC"); +_USB_STRING_(ID4, u"usb_cdcD"); +_USB_STRING_(ID5, u"usb_cdcE"); +_USB_STRING_(ID6, u"usb_cdcF"); +_USB_STRING_(ID7, u"usb_cdcG"); + +static const void* const StringDescriptor[iDESCR_AMOUNT] = { + [iLANGUAGE_DESCR] = &LD, + [iMANUFACTURER_DESCR] = &MD, + [iPRODUCT_DESCR] = &PD, + [iSERIAL_DESCR] = &SD, + [iINTERFACE_DESCR1] = &ID1, + [iINTERFACE_DESCR2] = &ID2, + [iINTERFACE_DESCR3] = &ID3, + [iINTERFACE_DESCR4] = &ID4, + [iINTERFACE_DESCR5] = &ID5, + [iINTERFACE_DESCR6] = &ID6, + [iINTERFACE_DESCR7] = &ID7, +}; + +static void wr0(const uint8_t *buf, uint16_t size, uint16_t askedsize){ + if(askedsize < size) size = askedsize; // shortened request + if(size < USB_EP0BUFSZ){ + EP_WriteIRQ(0, buf, size); + DBG("short wr0"); + DBGs(uhex2str(size)); + return; + } + DBG("long wr0"); + DBGs(uhex2str(size)); + while(size){ + uint16_t l = size; + if(l > USB_EP0BUFSZ) l = USB_EP0BUFSZ; + EP_WriteIRQ(0, buf, l); + buf += l; + size -= l; + uint8_t needzlp = (l == USB_EP0BUFSZ) ? 1 : 0; + if(size || needzlp){ // send last data buffer + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); + // keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx + USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX)) + ^ USB_EPnR_STAT_TX; + uint32_t ctr = 1000000; + while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;}; + if((USB->ISTR & USB_ISTR_CTR) == 0){ + return; + } + if(needzlp) EP_WriteIRQ(0, NULL, 0); + } + } +} + +void get_descriptor(config_pack_t *pack){ + uint8_t descrtype = pack->wValue >> 8, + descridx = pack->wValue & 0xff; + switch(descrtype){ + case DEVICE_DESCRIPTOR: + DBG("DEVICE_DESCRIPTOR"); + wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor), pack->wLength); + break; + case CONFIGURATION_DESCRIPTOR: + DBG("CONFIGURATION_DESCRIPTOR"); + wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor), pack->wLength); + break; + case STRING_DESCRIPTOR: + DBG("STRING_DESCRIPTOR"); + if(descridx < iDESCR_AMOUNT){ + wr0((const uint8_t *)StringDescriptor[descridx], *((uint8_t*)StringDescriptor[descridx]), pack->wLength); + DBGs(uhex2str(descridx)); + }else{ + EP_WriteIRQ(0, NULL, 0); + DBG("Wrong index"); + DBGs(uhex2str(descridx)); + } + break; + case DEVICE_QUALIFIER_DESCRIPTOR: + DBG("DEVICE_QUALIFIER_DESCRIPTOR"); + wr0(USB_DeviceQualifierDescriptor, sizeof(USB_DeviceQualifierDescriptor), pack->wLength); + break; + /* case HID_REPORT_DESCRIPTOR: + wr0(HID_ReportDescriptor, sizeof(HID_ReportDescriptor), pack->wLength); + break;*/ + default: + break; + } +} diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.h b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.h new file mode 100644 index 0000000..ac6fb5c --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_descr.h @@ -0,0 +1,69 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +#include "usb_lib.h" + +// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor +// bcdUSB: 1.10 +#define bcdUSB 0x0110 +// Class - Misc (EF), subclass - common (2), protocol - interface association descr (1) +#define bDeviceSubClass 0x02 +#define bDeviceProtocol 0x01 +#define idVendor 0x0483 +#define idProduct 0x5740 +#define bcdDevice_Ver 0x0200 +#define bNumConfigurations 1 + +// amount of interfaces and endpoints (except 0) used +#define bNumInterfaces 14 +#define bTotNumEndpoints 7 +#define bNumCsInterfaces 7 + +// powered +#define BusPowered (1<<7) +#define SelfPowered (1<<6) +#define RemoteWakeup (1<<5) + +// buffer sizes +// for USB FS EP0 buffers are from 8 to 64 bytes long +#define USB_EP0BUFSZ 64 +// virtual +#define USB_EP1BUFSZ 10 +// Rx/Tx EPs: (512-64-128)/14 rouded to 8 +#define USB_RXBUFSZ 16 +#define USB_TXBUFSZ 16 + +// string descriptors +enum{ + iLANGUAGE_DESCR, + iMANUFACTURER_DESCR, + iPRODUCT_DESCR, + iSERIAL_DESCR, + iINTERFACE_DESCR1, + iINTERFACE_DESCR2, + iINTERFACE_DESCR3, + iINTERFACE_DESCR4, + iINTERFACE_DESCR5, + iINTERFACE_DESCR6, + iINTERFACE_DESCR7, + iDESCR_AMOUNT +}; + +void get_descriptor(config_pack_t *pack); diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.c b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.c new file mode 100644 index 0000000..7dc25ba --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.c @@ -0,0 +1,296 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "ringbuffer.h" +#include "usart.h" +#include "usb_descr.h" +#include "usb_dev.h" + +// Class-Specific Control Requests +#define SEND_ENCAPSULATED_COMMAND 0x00 // unused +#define GET_ENCAPSULATED_RESPONSE 0x01 // unused +#define SET_COMM_FEATURE 0x02 // unused +#define GET_COMM_FEATURE 0x03 // unused +#define CLEAR_COMM_FEATURE 0x04 // unused +#define SET_LINE_CODING 0x20 +#define GET_LINE_CODING 0x21 +#define SET_CONTROL_LINE_STATE 0x22 +#define SEND_BREAK 0x23 + +// control line states +#define CONTROL_DTR 0x01 +#define CONTROL_RTS 0x02 + +// inbuf overflow when receiving +static volatile uint8_t bufovrfl[bTotNumEndpoints] = {0}; + +// receive buffer: hold data until chkin() call +static uint8_t volatile rcvbuf[bTotNumEndpoints][USB_RXBUFSZ]; +static uint8_t volatile rcvbuflen[bTotNumEndpoints] = {0}; +// line coding +#define DEFLC {115200, 0, 0, 8} +static usb_LineCoding lineCoding[bTotNumEndpoints] = {DEFLC, DEFLC, DEFLC, DEFLC, DEFLC, DEFLC, DEFLC}; +// CDC configured and ready to use +volatile uint8_t CDCready[bTotNumEndpoints] = {0}; + +// ring buffers for incoming and outgoing data +static uint8_t obuf[bTotNumEndpoints][RBOUTSZ], ibuf[bTotNumEndpoints][RBINSZ]; +#define OBUF(N) {.data = obuf[N], .length = RBOUTSZ, .head = 0, .tail = 0} +static volatile ringbuffer rbout[bTotNumEndpoints] = {OBUF(0), OBUF(1), OBUF(2), OBUF(3), OBUF(4), OBUF(5), OBUF(6)}; +#define IBUF(N) {.data = ibuf[N], .length = RBINSZ, .head = 0, .tail = 0} +static volatile ringbuffer rbin[bTotNumEndpoints] = {IBUF(0), IBUF(1), IBUF(2), IBUF(3), IBUF(4), IBUF(5), IBUF(6)}; +// last send data size +static volatile int lastdsz[bTotNumEndpoints] = {0}; + +static void chkin(uint8_t ifno){ + if(bufovrfl[ifno]) return; // allow user to know that previous buffer was overflowed and cleared + if(!rcvbuflen[ifno]) return; + int w = RB_write((ringbuffer*)&rbin[ifno], (uint8_t*)rcvbuf[ifno], rcvbuflen[ifno]); + if(w < 0){ + DBG("Can't write into buffer"); + return; + } + if(w != rcvbuflen[ifno]) bufovrfl[ifno] = 1; + DBG("Put data into buffer"); + rcvbuflen[ifno] = 0; + uint16_t status = KEEP_DTOG(USB->EPnR[1+ifno]); // don't change DTOG + USB->EPnR[1+ifno] = (status & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // prepare to get next data portion +} + +// called from transmit EP to send next data portion or by user - when new transmission starts +static void send_next(uint8_t ifno){ + uint8_t usbbuff[USB_TXBUFSZ]; + int buflen = RB_read((ringbuffer*)&rbout[ifno], (uint8_t*)usbbuff, USB_TXBUFSZ); + if(buflen == 0){ + if(lastdsz[ifno] == 64) EP_Write(1+ifno, NULL, 0); // send ZLP after 64 bits packet when nothing more to send + lastdsz[ifno] = 0; + return; + }else if(buflen < 0){ + DBG("Buff busy"); + lastdsz[ifno] = 0; + return; + } + DBG("Got data in buf"); + DBGs(uhex2str(buflen)); + DBGs(uhex2str(ifno)); + EP_Write(1+ifno, (uint8_t*)usbbuff, buflen); + lastdsz[ifno] = buflen; +} + +// data IN/OUT handler +static void rxtx_handler(){ + uint8_t ifno = (USB->ISTR & USB_ISTR_EPID) - 1; + DBG("rxtx_handler"); + DBGs(uhex2str(ifno)); + if(ifno > bTotNumEndpoints-1){ + DBG("wront ifno"); + return; + } + uint16_t epstatus = KEEP_DTOG(USB->EPnR[1+ifno]); + if(RX_FLAG(epstatus)){ // receive data + DBG("Got data"); + if(rcvbuflen[ifno]){ + bufovrfl[ifno] = 1; // lost last data + rcvbuflen[ifno] = 0; + DBG("OVERFULL"); + } + rcvbuflen[ifno] = EP_Read(1+ifno, (uint8_t*)rcvbuf[ifno]); + DBGs(uhex2str(rcvbuflen[ifno])); + USB->EPnR[1+ifno] = epstatus & ~(USB_EPnR_CTR_RX | USB_EPnR_STAT_RX | USB_EPnR_STAT_TX); // keep RX in STALL state until read data + chkin(ifno); // try to write current data into RXbuf if it's not busy + }else{ // tx successfull + DBG("Tx OK"); + USB->EPnR[1+ifno] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX; + send_next(ifno); + } +} + +// weak handlers: change them somewhere else if you want to setup USART +// SET_LINE_CODING +void WEAK linecoding_handler(uint8_t ifno, usb_LineCoding *lc){ + lineCoding[ifno] = *lc; + DBG("linecoding_handler"); + DBGs(uhex2str(ifno)); +} + +// SET_CONTROL_LINE_STATE +void WEAK clstate_handler(uint8_t ifno, uint16_t val){ + DBG("clstate_handler"); + DBGs(uhex2str(ifno)); + DBGs(uhex2str(val)); + CDCready[ifno] = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected +} + +// SEND_BREAK +void WEAK break_handler(uint8_t ifno){ + CDCready[ifno] = 0; + DBG("break_handler()"); + DBGs(uhex2str(ifno)); +} + + +// USB is configured: setup endpoints +void set_configuration(){ + DBG("set_configuration()"); + for(int i = 0; i < bTotNumEndpoints; ++i){ + int r = EP_Init(1+i, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_handler); + if(r){ + DBG("Can't init EP"); + DBGs(uhex2str(i)); + DBGs(uhex2str(r)); + } + } +} + +// PL2303 CLASS request +void usb_class_request(config_pack_t *req, uint8_t *data, uint16_t datalen){ + uint8_t recipient = REQUEST_RECIPIENT(req->bmRequestType); + uint8_t dev2host = (req->bmRequestType & 0x80) ? 1 : 0; + uint8_t ifno = req->wIndex >> 1; + if(ifno > bTotNumEndpoints-1 && ifno != 0xff){ + DBG("wront ifno"); + return; + } + DBG("usb_class_request"); + DBGs(uhex2str(req->bRequest)); + switch(recipient){ + case REQ_RECIPIENT_INTERFACE: + switch(req->bRequest){ + case SET_LINE_CODING: + DBG("SET_LINE_CODING"); + if(!data || !datalen) break; // wait for data + if(datalen == sizeof(usb_LineCoding)) + linecoding_handler(ifno, (usb_LineCoding*)data); + break; + case GET_LINE_CODING: + DBG("GET_LINE_CODING"); + EP_WriteIRQ(0, (uint8_t*)&lineCoding[ifno], sizeof(lineCoding)); + break; + case SET_CONTROL_LINE_STATE: + DBG("SET_CONTROL_LINE_STATE"); + clstate_handler(ifno, req->wValue); + break; + case SEND_BREAK: + DBG("SEND_BREAK"); + break_handler(ifno); + break; + default: + DBG("Wrong"); + DBGs(uhex2str(req->bRequest)); + DBGs(uhex2str(datalen)); + } + break; + default: + DBG("Wrong"); + DBGs(uhex2str(recipient)); + DBGs(uhex2str(datalen)); + DBGs(uhex2str(req->bRequest)); + if(dev2host) EP_WriteIRQ(0, NULL, 0); + } + if(!dev2host) EP_WriteIRQ(0, NULL, 0); +} + +// blocking send full content of ring buffer +int USB_sendall(uint8_t ifno){ + while(lastdsz[ifno] > 0){ + if(!CDCready[ifno]) return FALSE; + } + return TRUE; +} + +// put `buf` into queue to send +int USB_send(uint8_t ifno, const uint8_t *buf, int len){ + if(!buf || !CDCready[ifno] || !len) return FALSE; + DBG("USB_send"); + while(len){ + int a = RB_write((ringbuffer*)&rbout[ifno], buf, len); + if(a > 0){ + len -= a; + buf += a; + } else if (a < 0) continue; // do nothing if buffer is in reading state + if(lastdsz[ifno] == 0) send_next(ifno); // need to run manually - all data sent, so no IRQ on IN + } + return TRUE; +} + +int USB_putbyte(uint8_t ifno, uint8_t byte){ + if(!CDCready[ifno]) return FALSE; + int l = 0; + while((l = RB_write((ringbuffer*)&rbout[ifno], &byte, 1)) != 1){ + if(l < 0) continue; + } + if(lastdsz[ifno] == 0) send_next(ifno); // need to run manually - all data sent, so no IRQ on IN + return TRUE; +} + +int USB_sendstr(uint8_t ifno, const char *string){ + if(!string || !CDCready[ifno]) return FALSE; + int len = strlen(string); + if(!len) return FALSE; + 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(uint8_t ifno, uint8_t *buf, int len){ + chkin(ifno); + if(bufovrfl[ifno]){ + DBG("Buffer overflow"); + DBGs(uhex2str(ifno)); + while(1 != RB_clearbuf((ringbuffer*)&rbin[ifno])); + bufovrfl[ifno] = 0; + return -1; + } + int sz = RB_read((ringbuffer*)&rbin[ifno], buf, len); + if(sz < 0) return 0; // buffer in writting state + DBG("usb read"); + 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(uint8_t ifno, char *buf, int len){ + chkin(ifno); + if(bufovrfl[ifno]){ + while(1 != RB_clearbuf((ringbuffer*)&rbin[ifno])); + bufovrfl[ifno] = 0; + return -1; + } + int l = RB_readto((ringbuffer*)&rbin[ifno], '\n', (uint8_t*)buf, len); + if(l < 1){ + if(rbin[ifno].length == RB_datalen((ringbuffer*)&rbin[ifno])){ // buffer is full but no '\n' found + while(1 != RB_clearbuf((ringbuffer*)&rbin[ifno])); + return -1; + } + return 0; + } + if(l == 0) return 0; + buf[l-1] = 0; // replace '\n' with strend + return l; +} + diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.h b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.h new file mode 100644 index 0000000..a2926f2 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_dev.h @@ -0,0 +1,56 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include "usb_descr.h" +#include "usb_lib.h" + +typedef struct { + uint32_t dwDTERate; + uint8_t bCharFormat; +#define USB_CDC_1_STOP_BITS 0 +#define USB_CDC_1_5_STOP_BITS 1 +#define USB_CDC_2_STOP_BITS 2 + uint8_t bParityType; +#define USB_CDC_NO_PARITY 0 +#define USB_CDC_ODD_PARITY 1 +#define USB_CDC_EVEN_PARITY 2 +#define USB_CDC_MARK_PARITY 3 +#define USB_CDC_SPACE_PARITY 4 + uint8_t bDataBits; +} __attribute__ ((packed)) usb_LineCoding; + +extern volatile uint8_t CDCready[bTotNumEndpoints]; + +void break_handler(uint8_t ifno); +void clstate_handler(uint8_t ifno, uint16_t val); +void linecoding_handler(uint8_t ifno, usb_LineCoding *lc); + + +// sizes of ringbuffers for outgoing and incoming data +#define RBOUTSZ (256) +#define RBINSZ (256) + +#define newline(ifno) USB_putbyte(ifno, '\n') +#define USND(ifno, s) do{USB_sendstr(ifno, s); USB_putbyte(ifno, '\n');}while(0) + +int USB_sendall(uint8_t ifno); +int USB_send(uint8_t ifno, const uint8_t *buf, int len); +int USB_putbyte(uint8_t ifno, uint8_t byte); +int USB_sendstr(uint8_t ifno, const char *string); +int USB_receive(uint8_t ifno, uint8_t *buf, int len); +int USB_receivestr(uint8_t ifno, char *buf, int len); diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.c b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.c new file mode 100644 index 0000000..be534bc --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.c @@ -0,0 +1,414 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +#include "usart.h" +#include "usb_lib.h" +#include "usb_descr.h" +#include "usb_dev.h" + +#undef DBG +#define DBG(x) +#undef DBGs +#define DBGs(x) + +static ep_t endpoints[STM32ENDPOINTS]; + +static uint16_t USB_Addr = 0; +static uint8_t setupdatabuf[EP0DATABUF_SIZE]; +static config_pack_t *setup_packet = (config_pack_t*) setupdatabuf; +volatile uint8_t usbON = 0; // device is configured and active + +static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured) +static inline void std_d2h_req(){ + uint16_t st = 0; + switch(setup_packet->bRequest){ + case GET_DESCRIPTOR: + DBG("GET_DESCRIPTOR"); + get_descriptor(setup_packet); + break; + case GET_STATUS: + DBG("GET_STATUS"); + EP_WriteIRQ(0, (uint8_t *)&st, 2); // send status: Bus Powered + break; + case GET_CONFIGURATION: + DBG("GET_CONFIGURATION"); + EP_WriteIRQ(0, (uint8_t*)&configuration, 1); + break; + default: + DBG("Wrong"); + DBGs(uhex2str(setup_packet->bRequest)); + EP_WriteIRQ(0, NULL, 0); + break; + } +} + +static inline void std_h2d_req(){ + switch(setup_packet->bRequest){ + case SET_ADDRESS: + DBG("SET_ADDRESS"); + // new address will be assigned later - after acknowlegement or request to host + USB_Addr = setup_packet->wValue; + DBGs(uhex2str(USB_Addr)); + break; + case SET_CONFIGURATION: + DBG("SET_CONFIGURATION"); + // Now device configured + configuration = setup_packet->wValue; + set_configuration(); + usbON = 1; + break; + default: + DBG("Wrong"); + DBGs(uhex2str(setup_packet->bRequest)); + break; + } +} + +void WEAK usb_standard_request(){ + uint8_t recipient = REQUEST_RECIPIENT(setup_packet->bmRequestType); + uint8_t dev2host = (setup_packet->bmRequestType & 0x80) ? 1 : 0; + switch(recipient){ + case REQ_RECIPIENT_DEVICE: + DBG("REQ_RECIPIENT_DEVICE"); + if(dev2host){ + std_d2h_req(); + }else{ + std_h2d_req(); + } + break; + case REQ_RECIPIENT_INTERFACE: + DBG("REQ_RECIPIENT_INTERFACE"); + if(dev2host && setup_packet->bRequest == GET_DESCRIPTOR){ + get_descriptor(setup_packet); + } + break; + case REQ_RECIPIENT_ENDPOINT: + DBG("REQ_RECIPIENT_ENDPOINT"); + if(setup_packet->bRequest == CLEAR_FEATURE){ + }else{ + DBG("Wrong"); + } + break; + default: + DBG("Wrong"); + DBGs(uhex2str(recipient)); + break; + } + if(!dev2host) EP_WriteIRQ(0, NULL, 0); +} + +void WEAK usb_class_request(config_pack_t *req, uint8_t _U_ *data, uint16_t _U_ datalen){ + DBG("REQ_TYPE_CLASS"); + switch(req->bRequest){ + case GET_INTERFACE: + DBG("GI"); + break; + case SET_CONFIGURATION: // set featuring by req->wValue + DBG("SC"); + break; + default: + DBG("Wrong"); + DBGs(uhex2str(req->bmRequestType)); + DBGs(uhex2str(req->bRequest)); + DBGs(uhex2str(req->wIndex)); + DBGs(uhex2str(req->wLength)); + DBGs(uhex2str(req->wValue)); + } + if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev + EP_WriteIRQ(0, NULL, 0); +} + +void WEAK usb_vendor_request(config_pack_t _U_ *packet, uint8_t _U_ *data, uint16_t _U_ datalen){ + DBG("vendor"); + if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev + EP_WriteIRQ(0, NULL, 0); +} + +/* +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 ep0dbuflen = 0; + uint8_t ep0databuf[EP0DATABUF_SIZE]; + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications + int rxflag = RX_FLAG(epstatus); + if(rxflag){ DBG("EP0_Handler"); } + // check direction + if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit) + if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack + DBG("USB_EPnR_SETUP"); + EP_Read(0, setupdatabuf); + // interrupt handler will be called later + }else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf + if(endpoints[0].rx_cnt){ DBG("data"); DBGs(uhex2str(endpoints[0].rx_cnt));} + ep0dbuflen = EP_Read(0, ep0databuf); + } + } + if(rxflag){ + uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType); + switch(reqtype){ + case REQ_TYPE_STANDARD: + if(SETUP_FLAG(epstatus)){ + DBG("REQ_TYPE_STANDARD"); + usb_standard_request(); + }else{ + DBG("REQ_TYPE_STANDARD without SETUP_FLAG"); + } + break; + case REQ_TYPE_CLASS: + DBG("REQ_TYPE_CLASS"); + usb_class_request(setup_packet, ep0databuf, ep0dbuflen); + break; + case REQ_TYPE_VENDOR: + DBG("REQ_TYPE_VENDOR"); + usb_vendor_request(setup_packet, ep0databuf, ep0dbuflen); + break; + default: + DBG("Wrong"); + DBGs(uhex2str(reqtype)); + EP_WriteIRQ(0, NULL, 0); + break; + } + } + if(TX_FLAG(epstatus)){ + // now we can change address after enumeration + if ((USB->DADDR & USB_DADDR_ADD) != USB_Addr){ + USB->DADDR = USB_DADDR_EF | USB_Addr; + usbON = 0; + DBG("Enum"); + DBGs(uhex2str(USB_Addr)); + } + } + //epstatus = KEEP_DTOG(USB->EPnR[0]); + if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission + else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged + // keep DTOGs, clear CTR_RX,TX, set RX VALID + USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX; +} + +/** + * 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 epstatus = KEEP_DTOG(USB->EPnR[number]); + // keep DTOGs and RX stat, clear CTR_TX & set TX VALID to start transmission + USB->EPnR[number] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_RX)) ^ USB_EPnR_STAT_TX; +} + +/* + * 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; +} + + +static uint16_t lastaddr = LASTADDR_DEFAULT; +/** + * Endpoint initialisation + * @param number - EP num (0...7) + * @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT) + * @param txsz - transmission buffer size @ USB/CAN buffer + * @param rxsz - reception buffer size @ USB/CAN buffer + * @param uint16_t (*func)(ep_t *ep) - EP handler function + * @return 0 if all OK + */ +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ + if(number >= STM32ENDPOINTS) return 4; // out of configured amount + if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large + if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable + USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); + USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; + if(rxsz & 1) return 3; // wrong rx buffer size + uint16_t countrx = 0; + if(rxsz < 64) countrx = rxsz / 2; + else{ + if(rxsz & 0x1f) return 3; // should be multiple of 32 + countrx = 31 + rxsz / 32; + } + USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr; + endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ); + endpoints[number].txbufsz = txsz; + lastaddr += txsz; + USB_BTABLE->EP[number].USB_COUNT_TX = 0; + USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr; + endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ); + lastaddr += rxsz; + USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10; + endpoints[number].func = func; + return 0; +} + +// standard IRQ handler +void USB_IRQ(){ + uint32_t CNTR = USB->CNTR; + USB->CNTR = 0; + if(USB->ISTR & USB_ISTR_RESET){ + DBG("USB_ISTR_RESET"); + usbON = 0; + // Reinit registers + CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM; + // Endpoint 0 - CONTROL + // ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes! + lastaddr = LASTADDR_DEFAULT; + // clear address, leave only enable bit + USB->DADDR = USB_DADDR_EF; + USB->ISTR = ~USB_ISTR_RESET; + if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0BUFSZ, USB_EP0BUFSZ, EP0_Handler)){ + DBG("Can't init EP0"); + return; + }; + } + if(USB->ISTR & USB_ISTR_CTR){ + // EP number + uint8_t n = USB->ISTR & USB_ISTR_EPID; + // copy received bytes amount + endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter + // call EP handler + if(endpoints[n].func) endpoints[n].func(); + } + if(USB->ISTR & USB_ISTR_WKUP){ // wakeup + DBG("USB_ISTR_WKUP"); +#ifndef STM32F0 + CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM); // clear suspend flags +#else + CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM); +#endif + USB->ISTR = ~USB_ISTR_WKUP; + } + if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep + DBG("USB_ISTR_SUSP"); + usbON = 0; +#ifndef STM32F0 + CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM; +#else + CNTR |= USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM; +#endif + CNTR &= ~(USB_CNTR_SUSPM); + USB->ISTR = ~USB_ISTR_SUSP; + } + USB->CNTR = CNTR; // rewoke interrupts +} + +// here we suppose that all PIN settings done in hw_setup earlier +void USB_setup(){ +#if defined STM32F3 + NVIC_DisableIRQ(USB_LP_IRQn); + // remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303 + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG + SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP; +#elif defined STM32F1 + NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); + NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); +#elif defined STM32F0 + NVIC_DisableIRQ(USB_IRQn); + RCC->APB1ENR |= RCC_APB1ENR_CRSEN; + RCC->CFGR3 &= ~RCC_CFGR3_USBSW; // reset USB + RCC->CR2 |= RCC_CR2_HSI48ON; // turn ON HSI48 + uint32_t tmout = 16000000; + while(!(RCC->CR2 & RCC_CR2_HSI48RDY)){if(--tmout == 0) break;} + FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY; + CRS->CFGR &= ~CRS_CFGR_SYNCSRC; + CRS->CFGR |= CRS_CFGR_SYNCSRC_1; // USB SOF selected as sync source + CRS->CR |= CRS_CR_AUTOTRIMEN; // enable auto trim + CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only + RCC->CFGR |= RCC_CFGR_SW; +#endif + RCC->APB1ENR |= RCC_APB1ENR_USBEN; + //?? + USB->CNTR = USB_CNTR_FRES; // Force USB Reset + for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms + USB->CNTR = 0; + USB->BTABLE = 0; + USB->DADDR = 0; + USB->ISTR = 0; + USB->CNTR = USB_CNTR_RESETM; // allow only reset interrupts +#if defined STM32F3 + NVIC_EnableIRQ(USB_LP_IRQn); +#elif defined STM32F1 + NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); +#elif defined STM32F0 + USB->BCDR |= USB_BCDR_DPPU; + NVIC_EnableIRQ(USB_IRQn); +#endif +} + + +#if defined STM32F3 +void usb_lp_isr() __attribute__ ((alias ("USB_IRQ"))); +#elif defined STM32F1 +void usb_lp_can_rx0_isr() __attribute__ ((alias ("USB_IRQ"))); +#elif defined STM32F0 +void usb_isr() __attribute__ ((alias ("USB_IRQ"))); +#endif diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.h b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.h new file mode 100644 index 0000000..c6e70b0 --- /dev/null +++ b/F1:F103/USB_New_concept/USB_NEW_concept_7inone/usb_lib.h @@ -0,0 +1,324 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include + +/****************************************************************** + * Hardware registers etc * + *****************************************************************/ +#if defined STM32F0 +#include +#elif defined STM32F1 +#include +// there's no this define in standard header +#define USB_BASE ((uint32_t)0x40005C00) +#elif defined STM32F3 +#include +#endif + +// max endpoints number +#define STM32ENDPOINTS 8 +/** + * Buffers size definition + **/ + +// F0 - USB2_16; F1 - USB1_16; F3 - 1/2 depending on series +#if !defined USB1_16 && !defined USB2_16 +#if defined STM32F0 +#define USB2_16 +#elif defined STM32F1 +#define USB1_16 +#else +#error "Can't determine USB1_16 or USB2_16, define by hands" +#endif +#endif + +// BTABLE_SIZE FOR STM32F3: +// In STM32F303/302xB/C, 512 bytes SRAM is not shared with CAN. +// In STM32F302x6/x8 and STM32F30xxD/E, 726 bytes dedicated SRAM and 256 bytes shared SRAM with CAN i.e. +// 1Kbytes dedicated SRAM in case CAN is disabled. +// remember, that USB_BTABLE_SIZE will be divided by ACCESSZ, so don't divide it twice for 32-bit addressing + +#ifdef NOCAN +#if defined STM32F0 +#define USB_BTABLE_SIZE 1024 +#elif defined STM32F3 +#define USB_BTABLE_SIZE 512 +#warning "Please, check real buffer size due to docs" +#else +#error "define STM32F0 or STM32F3" +#endif +#else // !NOCAN: F0/F3 with CAN or F1 (can't simultaneously run CAN and USB) +#if defined STM32F0 +#define USB_BTABLE_SIZE 768 +#elif defined STM32F3 +#define USB_BTABLE_SIZE 512 +#warning "Please, check real buffer size due to docs" +#else // STM32F103: 1024 bytes but with 32-bit addressing +#define USB_BTABLE_SIZE 1024 +#endif +#endif // NOCAN + +// first 64 bytes of USB_BTABLE are registers! + +#define USB_BTABLE_BASE 0x40006000 +#define USB ((USB_TypeDef *) USB_BASE) + +#ifdef USB_BTABLE +#undef USB_BTABLE +#endif +#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE)) +#define USB_ISTR_EPID 0x0000000F +#define USB_FNR_LSOF_0 0x00000800 +#define USB_FNR_lSOF_1 0x00001000 +#define USB_LPMCSR_BESL_0 0x00000010 +#define USB_LPMCSR_BESL_1 0x00000020 +#define USB_LPMCSR_BESL_2 0x00000040 +#define USB_LPMCSR_BESL_3 0x00000080 +#define USB_EPnR_CTR_RX 0x00008000 +#define USB_EPnR_DTOG_RX 0x00004000 +#define USB_EPnR_STAT_RX 0x00003000 +#define USB_EPnR_STAT_RX_0 0x00001000 +#define USB_EPnR_STAT_RX_1 0x00002000 +#define USB_EPnR_SETUP 0x00000800 +#define USB_EPnR_EP_TYPE 0x00000600 +#define USB_EPnR_EP_TYPE_0 0x00000200 +#define USB_EPnR_EP_TYPE_1 0x00000400 +#define USB_EPnR_EP_KIND 0x00000100 +#define USB_EPnR_CTR_TX 0x00000080 +#define USB_EPnR_DTOG_TX 0x00000040 +#define USB_EPnR_STAT_TX 0x00000030 +#define USB_EPnR_STAT_TX_0 0x00000010 +#define USB_EPnR_STAT_TX_1 0x00000020 +#define USB_EPnR_EA 0x0000000F +#define USB_COUNTn_RX_BLSIZE 0x00008000 +#define USB_COUNTn_NUM_BLOCK 0x00007C00 +#define USB_COUNTn_RX 0x0000003F + +#define USB_TypeDef USB_TypeDef_custom + +typedef struct { + __IO uint32_t EPnR[STM32ENDPOINTS]; + __IO uint32_t RESERVED[STM32ENDPOINTS]; + __IO uint32_t CNTR; + __IO uint32_t ISTR; + __IO uint32_t FNR; + __IO uint32_t DADDR; + __IO uint32_t BTABLE; +#ifdef STM32F0 + __IO uint32_t LPMCSR; + __IO uint32_t BCDR; +#endif +} USB_TypeDef; + +// F303 D/E have 2x16 access scheme +typedef struct{ +#if defined USB2_16 + __IO uint16_t USB_ADDR_TX; + __IO uint16_t USB_COUNT_TX; + __IO uint16_t USB_ADDR_RX; + __IO uint16_t USB_COUNT_RX; +#define ACCESSZ (1) +#define BUFTYPE uint8_t +#elif defined USB1_16 + __IO uint32_t USB_ADDR_TX; + __IO uint32_t USB_COUNT_TX; + __IO uint32_t USB_ADDR_RX; + __IO uint32_t USB_COUNT_RX; +#define ACCESSZ (2) +#define BUFTYPE uint16_t +#else +#error "Define USB1_16 or USB2_16" +#endif +} USB_EPDATA_TypeDef; + + +typedef struct{ + __IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS]; +} USB_BtableDef; + +#define EP0DATABUF_SIZE (64) +#define LASTADDR_DEFAULT (STM32ENDPOINTS * 8) + +/****************************************************************** + * Defines from usb.h * + *****************************************************************/ + +/* + * Device and/or Interface Class codes + */ +#define USB_CLASS_PER_INTERFACE 0 +#define USB_CLASS_AUDIO 1 +#define USB_CLASS_COMM 2 +#define USB_CLASS_HID 3 +#define USB_CLASS_PRINTER 7 +#define USB_CLASS_PTP 6 +#define USB_CLASS_MASS_STORAGE 8 +#define USB_CLASS_HUB 9 +#define USB_CLASS_DATA 10 +#define USB_CLASS_MISC 0xef +#define USB_CLASS_VENDOR_SPEC 0xff + +/* + * Descriptor types + */ +#define USB_DT_DEVICE 0x01 +#define USB_DT_CONFIG 0x02 +#define USB_DT_STRING 0x03 +#define USB_DT_INTERFACE 0x04 +#define USB_DT_ENDPOINT 0x05 +#define USB_DT_QUALIFIER 0x06 +#define USB_DT_IAD 0x0B + +#define USB_DT_HID 0x21 +#define USB_DT_REPORT 0x22 +#define USB_DT_PHYSICAL 0x23 +#define USB_DT_CS_INTERFACE 0x24 +#define USB_DT_HUB 0x29 + +/* + * Descriptor sizes per descriptor type + */ +#define USB_DT_DEVICE_SIZE 18 +#define USB_DT_CONFIG_SIZE 9 +#define USB_DT_INTERFACE_SIZE 9 +#define USB_DT_HID_SIZE 9 +#define USB_DT_ENDPOINT_SIZE 7 +#define USB_DT_QUALIFIER_SIZE 10 +#define USB_DT_CS_INTERFACE_SIZE 5 +#define USB_DT_IAD_SIZE 8 + + +// bmRequestType & 0x80 == dev2host (1) or host2dev (0) +// recipient: bmRequestType & 0x1f +#define REQUEST_RECIPIENT(b) (b & 0x1f) +#define REQ_RECIPIENT_DEVICE 0 +#define REQ_RECIPIENT_INTERFACE 1 +#define REQ_RECIPIENT_ENDPOINT 2 +#define REQ_RECIPIENT_OTHER 3 +// type: [bmRequestType & 0x60 >> 5] +#define REQUEST_TYPE(b) ((b&0x60)>>5) +#define REQ_TYPE_STANDARD 0 +#define REQ_TYPE_CLASS 1 +#define REQ_TYPE_VENDOR 2 +#define REQ_TYPE_RESERVED 3 + + +//#define VENDOR_REQUEST 0x01 + +// standard device requests +#define GET_STATUS 0x00 +#define CLEAR_FEATURE 0x01 +#define SET_FEATURE 0x03 +#define SET_ADDRESS 0x05 +#define GET_DESCRIPTOR 0x06 +#define SET_DESCRIPTOR 0x07 +#define GET_CONFIGURATION 0x08 +#define SET_CONFIGURATION 0x09 +// and some standard interface requests +#define GET_INTERFACE 0x0A +#define SET_INTERFACE 0x0B +// and some standard endpoint requests +#define SYNC_FRAME 0x0C + +// Types of descriptors +#define DEVICE_DESCRIPTOR 0x01 +#define CONFIGURATION_DESCRIPTOR 0x02 +#define STRING_DESCRIPTOR 0x03 +#define DEVICE_QUALIFIER_DESCRIPTOR 0x06 +#define DEBUG_DESCRIPTOR 0x0a +#define HID_REPORT_DESCRIPTOR 0x22 + +// EP types for EP_init +#define EP_TYPE_BULK 0x00 +#define EP_TYPE_CONTROL 0x01 +#define EP_TYPE_ISO 0x02 +#define EP_TYPE_INTERRUPT 0x03 + +// EP types for descriptors +#define USB_BM_ATTR_CONTROL 0x00 +#define USB_BM_ATTR_ISO 0x01 +#define USB_BM_ATTR_BULK 0x02 +#define USB_BM_ATTR_INTERRUPT 0x03 + + +/****************************************************************** + * Other stuff * + *****************************************************************/ + +#define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX) +#define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX) +#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)) + +#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} + +// 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)(); // endpoint action function + unsigned rx_cnt : 10; // received data counter +} ep_t; + +extern volatile uint8_t usbON; + +void USB_setup(); +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)()); +void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size); +void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size); +int EP_Read(uint8_t number, uint8_t *buf); + +// could be [re]defined in usb_dev.c +extern void usb_class_request(config_pack_t *packet, uint8_t *data, uint16_t datalen); +extern void usb_vendor_request(config_pack_t *packet, uint8_t *data, uint16_t datalen); +extern void set_configuration(); diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_CDC/someusb.bin b/F1:F103/USB_New_concept/USB_NEW_concept_CDC/someusb.bin index a0be886e4c422a457993cd71699fe751a3dd06e7..c2ede1f361636fbd32eb11f71ac312807ba48bc1 100755 GIT binary patch literal 6900 zcmd5>e{d7moqwy9EXfvFSO#Rt2H9egwo4mm3Pn2zLKyGjnq{ox6}Tc#>GhsME_#r*r*7!Z(v(lNsIh=F;m-QbP>3 zwYGJi@2(^}B)Pdi&&<<)_w9S%_x=6j!M?(LIXAO;8<=hVeS_WiM{8)%G(CS~`uv{z zaeUePKiR){Z}Y}4p8qmD|KH}5*VvUBX|%t(vwdbge<5a<_RX*pJ+e*eRji%uv%loK zXBLIdNldZ#~x=+Q;0Dx#GDv5E|eT+SBi5}MV3ikPhLCw7Q3=pQsvE3@4zNt z<@oM{mb|KPqAIHj6RNUqeBozIyh->p$MP(*UfO%_w_%U$l6K$O-{q0lNv4(_*(rtY z{6~01b^zDv{pPG)ng*V``raz3^WHC7JfO|7qeNYXZJA{{jR6)i=;5QgWbOE+1#5r_ zHB+K8HRkBhhWVLV-YnK}H^yoro)_$Snzwe)Dr4^KJoA4B@5i9_-&sC6{sZtB2REE} z^>*?!H_7>H^VOa7-YwTQ@0%$GigUjzS|rvoBAX>E(dTzZZA9U9mT~5R19CS@UWE(t zovszhX7(`VJ3s!9cN(8ySD63T^YyLt>ueR97_gkfjuf<#47y~o$*c46TG9SyTfD_P$lXuB+z%O?CXL!IT0Y8s<6X4_0@y=eQTOO1K z_2;v&funTVp>BBqaDS(N_WQ1G`5C}RF#mhNeb|2p`;$XN5z)vp2QbIaupuT`b$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!yTz4rhB diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_CDC/someusb.creator.user b/F1:F103/USB_New_concept/USB_NEW_concept_CDC/someusb.creator.user index 04184f9..db7f967 100644 --- a/F1:F103/USB_New_concept/USB_NEW_concept_CDC/someusb.creator.user +++ b/F1:F103/USB_New_concept/USB_NEW_concept_CDC/someusb.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.c b/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.c index ad3c79b..be534bc 100644 --- a/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.c +++ b/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.c @@ -21,13 +21,11 @@ #include "usb_descr.h" #include "usb_dev.h" - #undef DBG #define DBG(x) #undef DBGs #define DBGs(x) - static ep_t endpoints[STM32ENDPOINTS]; static uint16_t USB_Addr = 0; @@ -153,8 +151,9 @@ bmRequestType: 76543210 static void EP0_Handler(){ uint8_t ep0dbuflen = 0; uint8_t ep0databuf[EP0DATABUF_SIZE]; - uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications - //DBG("EP0_Handler"); + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications + int rxflag = RX_FLAG(epstatus); + if(rxflag){ DBG("EP0_Handler"); } // check direction if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit) if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack @@ -166,13 +165,16 @@ static void EP0_Handler(){ ep0dbuflen = EP_Read(0, ep0databuf); } } - int rxflag = RX_FLAG(epstatus); if(rxflag){ uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType); switch(reqtype){ case REQ_TYPE_STANDARD: - DBG("REQ_TYPE_STANDARD"); - usb_standard_request(); + if(SETUP_FLAG(epstatus)){ + DBG("REQ_TYPE_STANDARD"); + usb_standard_request(); + }else{ + DBG("REQ_TYPE_STANDARD without SETUP_FLAG"); + } break; case REQ_TYPE_CLASS: DBG("REQ_TYPE_CLASS"); @@ -198,7 +200,7 @@ static void EP0_Handler(){ DBGs(uhex2str(USB_Addr)); } } - epstatus = KEEP_DTOG(USB->EPnR[0]); + //epstatus = KEEP_DTOG(USB->EPnR[0]); if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged // keep DTOGs, clear CTR_RX,TX, set RX VALID @@ -284,11 +286,11 @@ static uint16_t lastaddr = LASTADDR_DEFAULT; */ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ if(number >= STM32ENDPOINTS) return 4; // out of configured amount - if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large + if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; - if(rxsz & 1 || rxsz > USB_BTABLE_SIZE) return 3; // wrong rx buffer size + if(rxsz & 1) return 3; // wrong rx buffer size uint16_t countrx = 0; if(rxsz < 64) countrx = rxsz / 2; else{ diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.h b/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.h index c1a180f..c6e70b0 100644 --- a/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.h +++ b/F1:F103/USB_New_concept/USB_NEW_concept_CDC/usb_lib.h @@ -184,6 +184,7 @@ typedef struct{ #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_QUALIFIER 0x06 +#define USB_DT_IAD 0x0B #define USB_DT_HID 0x21 #define USB_DT_REPORT 0x22 @@ -201,7 +202,7 @@ typedef struct{ #define USB_DT_ENDPOINT_SIZE 7 #define USB_DT_QUALIFIER_SIZE 10 #define USB_DT_CS_INTERFACE_SIZE 5 - +#define USB_DT_IAD_SIZE 8 // bmRequestType & 0x80 == dev2host (1) or host2dev (0) diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_HID/someusb.bin b/F1:F103/USB_New_concept/USB_NEW_concept_HID/someusb.bin index 4310e47da98ec4fc08d86efbfe719da5963e53a9..8b583f6b8af4b8bb10eea8da7b2019e0d085474b 100755 GIT binary patch delta 677 zcmY+CZ%7ky7{`CVyW6?DVV$j9hUB@uaP53?YYBmw=c3_Ui8gzq5wnI28)+rfi{fVf zK`NnjAQ41?OQhn!QGKKfOkQ$g9gT+#%+z5hWd2OH+G~d z)DxauSSO=#;+uh}%IqkBK93w}GhbWDQ($6#w?_&=ejrbg0z)#v2OMO1reE|fqD}T~7*|F7j@uvu zn_G=wZ-V3KK0m7R7{0-gd)9(&4&l(x<_jmiW?`ft_)UR|xcZ`wc%R9l!BHF3^{96xEY53sT`sxb{gW>Thc)@CQb9gh zd!*P6k24>rZE6ih$k0)x^`B1PQ}DzTdcc#MMK*FlEt^UwCY&LRvnMfIcl9v_I*25& z(t(CHf8e?TxN-UK{8*aI#X#xT)>1^#6NAVJGR297i*s4@?Bybw;g=8qGw2H+Fa8!m zj>hWOg&*)VT?^{6=<=E|BZzEuR9J0mCNr$;gEQ@7gqR5>5pJJuT#_RcP%i{H)j@bLdV56_oU zwUh=n!2YIy;ZC%kiB_=C9d*|cpQ-4f?GtyO5IQvi(g$@Xpm@p%44s%U%yW#I8S+KP znQ*Z4LFxllmSr>`G|K}%W@Ii;!{Lb?9w`j>Udq$JYbIUhKHayQFkb6I+b0f+ySnpED?#p-T?l%b;xuOi7Yg-ORy!bx#uvVjwD?e_e$A$yqMY}16bts-cK z4sse;j6$uiMYy2=?yz<(`FBFpdXxHG9Blj~HpI&cF0y*Z0hxA)S~$6c;>#KIh<$|s zNT3yVd~IC>IU}t8#D9nF#_A>1JyX;F4w394BD+Bjw2q_(HU1>E#1UeYw23WEKiuc7Oqn)a8W7YLZwL(p2d{2sf4!<-4U>U1atf5?@4Y}3$E z@U^w%j7VwP3cI9_bQL@&{i5sWX8c5jr;12! Uqkrq72^ijB?qP<~F=Gq-10f6r_y7O^ diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.c b/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.c index 9db1799..be534bc 100644 --- a/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.c +++ b/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.c @@ -21,12 +21,10 @@ #include "usb_descr.h" #include "usb_dev.h" -/* #undef DBG #define DBG(x) #undef DBGs #define DBGs(x) -*/ static ep_t endpoints[STM32ENDPOINTS]; @@ -54,6 +52,7 @@ static inline void std_d2h_req(){ default: DBG("Wrong"); DBGs(uhex2str(setup_packet->bRequest)); + EP_WriteIRQ(0, NULL, 0); break; } } @@ -90,19 +89,17 @@ void WEAK usb_standard_request(){ std_d2h_req(); }else{ std_h2d_req(); - //EP_WriteIRQ(0, NULL, 0); } break; case REQ_RECIPIENT_INTERFACE: DBG("REQ_RECIPIENT_INTERFACE"); if(dev2host && setup_packet->bRequest == GET_DESCRIPTOR){ get_descriptor(setup_packet); - }//else EP_WriteIRQ(0, NULL, 0); + } break; case REQ_RECIPIENT_ENDPOINT: DBG("REQ_RECIPIENT_ENDPOINT"); if(setup_packet->bRequest == CLEAR_FEATURE){ - //EP_WriteIRQ(0, NULL, 0); }else{ DBG("Wrong"); } @@ -154,26 +151,30 @@ bmRequestType: 76543210 static void EP0_Handler(){ uint8_t ep0dbuflen = 0; uint8_t ep0databuf[EP0DATABUF_SIZE]; - uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications - //DBG("EP0_Handler"); + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications + int rxflag = RX_FLAG(epstatus); + if(rxflag){ DBG("EP0_Handler"); } // check direction if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit) if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack - //DBG("USB_EPnR_SETUP"); + DBG("USB_EPnR_SETUP"); EP_Read(0, setupdatabuf); // interrupt handler will be called later }else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf - //if(endpoints[0].rx_cnt){ DBG("data");} + if(endpoints[0].rx_cnt){ DBG("data"); DBGs(uhex2str(endpoints[0].rx_cnt));} ep0dbuflen = EP_Read(0, ep0databuf); } } - int rxflag = RX_FLAG(epstatus); if(rxflag){ uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType); switch(reqtype){ case REQ_TYPE_STANDARD: - DBG("REQ_TYPE_STANDARD"); - usb_standard_request(); + if(SETUP_FLAG(epstatus)){ + DBG("REQ_TYPE_STANDARD"); + usb_standard_request(); + }else{ + DBG("REQ_TYPE_STANDARD without SETUP_FLAG"); + } break; case REQ_TYPE_CLASS: DBG("REQ_TYPE_CLASS"); @@ -199,7 +200,7 @@ static void EP0_Handler(){ DBGs(uhex2str(USB_Addr)); } } - epstatus = KEEP_DTOG(USB->EPnR[0]); + //epstatus = KEEP_DTOG(USB->EPnR[0]); if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged // keep DTOGs, clear CTR_RX,TX, set RX VALID @@ -243,8 +244,8 @@ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){ EP_WriteIRQ(number, buf, size); uint16_t epstatus = KEEP_DTOG(USB->EPnR[number]); - // keep DTOGs, clear CTR_TX & set TX VALID to start transmission - USB->EPnR[number] = (epstatus & ~(USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_TX; + // keep DTOGs and RX stat, clear CTR_TX & set TX VALID to start transmission + USB->EPnR[number] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_RX)) ^ USB_EPnR_STAT_TX; } /* @@ -285,11 +286,11 @@ static uint16_t lastaddr = LASTADDR_DEFAULT; */ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ if(number >= STM32ENDPOINTS) return 4; // out of configured amount - if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large + if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; - if(rxsz & 1 || rxsz > USB_BTABLE_SIZE) return 3; // wrong rx buffer size + if(rxsz & 1) return 3; // wrong rx buffer size uint16_t countrx = 0; if(rxsz < 64) countrx = rxsz / 2; else{ diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.h b/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.h index 86ecc50..c6e70b0 100644 --- a/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.h +++ b/F1:F103/USB_New_concept/USB_NEW_concept_HID/usb_lib.h @@ -172,6 +172,7 @@ typedef struct{ #define USB_CLASS_MASS_STORAGE 8 #define USB_CLASS_HUB 9 #define USB_CLASS_DATA 10 +#define USB_CLASS_MISC 0xef #define USB_CLASS_VENDOR_SPEC 0xff /* @@ -183,10 +184,12 @@ typedef struct{ #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_QUALIFIER 0x06 +#define USB_DT_IAD 0x0B #define USB_DT_HID 0x21 #define USB_DT_REPORT 0x22 #define USB_DT_PHYSICAL 0x23 +#define USB_DT_CS_INTERFACE 0x24 #define USB_DT_HUB 0x29 /* @@ -198,7 +201,8 @@ typedef struct{ #define USB_DT_HID_SIZE 9 #define USB_DT_ENDPOINT_SIZE 7 #define USB_DT_QUALIFIER_SIZE 10 - +#define USB_DT_CS_INTERFACE_SIZE 5 +#define USB_DT_IAD_SIZE 8 // bmRequestType & 0x80 == dev2host (1) or host2dev (0) diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_PL2303/someusb.bin b/F1:F103/USB_New_concept/USB_NEW_concept_PL2303/someusb.bin index 1f16cf8400d1a413b89c8c81422497582f8899c6..87f60a74a192873640259081b50ccc8248925eda 100755 GIT binary patch delta 847 zcmY+DS!feM7{|Za-6We-&1z^hwb;!DHD;l1&4Z|Dwozl63dI8y^Xsa*!P+OGh_E0NmO}#)oV5`O(ETUQQfV7Qv9vb7sIP%$-B^SR(WCI*#kKx|{=+Axt17ubeMCzt~x^y0X6WwfRIl z^V!5WKXS)sOLQZQP9q%A{Mv&=ERylVPO6*SNAVWyJtH3bArZNh!P z+ZjIISl)=6`P)M!4kYf@DRH?D;zk{WhO$+P*E~92U)rtW1ply-PO9|jfJp~;AX!B{ z*ieJdN(Yh>MfZzl@3Y#lSDydFzoWX;A@5Jg%W{<5_GFKnc@%$#M{eD~oRl>No_qSF z;9JZ}iwf2ebBs^r+AyckUurDOg+KPr{I%EHsTrF?TTAa$svX2YRz4bd_*ee+hJ8f- zt^qF2T#xPC)Ocv{KuI@l;p+lZ^w8kJ^``?ay~^v>k!$pT9Wuis=_aGdbTale}0n3>EL36nc+mb+Q`oD(|e5z?s^s5r8fq06Cvq^(TLAU z{2Yfk{^h$*bkdeTP00Ii*zzqNYabF!rxuj4qnfreP0oEQl^Xnshaxq#LVIjjT>emI z_;Na%muvS7aFbmrn}xKSq<@R_FPF8e23hx~W@%Yjv#6$KYL9kSjr(U69d7f@Zn{O(=dsGZY5RYG;&kDg=IDYqO_|O zf9UlE5uX`8oSqn#-Niv@p!gZ&{QJ`r!oDD^Z72@P0!<3`q!ICYRvJi<22v|dNM93K zK_un}JuxJJ=udsT)Qvw#IZA&5Vz36}SS*uS0KkI+@-Y$vi5bE|8rU8d*oY0-F2!<< zLKmY>+jMlItJLO4=ziCsoauCKuN~M&1fAe_B7kquzq0G`m_{`AF8K{@get;Lf)d@u GUH=8StWr_{ delta 864 zcmY*YUuaWT9R8hqZj zPjz+wGF5MhtRL6eaG(=k?GT1$OCC0JQ;ZI!(B__!2f9SAjqhXr?Pn)i+X%OUYr7l? z5XMUipscbX@-e==;{utGW=riN3L2OR?#B@9#UPI)_9bPPzH};mu4CFDMd|Nu)-u>k zqy~c$vC_<62M5Wx=1nPfdYfYD?ATPlrLnGg#T9xqkXV}rn@*S~me^rk!fdY`R# z3Q6Cjb+>_=o%=qFnu~!3;X_D145wPy0W3L9k z&O}t4RySZ2_+Y6Ps%5+KvpMIfv(_0CUj^q5GYa@w4EeR(gK7swS!+EO1{{rKr3V+sCdx#7Jf_}!|ffaID> z;RL^8w3i!gJheADNERh%G3;3Gf$0uxiB-$lio^BXT@#c+Vk~M7{UkR{1+p3~U*@g3 z8;+p%QQoEcF|U4wK{f34x%}DI6r%=b7-e5mr-(w_49G$<5Z{wrZ9+@fuFE;ET(nlq zxt1nq7Qej_%+1kqv4-a3q30+^vpdq55M;=(0ktEFq%2uDQZ1jX_~N?K9EJ0Y6rvd% ztJK9+%9^9P%u3brHx)$vj%)KpnhV5D=L@o=s68&i4D1Iz)8t;W{uvMaZ2@@4CsO^< z;0%&M4cqr_Op^u8oBW*?=eN4=Fla3EpJx;5&uEPnR[0]; // EP0R on input -> return this value after modifications - //DBG("EP0_Handler"); + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications + int rxflag = RX_FLAG(epstatus); + if(rxflag){ DBG("EP0_Handler"); } // check direction if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit) if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack @@ -166,13 +165,16 @@ static void EP0_Handler(){ ep0dbuflen = EP_Read(0, ep0databuf); } } - int rxflag = RX_FLAG(epstatus); if(rxflag){ uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType); switch(reqtype){ case REQ_TYPE_STANDARD: - DBG("REQ_TYPE_STANDARD"); - usb_standard_request(); + if(SETUP_FLAG(epstatus)){ + DBG("REQ_TYPE_STANDARD"); + usb_standard_request(); + }else{ + DBG("REQ_TYPE_STANDARD without SETUP_FLAG"); + } break; case REQ_TYPE_CLASS: DBG("REQ_TYPE_CLASS"); @@ -198,7 +200,7 @@ static void EP0_Handler(){ DBGs(uhex2str(USB_Addr)); } } - epstatus = KEEP_DTOG(USB->EPnR[0]); + //epstatus = KEEP_DTOG(USB->EPnR[0]); if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged // keep DTOGs, clear CTR_RX,TX, set RX VALID @@ -284,11 +286,11 @@ static uint16_t lastaddr = LASTADDR_DEFAULT; */ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ if(number >= STM32ENDPOINTS) return 4; // out of configured amount - if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large + if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; - if(rxsz & 1 || rxsz > USB_BTABLE_SIZE) return 3; // wrong rx buffer size + if(rxsz & 1) return 3; // wrong rx buffer size uint16_t countrx = 0; if(rxsz < 64) countrx = rxsz / 2; else{ diff --git a/F1:F103/USB_New_concept/USB_NEW_concept_PL2303/usb_lib.h b/F1:F103/USB_New_concept/USB_NEW_concept_PL2303/usb_lib.h index c1a180f..c6e70b0 100644 --- a/F1:F103/USB_New_concept/USB_NEW_concept_PL2303/usb_lib.h +++ b/F1:F103/USB_New_concept/USB_NEW_concept_PL2303/usb_lib.h @@ -184,6 +184,7 @@ typedef struct{ #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_QUALIFIER 0x06 +#define USB_DT_IAD 0x0B #define USB_DT_HID 0x21 #define USB_DT_REPORT 0x22 @@ -201,7 +202,7 @@ typedef struct{ #define USB_DT_ENDPOINT_SIZE 7 #define USB_DT_QUALIFIER_SIZE 10 #define USB_DT_CS_INTERFACE_SIZE 5 - +#define USB_DT_IAD_SIZE 8 // bmRequestType & 0x80 == dev2host (1) or host2dev (0)