Adding in USB module updates from Teensyduino 1.18rc2.

- For potential bug fixes
- Not copied verbatim, some additions were left out
This commit is contained in:
Jacob Alexander 2014-02-02 16:19:58 -08:00
parent 71268de9d1
commit 25304734af
10 changed files with 514 additions and 148 deletions

View file

@ -1,3 +1,33 @@
/* Teensyduino Core Library
* http://www.pjrc.com/teensy/
* Copyright (c) 2013 PJRC.COM, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* 1. The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* 2. If the Software is incorporated into a build system that allows
* selection among a list of target devices, then similar target
* devices manufactured by PJRC.COM must be included in the list of
* target devices and selectable in the same manner.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Lib/USBLib.h>
#include "usb_dev.h"
#include "usb_mem.h"
@ -10,7 +40,21 @@ typedef struct {
} bdt_t;
__attribute__ ((section(".usbdescriptortable"), used))
static bdt_t table[64];
static bdt_t table[(NUM_ENDPOINTS+1)*4];
static usb_packet_t *rx_first[NUM_ENDPOINTS];
static usb_packet_t *rx_last[NUM_ENDPOINTS];
static usb_packet_t *tx_first[NUM_ENDPOINTS];
static usb_packet_t *tx_last[NUM_ENDPOINTS];
uint16_t usb_rx_byte_count_data[NUM_ENDPOINTS];
static uint8_t tx_state[NUM_ENDPOINTS];
#define TX_STATE_BOTH_FREE_EVEN_FIRST 0
#define TX_STATE_BOTH_FREE_ODD_FIRST 1
#define TX_STATE_EVEN_FREE 2
#define TX_STATE_ODD_FREE 3
#define TX_STATE_NONE_FREE_EVEN_FIRST 4
#define TX_STATE_NONE_FREE_ODD_FIRST 5
#define BDT_OWN 0x80
#define BDT_DATA1 0x40
@ -105,7 +149,7 @@ static void endpoint0_transmit(const void *data, uint32_t len)
static uint8_t reply_buffer[8];
static void usbdev_setup(void)
static void usb_setup(void)
{
const uint8_t *data = NULL;
uint32_t datalen = 0;
@ -125,10 +169,42 @@ static void usbdev_setup(void)
reg = &USB0_ENDPT1;
cfg = usb_endpoint_config_table;
// clear all BDT entries, free any allocated memory...
for (i=4; i <= NUM_ENDPOINTS*4; i++) {
for (i=4; i < (NUM_ENDPOINTS+1)*4; i++) {
if (table[i].desc & BDT_OWN) {
usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8));
table[i].desc = 0;
}
}
// free all queued packets
for (i=0; i < NUM_ENDPOINTS; i++) {
usb_packet_t *p, *n;
p = rx_first[i];
while (p) {
n = p->next;
usb_free(p);
p = n;
}
rx_first[i] = NULL;
rx_last[i] = NULL;
p = tx_first[i];
while (p) {
n = p->next;
usb_free(p);
p = n;
}
tx_first[i] = NULL;
tx_last[i] = NULL;
usb_rx_byte_count_data[i] = 0;
switch (tx_state[i]) {
case TX_STATE_EVEN_FREE:
case TX_STATE_NONE_FREE_EVEN_FIRST:
tx_state[i] = TX_STATE_BOTH_FREE_EVEN_FIRST;
break;
case TX_STATE_ODD_FREE:
case TX_STATE_NONE_FREE_ODD_FIRST:
tx_state[i] = TX_STATE_BOTH_FREE_ODD_FIRST;
break;
default:
break;
}
}
usb_rx_memory_needed = 0;
@ -213,7 +289,14 @@ static void usbdev_setup(void)
//(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
data = list->addr;
datalen = list->length;
if ((setup.wValue >> 8) == 3) {
// for string descriptors, use the descriptor's
// length field, allowing runtime configured
// length.
datalen = *(list->addr);
} else {
datalen = list->length;
}
#if 0
serial_print("Desc found, ");
serial_phex32((uint32_t)data);
@ -245,7 +328,7 @@ static void usbdev_setup(void)
#endif
// TODO: this does not work... why?
#if defined(KEYBOARD_INTERFACE)
#if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
case 0x0921: // HID SET_REPORT
//serial_print(":)\n");
return;
@ -361,7 +444,7 @@ static void usb_control(uint32_t stat)
serial_print("\n");
#endif
// actually "do" the setup request
usbdev_setup();
usb_setup();
// unfreeze the USB, now that we're ready
USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
break;
@ -371,17 +454,15 @@ static void usb_control(uint32_t stat)
#ifdef CDC_STATUS_INTERFACE
if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
int i;
uint8_t *dst = usb_cdc_line_coding;
uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
//serial_print("set line coding ");
for (i=0; i<7; i++) {
//serial_phex(*buf);
*dst++ = *buf++;
}
//serial_phex32(*(uint32_t *)usb_cdc_line_coding);
//serial_phex32(usb_cdc_line_coding[0]);
//serial_print("\n");
// XXX - Not sure why this was casted to uint32_t... -HaaTa
//if (*(uint32_t *)usb_cdc_line_coding == 134) usb_reboot_timer = 15;
if (*usb_cdc_line_coding == 134) usb_reboot_timer = 15;
if (usb_cdc_line_coding[0] == 134) usb_reboot_timer = 15;
endpoint0_transmit(NULL, 0);
}
#endif
@ -452,6 +533,7 @@ usb_packet_t *usb_rx(uint32_t endpoint)
__disable_irq();
ret = rx_first[endpoint];
if (ret) rx_first[endpoint] = ret->next;
usb_rx_byte_count_data[endpoint] -= ret->len;
__enable_irq();
//serial_print("rx, epidx=");
//serial_phex(endpoint);
@ -473,13 +555,6 @@ static uint32_t usb_queue_byte_count(const usb_packet_t *p)
return count;
}
uint32_t usb_rx_byte_count(uint32_t endpoint)
{
endpoint--;
if (endpoint >= NUM_ENDPOINTS) return 0;
return usb_queue_byte_count(rx_first[endpoint]);
}
uint32_t usb_tx_byte_count(uint32_t endpoint)
{
endpoint--;
@ -494,9 +569,8 @@ uint32_t usb_tx_packet_count(uint32_t endpoint)
endpoint--;
if (endpoint >= NUM_ENDPOINTS) return 0;
p = tx_first[endpoint];
__disable_irq();
for ( ; p; p = p->next) count++;
for (p = tx_first[endpoint]; p; p = p->next) count++;
__enable_irq();
return count;
}
@ -572,11 +646,11 @@ void usb_tx(uint32_t endpoint, usb_packet_t *packet)
next = TX_STATE_EVEN_FREE;
break;
case TX_STATE_EVEN_FREE:
next = TX_STATE_NONE_FREE;
next = TX_STATE_NONE_FREE_ODD_FIRST;
break;
case TX_STATE_ODD_FREE:
b++;
next = TX_STATE_NONE_FREE;
next = TX_STATE_NONE_FREE_EVEN_FIRST;
break;
default:
if (tx_first[endpoint] == NULL) {
@ -679,9 +753,12 @@ void usb_isr(void)
tx_state[endpoint] = TX_STATE_EVEN_FREE;
break;
case TX_STATE_EVEN_FREE:
tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
break;
case TX_STATE_ODD_FREE:
tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
break;
default:
tx_state[endpoint] = TX_STATE_NONE_FREE;
break;
}
b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
@ -705,37 +782,42 @@ void usb_isr(void)
}
} else { // receive
packet->len = b->desc >> 16;
packet->index = 0;
packet->next = NULL;
if (rx_first[endpoint] == NULL) {
//serial_print("rx 1st, epidx=");
//serial_phex(endpoint);
//serial_print(", packet=");
//serial_phex32((uint32_t)packet);
//serial_print("\n");
rx_first[endpoint] = packet;
if (packet->len > 0) {
packet->index = 0;
packet->next = NULL;
if (rx_first[endpoint] == NULL) {
//serial_print("rx 1st, epidx=");
//serial_phex(endpoint);
//serial_print(", packet=");
//serial_phex32((uint32_t)packet);
//serial_print("\n");
rx_first[endpoint] = packet;
} else {
//serial_print("rx Nth, epidx=");
//serial_phex(endpoint);
//serial_print(", packet=");
//serial_phex32((uint32_t)packet);
//serial_print("\n");
rx_last[endpoint]->next = packet;
}
rx_last[endpoint] = packet;
usb_rx_byte_count_data[endpoint] += packet->len;
// TODO: implement a per-endpoint maximum # of allocated packets
// so a flood of incoming data on 1 endpoint doesn't starve
// the others if the user isn't reading it regularly
packet = usb_malloc();
if (packet) {
b->addr = packet->buf;
b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
} else {
//serial_print("starving ");
//serial_phex(endpoint + 1);
//serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
b->desc = 0;
usb_rx_memory_needed++;
}
} else {
//serial_print("rx Nth, epidx=");
//serial_phex(endpoint);
//serial_print(", packet=");
//serial_phex32((uint32_t)packet);
//serial_print("\n");
rx_last[endpoint]->next = packet;
}
rx_last[endpoint] = packet;
// TODO: implement a per-endpoint maximum # of allocated packets
// so a flood of incoming data on 1 endpoint doesn't starve
// the others if the user isn't reading it regularly
packet = usb_malloc();
if (packet) {
b->addr = packet->buf;
b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
} else {
//serial_print("starving ");
//serial_phex(endpoint + 1);
//serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
b->desc = 0;
usb_rx_memory_needed++;
}
}
@ -819,6 +901,8 @@ void usb_init(void)
//serial_begin(BAUD2DIV(115200));
//serial_print("usb_init\n");
//usb_init_serialnumber();
for (i=0; i <= NUM_ENDPOINTS*4; i++) {
table[i].desc = 0;
table[i].addr = 0;
@ -855,6 +939,7 @@ void usb_init(void)
USB0_INTEN = USB_INTEN_USBRSTEN;
// enable interrupt in NVIC...
NVIC_SET_PRIORITY(IRQ_USBOTG, 112);
NVIC_ENABLE_IRQ(IRQ_USBOTG);
// enable d+ pullup