mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 11:54:30 +03:00
change USB-CAN according to new USB doctrine: iInterface for each device
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the usbcanrb project.
|
||||
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* 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
|
||||
@@ -17,19 +17,20 @@
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
#include "usbhw.h"
|
||||
|
||||
ep_t endpoints[STM32ENDPOINTS];
|
||||
|
||||
usb_dev_t USB_Dev;
|
||||
static uint16_t USB_Addr = 0;
|
||||
static usb_LineCoding lineCoding = {115200, 0, 0, 8};
|
||||
config_pack_t setup_packet;
|
||||
uint8_t ep0databuf[EP0DATABUF_SIZE];
|
||||
//uint8_t ep0dbuflen = 0;
|
||||
uint8_t ep0databuf[EP0DATABUF_SIZE], setupdatabuf[EP0DATABUF_SIZE];
|
||||
config_pack_t *setup_packet = (config_pack_t*) setupdatabuf;
|
||||
|
||||
usb_LineCoding getLineCoding(){return lineCoding;}
|
||||
|
||||
uint8_t usbON = 0; // device disconnected from terminal
|
||||
volatile uint8_t usbON = 0; // device disconnected from terminal
|
||||
|
||||
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
|
||||
#define bcdUSB_L 0x10
|
||||
@@ -54,9 +55,9 @@ static const uint8_t USB_DeviceDescriptor[] = {
|
||||
0x23, // idProduct_H
|
||||
0x00, // bcdDevice_Ver_L
|
||||
0x03, // bcdDevice_Ver_H
|
||||
0x01, // iManufacturer
|
||||
0x02, // iProduct
|
||||
0x00, // iSerialNumber
|
||||
iMANUFACTURER_DESCR, // iManufacturer
|
||||
iPRODUCT_DESCR, // iProduct
|
||||
iSERIAL_DESCR, // iSerialNumber
|
||||
bNumConfigurations // bNumConfigurations
|
||||
};
|
||||
|
||||
@@ -96,7 +97,7 @@ static const uint8_t USB_ConfigDescriptor[] = {
|
||||
0xff, /* bInterfaceClass */
|
||||
0x00, /* bInterfaceSubClass */
|
||||
0x00, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface: */
|
||||
iINTERFACE_DESCR, /* iInterface: */
|
||||
///////////////////////////////////////////////////
|
||||
/*Endpoint 1 Descriptor*/
|
||||
0x07, /* bLength: Endpoint Descriptor size */
|
||||
@@ -126,11 +127,19 @@ static const uint8_t USB_ConfigDescriptor[] = {
|
||||
0x00, /* bInterval: ignore for Bulk transfer */
|
||||
};
|
||||
|
||||
_USB_LANG_ID_(USB_StringLangDescriptor, LANG_US);
|
||||
// these descriptors are not used in PL2303 emulator!
|
||||
_USB_STRING_(USB_StringSerialDescriptor, u"0");
|
||||
_USB_STRING_(USB_StringManufacturingDescriptor, u"Prolific Technology Inc.");
|
||||
_USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller");
|
||||
_USB_LANG_ID_(LD, LANG_US);
|
||||
_USB_STRING_(SD, u"0.0.1");
|
||||
_USB_STRING_(MD, u"Prolific Technology Inc.");
|
||||
_USB_STRING_(PD, u"USB-Serial Controller");
|
||||
_USB_STRING_(ID, u"can-usb");
|
||||
static void const *StringDescriptor[iDESCR_AMOUNT] = {
|
||||
[iLANGUAGE_DESCR] = &LD,
|
||||
[iMANUFACTURER_DESCR] = &MD,
|
||||
[iPRODUCT_DESCR] = &PD,
|
||||
[iSERIAL_DESCR] = &SD,
|
||||
[iINTERFACE_DESCR] = &ID
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* default handlers
|
||||
@@ -149,8 +158,8 @@ void WEAK break_handler(){
|
||||
|
||||
// handler of vendor requests
|
||||
void WEAK vendor_handler(config_pack_t *packet){
|
||||
uint16_t c;
|
||||
if(packet->bmRequestType & 0x80){ // read
|
||||
uint8_t c;
|
||||
switch(packet->wValue){
|
||||
case 0x8484:
|
||||
c = 2;
|
||||
@@ -164,14 +173,15 @@ void WEAK vendor_handler(config_pack_t *packet){
|
||||
default:
|
||||
c = 0;
|
||||
}
|
||||
EP_WriteIRQ(0, &c, 1);
|
||||
EP_WriteIRQ(0, (uint8_t*)&c, 1);
|
||||
}else{ // write ZLP
|
||||
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||
c = 0;
|
||||
EP_WriteIRQ(0, (uint8_t *)&c, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void wr0(const uint8_t *buf, uint16_t size){
|
||||
if(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request
|
||||
if(setup_packet->wLength < size) size = setup_packet->wLength; // shortened request
|
||||
if(size < endpoints[0].txbufsz){
|
||||
EP_WriteIRQ(0, buf, size);
|
||||
return;
|
||||
@@ -199,24 +209,18 @@ static void wr0(const uint8_t *buf, uint16_t size){
|
||||
}
|
||||
|
||||
static inline void get_descriptor(){
|
||||
switch(setup_packet.wValue){
|
||||
uint8_t descrtype = setup_packet->wValue >> 8,
|
||||
descridx = setup_packet->wValue & 0xff;
|
||||
switch(descrtype){
|
||||
case DEVICE_DESCRIPTOR:
|
||||
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
|
||||
break;
|
||||
case CONFIGURATION_DESCRIPTOR:
|
||||
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
|
||||
break;
|
||||
case STRING_LANG_DESCRIPTOR:
|
||||
wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
|
||||
break;
|
||||
case STRING_MAN_DESCRIPTOR:
|
||||
wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
|
||||
break;
|
||||
case STRING_PROD_DESCRIPTOR:
|
||||
wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
|
||||
break;
|
||||
case STRING_SN_DESCRIPTOR:
|
||||
wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
|
||||
case STRING_DESCRIPTOR:
|
||||
if(descridx < iDESCR_AMOUNT) wr0((const uint8_t *)StringDescriptor[descridx], *((uint8_t*)StringDescriptor[descridx]));
|
||||
else EP_WriteIRQ(0, (uint8_t*)0, 0);
|
||||
break;
|
||||
case DEVICE_QUALIFIER_DESCRIPTOR:
|
||||
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
|
||||
@@ -226,10 +230,10 @@ static inline void get_descriptor(){
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
|
||||
static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
|
||||
static inline void std_d2h_req(){
|
||||
uint16_t status = 0; // bus powered
|
||||
switch(setup_packet.bRequest){
|
||||
switch(setup_packet->bRequest){
|
||||
case GET_DESCRIPTOR:
|
||||
get_descriptor();
|
||||
break;
|
||||
@@ -237,23 +241,54 @@ static inline void std_d2h_req(){
|
||||
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered
|
||||
break;
|
||||
case GET_CONFIGURATION:
|
||||
EP_WriteIRQ(0, &configuration, 1);
|
||||
EP_WriteIRQ(0, (uint8_t*)&configuration, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// interrupt IN handler (never used?)
|
||||
static void EP1_Handler(){
|
||||
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
|
||||
if(RX_FLAG(epstatus)) epstatus = (epstatus & ~USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_RX; // set valid RX
|
||||
else epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX);
|
||||
// clear CTR
|
||||
epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX));
|
||||
USB->EPnR[1] = epstatus;
|
||||
}
|
||||
|
||||
// data IN/OUT handlers
|
||||
static void transmit_Handler(){ // EP3IN
|
||||
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[3]);
|
||||
// clear CTR keep DTOGs & STATs
|
||||
USB->EPnR[3] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr
|
||||
send_next();
|
||||
}
|
||||
|
||||
static void receive_Handler(){ // EP2OUT
|
||||
uint8_t buf[USB_RXBUFSZ];
|
||||
uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]);
|
||||
uint8_t sz = EP_Read(2, (uint8_t*)buf);
|
||||
if(sz){
|
||||
if(RB_write((ringbuffer*)&rbin, buf, sz) != sz) bufovrfl = 1;
|
||||
}
|
||||
// keep stat_tx & set ACK rx, clear RX ctr
|
||||
USB->EPnR[2] = (epstatus & ~USB_EPnR_CTR_RX) ^ USB_EPnR_STAT_RX;
|
||||
}
|
||||
|
||||
static inline void std_h2d_req(){
|
||||
switch(setup_packet.bRequest){
|
||||
switch(setup_packet->bRequest){
|
||||
case SET_ADDRESS:
|
||||
// new address will be assigned later - after acknowlegement or request to host
|
||||
USB_Dev.USB_Addr = setup_packet.wValue;
|
||||
USB_Addr = setup_packet->wValue;
|
||||
break;
|
||||
case SET_CONFIGURATION:
|
||||
// Now device configured
|
||||
USB_Dev.USB_Status = USB_STATE_CONFIGURED;
|
||||
configuration = setup_packet.wValue;
|
||||
configuration = setup_packet->wValue;
|
||||
EP_Init(1, EP_TYPE_INTERRUPT, USB_EP1BUFSZ, 0, EP1_Handler); // IN1 - transmit
|
||||
EP_Init(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, receive_Handler); // OUT2 - receive data
|
||||
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, transmit_Handler); // IN3 - transmit data
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -271,8 +306,8 @@ bmRequestType: 76543210
|
||||
*/
|
||||
void EP0_Handler(){
|
||||
uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications
|
||||
uint8_t reqtype = setup_packet.bmRequestType & 0x7f;
|
||||
uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0;
|
||||
uint8_t reqtype = setup_packet->bmRequestType & 0x7f;
|
||||
uint8_t dev2host = (setup_packet->bmRequestType & 0x80) ? 1 : 0;
|
||||
int rxflag = RX_FLAG(epstatus);
|
||||
if(rxflag && SETUP_FLAG(epstatus)){
|
||||
switch(reqtype){
|
||||
@@ -285,15 +320,15 @@ void EP0_Handler(){
|
||||
}
|
||||
break;
|
||||
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request
|
||||
if(setup_packet.bRequest == CLEAR_FEATURE){
|
||||
if(setup_packet->bRequest == CLEAR_FEATURE){
|
||||
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||
}
|
||||
break;
|
||||
case VENDOR_REQUEST_TYPE:
|
||||
vendor_handler(&setup_packet);
|
||||
vendor_handler(setup_packet);
|
||||
break;
|
||||
case CONTROL_REQUEST_TYPE:
|
||||
switch(setup_packet.bRequest){
|
||||
switch(setup_packet->bRequest){
|
||||
case GET_LINE_CODING:
|
||||
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
|
||||
break;
|
||||
@@ -301,7 +336,7 @@ void EP0_Handler(){
|
||||
break;
|
||||
case SET_CONTROL_LINE_STATE:
|
||||
usbON = 1;
|
||||
clstate_handler(setup_packet.wValue);
|
||||
clstate_handler(setup_packet->wValue);
|
||||
break;
|
||||
case SEND_BREAK:
|
||||
usbON = 0;
|
||||
@@ -310,23 +345,22 @@ void EP0_Handler(){
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
|
||||
if(setup_packet->bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
|
||||
break;
|
||||
default:
|
||||
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||
}
|
||||
}else if(rxflag){ // got data over EP0 or host acknowlegement
|
||||
if(endpoints[0].rx_cnt){
|
||||
if(setup_packet.bRequest == SET_LINE_CODING){
|
||||
if(setup_packet->bRequest == SET_LINE_CODING){
|
||||
linecoding_handler((usb_LineCoding*)ep0databuf);
|
||||
}
|
||||
}
|
||||
} else if(TX_FLAG(epstatus)){ // package transmitted
|
||||
// now we can change address after enumeration
|
||||
if ((USB->DADDR & USB_DADDR_ADD) != USB_Dev.USB_Addr){
|
||||
USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr;
|
||||
// change state to ADRESSED
|
||||
USB_Dev.USB_Status = USB_STATE_ADDRESSED;
|
||||
if ((USB->DADDR & USB_DADDR_ADD) != USB_Addr){
|
||||
USB->DADDR = USB_DADDR_EF | USB_Addr;
|
||||
usbON = 0;
|
||||
}
|
||||
}
|
||||
epstatus = KEEP_DTOG(USB->EPnR[0]);
|
||||
@@ -343,13 +377,24 @@ void EP0_Handler(){
|
||||
* @param size - its size
|
||||
*/
|
||||
void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){
|
||||
if(size > USB_TXBUFSZ) size = USB_TXBUFSZ;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -372,10 +417,22 @@ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
|
||||
* @return amount of data read
|
||||
*/
|
||||
int EP_Read(uint8_t number, uint8_t *buf){
|
||||
int n = endpoints[number].rx_cnt;
|
||||
if(n){
|
||||
for(int i = 0; i < n; ++i)
|
||||
buf[i] = endpoints[number].rx_buf[i];
|
||||
}
|
||||
return n;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user