Archived
1
0

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 "usb_desc.h" #include "usb_desc.h"
@ -140,12 +170,15 @@ static uint8_t mouse_report_desc[] = {
0x05, 0x01, // Usage Page (Generic Desktop) 0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage (X) 0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y) 0x09, 0x31, // Usage (Y)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x7F, // Logical Maximum (32767)
0x75, 0x10, // Report Size (16),
0x95, 0x02, // Report Count (2),
0x81, 0x02, // Input (Data, Variable, Absolute)
0x09, 0x38, // Usage (Wheel)
0x15, 0x81, // Logical Minimum (-127) 0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127) 0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8), 0x75, 0x08, // Report Size (8),
0x95, 0x02, // Report Count (2),
0x81, 0x06, // Input (Data, Variable, Relative)
0x09, 0x38, // Usage (Wheel)
0x95, 0x01, // Report Count (1), 0x95, 0x01, // Report Count (1),
0x81, 0x06, // Input (Data, Variable, Relative) 0x81, 0x06, // Input (Data, Variable, Relative)
0xC0 // End Collection 0xC0 // End Collection
@ -287,8 +320,8 @@ static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
0, // bAlternateSetting 0, // bAlternateSetting
1, // bNumEndpoints 1, // bNumEndpoints
0x03, // bInterfaceClass (0x03 = HID) 0x03, // bInterfaceClass (0x03 = HID)
0x01, // bInterfaceSubClass (0x01 = Boot) 0x00, // bInterfaceSubClass (0x01 = Boot)
0x02, // bInterfaceProtocol (0x02 = Mouse) 0x00, // bInterfaceProtocol (0x02 = Mouse)
0, // iInterface 0, // iInterface
// HID interface descriptor, HID 1.11 spec, section 6.2.1 // HID interface descriptor, HID 1.11 spec, section 6.2.1
9, // bLength 9, // bLength

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.
*/
#ifndef _usb_desc_h_ #ifndef _usb_desc_h_
#define _usb_desc_h_ #define _usb_desc_h_

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 <Lib/USBLib.h>
#include "usb_dev.h" #include "usb_dev.h"
#include "usb_mem.h" #include "usb_mem.h"
@ -10,7 +40,21 @@ typedef struct {
} bdt_t; } bdt_t;
__attribute__ ((section(".usbdescriptortable"), used)) __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_OWN 0x80
#define BDT_DATA1 0x40 #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 uint8_t reply_buffer[8];
static void usbdev_setup(void) static void usb_setup(void)
{ {
const uint8_t *data = NULL; const uint8_t *data = NULL;
uint32_t datalen = 0; uint32_t datalen = 0;
@ -125,10 +169,42 @@ static void usbdev_setup(void)
reg = &USB0_ENDPT1; reg = &USB0_ENDPT1;
cfg = usb_endpoint_config_table; cfg = usb_endpoint_config_table;
// clear all BDT entries, free any allocated memory... // 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) { if (table[i].desc & BDT_OWN) {
usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8)); 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; usb_rx_memory_needed = 0;
@ -213,7 +289,14 @@ static void usbdev_setup(void)
//(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) { //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) { if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
data = list->addr; data = list->addr;
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; datalen = list->length;
}
#if 0 #if 0
serial_print("Desc found, "); serial_print("Desc found, ");
serial_phex32((uint32_t)data); serial_phex32((uint32_t)data);
@ -245,7 +328,7 @@ static void usbdev_setup(void)
#endif #endif
// TODO: this does not work... why? // TODO: this does not work... why?
#if defined(KEYBOARD_INTERFACE) #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
case 0x0921: // HID SET_REPORT case 0x0921: // HID SET_REPORT
//serial_print(":)\n"); //serial_print(":)\n");
return; return;
@ -361,7 +444,7 @@ static void usb_control(uint32_t stat)
serial_print("\n"); serial_print("\n");
#endif #endif
// actually "do" the setup request // actually "do" the setup request
usbdev_setup(); usb_setup();
// unfreeze the USB, now that we're ready // unfreeze the USB, now that we're ready
USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
break; break;
@ -371,17 +454,15 @@ static void usb_control(uint32_t stat)
#ifdef CDC_STATUS_INTERFACE #ifdef CDC_STATUS_INTERFACE
if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) { if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
int i; int i;
uint8_t *dst = usb_cdc_line_coding; uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
//serial_print("set line coding "); //serial_print("set line coding ");
for (i=0; i<7; i++) { for (i=0; i<7; i++) {
//serial_phex(*buf); //serial_phex(*buf);
*dst++ = *buf++; *dst++ = *buf++;
} }
//serial_phex32(*(uint32_t *)usb_cdc_line_coding); //serial_phex32(usb_cdc_line_coding[0]);
//serial_print("\n"); //serial_print("\n");
// XXX - Not sure why this was casted to uint32_t... -HaaTa if (usb_cdc_line_coding[0] == 134) usb_reboot_timer = 15;
//if (*(uint32_t *)usb_cdc_line_coding == 134) usb_reboot_timer = 15;
if (*usb_cdc_line_coding == 134) usb_reboot_timer = 15;
endpoint0_transmit(NULL, 0); endpoint0_transmit(NULL, 0);
} }
#endif #endif
@ -452,6 +533,7 @@ usb_packet_t *usb_rx(uint32_t endpoint)
__disable_irq(); __disable_irq();
ret = rx_first[endpoint]; ret = rx_first[endpoint];
if (ret) rx_first[endpoint] = ret->next; if (ret) rx_first[endpoint] = ret->next;
usb_rx_byte_count_data[endpoint] -= ret->len;
__enable_irq(); __enable_irq();
//serial_print("rx, epidx="); //serial_print("rx, epidx=");
//serial_phex(endpoint); //serial_phex(endpoint);
@ -473,13 +555,6 @@ static uint32_t usb_queue_byte_count(const usb_packet_t *p)
return count; 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) uint32_t usb_tx_byte_count(uint32_t endpoint)
{ {
endpoint--; endpoint--;
@ -494,9 +569,8 @@ uint32_t usb_tx_packet_count(uint32_t endpoint)
endpoint--; endpoint--;
if (endpoint >= NUM_ENDPOINTS) return 0; if (endpoint >= NUM_ENDPOINTS) return 0;
p = tx_first[endpoint];
__disable_irq(); __disable_irq();
for ( ; p; p = p->next) count++; for (p = tx_first[endpoint]; p; p = p->next) count++;
__enable_irq(); __enable_irq();
return count; return count;
} }
@ -572,11 +646,11 @@ void usb_tx(uint32_t endpoint, usb_packet_t *packet)
next = TX_STATE_EVEN_FREE; next = TX_STATE_EVEN_FREE;
break; break;
case TX_STATE_EVEN_FREE: case TX_STATE_EVEN_FREE:
next = TX_STATE_NONE_FREE; next = TX_STATE_NONE_FREE_ODD_FIRST;
break; break;
case TX_STATE_ODD_FREE: case TX_STATE_ODD_FREE:
b++; b++;
next = TX_STATE_NONE_FREE; next = TX_STATE_NONE_FREE_EVEN_FIRST;
break; break;
default: default:
if (tx_first[endpoint] == NULL) { if (tx_first[endpoint] == NULL) {
@ -679,9 +753,12 @@ void usb_isr(void)
tx_state[endpoint] = TX_STATE_EVEN_FREE; tx_state[endpoint] = TX_STATE_EVEN_FREE;
break; break;
case TX_STATE_EVEN_FREE: case TX_STATE_EVEN_FREE:
tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
break;
case TX_STATE_ODD_FREE: case TX_STATE_ODD_FREE:
tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
break;
default: default:
tx_state[endpoint] = TX_STATE_NONE_FREE;
break; break;
} }
b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0); b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
@ -705,6 +782,7 @@ void usb_isr(void)
} }
} else { // receive } else { // receive
packet->len = b->desc >> 16; packet->len = b->desc >> 16;
if (packet->len > 0) {
packet->index = 0; packet->index = 0;
packet->next = NULL; packet->next = NULL;
if (rx_first[endpoint] == NULL) { if (rx_first[endpoint] == NULL) {
@ -723,6 +801,7 @@ void usb_isr(void)
rx_last[endpoint]->next = packet; rx_last[endpoint]->next = packet;
} }
rx_last[endpoint] = packet; rx_last[endpoint] = packet;
usb_rx_byte_count_data[endpoint] += packet->len;
// TODO: implement a per-endpoint maximum # of allocated packets // TODO: implement a per-endpoint maximum # of allocated packets
// so a flood of incoming data on 1 endpoint doesn't starve // so a flood of incoming data on 1 endpoint doesn't starve
// the others if the user isn't reading it regularly // the others if the user isn't reading it regularly
@ -737,6 +816,9 @@ void usb_isr(void)
b->desc = 0; b->desc = 0;
usb_rx_memory_needed++; usb_rx_memory_needed++;
} }
} else {
b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
}
} }
@ -819,6 +901,8 @@ void usb_init(void)
//serial_begin(BAUD2DIV(115200)); //serial_begin(BAUD2DIV(115200));
//serial_print("usb_init\n"); //serial_print("usb_init\n");
//usb_init_serialnumber();
for (i=0; i <= NUM_ENDPOINTS*4; i++) { for (i=0; i <= NUM_ENDPOINTS*4; i++) {
table[i].desc = 0; table[i].desc = 0;
table[i].addr = 0; table[i].addr = 0;
@ -855,6 +939,7 @@ void usb_init(void)
USB0_INTEN = USB_INTEN_USBRSTEN; USB0_INTEN = USB_INTEN_USBRSTEN;
// enable interrupt in NVIC... // enable interrupt in NVIC...
NVIC_SET_PRIORITY(IRQ_USBOTG, 112);
NVIC_ENABLE_IRQ(IRQ_USBOTG); NVIC_ENABLE_IRQ(IRQ_USBOTG);
// enable d+ pullup // enable d+ pullup

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.
*/
#ifndef _usb_dev_h_ #ifndef _usb_dev_h_
#define _usb_dev_h_ #define _usb_dev_h_
@ -13,7 +43,6 @@ void usb_init(void);
uint8_t usb_configured(void); // is the USB port configured uint8_t usb_configured(void); // is the USB port configured
void usb_isr(void); void usb_isr(void);
usb_packet_t *usb_rx(uint32_t endpoint); usb_packet_t *usb_rx(uint32_t endpoint);
uint32_t usb_rx_byte_count(uint32_t endpoint);
uint32_t usb_tx_byte_count(uint32_t endpoint); uint32_t usb_tx_byte_count(uint32_t endpoint);
uint32_t usb_tx_packet_count(uint32_t endpoint); uint32_t usb_tx_packet_count(uint32_t endpoint);
void usb_tx(uint32_t endpoint, usb_packet_t *packet); void usb_tx(uint32_t endpoint, usb_packet_t *packet);
@ -23,13 +52,32 @@ void usb_device_reload();
extern volatile uint8_t usb_configuration; extern volatile uint8_t usb_configuration;
extern uint16_t usb_rx_byte_count_data[NUM_ENDPOINTS];
static inline uint32_t usb_rx_byte_count(uint32_t endpoint) __attribute__((always_inline));
static inline uint32_t usb_rx_byte_count(uint32_t endpoint)
{
endpoint--;
if (endpoint >= NUM_ENDPOINTS) return 0;
return usb_rx_byte_count_data[endpoint];
}
#ifdef CDC_DATA_INTERFACE #ifdef CDC_DATA_INTERFACE
extern uint8_t usb_cdc_line_coding[7]; extern uint32_t usb_cdc_line_coding[2];
extern volatile uint8_t usb_cdc_line_rtsdtr; extern volatile uint8_t usb_cdc_line_rtsdtr;
extern volatile uint8_t usb_cdc_transmit_flush_timer; extern volatile uint8_t usb_cdc_transmit_flush_timer;
extern void usb_serial_flush_callback(void); extern void usb_serial_flush_callback(void);
#endif #endif
#ifdef KEYBOARD_INTERFACE
extern uint8_t keyboard_modifier_keys;
extern uint8_t keyboard_keys[6];
extern uint8_t keyboard_protocol;
extern uint8_t keyboard_idle_config;
extern uint8_t keyboard_idle_count;
extern volatile uint8_t keyboard_leds;
#endif
#endif #endif

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 "usb_dev.h" #include "usb_dev.h"
#include "usb_keyboard.h" #include "usb_keyboard.h"
#include <Lib/USBLib.h> #include <Lib/USBLib.h>
@ -22,7 +52,7 @@ static uint8_t transmit_previous_timeout=0;
// send the contents of keyboard_keys and keyboard_modifier_keys // send the contents of keyboard_keys and keyboard_modifier_keys
uint8_t usb_keyboard_send(void) int usb_keyboard_send(void)
{ {
uint32_t wait_count=0; uint32_t wait_count=0;
usb_packet_t *tx_packet; usb_packet_t *tx_packet;

View File

@ -1,10 +1,40 @@
/* 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.
*/
#ifndef USBkeyboard_h_ #ifndef USBkeyboard_h_
#define USBkeyboard_h_ #define USBkeyboard_h_
#include <inttypes.h> #include <inttypes.h>
#include "output_com.h" #include "output_com.h"
uint8_t usb_keyboard_send(void); int usb_keyboard_send(void);
#endif // USBkeyboard_h_ #endif // USBkeyboard_h_

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 <Lib/USBLib.h>
#include "usb_dev.h" #include "usb_dev.h"
#include "usb_mem.h" #include "usb_mem.h"

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.
*/
#ifndef _usb_mem_h_ #ifndef _usb_mem_h_
#define _usb_mem_h_ #define _usb_mem_h_

View File

@ -1,11 +1,42 @@
/* 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 "usb_dev.h" #include "usb_dev.h"
#include "usb_serial.h" #include "usb_serial.h"
#include <Lib/USBLib.h> #include <Lib/USBLib.h>
#include <string.h> // For memcpy
// defined by usb_dev.h -> usb_desc.h // defined by usb_dev.h -> usb_desc.h
#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE) #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
uint8_t usb_cdc_line_coding[7]; uint32_t usb_cdc_line_coding[2];
volatile uint8_t usb_cdc_line_rtsdtr=0; volatile uint8_t usb_cdc_line_rtsdtr=0;
volatile uint8_t usb_cdc_transmit_flush_timer=0; volatile uint8_t usb_cdc_transmit_flush_timer=0;
@ -15,27 +46,17 @@ static volatile uint8_t tx_noautoflush=0;
#define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */ #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
static void usb_serial_receive(void)
{
if (!usb_configuration) return;
if (rx_packet) return;
while (1) {
rx_packet = usb_rx(CDC_RX_ENDPOINT);
if (rx_packet == NULL) return;
if (rx_packet->len > 0) return;
usb_free(rx_packet);
rx_packet = NULL;
}
}
// get the next character, or -1 if nothing received // get the next character, or -1 if nothing received
int usb_serial_getchar(void) int usb_serial_getchar(void)
{ {
unsigned int i; unsigned int i;
int c; int c;
usb_serial_receive(); if (!rx_packet) {
if (!usb_configuration) return -1;
rx_packet = usb_rx(CDC_RX_ENDPOINT);
if (!rx_packet) return -1; if (!rx_packet) return -1;
}
i = rx_packet->index; i = rx_packet->index;
c = rx_packet->buf[i++]; c = rx_packet->buf[i++];
if (i >= rx_packet->len) { if (i >= rx_packet->len) {
@ -50,7 +71,11 @@ int usb_serial_getchar(void)
// peek at the next character, or -1 if nothing received // peek at the next character, or -1 if nothing received
int usb_serial_peekchar(void) int usb_serial_peekchar(void)
{ {
usb_serial_receive(); if (!rx_packet) {
if (!usb_configuration) return -1;
rx_packet = usb_rx(CDC_RX_ENDPOINT);
if (!rx_packet) return -1;
}
if (!rx_packet) return -1; if (!rx_packet) return -1;
return rx_packet->buf[rx_packet->index]; return rx_packet->buf[rx_packet->index];
} }
@ -58,15 +83,44 @@ int usb_serial_peekchar(void)
// number of bytes available in the receive buffer // number of bytes available in the receive buffer
int usb_serial_available(void) int usb_serial_available(void)
{ {
int count=0; int count;
if (usb_configuration) {
count = usb_rx_byte_count(CDC_RX_ENDPOINT); count = usb_rx_byte_count(CDC_RX_ENDPOINT);
}
if (rx_packet) count += rx_packet->len - rx_packet->index; if (rx_packet) count += rx_packet->len - rx_packet->index;
return count; return count;
} }
// read a block of bytes to a buffer
int usb_serial_read(void *buffer, uint32_t size)
{
uint8_t *p = (uint8_t *)buffer;
uint32_t qty, count=0;
while (size) {
if (!usb_configuration) break;
if (!rx_packet) {
rx:
rx_packet = usb_rx(CDC_RX_ENDPOINT);
if (!rx_packet) break;
if (rx_packet->len == 0) {
usb_free(rx_packet);
goto rx;
}
}
qty = rx_packet->len - rx_packet->index;
if (qty > size) qty = size;
memcpy(p, rx_packet->buf + rx_packet->index, qty);
p += qty;
count += qty;
size -= qty;
rx_packet->index += qty;
if (rx_packet->index >= rx_packet->len) {
usb_free(rx_packet);
rx_packet = NULL;
}
}
return count;
}
// discard any buffered input // discard any buffered input
void usb_serial_flush_input(void) void usb_serial_flush_input(void)
{ {
@ -110,51 +164,12 @@ static uint8_t transmit_previous_timeout=0;
// transmit a character. 0 returned on success, -1 on error // transmit a character. 0 returned on success, -1 on error
int usb_serial_putchar(uint8_t c) int usb_serial_putchar(uint8_t c)
{ {
#if 1
return usb_serial_write(&c, 1); return usb_serial_write(&c, 1);
#endif
#if 0
uint32_t wait_count;
tx_noautoflush = 1;
if (!tx_packet) {
wait_count = 0;
while (1) {
if (!usb_configuration) {
tx_noautoflush = 0;
return -1;
}
if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
tx_noautoflush = 1;
tx_packet = usb_malloc();
if (tx_packet) break;
tx_noautoflush = 0;
}
if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
transmit_previous_timeout = 1;
return -1;
}
}
}
transmit_previous_timeout = 0;
tx_packet->buf[tx_packet->index++] = c;
if (tx_packet->index < CDC_TX_SIZE) {
usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
} else {
tx_packet->len = CDC_TX_SIZE;
usb_cdc_transmit_flush_timer = 0;
usb_tx(CDC_TX_ENDPOINT, tx_packet);
tx_packet = NULL;
}
tx_noautoflush = 0;
return 0;
#endif
} }
int usb_serial_write(const void *buffer, uint32_t size) int usb_serial_write(const void *buffer, uint32_t size)
{ {
#if 1
uint32_t len; uint32_t len;
uint32_t wait_count; uint32_t wait_count;
const uint8_t *src = (const uint8_t *)buffer; const uint8_t *src = (const uint8_t *)buffer;
@ -189,52 +204,53 @@ int usb_serial_write(const void *buffer, uint32_t size)
tx_packet->index += len; tx_packet->index += len;
size -= len; size -= len;
while (len-- > 0) *dest++ = *src++; while (len-- > 0) *dest++ = *src++;
if (tx_packet->index < CDC_TX_SIZE) { if (tx_packet->index >= CDC_TX_SIZE) {
usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
} else {
tx_packet->len = CDC_TX_SIZE; tx_packet->len = CDC_TX_SIZE;
usb_cdc_transmit_flush_timer = 0;
usb_tx(CDC_TX_ENDPOINT, tx_packet); usb_tx(CDC_TX_ENDPOINT, tx_packet);
tx_packet = NULL; tx_packet = NULL;
} }
usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
} }
tx_noautoflush = 0; tx_noautoflush = 0;
return 0; return 0;
#endif
#if 0
const uint8_t *p = (const uint8_t *)buffer;
int r;
while (size) {
r = usb_serial_putchar(*p++);
if (r < 0) return -1;
size--;
}
return 0;
#endif
} }
void usb_serial_flush_output(void) void usb_serial_flush_output(void)
{ {
if (!usb_configuration) return; if (!usb_configuration) return;
//serial_print("usb_serial_flush_output\n"); tx_noautoflush = 1;
if (tx_packet && tx_packet->index > 0) { if (tx_packet) {
usb_cdc_transmit_flush_timer = 0; usb_cdc_transmit_flush_timer = 0;
tx_packet->len = tx_packet->index; tx_packet->len = tx_packet->index;
usb_tx(CDC_TX_ENDPOINT, tx_packet); usb_tx(CDC_TX_ENDPOINT, tx_packet);
tx_packet = NULL; tx_packet = NULL;
} else {
usb_packet_t *tx = usb_malloc();
if (tx) {
usb_cdc_transmit_flush_timer = 0;
usb_tx(CDC_TX_ENDPOINT, tx);
} else {
usb_cdc_transmit_flush_timer = 1;
} }
// while (usb_tx_byte_count(CDC_TX_ENDPOINT) > 0) ; // wait }
tx_noautoflush = 0;
} }
void usb_serial_flush_callback(void) void usb_serial_flush_callback(void)
{ {
if (tx_noautoflush) return; if (tx_noautoflush) return;
//serial_print("usb_flush_callback \n"); if (tx_packet) {
tx_packet->len = tx_packet->index; tx_packet->len = tx_packet->index;
usb_tx(CDC_TX_ENDPOINT, tx_packet); usb_tx(CDC_TX_ENDPOINT, tx_packet);
tx_packet = NULL; tx_packet = NULL;
//serial_print("usb_flush_callback end\n"); } else {
usb_packet_t *tx = usb_malloc();
if (tx) {
usb_tx(CDC_TX_ENDPOINT, tx);
} else {
usb_cdc_transmit_flush_timer = 1;
}
}
} }
#endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE

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.
*/
#ifndef USBserial_h_ #ifndef USBserial_h_
#define USBserial_h_ #define USBserial_h_
@ -12,14 +42,18 @@
int usb_serial_getchar(void); int usb_serial_getchar(void);
int usb_serial_peekchar(void); int usb_serial_peekchar(void);
int usb_serial_available(void); int usb_serial_available(void);
int usb_serial_read(void *buffer, uint32_t size);
void usb_serial_flush_input(void); void usb_serial_flush_input(void);
int usb_serial_putchar(uint8_t c); int usb_serial_putchar(uint8_t c);
int usb_serial_write(const void *buffer, uint32_t size); int usb_serial_write(const void *buffer, uint32_t size);
void usb_serial_flush_output(void); void usb_serial_flush_output(void);
extern uint8_t usb_cdc_line_coding[7]; extern uint32_t usb_cdc_line_coding[2];
extern volatile uint8_t usb_cdc_line_rtsdtr; extern volatile uint8_t usb_cdc_line_rtsdtr;
extern volatile uint8_t usb_cdc_transmit_flush_timer; extern volatile uint8_t usb_cdc_transmit_flush_timer;
extern volatile uint8_t usb_configuration; extern volatile uint8_t usb_configuration;
#define USB_SERIAL_DTR 0x01
#define USB_SERIAL_RTS 0x02
#endif // USBserial_h_ #endif // USBserial_h_