diff --git a/F3:F303/Seven_CDCs/main.c b/F3:F303/Seven_CDCs/main.c index b09f5d4..3871534 100644 --- a/F3:F303/Seven_CDCs/main.c +++ b/F3:F303/Seven_CDCs/main.c @@ -53,6 +53,7 @@ int main(void){ if(Tms - ctr > 499){ ctr = Tms; pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0 + USB_sendstr(CMD_IDX, "1"); //DBGmesg(u2str(Tms)); //DBGnl(); } diff --git a/F3:F303/Seven_CDCs/sevenCDCs.bin b/F3:F303/Seven_CDCs/sevenCDCs.bin index a66c56d..9329eaa 100755 Binary files a/F3:F303/Seven_CDCs/sevenCDCs.bin 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 index f451ae4..a2aab10 100644 --- a/F3:F303/Seven_CDCs/usart.c +++ b/F3:F303/Seven_CDCs/usart.c @@ -87,7 +87,7 @@ void usart_config(uint8_t ifNo, usb_LineCoding *lc){ void usart1_exti25_isr(){ if(USART1->ISR & USART_ISR_RXNE){ // RX not emty - receive next char - DBG("got\n"); + //DBG("got\n"); // read RDR clears flag uint8_t rb = USART1->RDR; USB_putbyte(USART1_IDX, rb); @@ -96,7 +96,7 @@ void usart1_exti25_isr(){ void usart2_exti26_isr(){ if(USART2->ISR & USART_ISR_RXNE){ - DBG("got\n"); + //DBG("got\n"); uint8_t rb = USART2->RDR; USB_putbyte(USART2_IDX, rb); } @@ -104,7 +104,7 @@ void usart2_exti26_isr(){ void usart3_exti28_isr(){ if(USART3->ISR & USART_ISR_RXNE){ - DBG("got\n"); + //DBG("got\n"); uint8_t rb = USART3->RDR; USB_putbyte(USART3_IDX, rb); } diff --git a/F3:F303/Seven_CDCs/usb.c b/F3:F303/Seven_CDCs/usb.c index de17081..9129679 100644 --- a/F3:F303/Seven_CDCs/usb.c +++ b/F3:F303/Seven_CDCs/usb.c @@ -22,24 +22,24 @@ #include "usb.h" #include "usb_lib.h" -static volatile uint8_t usbbuff[USB_TRBUFSZ]; // temporary buffer for sending data +static volatile uint8_t usbbuff[USB_TXBUFSZ]; // temporary buffer for sending data // ring buffers for incoming and outgoing data -static uint8_t obuf[WORK_EPs][RBOUTSZ], ibuf[WORK_EPs][RBINSZ]; +static uint8_t obuf[MAX_IDX][RBOUTSZ], ibuf[MAX_IDX][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)}; +volatile ringbuffer rbout[MAX_IDX] = {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} -volatile ringbuffer rbin[WORK_EPs] = {IBUF(0), IBUF(1), IBUF(2), IBUF(3), IBUF(4), IBUF(5), IBUF(6)}; +volatile ringbuffer rbin[MAX_IDX] = {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,1}; -volatile uint8_t bufovrfl[WORK_EPs] = {0}; +volatile uint8_t bufisempty[MAX_IDX] = {1,1,1,1,1,1,1}; +volatile uint8_t bufovrfl[MAX_IDX] = {0}; // here and later: ifNo is index of buffers, i.e. ifNo = epno-1 !!! void send_next(int ifNo){ if(bufisempty[ifNo]) return; - static uint8_t lastdsz[WORK_EPs] = {0}; - int buflen = RB_read((ringbuffer*)&rbout[ifNo], (uint8_t*)usbbuff, USB_TRBUFSZ); + static uint8_t lastdsz[MAX_EPNO] = {0}; + int buflen = RB_read((ringbuffer*)&rbout[ifNo], (uint8_t*)usbbuff, USB_TXBUFSZ); if(!buflen){ - if(lastdsz[ifNo] == USB_TRBUFSZ) EP_Write(ifNo+1, NULL, 0); // send ZLP after 64 bits packet when nothing more to send + if(lastdsz[ifNo] == USB_TXBUFSZ) EP_Write(ifNo+1, NULL, 0); // send ZLP after 64 bits packet when nothing more to send lastdsz[ifNo] = 0; bufisempty[ifNo] = 1; return; @@ -104,13 +104,12 @@ int USB_sendstr(int ifNo, const char *string){ * @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 -1; } + int sz = RB_read((ringbuffer*)&rbin[ifNo], buf, len); return sz; } @@ -121,14 +120,13 @@ int USB_receive(int ifNo, uint8_t *buf, int len){ * @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; + 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) return 0; + buf[--l] = 0; // replace '\n' with strend return l; } diff --git a/F3:F303/Seven_CDCs/usb.h b/F3:F303/Seven_CDCs/usb.h index 01c0d28..f79fe1b 100644 --- a/F3:F303/Seven_CDCs/usb.h +++ b/F3:F303/Seven_CDCs/usb.h @@ -31,8 +31,6 @@ #define STR_HELPER(s) #s #define STR(s) STR_HELPER(s) -// total amount of working EPs -#define WORK_EPs 7 // functional EPs #define CMD_EPNO 1 #define DBG_EPNO 2 @@ -41,6 +39,7 @@ #define USART3_EPNO 5 #define USART4_EPNO 6 #define CAN_EPNO 7 +// total amount of working EPs #define MAX_EPNO 7 #define USARTMAX_EPNO USART3_EPNO @@ -52,7 +51,7 @@ #define USART2_IDX (USART2_EPNO-1) #define USART3_IDX (USART3_EPNO-1) #define USART4_IDX (USART4_EPNO-1) -#define MAX_IDX 7 +#define MAX_IDX (MAX_EPNO) extern volatile ringbuffer rbout[], rbin[]; extern volatile uint8_t bufisempty[], bufovrfl[]; diff --git a/F3:F303/Seven_CDCs/usb_lib.c b/F3:F303/Seven_CDCs/usb_lib.c index 5bedf72..3b8067d 100644 --- a/F3:F303/Seven_CDCs/usb_lib.c +++ b/F3:F303/Seven_CDCs/usb_lib.c @@ -153,16 +153,16 @@ static const uint8_t USB_ConfigDescriptor[] = { 0x05, /* bDescriptorType: Endpoint */ 0x81, /* bEndpointAddress IN1 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*Endpoint OUT Descriptor*/ 0x07, /* bLength: Endpoint Descriptor size */ 0x05, /* bDescriptorType: Endpoint */ 0x01, /* bEndpointAddress OUT1 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_RXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*---------------------------------------------------------------------------*/ @@ -234,16 +234,16 @@ static const uint8_t USB_ConfigDescriptor[] = { 0x05, /* bDescriptorType: Endpoint */ /**/0x82, /* bEndpointAddress IN2 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*Endpoint OUT Descriptor */ 0x07, /* bLength: Endpoint Descriptor size */ 0x05, /* bDescriptorType: Endpoint */ /**/0x02, /* bEndpointAddress OUT2 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_RXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*---------------------------------------------------------------------------*/ @@ -315,16 +315,16 @@ static const uint8_t USB_ConfigDescriptor[] = { 0x05, /* bDescriptorType: Endpoint */ /**/0x83, /* bEndpointAddress IN3 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*Endpoint OUT Descriptor */ 0x07, /* bLength: Endpoint Descriptor size */ 0x05, /* bDescriptorType: Endpoint */ /**/0x03, /* bEndpointAddress OUT3 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_RXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*---------------------------------------------------------------------------*/ @@ -396,16 +396,16 @@ static const uint8_t USB_ConfigDescriptor[] = { 0x05, /* bDescriptorType: Endpoint */ /**/0x84, /* bEndpointAddress IN4 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*Endpoint OUT Descriptor */ 0x07, /* bLength: Endpoint Descriptor size */ 0x05, /* bDescriptorType: Endpoint */ /**/0x04, /* bEndpointAddress OUT4 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_RXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*---------------------------------------------------------------------------*/ @@ -477,16 +477,16 @@ static const uint8_t USB_ConfigDescriptor[] = { 0x05, /* bDescriptorType: Endpoint */ /**/0x85, /* bEndpointAddress IN5 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*Endpoint OUT Descriptor */ 0x07, /* bLength: Endpoint Descriptor size */ 0x05, /* bDescriptorType: Endpoint */ /**/0x05, /* bEndpointAddress OUT5 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_RXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*---------------------------------------------------------------------------*/ @@ -558,16 +558,16 @@ static const uint8_t USB_ConfigDescriptor[] = { 0x05, /* bDescriptorType: Endpoint */ /**/0x86, /* bEndpointAddress IN6 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*Endpoint OUT Descriptor */ 0x07, /* bLength: Endpoint Descriptor size */ 0x05, /* bDescriptorType: Endpoint */ /**/0x06, /* bEndpointAddress OUT6 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_RXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*---------------------------------------------------------------------------*/ @@ -639,16 +639,16 @@ static const uint8_t USB_ConfigDescriptor[] = { 0x05, /* bDescriptorType: Endpoint */ /**/0x87, /* bEndpointAddress IN7 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_TXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*Endpoint OUT Descriptor */ 0x07, /* bLength: Endpoint Descriptor size */ 0x05, /* bDescriptorType: Endpoint */ /**/0x07, /* bEndpointAddress OUT7 */ 0x02, /* bmAttributes: Bulk */ - (USB_TRBUFSZ & 0xff), /* wMaxPacketSize: 64 */ - (USB_TRBUFSZ >> 8), + (USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */ + (USB_RXBUFSZ >> 8), 0x00, /* bInterval: ignore for Bulk transfer */ /*---------------------------------------------------------------------------*/ }; @@ -749,18 +749,18 @@ static inline void std_d2h_req(){ // Rx and Tx handlers for EP1..EP7 static void rxtx_Handler(uint8_t epno){ - uint8_t buf[USB_TRBUFSZ]; + uint8_t buf[USB_RXBUFSZ]; int idx = epno - 1; uint16_t epstatus = KEEP_DTOG(USB->EPnR[epno]); - //uint16_t epstatus = USB->EPnR[epno]; if(RX_FLAG(epstatus)){ 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; - //USB->EPnR[epno] = (KEEP_DTOG(epstatus) & ~USB_EPnR_CTR_RX) ^ USB_EPnR_STAT_RX; uint8_t sz = EP_Read(epno, (uint8_t*)buf); + /* DBG("epno"); DBGmesg(u2str(epno)); DBGmesg(" ("); DBGmesg(u2str(sz)); DBGmesg(") > "); hexdump(DBG_IDX, buf, sz); DBGnl(); + */ if(sz){ switch(epno){ case USART1_EPNO: @@ -779,14 +779,10 @@ static void rxtx_Handler(uint8_t epno){ if(RB_write((ringbuffer*)&rbin[idx], buf, sz) != sz) bufovrfl[idx] = 1; } } - /*if(epno != DBG_EPNO){ - MSG(); - }*/ // set ACK Rx USB->EPnR[epno] = (KEEP_DTOG(USB->EPnR[epno]) & ~(USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX; }else{ USB->EPnR[epno] = (epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX; // clear TX ctr - //USB->EPnR[epno] = (KEEP_DTOG_STAT(epstatus) & ~(USB_EPnR_CTR_TX)); // clear TX ctr send_next(idx); } } @@ -800,8 +796,8 @@ static inline void std_h2d_req(){ 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); + for(uint8_t i = 1; i <= MAX_EPNO; ++i){ + EP_Init(i, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_Handler); } break; default: diff --git a/F3:F303/Seven_CDCs/usbhw.h b/F3:F303/Seven_CDCs/usbhw.h index 51457ae..cac8ecf 100644 --- a/F3:F303/Seven_CDCs/usbhw.h +++ b/F3:F303/Seven_CDCs/usbhw.h @@ -30,9 +30,10 @@ //#define USB_EP0_BASEADDR 64 // for USB FS EP0 buffers are from 8 to 64 bytes long // For STM32F303CBT6 ACCESSZ=2, so real available buffers size for all EPs = only 192 bytes (128 - EP0) -#define USB_EP0_BUFSZ 64 -// USB transmit/receive buffer size -#define USB_TRBUFSZ 4 +#define USB_EP0_BUFSZ 16 +// USB transmit/receive buffer size; RXBUFSZ>=8 !!! +#define USB_RXBUFSZ 14 +#define USB_TXBUFSZ 8 // EP1 - interrupt - buffer size #define USB_EP1BUFSZ 8 diff --git a/F3:F303/Seven_CDCs/version.inc b/F3:F303/Seven_CDCs/version.inc index 75bf78b..c982bd1 100644 --- a/F3:F303/Seven_CDCs/version.inc +++ b/F3:F303/Seven_CDCs/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "102" +#define BUILD_NUMBER "122" #define BUILD_DATE "2023-04-29"