Browse Source

USB serial use different packet sizes at 12 vs 480

teensy4-core
PaulStoffregen 5 years ago
parent
commit
17d02bcaf0
3 changed files with 28 additions and 16 deletions
  1. +5
    -5
      teensy4/usb_desc.c
  2. +4
    -2
      teensy4/usb_desc.h
  3. +19
    -9
      teensy4/usb_serial.c

+ 5
- 5
teensy4/usb_desc.c View File

5, // bDescriptorType 5, // bDescriptorType
CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
0x03, // bmAttributes (0x03=intr) 0x03, // bmAttributes (0x03=intr)
CDC_ACM_SIZE, 0, // wMaxPacketSize
LSB(CDC_ACM_SIZE),MSB(CDC_ACM_SIZE), // wMaxPacketSize
16, // bInterval 16, // bInterval
// interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
9, // bLength 9, // bLength
5, // bDescriptorType 5, // bDescriptorType
CDC_RX_ENDPOINT, // bEndpointAddress CDC_RX_ENDPOINT, // bEndpointAddress
0x02, // bmAttributes (0x02=bulk) 0x02, // bmAttributes (0x02=bulk)
CDC_RX_SIZE, 0, // wMaxPacketSize
LSB(CDC_RX_SIZE_480),MSB(CDC_RX_SIZE_480),// wMaxPacketSize
0, // bInterval 0, // bInterval
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
7, // bLength 7, // bLength
5, // bDescriptorType 5, // bDescriptorType
CDC_TX_ENDPOINT | 0x80, // bEndpointAddress CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
0x02, // bmAttributes (0x02=bulk) 0x02, // bmAttributes (0x02=bulk)
CDC_TX_SIZE, 0, // wMaxPacketSize
LSB(CDC_TX_SIZE_480),MSB(CDC_TX_SIZE_480),// wMaxPacketSize
0, // bInterval 0, // bInterval
#endif // CDC_DATA_INTERFACE #endif // CDC_DATA_INTERFACE


5, // bDescriptorType 5, // bDescriptorType
CDC_RX_ENDPOINT, // bEndpointAddress CDC_RX_ENDPOINT, // bEndpointAddress
0x02, // bmAttributes (0x02=bulk) 0x02, // bmAttributes (0x02=bulk)
CDC_RX_SIZE, 0, // wMaxPacketSize
LSB(CDC_RX_SIZE_12),MSB(CDC_RX_SIZE_12),// wMaxPacketSize
0, // bInterval 0, // bInterval
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
7, // bLength 7, // bLength
5, // bDescriptorType 5, // bDescriptorType
CDC_TX_ENDPOINT | 0x80, // bEndpointAddress CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
0x02, // bmAttributes (0x02=bulk) 0x02, // bmAttributes (0x02=bulk)
CDC_TX_SIZE, 0, // wMaxPacketSize
LSB(CDC_TX_SIZE_12),MSB(CDC_TX_SIZE_12),// wMaxPacketSize
0, // bInterval 0, // bInterval
#endif // CDC_DATA_INTERFACE #endif // CDC_DATA_INTERFACE



+ 4
- 2
teensy4/usb_desc.h View File

#define CDC_RX_ENDPOINT 3 #define CDC_RX_ENDPOINT 3
#define CDC_TX_ENDPOINT 4 #define CDC_TX_ENDPOINT 4
#define CDC_ACM_SIZE 16 #define CDC_ACM_SIZE 16
#define CDC_RX_SIZE 64
#define CDC_TX_SIZE 64
#define CDC_RX_SIZE_480 512
#define CDC_TX_SIZE_480 512
#define CDC_RX_SIZE_12 64
#define CDC_TX_SIZE_12 64
#define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT #define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT
#define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_BULK + ENDPOINT_TRANSMIT_UNUSED #define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_BULK + ENDPOINT_TRANSMIT_UNUSED
#define ENDPOINT4_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_BULK #define ENDPOINT4_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_BULK

+ 19
- 9
teensy4/usb_serial.c View File

//static usb_packet_t *rx_packet=NULL; //static usb_packet_t *rx_packet=NULL;
//static usb_packet_t *tx_packet=NULL; //static usb_packet_t *tx_packet=NULL;
static volatile uint8_t tx_noautoflush=0; static volatile uint8_t tx_noautoflush=0;
extern volatile uint8_t usb_high_speed;


// TODO: should be 2 different timeouts, high speed (480) vs full speed (12) // TODO: should be 2 different timeouts, high speed (480) vs full speed (12)
#define TRANSMIT_FLUSH_TIMEOUT 75 /* in microseconds */ #define TRANSMIT_FLUSH_TIMEOUT 75 /* in microseconds */
static void usb_serial_flush_callback(void); static void usb_serial_flush_callback(void);


#define TX_NUM 7 #define TX_NUM 7
#define TX_SIZE 256 /* should be a multiple of CDC_TX_SIZE */
#define TX_SIZE 1024 /* should be a multiple of CDC_TX_SIZE */
static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32))); static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
static uint8_t txbuffer[TX_SIZE * TX_NUM]; static uint8_t txbuffer[TX_SIZE * TX_NUM];
static uint8_t tx_head=0; static uint8_t tx_head=0;
static uint16_t tx_available=0; static uint16_t tx_available=0;
static uint16_t tx_packet_size=0;


#define RX_NUM 3 #define RX_NUM 3
static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32))); static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
static uint8_t rx_buffer[RX_NUM * CDC_RX_SIZE];
static uint8_t rx_buffer[RX_NUM * CDC_RX_SIZE_480];
static uint16_t rx_count[RX_NUM]; static uint16_t rx_count[RX_NUM];
static uint16_t rx_index[RX_NUM]; static uint16_t rx_index[RX_NUM];
static uint16_t rx_packet_size=0;


static void rx_event(transfer_t *t) static void rx_event(transfer_t *t)
{ {
int len = CDC_RX_SIZE - ((t->status >> 16) & 0x7FFF);
int len = rx_packet_size - ((t->status >> 16) & 0x7FFF);
int index = t->callback_param; int index = t->callback_param;
//printf("rx event, len=%d, i=%d\n", len, index); //printf("rx event, len=%d, i=%d\n", len, index);
rx_count[index] = len; rx_count[index] = len;
void usb_serial_configure(void) void usb_serial_configure(void)
{ {
printf("usb_serial_configure\n"); printf("usb_serial_configure\n");
if (usb_high_speed) {
tx_packet_size = CDC_TX_SIZE_480;
rx_packet_size = CDC_RX_SIZE_480;
} else {
tx_packet_size = CDC_TX_SIZE_12;
rx_packet_size = CDC_RX_SIZE_12;
}
memset(tx_transfer, 0, sizeof(tx_transfer)); memset(tx_transfer, 0, sizeof(tx_transfer));
tx_head = 0; tx_head = 0;
tx_available = 0; tx_available = 0;
memset(rx_transfer, 0, sizeof(rx_transfer)); memset(rx_transfer, 0, sizeof(rx_transfer));
memset(rx_count, 0, sizeof(rx_count)); memset(rx_count, 0, sizeof(rx_count));
memset(rx_index, 0, sizeof(rx_index)); memset(rx_index, 0, sizeof(rx_index));
usb_config_tx(CDC_ACM_ENDPOINT, CDC_ACM_SIZE, 0, NULL);
usb_config_rx(CDC_RX_ENDPOINT, CDC_RX_SIZE, 0, rx_event);
usb_config_tx(CDC_TX_ENDPOINT, CDC_TX_SIZE, 0, NULL);
usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, CDC_RX_SIZE, 0);
usb_config_tx(CDC_ACM_ENDPOINT, CDC_ACM_SIZE, 0, NULL); // size same 12 & 480
usb_config_rx(CDC_RX_ENDPOINT, rx_packet_size, 0, rx_event);
usb_config_tx(CDC_TX_ENDPOINT, tx_packet_size, 0, NULL);
usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, rx_packet_size, 0);
usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0); usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
timer_config(usb_serial_flush_callback, TRANSMIT_FLUSH_TIMEOUT); timer_config(usb_serial_flush_callback, TRANSMIT_FLUSH_TIMEOUT);
} }
int c = rx_buffer[rx_index[0]++]; int c = rx_buffer[rx_index[0]++];
if (rx_index[0] >= rx_count[0]) { if (rx_index[0] >= rx_count[0]) {
// reschedule transfer // reschedule transfer
usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, CDC_RX_SIZE, 0);
usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, rx_packet_size, 0);
usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0); usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
} }
return c; return c;
{ {
if (rx_index[0] < rx_count[0]) { if (rx_index[0] < rx_count[0]) {
rx_index[0] = rx_count[0]; rx_index[0] = rx_count[0];
usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, CDC_RX_SIZE, 0);
usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, rx_packet_size, 0);
usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0); usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
} }
} }

Loading…
Cancel
Save