seems like all works @ F103

This commit is contained in:
Edward Emelianov
2025-01-08 18:00:07 +03:00
parent fd05ff73e6
commit 207ab57e99
33 changed files with 2244 additions and 41 deletions

View File

@@ -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{

View File

@@ -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)