all descriptors read - OK, interfaces work - bad (only first two works - why? need to debug)

This commit is contained in:
Edward Emelianov
2026-02-11 23:34:14 +03:00
parent 3a903d7d8c
commit e17058b2de
16 changed files with 565 additions and 259 deletions

View File

@@ -17,6 +17,7 @@
#include <string.h>
#include "hardware.h"
#include "ringbuffer.h"
#include "usb_descr.h"
#include "usb_dev.h"
@@ -37,170 +38,226 @@
#define CONTROL_RTS 0x02
// inbuf overflow when receiving
static volatile uint8_t bufovrfl = 0;
static volatile uint8_t bufovrfl[InterfacesAmount] = {0};
// receive buffer: hold data until chkin() call
static uint8_t volatile rcvbuf[USB_RXBUFSZ];
static uint8_t volatile rcvbuflen = 0;
static uint8_t volatile rcvbuf[InterfacesAmount][USB_RXBUFSZ];
static uint8_t volatile rcvbuflen[InterfacesAmount] = {0};
// line coding
usb_LineCoding WEAK lineCoding = {115200, 0, 0, 8};
#define DEFL {115200, 0, 0, 8}
usb_LineCoding lineCoding[InterfacesAmount] = {DEFL,DEFL,DEFL,DEFL,DEFL,DEFL,DEFL};
// CDC configured and ready to use
volatile uint8_t CDCready = 0;
volatile uint8_t CDCready[InterfacesAmount] = {0};
// ring buffers for incoming and outgoing data
static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ];
static volatile ringbuffer rbout = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0};
static volatile ringbuffer rbin = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0};
// last send data size
static volatile int lastdsz = 0;
static uint8_t obuf[InterfacesAmount][RBOUTSZ], ibuf[InterfacesAmount][RBINSZ];
#define OBUF(N) {.data = obuf[N], .length = RBOUTSZ, .head = 0, .tail = 0}
static volatile ringbuffer rbout[InterfacesAmount] = {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[InterfacesAmount] = {IBUF(0), IBUF(1), IBUF(2), IBUF(3), IBUF(4), IBUF(5), IBUF(6)};
// last send data size (<0 if USB transfer ready)
static volatile int lastdsz[InterfacesAmount] = {-1, -1, -1, -1, -1, -1, -1};
static void chkin(){
if(bufovrfl) return; // allow user to know that previous buffer was overflowed and cleared
if(!rcvbuflen) return;
int w = RB_write((ringbuffer*)&rbin, (uint8_t*)rcvbuf, rcvbuflen);
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){
return;
}
if(w != rcvbuflen) bufovrfl = 1;
rcvbuflen = 0;
uint16_t status = KEEP_DTOG(USB->EPnR[1]); // don't change DTOG
USB->EPnR[1] = (status & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // prepare to get next data portion
if(w != rcvbuflen[ifno]) bufovrfl[ifno] = 1;
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(){
static void send_next(uint8_t ifno){
uint8_t usbbuff[USB_TXBUFSZ];
int buflen = RB_read((ringbuffer*)&rbout, (uint8_t*)usbbuff, USB_TXBUFSZ);
if(buflen == 0){
if(lastdsz == 64) EP_Write(1, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
lastdsz = 0;
return;
}else if(buflen < 0){
lastdsz = 0;
int buflen = RB_read((ringbuffer*)&rbout[ifno], (uint8_t*)usbbuff, USB_TXBUFSZ);
if(!CDCready[ifno]){
lastdsz[ifno] = -1;
return;
}
EP_Write(1, (uint8_t*)usbbuff, buflen);
lastdsz = buflen;
if(buflen == 0){
if(lastdsz[ifno] == USB_TXBUFSZ){
EP_Write(1+ifno, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
lastdsz[ifno] = 0;
}else lastdsz[ifno] = -1; // OK. User can start sending data
return;
}else if(buflen < 0){
lastdsz[ifno] = -1;
return;
}
EP_Write(1+ifno, (uint8_t*)usbbuff, buflen);
lastdsz[ifno] = buflen;
}
// data IN/OUT handler
static void rxtx_handler(){
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
uint8_t ifno = (USB->ISTR & USB_ISTR_EPID) - 1;
if(ifno > InterfacesAmount-1){
return;
}
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1+ifno]);
if(RX_FLAG(epstatus)){ // receive data
if(rcvbuflen){
bufovrfl = 1; // lost last data
rcvbuflen = 0;
if(rcvbuflen[ifno]){
bufovrfl[ifno] = 1; // lost last data
rcvbuflen[ifno] = 0;
}
rcvbuflen = EP_Read(1, (uint8_t*)rcvbuf);
USB->EPnR[1] = epstatus & ~(USB_EPnR_CTR_RX | USB_EPnR_STAT_RX | USB_EPnR_STAT_TX); // keep RX in STALL state until read data
chkin(); // try to write current data into RXbuf if it's not busy
rcvbuflen[ifno] = EP_Read(1+ifno, (uint8_t*)rcvbuf[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
USB->EPnR[1] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX;
send_next();
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(usb_LineCoding *lc){
lineCoding = *lc;
void linecoding_handler(uint8_t ifno, usb_LineCoding *lc){
lineCoding[ifno] = *lc;
// TODO: set up interfaces
}
// clear IN/OUT buffers on connection
static void clearbufs(uint8_t ifno){
uint32_t T0 = Tms;
while(Tms - T0 < 10){ // wait no more than 10ms
if(1 == RB_clearbuf((ringbuffer*)&rbin[ifno])) break;
}
T0 = Tms;
while(Tms - T0 < 10){
if(1 == RB_clearbuf((ringbuffer*)&rbout[ifno])) break;
}
}
// SET_CONTROL_LINE_STATE
void WEAK clstate_handler(uint16_t val){
CDCready = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected
void clstate_handler(uint8_t ifno, uint16_t val){
CDCready[ifno] = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected
lastdsz[ifno] = -1;
if(val) clearbufs(ifno);
}
// SEND_BREAK
void WEAK break_handler(){
CDCready = 0;
// SEND_BREAK - disconnect interface and clear its buffers
// this is a fake handler as classic CDC ACM never receives this
void break_handler(uint8_t ifno){
CDCready[ifno] = 0;
}
// USB is configured: setup endpoints
void set_configuration(){
EP_Init(1, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_handler); // IN1 and OUT1
for(int i = 0; i < InterfacesAmount; ++i){
IWDG->KR = IWDG_REFRESH;
int r = EP_Init(1+i, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_handler);
if(r){
// OOPS, can't init EP. What to do? Cry?
break;
}
}
}
// PL2303 CLASS request
// USB CDC 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 > InterfacesAmount-1){ // wrong interface number
EP_WriteIRQ(0, NULL, 0);
return;
}
switch(recipient){
case REQ_RECIPIENT_INTERFACE:
switch(req->bRequest){
case SET_LINE_CODING:
if(!data || !datalen) break; // wait for data
if(datalen == sizeof(usb_LineCoding))
linecoding_handler((usb_LineCoding*)data);
break;
case GET_LINE_CODING:
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break;
case SET_CONTROL_LINE_STATE:
clstate_handler(req->wValue);
break;
case SEND_BREAK:
break_handler();
break;
default:
break;
}
break;
case REQ_RECIPIENT_INTERFACE:
switch(req->bRequest){
case 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:
EP_WriteIRQ(0, (uint8_t*)&lineCoding[ifno], sizeof(lineCoding));
break;
case SET_CONTROL_LINE_STATE:
clstate_handler(ifno, req->wValue);
break;
case SEND_BREAK:
break_handler(ifno);
break;
default:
if(dev2host) EP_WriteIRQ(0, NULL, 0);
// WTF?
break;
}
break;
default:
// WTF?
if(dev2host) EP_WriteIRQ(0, NULL, 0);
}
if(!dev2host) EP_WriteIRQ(0, NULL, 0);
}
// blocking send full content of ring buffer
int USB_sendall(){
while(lastdsz > 0){
if(!CDCready) return FALSE;
int USB_sendall(uint8_t ifno){
uint32_t T0 = Tms;
while(lastdsz[ifno] > 0){
if(Tms - T0 > DISCONN_TMOUT){
break_handler(ifno);
return FALSE;
}
if(!CDCready[ifno]) return FALSE;
IWDG->KR = IWDG_REFRESH;
}
return TRUE;
}
// put `buf` into queue to send
int USB_send(const uint8_t *buf, int len){
if(!buf || !CDCready || !len) return FALSE;
int USB_send(uint8_t ifno, const uint8_t *buf, int len){
if(!buf || !CDCready[ifno] || !len) return FALSE;
uint32_t T0 = Tms;
while(len){
IWDG->KR = IWDG_REFRESH;
int l = RB_datalen((ringbuffer*)&rbout);
if(l < 0) continue;
int portion = rbout.length - 1 - l;
if(portion < 1){
if(lastdsz == 0) send_next();
continue;
if(Tms - T0 > DISCONN_TMOUT){
break_handler(ifno);
return FALSE;
}
if(portion > len) portion = len;
int a = RB_write((ringbuffer*)&rbout, buf, portion);
if(!CDCready[ifno]) return FALSE;
IWDG->KR = IWDG_REFRESH;
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 == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
}else if(a == 0){ // overfull
if(lastdsz[ifno] < 0) send_next(ifno);
}
}
if(buf[len-1] == '\n' && lastdsz[ifno] < 0) send_next(ifno);
return TRUE;
}
int USB_putbyte(uint8_t byte){
if(!CDCready) return FALSE;
int USB_putbyte(uint8_t ifno, uint8_t byte){
if(!CDCready[ifno]) return FALSE;
int l = 0;
while((l = RB_write((ringbuffer*)&rbout, &byte, 1)) != 1){
if(l < 0) continue;
uint32_t T0 = Tms;
while((l = RB_write((ringbuffer*)&rbout[ifno], &byte, 1)) != 1){
if(Tms - T0 > DISCONN_TMOUT){
break_handler(ifno);
return FALSE;
}
if(!CDCready[ifno]) return FALSE;
IWDG->KR = IWDG_REFRESH;
if(l == 0){ // overfull
if(lastdsz[ifno] < 0) send_next(ifno);
continue;
}
}
if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
// send line if got EOL
if(byte == '\n' && lastdsz[ifno] < 0) send_next(ifno);
return TRUE;
}
int USB_sendstr(const char *string){
if(!string || !CDCready) return FALSE;
int len = 0;
const char *b = string;
while(*b++) ++len;
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((const uint8_t*)string, len);
return USB_send(ifno, (const uint8_t*)string, len);
}
/**
@@ -209,14 +266,14 @@ int USB_sendstr(const char *string){
* @param len - length of `buf`
* @return amount of received bytes (negative, if overfull happened)
*/
int USB_receive(uint8_t *buf, int len){
chkin();
if(bufovrfl){
while(1 != RB_clearbuf((ringbuffer*)&rbin));
bufovrfl = 0;
int USB_receive(uint8_t ifno, uint8_t *buf, int len){
chkin(ifno); // rxtx_handler could leave last message unwritten if buffer was busy
if(bufovrfl[ifno]){
while(1 != RB_clearbuf((ringbuffer*)&rbin[ifno])); // run watchdog in case of problems
bufovrfl[ifno] = 0;
return -1;
}
int sz = RB_read((ringbuffer*)&rbin, buf, len);
int sz = RB_read((ringbuffer*)&rbin[ifno], buf, len);
if(sz < 0) return 0; // buffer in writting state
return sz;
}
@@ -227,17 +284,17 @@ int USB_receive(uint8_t *buf, int len){
* @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(char *buf, int len){
chkin();
if(bufovrfl){
while(1 != RB_clearbuf((ringbuffer*)&rbin));
bufovrfl = 0;
int USB_receivestr(uint8_t ifno, char *buf, int len){
chkin(ifno); // rxtx_handler could leave last message unwritten if buffer was busy
if(bufovrfl[ifno]){
while(1 != RB_clearbuf((ringbuffer*)&rbin[ifno]));
bufovrfl[ifno] = 0;
return -1;
}
int l = RB_readto((ringbuffer*)&rbin, '\n', (uint8_t*)buf, len);
int l = RB_readto((ringbuffer*)&rbin[ifno], '\n', (uint8_t*)buf, len);
if(l < 1){
if(rbin.length == RB_datalen((ringbuffer*)&rbin)){ // buffer is full but no '\n' found
while(1 != RB_clearbuf((ringbuffer*)&rbin));
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;
@@ -246,4 +303,3 @@ int USB_receivestr(char *buf, int len){
buf[l-1] = 0; // replace '\n' with strend
return l;
}