]> git.donarmstrong.com Git - kiibohd-controller.git/commitdiff
Moving USB to Output in preparation for additional Output types.
authorJacob Alexander <haata@kiibohd.com>
Mon, 20 Jan 2014 00:40:36 +0000 (16:40 -0800)
committerJacob Alexander <haata@kiibohd.com>
Sat, 22 Mar 2014 21:09:01 +0000 (14:09 -0700)
* Initial cli code

38 files changed:
CMakeLists.txt
Debug/cli/cli.c [new file with mode: 0644]
Debug/cli/cli.h [new file with mode: 0644]
Debug/cli/setup.cmake [new file with mode: 0644]
Debug/full/setup.cmake
Debug/print/print.c
Debug/print/print.h
Output/pjrcUSB/arm/usb_desc.c [new file with mode: 0644]
Output/pjrcUSB/arm/usb_desc.h [new file with mode: 0644]
Output/pjrcUSB/arm/usb_dev.c [new file with mode: 0644]
Output/pjrcUSB/arm/usb_dev.h [new file with mode: 0644]
Output/pjrcUSB/arm/usb_keyboard.c [new file with mode: 0644]
Output/pjrcUSB/arm/usb_keyboard.h [new file with mode: 0644]
Output/pjrcUSB/arm/usb_mem.c [new file with mode: 0644]
Output/pjrcUSB/arm/usb_mem.h [new file with mode: 0644]
Output/pjrcUSB/arm/usb_serial.c [new file with mode: 0644]
Output/pjrcUSB/arm/usb_serial.h [new file with mode: 0644]
Output/pjrcUSB/avr/usb_keyboard_debug.c [new file with mode: 0644]
Output/pjrcUSB/avr/usb_keyboard_debug.h [new file with mode: 0644]
Output/pjrcUSB/output_com.c [new file with mode: 0644]
Output/pjrcUSB/output_com.h [new file with mode: 0644]
Output/pjrcUSB/setup.cmake [new file with mode: 0644]
USB/pjrc/arm/usb_desc.c [deleted file]
USB/pjrc/arm/usb_desc.h [deleted file]
USB/pjrc/arm/usb_dev.c [deleted file]
USB/pjrc/arm/usb_dev.h [deleted file]
USB/pjrc/arm/usb_keyboard.c [deleted file]
USB/pjrc/arm/usb_keyboard.h [deleted file]
USB/pjrc/arm/usb_mem.c [deleted file]
USB/pjrc/arm/usb_mem.h [deleted file]
USB/pjrc/arm/usb_serial.c [deleted file]
USB/pjrc/arm/usb_serial.h [deleted file]
USB/pjrc/avr/usb_keyboard_debug.c [deleted file]
USB/pjrc/avr/usb_keyboard_debug.h [deleted file]
USB/pjrc/setup.cmake [deleted file]
USB/pjrc/usb_com.c [deleted file]
USB/pjrc/usb_com.h [deleted file]
setup.cmake

index 7ce02108193f337f1439bed69a722b534fd1e0bc..177ee14a19b82ba4b951cf13e3b6750c168280b6 100644 (file)
@@ -70,7 +70,7 @@ set( SRCS
        ${COMPILER_SRCS}
        ${SCAN_SRCS}
        ${MACRO_SRCS}
-       ${USB_SRCS}
+       ${OUTPUT_SRCS}
        ${DEBUG_SRCS}
 )
 
@@ -84,10 +84,10 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
 #
 
 #| Check for whether the set modules are compatible with the specified compiler family
-ModuleCompatibility( ${ScanModulePath}  ${ScanModuleCompatibility}  )
-ModuleCompatibility( ${MacroModulePath} ${MacroModuleCompatibility} )
-ModuleCompatibility( ${USBModulePath}   ${USBModuleCompatibility}   )
-ModuleCompatibility( ${DebugModulePath} ${DebugModuleCompatibility} )
+ModuleCompatibility( ${ScanModulePath}   ${ScanModuleCompatibility}   )
+ModuleCompatibility( ${MacroModulePath}  ${MacroModuleCompatibility}  )
+ModuleCompatibility( ${OutputModulePath} ${OutputModuleCompatibility} )
+ModuleCompatibility( ${DebugModulePath}  ${DebugModuleCompatibility}  )
 
 
 
diff --git a/Debug/cli/cli.c b/Debug/cli/cli.c
new file mode 100644 (file)
index 0000000..0e81893
--- /dev/null
@@ -0,0 +1,37 @@
+/* Copyright (C) 2014 by Jacob Alexander
+ *
+ * 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:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * 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.
+ */
+
+// ----- Includes -----
+
+// Compiler Includes
+//#include <stdarg.h>
+
+// Project Includes
+#include "cli.h"
+
+
+
+// ----- Functions -----
+
+void initCLI()
+{
+}
+
diff --git a/Debug/cli/cli.h b/Debug/cli/cli.h
new file mode 100644 (file)
index 0000000..50f7a3b
--- /dev/null
@@ -0,0 +1,49 @@
+/* Copyright (C) 2014 by Jacob Alexander
+ *
+ * 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:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * 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 cli_h__
+#define cli_h__
+
+// ----- Includes -----
+
+// Compiler Includes
+#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
+
+#elif defined(_mk20dx128_)
+
+#include "arm/usb_serial.h"
+
+#endif
+
+
+
+// ----- Defines -----
+
+
+
+// ----- Functions and Corresponding Function Aliases -----
+
+void initCLI();
+
+
+
+#endif
+
diff --git a/Debug/cli/setup.cmake b/Debug/cli/setup.cmake
new file mode 100644 (file)
index 0000000..82ec440
--- /dev/null
@@ -0,0 +1,42 @@
+###| CMake Kiibohd Controller Debug Module |###
+#
+# Written by Jacob Alexander in 2014 for the Kiibohd Controller
+#
+# Released into the Public Domain
+#
+###
+
+
+###
+# Module C files
+#
+
+set( DEBUG_SRCS
+       cli.c
+)
+
+
+###
+# Setup File Dependencies
+#
+
+
+###
+# Module Specific Options
+#
+
+
+###
+# Just in case, you only want this module and are using others as well
+#
+add_definitions( -I${HEAD_DIR}/Debug/off )
+
+
+###
+# Compiler Family Compatibility
+#
+set( DebugModuleCompatibility
+       arm
+       avr
+)
+
index a270941b837c92160d520df295b5c96edcd32bc4..ab280ffd83aea2d29102e92793bb2eb35de1dd90 100644 (file)
@@ -1,6 +1,6 @@
 ###| CMake Kiibohd Controller Debug Module |###
 #
-# Written by Jacob Alexander in 2011 for the Kiibohd Controller
+# Written by Jacob Alexander in 2011-2014 for the Kiibohd Controller
 #
 # Released into the Public Domain
 #
@@ -13,6 +13,7 @@
 
 #| XXX Requires the ../ due to how the paths are constructed
 set( DEBUG_SRCS
+       ../cli/cli.c
        ../led/led.c
        ../print/print.c
 )
@@ -22,13 +23,14 @@ set( DEBUG_SRCS
 # Setup File Dependencies
 #
 add_file_dependencies( ../led/led.c ../led/led.h )
-add_file_dependencies( ../led/print.c ../led/print.h )
+add_file_dependencies( ../print/print.c ../print/print.h )
 
 
 ###
 # Module Specific Options
 #
 add_definitions(
+       -I${HEAD_DIR}/Debug/cli
        -I${HEAD_DIR}/Debug/led
        -I${HEAD_DIR}/Debug/print
 )
index 7c4f2ed846121b6fc2dabf5d0bdd9a609b2758c3..bb97d3f60164179689fae0c953001052bf461b5d 100644 (file)
@@ -1,15 +1,15 @@
 /* Copyright (C) 2011-2013 by Jacob Alexander
- * 
+ *
  * 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:
- * 
+ *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
- * 
+ *
  * 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
index 7b87004222a19bb8a23a32a245daff5f9afba37b..18fad6d91cde660910b8e5a1b938df15dcf9e1d0 100644 (file)
@@ -1,15 +1,15 @@
 /* Copyright (C) 2011-2013 by Jacob Alexander
- * 
+ *
  * 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:
- * 
+ *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
- * 
+ *
  * 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
diff --git a/Output/pjrcUSB/arm/usb_desc.c b/Output/pjrcUSB/arm/usb_desc.c
new file mode 100644 (file)
index 0000000..564c79e
--- /dev/null
@@ -0,0 +1,473 @@
+#include "usb_desc.h"
+
+
+// USB Descriptors are binary data which the USB host reads to
+// automatically detect a USB device's capabilities.  The format
+// and meaning of every field is documented in numerous USB
+// standards.  When working with USB descriptors, despite the
+// complexity of the standards and poor writing quality in many
+// of those documents, remember descriptors are nothing more
+// than constant binary data that tells the USB host what the
+// device can do.  Computers will load drivers based on this data.
+// Those drivers then communicate on the endpoints specified by
+// the descriptors.
+
+// To configure a new combination of interfaces or make minor
+// changes to existing configuration (eg, change the name or ID
+// numbers), usually you would edit "usb_desc.h".  This file
+// is meant to be configured by the header, so generally it is
+// only edited to add completely new USB interfaces or features.
+
+
+
+// **************************************************************
+//   USB Device
+// **************************************************************
+
+#define LSB(n) ((n) & 255)
+#define MSB(n) (((n) >> 8) & 255)
+
+// USB Device Descriptor.  The USB host reads this first, to learn
+// what type of device is connected.
+static uint8_t device_descriptor[] = {
+        18,                                     // bLength
+        1,                                      // bDescriptorType
+        0x00, 0x02,                             // bcdUSB
+#ifdef DEVICE_CLASS
+        DEVICE_CLASS,                           // bDeviceClass
+#else
+       0,
+#endif
+#ifdef DEVICE_SUBCLASS
+        DEVICE_SUBCLASS,                        // bDeviceSubClass
+#else
+       0,
+#endif
+#ifdef DEVICE_PROTOCOL
+        DEVICE_PROTOCOL,                        // bDeviceProtocol
+#else
+       0,
+#endif
+        EP0_SIZE,                               // bMaxPacketSize0
+        LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
+        LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
+        0x00, 0x01,                             // bcdDevice
+        1,                                      // iManufacturer
+        2,                                      // iProduct
+        3,                                      // iSerialNumber
+        1                                       // bNumConfigurations
+};
+
+// These descriptors must NOT be "const", because the USB DMA
+// has trouble accessing flash memory with enough bandwidth
+// while the processor is executing from flash.
+
+
+
+// **************************************************************
+//   HID Report Descriptors
+// **************************************************************
+
+// Each HID interface needs a special report descriptor that tells
+// the meaning and format of the data.
+
+#ifdef KEYBOARD_INTERFACE
+// Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
+static uint8_t keyboard_report_desc[] = {
+        0x05, 0x01,             //  Usage Page (Generic Desktop),
+        0x09, 0x06,             //  Usage (Keyboard),
+        0xA1, 0x01,             //  Collection (Application),
+        0x75, 0x01,             //  Report Size (1),
+        0x95, 0x08,             //  Report Count (8),
+        0x05, 0x07,             //  Usage Page (Key Codes),
+        0x19, 0xE0,             //  Usage Minimum (224),
+        0x29, 0xE7,             //  Usage Maximum (231),
+        0x15, 0x00,             //  Logical Minimum (0),
+        0x25, 0x01,             //  Logical Maximum (1),
+        0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Modifier byte
+        0x95, 0x08,             //  Report Count (8),
+        0x75, 0x01,             //  Report Size (1),
+        0x15, 0x00,             //  Logical Minimum (0),
+        0x25, 0x01,             //  Logical Maximum (1),
+        0x05, 0x0C,             //  Usage Page (Consumer),
+        0x09, 0xE9,             //  Usage (Volume Increment),
+        0x09, 0xEA,             //  Usage (Volume Decrement),
+        0x09, 0xE2,             //  Usage (Mute),
+        0x09, 0xCD,             //  Usage (Play/Pause),
+        0x09, 0xB5,             //  Usage (Scan Next Track),
+        0x09, 0xB6,             //  Usage (Scan Previous Track),
+        0x09, 0xB7,             //  Usage (Stop),
+        0x09, 0xB8,             //  Usage (Eject),
+        0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Media keys
+        0x95, 0x05,             //  Report Count (5),
+        0x75, 0x01,             //  Report Size (1),
+        0x05, 0x08,             //  Usage Page (LEDs),
+        0x19, 0x01,             //  Usage Minimum (1),
+        0x29, 0x05,             //  Usage Maximum (5),
+        0x91, 0x02,             //  Output (Data, Variable, Absolute), ;LED report
+        0x95, 0x01,             //  Report Count (1),
+        0x75, 0x03,             //  Report Size (3),
+        0x91, 0x03,             //  Output (Constant),                 ;LED report padding
+        0x95, 0x06,             //  Report Count (6),
+        0x75, 0x08,             //  Report Size (8),
+        0x15, 0x00,             //  Logical Minimum (0),
+        0x25, 0x7F,             //  Logical Maximum(104),
+        0x05, 0x07,             //  Usage Page (Key Codes),
+        0x19, 0x00,             //  Usage Minimum (0),
+        0x29, 0x7F,             //  Usage Maximum (104),
+        0x81, 0x00,             //  Input (Data, Array),                ;Normal keys
+        0xc0                    // End Collection
+};
+#endif
+
+#ifdef MOUSE_INTERFACE
+// Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
+static uint8_t mouse_report_desc[] = {
+        0x05, 0x01,                     // Usage Page (Generic Desktop)
+        0x09, 0x02,                     // Usage (Mouse)
+        0xA1, 0x01,                     // Collection (Application)
+        0x05, 0x09,                     //   Usage Page (Button)
+        0x19, 0x01,                     //   Usage Minimum (Button #1)
+        0x29, 0x03,                     //   Usage Maximum (Button #3)
+        0x15, 0x00,                     //   Logical Minimum (0)
+        0x25, 0x01,                     //   Logical Maximum (1)
+        0x95, 0x03,                     //   Report Count (3)
+        0x75, 0x01,                     //   Report Size (1)
+        0x81, 0x02,                     //   Input (Data, Variable, Absolute)
+        0x95, 0x01,                     //   Report Count (1)
+        0x75, 0x05,                     //   Report Size (5)
+        0x81, 0x03,                     //   Input (Constant)
+        0x05, 0x01,                     //   Usage Page (Generic Desktop)
+        0x09, 0x30,                     //   Usage (X)
+        0x09, 0x31,                     //   Usage (Y)
+        0x15, 0x81,                     //   Logical Minimum (-127)
+        0x25, 0x7F,                     //   Logical Maximum (127)
+        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),
+        0x81, 0x06,                     //   Input (Data, Variable, Relative)
+        0xC0                            // End Collection
+};
+#endif
+
+
+
+// **************************************************************
+//   USB Configuration
+// **************************************************************
+
+// USB Configuration Descriptor.  This huge descriptor tells all
+// of the devices capbilities.
+static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
+        // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
+        9,                                      // bLength;
+        2,                                      // bDescriptorType;
+        LSB(CONFIG_DESC_SIZE),                 // wTotalLength
+        MSB(CONFIG_DESC_SIZE),
+        NUM_INTERFACE,                          // bNumInterfaces
+        1,                                      // bConfigurationValue
+        0,                                      // iConfiguration
+        0xC0,                                   // bmAttributes
+        50,                                     // bMaxPower
+
+#ifdef CDC_IAD_DESCRIPTOR
+        // interface association descriptor, USB ECN, Table 9-Z
+        8,                                      // bLength
+        11,                                     // bDescriptorType
+        CDC_STATUS_INTERFACE,                   // bFirstInterface
+        2,                                      // bInterfaceCount
+        0x02,                                   // bFunctionClass
+        0x02,                                   // bFunctionSubClass
+        0x01,                                   // bFunctionProtocol
+        4,                                      // iFunction
+#endif
+
+#ifdef CDC_DATA_INTERFACE
+        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
+        9,                                      // bLength
+        4,                                      // bDescriptorType
+        CDC_STATUS_INTERFACE,                  // bInterfaceNumber
+        0,                                      // bAlternateSetting
+        1,                                      // bNumEndpoints
+        0x02,                                   // bInterfaceClass
+        0x02,                                   // bInterfaceSubClass
+        0x01,                                   // bInterfaceProtocol
+        0,                                      // iInterface
+        // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
+        5,                                      // bFunctionLength
+        0x24,                                   // bDescriptorType
+        0x00,                                   // bDescriptorSubtype
+        0x10, 0x01,                             // bcdCDC
+        // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
+        5,                                      // bFunctionLength
+        0x24,                                   // bDescriptorType
+        0x01,                                   // bDescriptorSubtype
+        0x01,                                   // bmCapabilities
+        1,                                      // bDataInterface
+        // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
+        4,                                      // bFunctionLength
+        0x24,                                   // bDescriptorType
+        0x02,                                   // bDescriptorSubtype
+        0x06,                                   // bmCapabilities
+        // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
+        5,                                      // bFunctionLength
+        0x24,                                   // bDescriptorType
+        0x06,                                   // bDescriptorSubtype
+        CDC_STATUS_INTERFACE,                   // bMasterInterface
+        CDC_DATA_INTERFACE,                     // bSlaveInterface0
+        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+        7,                                      // bLength
+        5,                                      // bDescriptorType
+        CDC_ACM_ENDPOINT | 0x80,                // bEndpointAddress
+        0x03,                                   // bmAttributes (0x03=intr)
+        CDC_ACM_SIZE, 0,                        // wMaxPacketSize
+        64,                                     // bInterval
+        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
+        9,                                      // bLength
+        4,                                      // bDescriptorType
+        CDC_DATA_INTERFACE,                     // bInterfaceNumber
+        0,                                      // bAlternateSetting
+        2,                                      // bNumEndpoints
+        0x0A,                                   // bInterfaceClass
+        0x00,                                   // bInterfaceSubClass
+        0x00,                                   // bInterfaceProtocol
+        0,                                      // iInterface
+        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+        7,                                      // bLength
+        5,                                      // bDescriptorType
+        CDC_RX_ENDPOINT,                        // bEndpointAddress
+        0x02,                                   // bmAttributes (0x02=bulk)
+        CDC_RX_SIZE, 0,                         // wMaxPacketSize
+        0,                                      // bInterval
+        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+        7,                                      // bLength
+        5,                                      // bDescriptorType
+        CDC_TX_ENDPOINT | 0x80,                 // bEndpointAddress
+        0x02,                                   // bmAttributes (0x02=bulk)
+        CDC_TX_SIZE, 0,                         // wMaxPacketSize
+        0,                                      // bInterval
+#endif // CDC_DATA_INTERFACE
+
+#ifdef KEYBOARD_INTERFACE
+        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
+        9,                                      // bLength
+        4,                                      // bDescriptorType
+        KEYBOARD_INTERFACE,                     // bInterfaceNumber
+        0,                                      // bAlternateSetting
+        1,                                      // bNumEndpoints
+        0x03,                                   // bInterfaceClass (0x03 = HID)
+        0x01,                                   // bInterfaceSubClass (0x01 = Boot)
+        0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
+        0,                                      // iInterface
+        // HID interface descriptor, HID 1.11 spec, section 6.2.1
+        9,                                      // bLength
+        0x21,                                   // bDescriptorType
+        0x11, 0x01,                             // bcdHID
+        0,                                      // bCountryCode
+        1,                                      // bNumDescriptors
+        0x22,                                   // bDescriptorType
+        LSB(sizeof(keyboard_report_desc)),      // wDescriptorLength
+        MSB(sizeof(keyboard_report_desc)),
+        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+        7,                                      // bLength
+        5,                                      // bDescriptorType
+        KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
+        0x03,                                   // bmAttributes (0x03=intr)
+        KEYBOARD_SIZE, 0,                       // wMaxPacketSize
+        KEYBOARD_INTERVAL,                      // bInterval
+#endif // KEYBOARD_INTERFACE
+
+#ifdef MOUSE_INTERFACE
+        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
+        9,                                      // bLength
+        4,                                      // bDescriptorType
+        MOUSE_INTERFACE,                        // bInterfaceNumber
+        0,                                      // bAlternateSetting
+        1,                                      // bNumEndpoints
+        0x03,                                   // bInterfaceClass (0x03 = HID)
+        0x01,                                   // bInterfaceSubClass (0x01 = Boot)
+        0x02,                                   // bInterfaceProtocol (0x02 = Mouse)
+        0,                                      // iInterface
+        // HID interface descriptor, HID 1.11 spec, section 6.2.1
+        9,                                      // bLength
+        0x21,                                   // bDescriptorType
+        0x11, 0x01,                             // bcdHID
+        0,                                      // bCountryCode
+        1,                                      // bNumDescriptors
+        0x22,                                   // bDescriptorType
+        LSB(sizeof(mouse_report_desc)),         // wDescriptorLength
+        MSB(sizeof(mouse_report_desc)),
+        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+        7,                                      // bLength
+        5,                                      // bDescriptorType
+        MOUSE_ENDPOINT | 0x80,                  // bEndpointAddress
+        0x03,                                   // bmAttributes (0x03=intr)
+        MOUSE_SIZE, 0,                          // wMaxPacketSize
+        MOUSE_INTERVAL,                         // bInterval
+#endif // MOUSE_INTERFACE
+};
+
+
+
+// **************************************************************
+//   String Descriptors
+// **************************************************************
+
+// The descriptors above can provide human readable strings,
+// referenced by index numbers.  These descriptors are the
+// actual string data
+
+struct usb_string_descriptor_struct {
+        uint8_t bLength;
+        uint8_t bDescriptorType;
+        uint16_t wString[];
+};
+
+static struct usb_string_descriptor_struct string0 = {
+        4,
+        3,
+        {0x0409}
+};
+
+static struct usb_string_descriptor_struct string1 = {
+        sizeof(STR_MANUFACTURER),
+        3,
+        STR_MANUFACTURER
+};
+static struct usb_string_descriptor_struct string2 = {
+       sizeof(STR_PRODUCT),
+        3,
+        STR_PRODUCT
+};
+static struct usb_string_descriptor_struct string3 = {
+       sizeof(STR_SERIAL),
+        3,
+       STR_SERIAL
+};
+
+
+// **************************************************************
+//   Descriptors List
+// **************************************************************
+
+// This table provides access to all the descriptor data above.
+
+const usb_descriptor_list_t usb_descriptor_list[] = {
+       //wValue, wIndex, address,          length
+       {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
+       {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
+#ifdef KEYBOARD_INTERFACE
+        {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
+        {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
+#endif
+#ifdef MOUSE_INTERFACE
+        {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
+        {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
+#endif
+        {0x0300, 0x0000, (const uint8_t *)&string0, 4},
+        {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
+        {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
+        {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)},
+       {0, 0, NULL, 0}
+};
+
+
+// **************************************************************
+//   Endpoint Configuration
+// **************************************************************
+
+#if 0
+// 0x00 = not used
+// 0x19 = Recieve only
+// 0x15 = Transmit only
+// 0x1D = Transmit & Recieve
+// 
+const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] = 
+{
+       0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00, 
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
+};
+#endif
+
+
+const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] = 
+{
+#if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
+       ENDPOINT1_CONFIG,
+#elif (NUM_ENDPOINTS >= 1)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
+       ENDPOINT2_CONFIG,
+#elif (NUM_ENDPOINTS >= 2)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
+       ENDPOINT3_CONFIG,
+#elif (NUM_ENDPOINTS >= 3)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
+       ENDPOINT4_CONFIG,
+#elif (NUM_ENDPOINTS >= 4)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
+       ENDPOINT5_CONFIG,
+#elif (NUM_ENDPOINTS >= 5)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
+       ENDPOINT6_CONFIG,
+#elif (NUM_ENDPOINTS >= 6)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
+       ENDPOINT7_CONFIG,
+#elif (NUM_ENDPOINTS >= 7)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
+       ENDPOINT8_CONFIG,
+#elif (NUM_ENDPOINTS >= 8)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
+       ENDPOINT9_CONFIG,
+#elif (NUM_ENDPOINTS >= 9)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
+       ENDPOINT10_CONFIG,
+#elif (NUM_ENDPOINTS >= 10)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
+       ENDPOINT11_CONFIG,
+#elif (NUM_ENDPOINTS >= 11)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
+       ENDPOINT12_CONFIG,
+#elif (NUM_ENDPOINTS >= 12)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
+       ENDPOINT13_CONFIG,
+#elif (NUM_ENDPOINTS >= 13)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
+       ENDPOINT14_CONFIG,
+#elif (NUM_ENDPOINTS >= 14)
+       ENDPOINT_UNUSED,
+#endif
+#if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
+       ENDPOINT15_CONFIG,
+#elif (NUM_ENDPOINTS >= 15)
+       ENDPOINT_UNUSED,
+#endif
+};
+
diff --git a/Output/pjrcUSB/arm/usb_desc.h b/Output/pjrcUSB/arm/usb_desc.h
new file mode 100644 (file)
index 0000000..f4e8dfc
--- /dev/null
@@ -0,0 +1,71 @@
+#ifndef _usb_desc_h_
+#define _usb_desc_h_
+
+// This header is NOT meant to be included when compiling
+// user sketches in Arduino.  The low-level functions
+// provided by usb_dev.c are meant to be called only by
+// code which provides higher-level interfaces to the user.
+
+#include <stdint.h>
+#include <stddef.h>
+#include "usb_com.h"
+
+#define ENDPOINT_UNUSED                        0x00
+#define ENDPOINT_TRANSIMIT_ONLY                0x15
+#define ENDPOINT_RECEIVE_ONLY          0x19
+#define ENDPOINT_TRANSMIT_AND_RECEIVE  0x1D
+
+// Some operating systems, especially Windows, may cache USB device
+// info.  Changes to the device name may not update on the same
+// computer unless the vendor or product ID numbers change, or the
+// "bcdDevice" revision code is increased.
+
+#define DEVICE_CLASS           0xEF
+#define DEVICE_SUBCLASS        0x02
+#define DEVICE_PROTOCOL        0x01
+#define EP0_SIZE               64
+#define NUM_ENDPOINTS          15
+#define NUM_INTERFACE          4
+#define CDC_IAD_DESCRIPTOR     1
+#define CDC_STATUS_INTERFACE   0
+#define CDC_DATA_INTERFACE     1       // Serial
+#define CDC_ACM_ENDPOINT       2
+#define CDC_RX_ENDPOINT       3
+#define CDC_TX_ENDPOINT       4
+#define CDC_ACM_SIZE          16
+#define CDC_RX_SIZE           64
+#define CDC_TX_SIZE           64
+#define KEYBOARD_INTERFACE    2        // Keyboard
+#define KEYBOARD_ENDPOINT     1
+#define KEYBOARD_SIZE         8
+#define KEYBOARD_INTERVAL     1
+#define MOUSE_INTERFACE       3        // Mouse
+#define MOUSE_ENDPOINT        5
+#define MOUSE_SIZE            8
+#define MOUSE_INTERVAL        2
+#define KEYBOARD_DESC_OFFSET   (9+8 + 9+5+5+4+5+7+9+7+7 + 9)
+#define MOUSE_DESC_OFFSET      (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9)
+#define CONFIG_DESC_SIZE       (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9+9+7)
+#define ENDPOINT1_CONFIG       ENDPOINT_TRANSIMIT_ONLY
+#define ENDPOINT2_CONFIG       ENDPOINT_TRANSIMIT_ONLY
+#define ENDPOINT3_CONFIG       ENDPOINT_RECEIVE_ONLY
+#define ENDPOINT4_CONFIG       ENDPOINT_TRANSIMIT_ONLY
+#define ENDPOINT5_CONFIG       ENDPOINT_TRANSIMIT_ONLY
+
+
+
+// NUM_ENDPOINTS = number of non-zero endpoints (0 to 15)
+extern const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS];
+
+typedef struct {
+       uint16_t        wValue;
+       uint16_t        wIndex;
+       const uint8_t   *addr;
+       uint16_t        length;
+} usb_descriptor_list_t;
+
+extern const usb_descriptor_list_t usb_descriptor_list[];
+
+
+#endif
+
diff --git a/Output/pjrcUSB/arm/usb_dev.c b/Output/pjrcUSB/arm/usb_dev.c
new file mode 100644 (file)
index 0000000..72dad59
--- /dev/null
@@ -0,0 +1,867 @@
+#include <Lib/USBLib.h>
+#include "usb_dev.h"
+#include "usb_mem.h"
+
+// buffer descriptor table
+
+typedef struct {
+       uint32_t desc;
+       void * addr;
+} bdt_t;
+
+__attribute__ ((section(".usbdescriptortable"), used))
+static bdt_t table[64];
+
+#define BDT_OWN                0x80
+#define BDT_DATA1      0x40
+#define BDT_DATA0      0x00
+#define BDT_DTS                0x08
+#define BDT_STALL      0x04
+#define BDT_PID(n)     (((n) >> 2) & 15)
+
+#define BDT_DESC(count, data)  (BDT_OWN | BDT_DTS \
+                               | ((data) ? BDT_DATA1 : BDT_DATA0) \
+                               | ((count) << 16))
+
+#define TX   1
+#define RX   0
+#define ODD  1
+#define EVEN 0
+#define DATA0 0
+#define DATA1 1
+#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
+#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
+
+
+static union {
+ struct {
+  union {
+   struct {
+       uint8_t bmRequestType;
+       uint8_t bRequest;
+   };
+       uint16_t wRequestAndType;
+  };
+       uint16_t wValue;
+       uint16_t wIndex;
+       uint16_t wLength;
+ };
+ struct {
+       uint32_t word1;
+       uint32_t word2;
+ };
+} setup;
+
+
+#define GET_STATUS             0
+#define CLEAR_FEATURE          1
+#define SET_FEATURE            3
+#define SET_ADDRESS            5
+#define GET_DESCRIPTOR         6
+#define SET_DESCRIPTOR         7
+#define GET_CONFIGURATION      8
+#define SET_CONFIGURATION      9
+#define GET_INTERFACE          10
+#define SET_INTERFACE          11
+#define SYNCH_FRAME            12
+
+// SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
+// transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
+// Status stage uses a DATA1 PID.
+
+static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
+static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
+static const uint8_t *ep0_tx_ptr = NULL;
+static uint16_t ep0_tx_len;
+static uint8_t ep0_tx_bdt_bank = 0;
+static uint8_t ep0_tx_data_toggle = 0;
+uint8_t usb_rx_memory_needed = 0;
+
+volatile uint8_t usb_configuration = 0;
+volatile uint8_t usb_reboot_timer = 0;
+
+
+static void endpoint0_stall(void)
+{
+       USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
+}
+
+
+static void endpoint0_transmit(const void *data, uint32_t len)
+{
+#if 0
+       serial_print("tx0:");
+       serial_phex32((uint32_t)data);
+       serial_print(",");
+       serial_phex16(len);
+       serial_print(ep0_tx_bdt_bank ? ", odd" : ", even");
+       serial_print(ep0_tx_data_toggle ? ", d1\n" : ", d0\n");
+#endif
+       table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
+       table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
+       ep0_tx_data_toggle ^= 1;
+       ep0_tx_bdt_bank ^= 1;
+}
+
+static uint8_t reply_buffer[8];
+
+static void usbdev_setup(void)
+{
+       const uint8_t *data = NULL;
+       uint32_t datalen = 0;
+       const usb_descriptor_list_t *list;
+       uint32_t size;
+       volatile uint8_t *reg;
+       uint8_t epconf;
+       const uint8_t *cfg;
+       int i;
+
+       switch (setup.wRequestAndType) {
+         case 0x0500: // SET_ADDRESS
+               break;
+         case 0x0900: // SET_CONFIGURATION
+               //serial_print("configure\n");
+               usb_configuration = setup.wValue;
+               reg = &USB0_ENDPT1;
+               cfg = usb_endpoint_config_table;
+               // clear all BDT entries, free any allocated memory...
+               for (i=4; i <= NUM_ENDPOINTS*4; i++) {
+                       if (table[i].desc & BDT_OWN) {
+                               usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8));
+                               table[i].desc = 0;
+                       }
+               }
+               usb_rx_memory_needed = 0;
+               for (i=1; i <= NUM_ENDPOINTS; i++) {
+                       epconf = *cfg++;
+                       *reg = epconf;
+                       reg += 4;
+                       if (epconf & USB_ENDPT_EPRXEN) {
+                               usb_packet_t *p;
+                               p = usb_malloc();
+                               if (p) {
+                                       table[index(i, RX, EVEN)].addr = p->buf;
+                                       table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
+                               } else {
+                                       table[index(i, RX, EVEN)].desc = 0;
+                                       usb_rx_memory_needed++;
+                               }
+                               p = usb_malloc();
+                               if (p) {
+                                       table[index(i, RX, ODD)].addr = p->buf;
+                                       table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
+                               } else {
+                                       table[index(i, RX, ODD)].desc = 0;
+                                       usb_rx_memory_needed++;
+                               }
+                       }
+                       table[index(i, TX, EVEN)].desc = 0;
+                       table[index(i, TX, ODD)].desc = 0;
+               }
+               break;
+         case 0x0880: // GET_CONFIGURATION
+               reply_buffer[0] = usb_configuration;
+               datalen = 1;
+               data = reply_buffer;
+               break;
+         case 0x0080: // GET_STATUS (device)
+               reply_buffer[0] = 0;
+               reply_buffer[1] = 0;
+               datalen = 2;
+               data = reply_buffer;
+               break;
+         case 0x0082: // GET_STATUS (endpoint)
+               if (setup.wIndex > NUM_ENDPOINTS) {
+                       // TODO: do we need to handle IN vs OUT here?
+                       endpoint0_stall();
+                       return;
+               }
+               reply_buffer[0] = 0;
+               reply_buffer[1] = 0;
+               if (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02) reply_buffer[0] = 1;
+               data = reply_buffer;
+               datalen = 2;
+               break;
+         case 0x0102: // CLEAR_FEATURE (endpoint)
+               i = setup.wIndex & 0x7F;
+               if (i > NUM_ENDPOINTS || setup.wValue != 0) {
+                       // TODO: do we need to handle IN vs OUT here?
+                       endpoint0_stall();
+                       return;
+               }
+               (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
+               // TODO: do we need to clear the data toggle here?
+               break;
+         case 0x0302: // SET_FEATURE (endpoint)
+               i = setup.wIndex & 0x7F;
+               if (i > NUM_ENDPOINTS || setup.wValue != 0) {
+                       // TODO: do we need to handle IN vs OUT here?
+                       endpoint0_stall();
+                       return;
+               }
+               (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
+               // TODO: do we need to clear the data toggle here?
+               break;
+         case 0x0680: // GET_DESCRIPTOR
+         case 0x0681:
+               //serial_print("desc:");
+               //serial_phex16(setup.wValue);
+               //serial_print("\n");
+               for (list = usb_descriptor_list; 1; list++) {
+                       if (list->addr == NULL) break;
+                       //if (setup.wValue == list->wValue && 
+                       //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
+                       if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
+                               data = list->addr;
+                               datalen = list->length;
+#if 0
+                               serial_print("Desc found, ");
+                               serial_phex32((uint32_t)data);
+                               serial_print(",");
+                               serial_phex16(datalen);
+                               serial_print(",");
+                               serial_phex(data[0]);
+                               serial_phex(data[1]);
+                               serial_phex(data[2]);
+                               serial_phex(data[3]);
+                               serial_phex(data[4]);
+                               serial_phex(data[5]);
+                               serial_print("\n");
+#endif
+                               goto send;
+                       }
+               }
+               //serial_print("desc: not found\n");
+               endpoint0_stall();
+               return;
+#if defined(CDC_STATUS_INTERFACE)
+         case 0x2221: // CDC_SET_CONTROL_LINE_STATE
+               usb_cdc_line_rtsdtr = setup.wValue;
+               //serial_print("set control line state\n");
+               break;
+         case 0x2021: // CDC_SET_LINE_CODING
+               //serial_print("set coding, waiting...\n");
+               return;
+#endif
+
+// TODO: this does not work... why?
+#if defined(KEYBOARD_INTERFACE)
+         case 0x0921: // HID SET_REPORT
+               //serial_print(":)\n");
+               return;
+         case 0x0A21: // HID SET_IDLE
+               break;
+         // case 0xC940:
+#endif
+         default:
+               endpoint0_stall();
+               return;
+       }
+       send:
+       //serial_print("setup send ");
+       //serial_phex32(data);
+       //serial_print(",");
+       //serial_phex16(datalen);
+       //serial_print("\n");
+
+       if (datalen > setup.wLength) datalen = setup.wLength;
+       size = datalen;
+       if (size > EP0_SIZE) size = EP0_SIZE;
+       endpoint0_transmit(data, size);
+       data += size;
+       datalen -= size;
+       if (datalen == 0 && size < EP0_SIZE) return;
+
+       size = datalen;
+       if (size > EP0_SIZE) size = EP0_SIZE;
+       endpoint0_transmit(data, size);
+       data += size;
+       datalen -= size;
+       if (datalen == 0 && size < EP0_SIZE) return;
+
+       ep0_tx_ptr = data;
+       ep0_tx_len = datalen;
+}
+
+
+
+//A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
+//experiences any configuration event (configuration events are explained in
+//Sections 9.1.1.5 and 9.4.5).
+
+//Configuring a device or changing an alternate setting causes all of the status
+//and configuration values associated with endpoints in the affected interfaces
+//to be set to their default values. This includes setting the data toggle of
+//any endpoint using data toggles to the value DATA0.
+
+//For endpoints using data toggle, regardless of whether an endpoint has the
+//Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
+//data toggle being reinitialized to DATA0.
+
+
+
+// #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
+
+static void usb_control(uint32_t stat)
+{
+       bdt_t *b;
+       uint32_t pid, size;
+       uint8_t *buf;
+       const uint8_t *data;
+
+       b = stat2bufferdescriptor(stat);
+       pid = BDT_PID(b->desc);
+       //count = b->desc >> 16;
+       buf = b->addr;
+       //serial_print("pid:");
+       //serial_phex(pid);
+       //serial_print(", count:");
+       //serial_phex(count);
+       //serial_print("\n");
+
+       switch (pid) {
+       case 0x0D: // Setup received from host
+               //serial_print("PID=Setup\n");
+               //if (count != 8) ; // panic?
+               // grab the 8 byte setup info
+               setup.word1 = *(uint32_t *)(buf);
+               setup.word2 = *(uint32_t *)(buf + 4);
+
+               // give the buffer back
+               b->desc = BDT_DESC(EP0_SIZE, DATA1);
+               //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
+               //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
+
+               // clear any leftover pending IN transactions
+               ep0_tx_ptr = NULL;
+               if (ep0_tx_data_toggle) {
+               }
+               //if (table[index(0, TX, EVEN)].desc & 0x80) {
+                       //serial_print("leftover tx even\n");
+               //}
+               //if (table[index(0, TX, ODD)].desc & 0x80) {
+                       //serial_print("leftover tx odd\n");
+               //}
+               table[index(0, TX, EVEN)].desc = 0;
+               table[index(0, TX, ODD)].desc = 0;
+               // first IN after Setup is always DATA1
+               ep0_tx_data_toggle = 1;
+
+#if 0
+               serial_print("bmRequestType:");
+               serial_phex(setup.bmRequestType);
+               serial_print(", bRequest:");
+               serial_phex(setup.bRequest);
+               serial_print(", wValue:");
+               serial_phex16(setup.wValue);
+               serial_print(", wIndex:");
+               serial_phex16(setup.wIndex);
+               serial_print(", len:");
+               serial_phex16(setup.wLength);
+               serial_print("\n");
+#endif
+               // actually "do" the setup request
+               usbdev_setup();
+               // unfreeze the USB, now that we're ready
+               USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
+               break;
+       case 0x01:  // OUT transaction received from host
+       case 0x02:
+               //serial_print("PID=OUT\n");
+#ifdef CDC_STATUS_INTERFACE
+               if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
+                       int i;
+                       uint8_t *dst = 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_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;
+                       endpoint0_transmit(NULL, 0);
+               }
+#endif
+#ifdef KEYBOARD_INTERFACE
+               if (setup.word1 == 0x02000921 && setup.word2 == ((1<<16)|KEYBOARD_INTERFACE)) {
+                       USBKeys_LEDs = buf[0];
+                       endpoint0_transmit(NULL, 0);
+               }
+#endif
+               // give the buffer back
+               b->desc = BDT_DESC(EP0_SIZE, DATA1);
+               break;
+
+       case 0x09: // IN transaction completed to host
+               //serial_print("PID=IN:");
+               //serial_phex(stat);
+               //serial_print("\n");
+
+               // send remaining data, if any...
+               data = ep0_tx_ptr;
+               if (data) {
+                       size = ep0_tx_len;
+                       if (size > EP0_SIZE) size = EP0_SIZE;
+                       endpoint0_transmit(data, size);
+                       data += size;
+                       ep0_tx_len -= size;
+                       ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
+               }
+
+               if (setup.bRequest == 5 && setup.bmRequestType == 0) {
+                       setup.bRequest = 0;
+                       //serial_print("set address: ");
+                       //serial_phex16(setup.wValue);
+                       //serial_print("\n");
+                       USB0_ADDR = setup.wValue;
+               }
+
+               break;
+       //default:
+               //serial_print("PID=unknown:");
+               //serial_phex(pid);
+               //serial_print("\n");
+       }
+       USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
+}
+
+
+
+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];
+
+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             4
+
+
+
+usb_packet_t *usb_rx(uint32_t endpoint)
+{
+       usb_packet_t *ret;
+       endpoint--;
+       if (endpoint >= NUM_ENDPOINTS) return NULL;
+       __disable_irq();
+       ret = rx_first[endpoint];
+       if (ret) rx_first[endpoint] = ret->next;
+       __enable_irq();
+       //serial_print("rx, epidx=");
+       //serial_phex(endpoint);
+       //serial_print(", packet=");
+       //serial_phex32(ret);
+       //serial_print("\n");
+       return ret;
+}
+
+static uint32_t usb_queue_byte_count(const usb_packet_t *p)
+{
+       uint32_t count=0;
+
+       __disable_irq();
+       for ( ; p; p = p->next) {
+               count += p->len;
+       }
+       __enable_irq();
+       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--;
+       if (endpoint >= NUM_ENDPOINTS) return 0;
+       return usb_queue_byte_count(tx_first[endpoint]);
+}
+
+uint32_t usb_tx_packet_count(uint32_t endpoint)
+{
+       const usb_packet_t *p;
+       uint32_t count=0;
+
+       endpoint--;
+       if (endpoint >= NUM_ENDPOINTS) return 0;
+       p = tx_first[endpoint];
+       __disable_irq();
+       for ( ; p; p = p->next) count++;
+       __enable_irq();
+       return count;
+}
+
+
+// Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
+// receive endpoints are starving for memory.  The intention is to give
+// endpoints needing receive memory priority over the user's code, which is
+// likely calling usb_malloc to obtain memory for transmitting.  When the
+// user is creating data very quickly, their consumption could starve reception
+// without this prioritization.  The packet buffer (input) is assigned to the
+// first endpoint needing memory.
+//
+void usb_rx_memory(usb_packet_t *packet)
+{
+       unsigned int i;
+       const uint8_t *cfg;
+
+       cfg = usb_endpoint_config_table;
+       //serial_print("rx_mem:");
+       __disable_irq();
+       for (i=1; i <= NUM_ENDPOINTS; i++) {
+               if (*cfg++ & USB_ENDPT_EPRXEN) {
+                       if (table[index(i, RX, EVEN)].desc == 0) {
+                               table[index(i, RX, EVEN)].addr = packet->buf;
+                               table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
+                               usb_rx_memory_needed--;
+                               __enable_irq();
+                               //serial_phex(i);
+                               //serial_print(",even\n");
+                               return;
+                       }
+                       if (table[index(i, RX, ODD)].desc == 0) {
+                               table[index(i, RX, ODD)].addr = packet->buf;
+                               table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
+                               usb_rx_memory_needed--;
+                               __enable_irq();
+                               //serial_phex(i);
+                               //serial_print(",odd\n");
+                               return;
+                       }
+               }
+       }
+       __enable_irq();
+       // we should never reach this point.  If we get here, it means
+       // usb_rx_memory_needed was set greater than zero, but no memory
+       // was actually needed.  
+       usb_rx_memory_needed = 0;
+       usb_free(packet);
+       return;
+}
+
+//#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
+//#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
+
+void usb_tx(uint32_t endpoint, usb_packet_t *packet)
+{
+       bdt_t *b = &table[index(endpoint, TX, EVEN)];
+       uint8_t next;
+
+       endpoint--;
+       if (endpoint >= NUM_ENDPOINTS) return;
+       __disable_irq();
+       //serial_print("txstate=");
+       //serial_phex(tx_state[endpoint]);
+       //serial_print("\n");
+       switch (tx_state[endpoint]) {
+         case TX_STATE_BOTH_FREE_EVEN_FIRST:
+               next = TX_STATE_ODD_FREE;
+               break;
+         case TX_STATE_BOTH_FREE_ODD_FIRST:
+               b++;
+               next = TX_STATE_EVEN_FREE;
+               break;
+         case TX_STATE_EVEN_FREE:
+               next = TX_STATE_NONE_FREE;
+               break;
+         case TX_STATE_ODD_FREE:
+               b++;
+               next = TX_STATE_NONE_FREE;
+               break;
+         default:
+               if (tx_first[endpoint] == NULL) {
+                       tx_first[endpoint] = packet;
+               } else {
+                       tx_last[endpoint]->next = packet;
+               }
+               tx_last[endpoint] = packet;
+               __enable_irq();
+               return;
+       }
+       tx_state[endpoint] = next;
+       b->addr = packet->buf;
+       b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
+       __enable_irq();
+}
+
+
+
+
+
+
+void _reboot_Teensyduino_(void)
+{
+       // TODO: initialize R0 with a code....
+       asm volatile("bkpt");
+}
+
+
+
+void usb_isr(void)
+{
+       uint8_t status, stat, t;
+
+       //serial_print("isr");
+       //status = USB0_ISTAT;
+       //serial_phex(status);
+       //serial_print("\n");
+       restart:
+       status = USB0_ISTAT;
+
+       if ((status & USB_INTEN_SOFTOKEN /* 04 */ )) {
+               if (usb_configuration) {
+                       t = usb_reboot_timer;
+                       if (t) {
+                               usb_reboot_timer = --t;
+                               if (!t) _reboot_Teensyduino_();
+                       }
+#ifdef CDC_DATA_INTERFACE
+                       t = usb_cdc_transmit_flush_timer;
+                       if (t) {
+                               usb_cdc_transmit_flush_timer = --t;
+                               if (t == 0) usb_serial_flush_callback();
+                       }
+#endif
+               }
+               USB0_ISTAT = USB_INTEN_SOFTOKEN;
+       }
+
+       if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
+               uint8_t endpoint;
+               stat = USB0_STAT;
+               //serial_print("token: ep=");
+               //serial_phex(stat >> 4);
+               //serial_print(stat & 0x08 ? ",tx" : ",rx");
+               //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
+               endpoint = stat >> 4;
+               if (endpoint == 0) {
+                       usb_control(stat);
+               } else {
+                       bdt_t *b = stat2bufferdescriptor(stat);
+                       usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
+#if 0
+                       serial_print("ep:");
+                       serial_phex(endpoint);
+                       serial_print(", pid:");
+                       serial_phex(BDT_PID(b->desc));
+                       serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
+                       serial_print(", count:");
+                       serial_phex(b->desc >> 16);
+                       serial_print("\n");
+#endif
+                       endpoint--;     // endpoint is index to zero-based arrays
+
+                       if (stat & 0x08) { // transmit
+                               usb_free(packet);
+                               packet = tx_first[endpoint];
+                               if (packet) {
+                                       //serial_print("tx packet\n");
+                                       tx_first[endpoint] = packet->next;
+                                       b->addr = packet->buf;
+                                       switch (tx_state[endpoint]) {
+                                         case TX_STATE_BOTH_FREE_EVEN_FIRST:
+                                               tx_state[endpoint] = TX_STATE_ODD_FREE;
+                                               break;
+                                         case TX_STATE_BOTH_FREE_ODD_FIRST:
+                                               tx_state[endpoint] = TX_STATE_EVEN_FREE;
+                                               break;
+                                         case TX_STATE_EVEN_FREE:
+                                         case TX_STATE_ODD_FREE:
+                                         default:
+                                               tx_state[endpoint] = TX_STATE_NONE_FREE;
+                                               break;
+                                       }
+                                       b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
+                               } else {
+                                       //serial_print("tx no packet\n");
+                                       switch (tx_state[endpoint]) {
+                                         case TX_STATE_BOTH_FREE_EVEN_FIRST:
+                                         case TX_STATE_BOTH_FREE_ODD_FIRST:
+                                               break;
+                                         case TX_STATE_EVEN_FREE:
+                                               tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
+                                               break;
+                                         case TX_STATE_ODD_FREE:
+                                               tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
+                                               break;
+                                         default:
+                                               tx_state[endpoint] = ((uint32_t)b & 8) ?
+                                                 TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
+                                               break;
+                                       }
+                               }
+                       } 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;
+                               } 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++;
+                               }
+                       }
+
+
+
+
+               }
+               USB0_ISTAT = USB_ISTAT_TOKDNE;
+               goto restart;
+       }
+
+
+
+       if (status & USB_ISTAT_USBRST /* 01 */ ) {
+               //serial_print("reset\n");
+
+               // initialize BDT toggle bits
+               USB0_CTL = USB_CTL_ODDRST;
+               ep0_tx_bdt_bank = 0;
+
+               // set up buffers to receive Setup and OUT packets
+               table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
+               table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
+               table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
+               table[index(0, RX, ODD)].addr = ep0_rx1_buf;
+               table[index(0, TX, EVEN)].desc = 0;
+               table[index(0, TX, ODD)].desc = 0;
+               
+               // activate endpoint 0
+               USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
+
+               // clear all ending interrupts
+               USB0_ERRSTAT = 0xFF;
+               USB0_ISTAT = 0xFF;
+
+               // set the address to zero during enumeration
+               USB0_ADDR = 0;
+
+               // enable other interrupts
+               USB0_ERREN = 0xFF;
+               USB0_INTEN = USB_INTEN_TOKDNEEN |
+                       USB_INTEN_SOFTOKEN |
+                       USB_INTEN_STALLEN |
+                       USB_INTEN_ERROREN |
+                       USB_INTEN_USBRSTEN |
+                       USB_INTEN_SLEEPEN;
+
+               // is this necessary?
+               USB0_CTL = USB_CTL_USBENSOFEN;
+               return;
+       }
+
+
+       if ((status & USB_ISTAT_STALL /* 80 */ )) {
+               //serial_print("stall:\n");
+               USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
+               USB0_ISTAT = USB_ISTAT_STALL;
+       }
+       if ((status & USB_ISTAT_ERROR /* 02 */ )) {
+               uint8_t err = USB0_ERRSTAT;
+               USB0_ERRSTAT = err;
+               //serial_print("err:");
+               //serial_phex(err);
+               //serial_print("\n");
+               USB0_ISTAT = USB_ISTAT_ERROR;
+       }
+
+       if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
+               //serial_print("sleep\n");
+               USB0_ISTAT = USB_ISTAT_SLEEP;
+       }
+
+}
+
+
+
+void usb_init(void)
+{
+       int i;
+
+       //serial_begin(BAUD2DIV(115200));
+       //serial_print("usb_init\n");
+
+       for (i=0; i <= NUM_ENDPOINTS*4; i++) {
+               table[i].desc = 0;
+               table[i].addr = 0;
+       }
+
+       // this basically follows the flowchart in the Kinetis
+       // Quick Reference User Guide, Rev. 1, 03/2012, page 141
+
+       // assume 48 MHz clock already running
+       // SIM - enable clock
+       SIM_SCGC4 |= SIM_SCGC4_USBOTG;
+
+       // reset USB module
+       USB0_USBTRC0 = USB_USBTRC_USBRESET;
+       while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
+
+       // set desc table base addr
+       USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
+       USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
+       USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
+
+       // clear all ISR flags
+       USB0_ISTAT = 0xFF;
+       USB0_ERRSTAT = 0xFF;
+       USB0_OTGISTAT = 0xFF;
+
+       USB0_USBTRC0 |= 0x40; // undocumented bit
+
+       // enable USB
+       USB0_CTL = USB_CTL_USBENSOFEN;
+       USB0_USBCTRL = 0;
+
+       // enable reset interrupt
+       USB0_INTEN = USB_INTEN_USBRSTEN;
+
+       // enable interrupt in NVIC...
+       NVIC_ENABLE_IRQ(IRQ_USBOTG);
+
+       // enable d+ pullup
+       USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
+}
+
+// return 0 if the USB is not configured, or the configuration
+// number selected by the HOST
+uint8_t usb_configured(void)
+{
+       return usb_configuration;
+}
+
diff --git a/Output/pjrcUSB/arm/usb_dev.h b/Output/pjrcUSB/arm/usb_dev.h
new file mode 100644 (file)
index 0000000..2ac3baa
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef _usb_dev_h_
+#define _usb_dev_h_
+
+// This header is NOT meant to be included when compiling
+// user sketches in Arduino.  The low-level functions
+// provided by usb_dev.c are meant to be called only by
+// code which provides higher-level interfaces to the user.
+
+#include "usb_mem.h"
+#include "usb_desc.h"
+
+void usb_init(void);
+uint8_t usb_configured(void);          // is the USB port configured
+void usb_isr(void);
+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_packet_count(uint32_t endpoint);
+void usb_tx(uint32_t endpoint, usb_packet_t *packet);
+void usb_tx_isr(uint32_t endpoint, usb_packet_t *packet);
+
+extern volatile uint8_t usb_configuration;
+
+#ifdef CDC_DATA_INTERFACE
+extern uint8_t usb_cdc_line_coding[7];
+extern volatile uint8_t usb_cdc_line_rtsdtr;
+extern volatile uint8_t usb_cdc_transmit_flush_timer;
+extern void usb_serial_flush_callback(void);
+#endif
+
+
+#endif
+
diff --git a/Output/pjrcUSB/arm/usb_keyboard.c b/Output/pjrcUSB/arm/usb_keyboard.c
new file mode 100644 (file)
index 0000000..ffe2293
--- /dev/null
@@ -0,0 +1,52 @@
+#include "usb_dev.h"
+#include "usb_keyboard.h"
+#include <Lib/USBLib.h>
+#include <string.h> // for memcpy()
+
+
+// Maximum number of transmit packets to queue so we don't starve other endpoints for memory
+#define TX_PACKET_LIMIT 4
+
+static uint8_t transmit_previous_timeout=0;
+
+// When the PC isn't listening, how long do we wait before discarding data?
+#define TX_TIMEOUT_MSEC 50
+
+#if F_CPU == 96000000
+  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
+#elif F_CPU == 48000000
+  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
+#elif F_CPU == 24000000
+  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
+#endif
+
+
+// send the contents of keyboard_keys and keyboard_modifier_keys
+uint8_t usb_keyboard_send(void)
+{
+       uint32_t wait_count=0;
+       usb_packet_t *tx_packet;
+
+       while (1) {
+               if (!usb_configuration) {
+                       return -1;
+               }
+               if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
+                       tx_packet = usb_malloc();
+                       if (tx_packet) break;
+               }
+               if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
+                       transmit_previous_timeout = 1;
+                       return -1;
+               }
+               yield();
+       }
+       *(tx_packet->buf) = USBKeys_Modifiers;
+       *(tx_packet->buf + 1) = 0;
+       memcpy(tx_packet->buf + 2, USBKeys_Array, USB_MAX_KEY_SEND);
+       tx_packet->len = 8;
+       usb_tx(KEYBOARD_ENDPOINT, tx_packet);
+
+       return 0;
+}
+
diff --git a/Output/pjrcUSB/arm/usb_keyboard.h b/Output/pjrcUSB/arm/usb_keyboard.h
new file mode 100644 (file)
index 0000000..49aa84d
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef USBkeyboard_h_
+#define USBkeyboard_h_
+
+#include <inttypes.h>
+#include "usb_com.h"
+
+uint8_t usb_keyboard_send(void);
+
+#endif // USBkeyboard_h_
+
diff --git a/Output/pjrcUSB/arm/usb_mem.c b/Output/pjrcUSB/arm/usb_mem.c
new file mode 100644 (file)
index 0000000..47956f6
--- /dev/null
@@ -0,0 +1,78 @@
+#include <Lib/USBLib.h>
+#include "usb_dev.h"
+#include "usb_mem.h"
+
+#define NUM_BUF 30
+
+__attribute__ ((section(".usbbuffers"), used))
+//static unsigned char usb_buffer_memory[NUM_BUF * sizeof(usb_packet_t)];
+unsigned char usb_buffer_memory[NUM_BUF * sizeof(usb_packet_t)];
+
+static uint32_t usb_buffer_available = 0xFFFFFFFF;
+
+// use bitmask and CLZ instruction to implement fast free list
+// http://www.archivum.info/gnu.gcc.help/2006-08/00148/Re-GCC-Inline-Assembly.html
+// http://gcc.gnu.org/ml/gcc/2012-06/msg00015.html
+// __builtin_clz()
+
+usb_packet_t * usb_malloc(void)
+{
+       unsigned int n, avail;
+       uint8_t *p;
+
+       __disable_irq();
+       avail = usb_buffer_available;
+       n = __builtin_clz(avail); // clz = count leading zeros
+       if (n >= NUM_BUF) {
+               __enable_irq();
+               return NULL;
+       }
+       //serial_print("malloc:");
+       //serial_phex(n);
+       //serial_print("\n");
+
+       usb_buffer_available = avail & ~(0x80000000 >> n);
+       __enable_irq();
+       p = usb_buffer_memory + (n * sizeof(usb_packet_t));
+       //serial_print("malloc:");
+       //serial_phex32((int)p);
+       //serial_print("\n");
+       *(uint32_t *)p = 0;
+       *(uint32_t *)(p + 4) = 0;
+       return (usb_packet_t *)p;
+}
+
+// for the receive endpoints to request memory
+extern uint8_t usb_rx_memory_needed;
+extern void usb_rx_memory(usb_packet_t *packet);
+
+void usb_free(usb_packet_t *p)
+{
+       unsigned int n, mask;
+
+       //serial_print("free:");
+       n = ((uint8_t *)p - usb_buffer_memory) / sizeof(usb_packet_t);
+       if (n >= NUM_BUF) return;
+       //serial_phex(n);
+       //serial_print("\n");
+
+       // if any endpoints are starving for memory to receive
+       // packets, give this memory to them immediately!
+       if (usb_rx_memory_needed && usb_configuration) {
+               //serial_print("give to rx:");
+               //serial_phex32((int)p);
+               //serial_print("\n");
+               usb_rx_memory(p);
+               return;
+       }
+
+       mask = (0x80000000 >> n);
+       __disable_irq();
+       usb_buffer_available |= mask;
+       __enable_irq();
+
+       //serial_print("free:");
+       //serial_phex32((int)p);
+       //serial_print("\n");
+}
+
diff --git a/Output/pjrcUSB/arm/usb_mem.h b/Output/pjrcUSB/arm/usb_mem.h
new file mode 100644 (file)
index 0000000..c3f1f8d
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef _usb_mem_h_
+#define _usb_mem_h_
+
+#include <stdint.h>
+
+typedef struct usb_packet_struct {
+       uint16_t len;
+       uint16_t index;
+       struct usb_packet_struct *next;
+       uint8_t buf[64];
+} usb_packet_t;
+
+usb_packet_t * usb_malloc(void);
+void usb_free(usb_packet_t *p);
+
+
+
+
+#endif
diff --git a/Output/pjrcUSB/arm/usb_serial.c b/Output/pjrcUSB/arm/usb_serial.c
new file mode 100644 (file)
index 0000000..a674488
--- /dev/null
@@ -0,0 +1,241 @@
+#include "usb_dev.h"
+#include "usb_serial.h"
+#include <Lib/USBLib.h>
+
+// defined by usb_dev.h -> usb_desc.h
+#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
+
+uint8_t usb_cdc_line_coding[7];
+volatile uint8_t usb_cdc_line_rtsdtr=0;
+volatile uint8_t usb_cdc_transmit_flush_timer=0;
+
+static usb_packet_t *rx_packet=NULL;
+static usb_packet_t *tx_packet=NULL;
+static volatile uint8_t tx_noautoflush=0;
+
+#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
+int usb_serial_getchar(void)
+{
+       unsigned int i;
+       int c;
+
+       usb_serial_receive();
+       if (!rx_packet) return -1;
+       i = rx_packet->index;
+       c = rx_packet->buf[i++];
+       if (i >= rx_packet->len) {
+               usb_free(rx_packet);
+               rx_packet = NULL;
+       } else {
+               rx_packet->index = i;
+       }
+       return c;
+}
+
+// peek at the next character, or -1 if nothing received
+int usb_serial_peekchar(void)
+{
+       usb_serial_receive();
+       if (!rx_packet) return -1;
+       return rx_packet->buf[rx_packet->index];
+}
+
+// number of bytes available in the receive buffer
+int usb_serial_available(void)
+{
+       int count=0;
+
+       if (usb_configuration) {
+               count = usb_rx_byte_count(CDC_RX_ENDPOINT);
+       }
+       if (rx_packet) count += rx_packet->len - rx_packet->index;
+       return count;
+}
+
+// discard any buffered input
+void usb_serial_flush_input(void)
+{
+       usb_packet_t *rx;
+
+       if (!usb_configuration) return;
+       if (rx_packet) {
+               usb_free(rx_packet);
+               rx_packet = NULL;
+       }
+       while (1) {
+               rx = usb_rx(CDC_RX_ENDPOINT);
+               if (!rx) break;
+               usb_free(rx);
+       }
+}
+
+// Maximum number of transmit packets to queue so we don't starve other endpoints for memory
+#define TX_PACKET_LIMIT 8
+
+// When the PC isn't listening, how long do we wait before discarding data?  If this is
+// too short, we risk losing data during the stalls that are common with ordinary desktop
+// software.  If it's too long, we stall the user's program when no software is running.
+#define TX_TIMEOUT_MSEC 70
+
+#if F_CPU == 96000000
+  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
+#elif F_CPU == 48000000
+  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
+#elif F_CPU == 24000000
+  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
+#endif
+
+// When we've suffered the transmit timeout, don't wait again until the computer
+// begins accepting data.  If no software is running to receive, we'll just discard
+// data as rapidly as Serial.print() can generate it, until there's something to
+// actually receive it.
+static uint8_t transmit_previous_timeout=0;
+
+
+// transmit a character.  0 returned on success, -1 on error
+int usb_serial_putchar(uint8_t c)
+{
+#if 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)
+{
+#if 1
+       uint32_t len;
+       uint32_t wait_count;
+       const uint8_t *src = (const uint8_t *)buffer;
+       uint8_t *dest;
+
+       tx_noautoflush = 1;
+       while (size > 0) {
+               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;
+                               }
+                               yield();
+                       }
+               }
+               transmit_previous_timeout = 0;
+               len = CDC_TX_SIZE - tx_packet->index;
+               if (len > size) len = size;
+               dest = tx_packet->buf + tx_packet->index;
+               tx_packet->index += len;
+               size -= len;
+               while (len-- > 0) *dest++ = *src++;
+               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
+#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)
+{
+       if (!usb_configuration) return;
+       //serial_print("usb_serial_flush_output\n");
+       if (tx_packet && tx_packet->index > 0) {
+               usb_cdc_transmit_flush_timer = 0;
+               tx_packet->len = tx_packet->index;
+               usb_tx(CDC_TX_ENDPOINT, tx_packet);
+               tx_packet = NULL;
+       }
+       // while (usb_tx_byte_count(CDC_TX_ENDPOINT) > 0) ; // wait
+}
+
+void usb_serial_flush_callback(void)
+{
+       if (tx_noautoflush) return;
+       //serial_print("usb_flush_callback \n");
+       tx_packet->len = tx_packet->index;
+       usb_tx(CDC_TX_ENDPOINT, tx_packet);
+       tx_packet = NULL;
+       //serial_print("usb_flush_callback end\n");
+}
+
+#endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE
+
diff --git a/Output/pjrcUSB/arm/usb_serial.h b/Output/pjrcUSB/arm/usb_serial.h
new file mode 100644 (file)
index 0000000..4dbfa05
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef USBserial_h_
+#define USBserial_h_
+
+#include <inttypes.h>
+
+// Compatibility defines from AVR
+#define PROGMEM
+#define PGM_P  const char *
+#define PSTR(str) (str)
+
+
+int usb_serial_getchar(void);
+int usb_serial_peekchar(void);
+int usb_serial_available(void);
+void usb_serial_flush_input(void);
+int usb_serial_putchar(uint8_t c);
+int usb_serial_write(const void *buffer, uint32_t size);
+void usb_serial_flush_output(void);
+extern uint8_t usb_cdc_line_coding[7];
+extern volatile uint8_t usb_cdc_line_rtsdtr;
+extern volatile uint8_t usb_cdc_transmit_flush_timer;
+extern volatile uint8_t usb_configuration;
+
+#endif // USBserial_h_
+
diff --git a/Output/pjrcUSB/avr/usb_keyboard_debug.c b/Output/pjrcUSB/avr/usb_keyboard_debug.c
new file mode 100644 (file)
index 0000000..3a792cf
--- /dev/null
@@ -0,0 +1,705 @@
+/* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
+ * http://www.pjrc.com/teensy/usb_keyboard.html
+ * Copyright (c) 2009 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:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * 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.
+ */
+
+// Version 1.0: Initial Release
+// Version 1.1: Add support for Teensy 2.0
+
+#define USB_SERIAL_PRIVATE_INCLUDE
+#include "usb_keyboard_debug.h"
+
+/**************************************************************************
+ *
+ *  Configurable Options
+ *
+ **************************************************************************/
+
+// USB devices are supposed to implment a halt feature, which is
+// rarely (if ever) used.  If you comment this line out, the halt
+// code will be removed, saving 102 bytes of space (gcc 4.3.0).
+// This is not strictly USB compliant, but works with all major
+// operating systems.
+#define SUPPORT_ENDPOINT_HALT
+
+
+
+/**************************************************************************
+ *
+ *  Endpoint Buffer Configuration
+ *
+ **************************************************************************/
+
+#define ENDPOINT0_SIZE         32
+
+#define KEYBOARD_INTERFACE     0
+#define KEYBOARD_ENDPOINT      3
+#define KEYBOARD_SIZE          8
+#define KEYBOARD_BUFFER                EP_DOUBLE_BUFFER
+
+#define DEBUG_INTERFACE                1
+#define DEBUG_TX_ENDPOINT      4
+#define DEBUG_TX_SIZE          32
+#define DEBUG_TX_BUFFER                EP_DOUBLE_BUFFER
+
+static const uint8_t PROGMEM endpoint_config_table[] = {
+       0,
+       0,
+       1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
+       1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER
+};
+
+
+/**************************************************************************
+ *
+ *  Descriptor Data
+ *
+ **************************************************************************/
+
+// Descriptors are the data that your computer reads when it auto-detects
+// this USB device (called "enumeration" in USB lingo).  The most commonly
+// changed items are editable at the top of this file.  Changing things
+// in here should only be done by those who've read chapter 9 of the USB
+// spec and relevant portions of any USB class specifications!
+
+
+static const uint8_t PROGMEM device_descriptor[] = {
+       18,                                     // bLength
+       1,                                      // bDescriptorType
+       0x00, 0x02,                             // bcdUSB
+       0,                                      // bDeviceClass
+       0,                                      // bDeviceSubClass
+       0,                                      // bDeviceProtocol
+       ENDPOINT0_SIZE,                         // bMaxPacketSize0
+       LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
+       LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
+       0x00, 0x01,                             // bcdDevice
+       1,                                      // iManufacturer
+       2,                                      // iProduct
+       3,                                      // iSerialNumber
+       1                                       // bNumConfigurations
+};
+
+// Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
+static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
+        0x05, 0x01,          // Usage Page (Generic Desktop),
+        0x09, 0x06,          // Usage (Keyboard),
+        0xA1, 0x01,          // Collection (Application),
+        0x75, 0x01,          //   Report Size (1),
+        0x95, 0x08,          //   Report Count (8),
+        0x05, 0x07,          //   Usage Page (Key Codes),
+        0x19, 0xE0,          //   Usage Minimum (224),
+        0x29, 0xE7,          //   Usage Maximum (231),
+        0x15, 0x00,          //   Logical Minimum (0),
+        0x25, 0x01,          //   Logical Maximum (1),
+        0x81, 0x02,          //   Input (Data, Variable, Absolute), ;Modifier byte
+        0x95, 0x01,          //   Report Count (1),
+        0x75, 0x08,          //   Report Size (8),
+        0x81, 0x03,          //   Input (Constant),                 ;Reserved byte
+        0x95, 0x05,          //   Report Count (5),
+        0x75, 0x01,          //   Report Size (1),
+        0x05, 0x08,          //   Usage Page (LEDs),
+        0x19, 0x01,          //   Usage Minimum (1),
+        0x29, 0x05,          //   Usage Maximum (5),
+        0x91, 0x02,          //   Output (Data, Variable, Absolute), ;LED report
+        0x95, 0x01,          //   Report Count (1),
+        0x75, 0x03,          //   Report Size (3),
+        0x91, 0x03,          //   Output (Constant),                 ;LED report padding
+        0x95, 0x06,          //   Report Count (6),
+        0x75, 0x08,          //   Report Size (8),
+        0x15, 0x00,          //   Logical Minimum (0),
+        0x25, 0x68,          //   Logical Maximum(104),
+        0x05, 0x07,          //   Usage Page (Key Codes),
+        0x19, 0x00,          //   Usage Minimum (0),
+        0x29, 0x68,          //   Usage Maximum (104),
+        0x81, 0x00,          //   Input (Data, Array),
+        0xc0                 // End Collection
+};
+
+static const uint8_t PROGMEM debug_hid_report_desc[] = {
+       //0x06, 0x30, 0xFF,                     // Usage Page 0xFF31 (vendor defined)
+       0x06, 0x31, 0xFF,                       // Usage Page 0xFF31 (vendor defined)
+       0x09, 0x74,                             // Usage 0x74
+       0xA1, 0x53,                             // Collection 0x53
+       0x75, 0x08,                             // report size = 8 bits
+       0x15, 0x00,                             // logical minimum = 0
+       0x26, 0xFF, 0x00,                       // logical maximum = 255
+       0x95, DEBUG_TX_SIZE,                    // report count
+       0x09, 0x75,                             // usage
+       0x81, 0x02,                             // Input (array)
+       0xC0                                    // end collection
+};
+
+#define CONFIG1_DESC_SIZE        (9+9+9+7+9+9+7)
+#define KEYBOARD_HID_DESC_OFFSET (9+9)
+#define DEBUG_HID_DESC_OFFSET    (9+9+9+7+9)
+static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
+       // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
+       9,                                      // bLength;
+       2,                                      // bDescriptorType;
+       LSB(CONFIG1_DESC_SIZE),                 // wTotalLength
+       MSB(CONFIG1_DESC_SIZE),
+       2,                                      // bNumInterfaces
+       1,                                      // bConfigurationValue
+       0,                                      // iConfiguration
+       0xC0,                                   // bmAttributes
+       50,                                     // bMaxPower
+       // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
+       9,                                      // bLength
+       4,                                      // bDescriptorType
+       KEYBOARD_INTERFACE,                     // bInterfaceNumber
+       0,                                      // bAlternateSetting
+       1,                                      // bNumEndpoints
+       0x03,                                   // bInterfaceClass (0x03 = HID)
+       0x01,                                   // bInterfaceSubClass (0x01 = Boot)
+       0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
+       0,                                      // iInterface
+       // HID interface descriptor, HID 1.11 spec, section 6.2.1
+       9,                                      // bLength
+       0x21,                                   // bDescriptorType
+       0x11, 0x01,                             // bcdHID
+       0,                                      // bCountryCode
+       1,                                      // bNumDescriptors
+       0x22,                                   // bDescriptorType
+       sizeof(keyboard_hid_report_desc),       // wDescriptorLength
+       0,
+       // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+       7,                                      // bLength
+       5,                                      // bDescriptorType
+       KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
+       0x03,                                   // bmAttributes (0x03=intr)
+       KEYBOARD_SIZE, 0,                       // wMaxPacketSize
+       1,                                      // bInterval
+       // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
+       9,                                      // bLength
+       4,                                      // bDescriptorType
+       DEBUG_INTERFACE,                        // bInterfaceNumber
+       0,                                      // bAlternateSetting
+       1,                                      // bNumEndpoints
+       0x03,                                   // bInterfaceClass (0x03 = HID)
+       0x00,                                   // bInterfaceSubClass
+       0x00,                                   // bInterfaceProtocol
+       0,                                      // iInterface
+       // HID interface descriptor, HID 1.11 spec, section 6.2.1
+       9,                                      // bLength
+       0x21,                                   // bDescriptorType
+       0x11, 0x01,                             // bcdHID
+       0,                                      // bCountryCode
+       1,                                      // bNumDescriptors
+       0x22,                                   // bDescriptorType
+       sizeof(debug_hid_report_desc),          // wDescriptorLength
+       0,
+       // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+       7,                                      // bLength
+       5,                                      // bDescriptorType
+       DEBUG_TX_ENDPOINT | 0x80,               // bEndpointAddress
+       0x03,                                   // bmAttributes (0x03=intr)
+       DEBUG_TX_SIZE, 0,                       // wMaxPacketSize
+       1                                       // bInterval
+};
+
+// If you're desperate for a little extra code memory, these strings
+// can be completely removed if iManufacturer, iProduct, iSerialNumber
+// in the device desciptor are changed to zeros.
+struct usb_string_descriptor_struct {
+       uint8_t bLength;
+       uint8_t bDescriptorType;
+       int16_t wString[];
+};
+static const struct usb_string_descriptor_struct PROGMEM string0 = {
+       4,
+       3,
+       {0x0409}
+};
+static const struct usb_string_descriptor_struct PROGMEM string1 = {
+       sizeof(STR_MANUFACTURER),
+       3,
+       STR_MANUFACTURER
+};
+static const struct usb_string_descriptor_struct PROGMEM string2 = {
+       sizeof(STR_PRODUCT),
+       3,
+       STR_PRODUCT
+};
+static const struct usb_string_descriptor_struct PROGMEM string3 = {
+       sizeof(STR_SERIAL),
+       3,
+       STR_SERIAL
+};
+
+// This table defines which descriptor data is sent for each specific
+// request from the host (in wValue and wIndex).
+static const struct descriptor_list_struct {
+       uint16_t        wValue;
+       uint16_t        wIndex;
+       const uint8_t   *addr;
+       uint8_t         length;
+} PROGMEM descriptor_list[] = {
+       {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
+       {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
+       {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
+       {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
+       {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
+       {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
+       {0x0300, 0x0000, (const uint8_t *)&string0, 4},
+       {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
+       {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
+       {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
+};
+#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
+
+
+/**************************************************************************
+ *
+ *  Variables - these are the only non-stack RAM usage
+ *
+ **************************************************************************/
+
+// zero when we are not configured, non-zero when enumerated
+static volatile uint8_t usb_configuration=0;
+
+// the time remaining before we transmit any partially full
+// packet, or send a zero length packet.
+static volatile uint8_t debug_flush_timer=0;
+
+
+/**************************************************************************
+ *
+ *  Public Functions - these are the API intended for the user
+ *
+ **************************************************************************/
+
+
+// initialize USB
+void usb_init(void)
+{
+       HW_CONFIG();
+       USB_FREEZE();                           // enable USB
+       PLL_CONFIG();                           // config PLL
+        while (!(PLLCSR & (1<<PLOCK))) ;       // wait for PLL lock
+        USB_CONFIG();                          // start USB clock
+        UDCON = 0;                             // enable attach resistor
+       usb_configuration = 0;
+        UDIEN = (1<<EORSTE)|(1<<SOFE);
+       sei();
+}
+
+// return 0 if the USB is not configured, or the configuration
+// number selected by the HOST
+uint8_t usb_configured(void)
+{
+       return usb_configuration;
+}
+
+// send the contents of USBKeys_Array and USBKeys_Modifiers
+int8_t usb_keyboard_send(void)
+{
+       uint8_t i, intr_state, timeout;
+
+       if (!usb_configuration) return -1;
+       intr_state = SREG;
+       cli();
+       UENUM = KEYBOARD_ENDPOINT;
+       timeout = UDFNUML + 50;
+       while (1) {
+               // are we ready to transmit?
+               if (UEINTX & (1<<RWAL)) break;
+               SREG = intr_state;
+               // has the USB gone offline?
+               if (!usb_configuration) return -1;
+               // have we waited too long?
+               if (UDFNUML == timeout) return -1;
+               // get ready to try checking again
+               intr_state = SREG;
+               cli();
+               UENUM = KEYBOARD_ENDPOINT;
+       }
+       UEDATX = USBKeys_Modifiers;
+       UEDATX = 0;
+       for (i=0; i<6; i++) {
+               UEDATX = USBKeys_Array[i];
+       }
+       UEINTX = 0x3A;
+       USBKeys_Idle_Count = 0;
+       SREG = intr_state;
+       return 0;
+}
+
+// transmit a character.  0 returned on success, -1 on error
+int8_t usb_debug_putchar(uint8_t c)
+{
+       static uint8_t previous_timeout=0;
+       uint8_t timeout, intr_state;
+
+       // if we're not online (enumerated and configured), error
+       if (!usb_configuration) return -1;
+       // interrupts are disabled so these functions can be
+       // used from the main program or interrupt context,
+       // even both in the same program!
+       intr_state = SREG;
+       cli();
+       UENUM = DEBUG_TX_ENDPOINT;
+       // if we gave up due to timeout before, don't wait again
+       if (previous_timeout) {
+               if (!(UEINTX & (1<<RWAL))) {
+                       SREG = intr_state;
+                       return -1;
+               }
+               previous_timeout = 0;
+       }
+       // wait for the FIFO to be ready to accept data
+       timeout = UDFNUML + 4;
+       while (1) {
+               // are we ready to transmit?
+               if (UEINTX & (1<<RWAL)) break;
+               SREG = intr_state;
+               // have we waited too long?
+               if (UDFNUML == timeout) {
+                       previous_timeout = 1;
+                       return -1;
+               }
+               // has the USB gone offline?
+               if (!usb_configuration) return -1;
+               // get ready to try checking again
+               intr_state = SREG;
+               cli();
+               UENUM = DEBUG_TX_ENDPOINT;
+       }
+       // actually write the byte into the FIFO
+       UEDATX = c;
+       // if this completed a packet, transmit it now!
+       if (!(UEINTX & (1<<RWAL))) {
+               UEINTX = 0x3A;
+               debug_flush_timer = 0;
+       } else {
+               debug_flush_timer = 2;
+       }
+       SREG = intr_state;
+       return 0;
+}
+
+
+// immediately transmit any buffered output.
+void usb_debug_flush_output(void)
+{
+       uint8_t intr_state;
+
+       intr_state = SREG;
+       cli();
+       if (debug_flush_timer) {
+               UENUM = DEBUG_TX_ENDPOINT;
+               while ((UEINTX & (1<<RWAL))) {
+                       UEDATX = 0;
+               }
+               UEINTX = 0x3A;
+               debug_flush_timer = 0;
+       }
+       SREG = intr_state;
+}
+
+
+
+/**************************************************************************
+ *
+ *  Private Functions - not intended for general user consumption....
+ *
+ **************************************************************************/
+
+
+
+// USB Device Interrupt - handle all device-level events
+// the transmit buffer flushing is triggered by the start of frame
+//
+ISR(USB_GEN_vect)
+{
+       uint8_t intbits, t, i;
+       static uint8_t div4=0;
+
+        intbits = UDINT;
+        UDINT = 0;
+        if (intbits & (1<<EORSTI)) {
+               UENUM = 0;
+               UECONX = 1;
+               UECFG0X = EP_TYPE_CONTROL;
+               UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
+               UEIENX = (1<<RXSTPE);
+               usb_configuration = 0;
+        }
+       if ((intbits & (1<<SOFI)) && usb_configuration) {
+               t = debug_flush_timer;
+               if (t) {
+                       debug_flush_timer = -- t;
+                       if (!t) {
+                               UENUM = DEBUG_TX_ENDPOINT;
+                               while ((UEINTX & (1<<RWAL))) {
+                                       UEDATX = 0;
+                               }
+                               UEINTX = 0x3A;
+                       }
+               }
+               if (USBKeys_Idle_Config && (++div4 & 3) == 0) {
+                       UENUM = KEYBOARD_ENDPOINT;
+                       if (UEINTX & (1<<RWAL)) {
+                               USBKeys_Idle_Count++;
+                               if (USBKeys_Idle_Count == USBKeys_Idle_Config) {
+                                       USBKeys_Idle_Count = 0;
+                                       UEDATX = USBKeys_Modifiers;
+                                       UEDATX = 0;
+                                       for (i=0; i<6; i++) {
+                                               UEDATX = USBKeys_Array[i];
+                                       }
+                                       UEINTX = 0x3A;
+                               }
+                       }
+               }
+       }
+}
+
+
+
+// Misc functions to wait for ready and send/receive packets
+static inline void usb_wait_in_ready(void)
+{
+       while (!(UEINTX & (1<<TXINI))) ;
+}
+static inline void usb_send_in(void)
+{
+       UEINTX = ~(1<<TXINI);
+}
+static inline void usb_wait_receive_out(void)
+{
+       while (!(UEINTX & (1<<RXOUTI))) ;
+}
+static inline void usb_ack_out(void)
+{
+       UEINTX = ~(1<<RXOUTI);
+}
+
+
+
+// USB Endpoint Interrupt - endpoint 0 is handled here.  The
+// other endpoints are manipulated by the user-callable
+// functions, and the start-of-frame interrupt.
+//
+ISR(USB_COM_vect)
+{
+        uint8_t intbits;
+       const uint8_t *list;
+        const uint8_t *cfg;
+       uint8_t i, n, len, en;
+       uint8_t bmRequestType;
+       uint8_t bRequest;
+       uint16_t wValue;
+       uint16_t wIndex;
+       uint16_t wLength;
+       uint16_t desc_val;
+       const uint8_t *desc_addr;
+       uint8_t desc_length;
+
+        UENUM = 0;
+       intbits = UEINTX;
+        if (intbits & (1<<RXSTPI)) {
+                bmRequestType = UEDATX;
+                bRequest = UEDATX;
+                wValue = UEDATX;
+                wValue |= (UEDATX << 8);
+                wIndex = UEDATX;
+                wIndex |= (UEDATX << 8);
+                wLength = UEDATX;
+                wLength |= (UEDATX << 8);
+                UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
+                if (bRequest == GET_DESCRIPTOR) {
+                       list = (const uint8_t *)descriptor_list;
+                       for (i=0; ; i++) {
+                               if (i >= NUM_DESC_LIST) {
+                                       UECONX = (1<<STALLRQ)|(1<<EPEN);  //stall
+                                       return;
+                               }
+                               desc_val = pgm_read_word(list);
+                               if (desc_val != wValue) {
+                                       list += sizeof(struct descriptor_list_struct);
+                                       continue;
+                               }
+                               list += 2;
+                               desc_val = pgm_read_word(list);
+                               if (desc_val != wIndex) {
+                                       list += sizeof(struct descriptor_list_struct)-2;
+                                       continue;
+                               }
+                               list += 2;
+                               desc_addr = (const uint8_t *)pgm_read_word(list);
+                               list += 2;
+                               desc_length = pgm_read_byte(list);
+                               break;
+                       }
+                       len = (wLength < 256) ? wLength : 255;
+                       if (len > desc_length) len = desc_length;
+                       do {
+                               // wait for host ready for IN packet
+                               do {
+                                       i = UEINTX;
+                               } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
+                               if (i & (1<<RXOUTI)) return;    // abort
+                               // send IN packet
+                               n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
+                               for (i = n; i; i--) {
+                                       UEDATX = pgm_read_byte(desc_addr++);
+                               }
+                               len -= n;
+                               usb_send_in();
+                       } while (len || n == ENDPOINT0_SIZE);
+                       return;
+                }
+               if (bRequest == SET_ADDRESS) {
+                       usb_send_in();
+                       usb_wait_in_ready();
+                       UDADDR = wValue | (1<<ADDEN);
+                       return;
+               }
+               if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
+                       usb_configuration = wValue;
+                       usb_send_in();
+                       cfg = endpoint_config_table;
+                       for (i=1; i<5; i++) {
+                               UENUM = i;
+                               en = pgm_read_byte(cfg++);
+                               UECONX = en;
+                               if (en) {
+                                       UECFG0X = pgm_read_byte(cfg++);
+                                       UECFG1X = pgm_read_byte(cfg++);
+                               }
+                       }
+                       UERST = 0x1E;
+                       UERST = 0;
+                       return;
+               }
+               if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
+                       usb_wait_in_ready();
+                       UEDATX = usb_configuration;
+                       usb_send_in();
+                       return;
+               }
+
+               if (bRequest == GET_STATUS) {
+                       usb_wait_in_ready();
+                       i = 0;
+                       #ifdef SUPPORT_ENDPOINT_HALT
+                       if (bmRequestType == 0x82) {
+                               UENUM = wIndex;
+                               if (UECONX & (1<<STALLRQ)) i = 1;
+                               UENUM = 0;
+                       }
+                       #endif
+                       UEDATX = i;
+                       UEDATX = 0;
+                       usb_send_in();
+                       return;
+               }
+               #ifdef SUPPORT_ENDPOINT_HALT
+               if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
+                 && bmRequestType == 0x02 && wValue == 0) {
+                       i = wIndex & 0x7F;
+                       if (i >= 1 && i <= MAX_ENDPOINT) {
+                               usb_send_in();
+                               UENUM = i;
+                               if (bRequest == SET_FEATURE) {
+                                       UECONX = (1<<STALLRQ)|(1<<EPEN);
+                               } else {
+                                       UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
+                                       UERST = (1 << i);
+                                       UERST = 0;
+                               }
+                               return;
+                       }
+               }
+               #endif
+               if (wIndex == KEYBOARD_INTERFACE) {
+                       if (bmRequestType == 0xA1) {
+                               if (bRequest == HID_GET_REPORT) {
+                                       usb_wait_in_ready();
+                                       UEDATX = USBKeys_Modifiers;
+                                       UEDATX = 0;
+                                       for (i=0; i<6; i++) {
+                                               UEDATX = USBKeys_Array[i];
+                                       }
+                                       usb_send_in();
+                                       return;
+                               }
+                               if (bRequest == HID_GET_IDLE) {
+                                       usb_wait_in_ready();
+                                       UEDATX = USBKeys_Idle_Config;
+                                       usb_send_in();
+                                       return;
+                               }
+                               if (bRequest == HID_GET_PROTOCOL) {
+                                       usb_wait_in_ready();
+                                       UEDATX = USBKeys_Protocol;
+                                       usb_send_in();
+                                       return;
+                               }
+                       }
+                       if (bmRequestType == 0x21) {
+                               if (bRequest == HID_SET_REPORT) {
+                                       usb_wait_receive_out();
+                                       USBKeys_LEDs = UEDATX;
+                                       usb_ack_out();
+                                       usb_send_in();
+                                       return;
+                               }
+                               if (bRequest == HID_SET_IDLE) {
+                                       USBKeys_Idle_Config = (wValue >> 8);
+                                       USBKeys_Idle_Count = 0;
+                                       //usb_wait_in_ready();
+                                       usb_send_in();
+                                       return;
+                               }
+                               if (bRequest == HID_SET_PROTOCOL) {
+                                       USBKeys_Protocol = wValue;
+                                       //usb_wait_in_ready();
+                                       usb_send_in();
+                                       return;
+                               }
+                       }
+               }
+               if (wIndex == DEBUG_INTERFACE) {
+                       if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
+                               len = wLength;
+                               do {
+                                       // wait for host ready for IN packet
+                                       do {
+                                               i = UEINTX;
+                                       } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
+                                       if (i & (1<<RXOUTI)) return;    // abort
+                                       // send IN packet
+                                       n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
+                                       for (i = n; i; i--) {
+                                               UEDATX = 0;
+                                       }
+                                       len -= n;
+                                       usb_send_in();
+                               } while (len || n == ENDPOINT0_SIZE);
+                               return;
+                       }
+               }
+       }
+       UECONX = (1<<STALLRQ) | (1<<EPEN);      // stall
+}
+
diff --git a/Output/pjrcUSB/avr/usb_keyboard_debug.h b/Output/pjrcUSB/avr/usb_keyboard_debug.h
new file mode 100644 (file)
index 0000000..987e4fd
--- /dev/null
@@ -0,0 +1,90 @@
+#ifndef usb_serial_h__
+#define usb_serial_h__
+
+#include <stdint.h>
+#include "usb_com.h"
+
+void usb_init(void);                   // initialize everything
+uint8_t usb_configured(void);          // is the USB port configured
+
+int8_t usb_keyboard_send(void);
+
+
+int8_t usb_debug_putchar(uint8_t c);   // transmit a character
+void usb_debug_flush_output(void);     // immediately transmit any buffered output
+#define USB_DEBUG_HID
+
+
+// Everything below this point is only intended for usb_serial.c
+#ifdef USB_SERIAL_PRIVATE_INCLUDE
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+
+#define EP_TYPE_CONTROL                        0x00
+#define EP_TYPE_BULK_IN                        0x81
+#define EP_TYPE_BULK_OUT               0x80
+#define EP_TYPE_INTERRUPT_IN           0xC1
+#define EP_TYPE_INTERRUPT_OUT          0xC0
+#define EP_TYPE_ISOCHRONOUS_IN         0x41
+#define EP_TYPE_ISOCHRONOUS_OUT                0x40
+
+#define EP_SINGLE_BUFFER               0x02
+#define EP_DOUBLE_BUFFER               0x06
+
+#define EP_SIZE(s)     ((s) == 64 ? 0x30 :     \
+                       ((s) == 32 ? 0x20 :     \
+                       ((s) == 16 ? 0x10 :     \
+                                    0x00)))
+
+#define MAX_ENDPOINT           4
+
+#define LSB(n) (n & 255)
+#define MSB(n) ((n >> 8) & 255)
+
+#if defined(__AVR_AT90USB162__)
+#define HW_CONFIG() 
+#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
+#define USB_CONFIG() (USBCON = (1<<USBE))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#elif defined(__AVR_ATmega32U4__)
+#define HW_CONFIG() (UHWCON = 0x01)
+#define PLL_CONFIG() (PLLCSR = 0x12)
+#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#elif defined(__AVR_AT90USB646__)
+#define HW_CONFIG() (UHWCON = 0x81)
+#define PLL_CONFIG() (PLLCSR = 0x1A)
+#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#elif defined(__AVR_AT90USB1286__)
+#define HW_CONFIG() (UHWCON = 0x81)
+#define PLL_CONFIG() (PLLCSR = 0x16)
+#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#endif
+
+// standard control endpoint request types
+#define GET_STATUS                     0
+#define CLEAR_FEATURE                  1
+#define SET_FEATURE                    3
+#define SET_ADDRESS                    5
+#define GET_DESCRIPTOR                 6
+#define GET_CONFIGURATION              8
+#define SET_CONFIGURATION              9
+#define GET_INTERFACE                  10
+#define SET_INTERFACE                  11
+// HID (human interface device)
+#define HID_GET_REPORT                 1
+#define HID_GET_IDLE                   2
+#define HID_GET_PROTOCOL               3
+#define HID_SET_REPORT                 9
+#define HID_SET_IDLE                   10
+#define HID_SET_PROTOCOL               11
+// CDC (communication class device)
+#define CDC_SET_LINE_CODING            0x20
+#define CDC_GET_LINE_CODING            0x21
+#define CDC_SET_CONTROL_LINE_STATE     0x22
+#endif
+#endif
+
diff --git a/Output/pjrcUSB/output_com.c b/Output/pjrcUSB/output_com.c
new file mode 100644 (file)
index 0000000..96fb0f7
--- /dev/null
@@ -0,0 +1,107 @@
+/* Copyright (C) 2011-2013 by Jacob Alexander
+ * 
+ * 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:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * 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.
+ */
+
+// ----- Includes -----
+
+// Compiler Includes
+#include <Lib/USBLib.h>
+
+// Project Includes
+#include <scan_loop.h>
+
+// USB Includes
+#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
+#include "avr/usb_keyboard_debug.h"
+#elif defined(_mk20dx128_)
+#include "arm/usb_keyboard.h"
+#include "arm/usb_dev.h"
+#endif
+
+// Local Includes
+#include "output_com.h"
+
+
+
+// ----- Variables -----
+
+// which modifier keys are currently pressed
+// 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
+// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
+         uint8_t USBKeys_Modifiers = 0;
+
+// which keys are currently pressed, up to 6 keys may be down at once
+         uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
+
+// The number of keys sent to the usb in the array
+         uint8_t USBKeys_Sent;
+
+// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
+volatile uint8_t USBKeys_LEDs = 0;
+
+// protocol setting from the host.  We use exactly the same report
+// either way, so this variable only stores the setting since we
+// are required to be able to report which setting is in use.
+         uint8_t USBKeys_Protocol = 1;
+
+// the idle configuration, how often we send the report to the
+// host (ms * 4) even when it hasn't changed
+         uint8_t USBKeys_Idle_Config = 125;
+
+// count until idle timeout
+         uint8_t USBKeys_Idle_Count = 0;
+
+
+
+// ----- Functions -----
+
+// USB Module Setup
+inline void usb_setup(void)
+{
+       // Initialize the USB, and then wait for the host to set configuration.
+       // If the Teensy is powered without a PC connected to the USB port,
+       // this will wait forever.
+       usb_init();
+       while ( !usb_configured() ) /* wait */ ;
+
+       // Wait an extra second for the PC's operating system to load drivers
+       // and do whatever it does to actually be ready for input
+       //_delay_ms(1000); // TODO
+}
+
+
+// USB Data Send
+inline void usb_send(void)
+{
+               // TODO undo potentially old keys
+               for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
+                       USBKeys_Array[c] = 0;
+
+               // Send keypresses
+               usb_keyboard_send();
+
+               // Clear modifiers and keys
+               USBKeys_Modifiers = 0;
+               USBKeys_Sent      = 0;
+
+               // Signal Scan Module we are finishedA
+               scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
+}
+
diff --git a/Output/pjrcUSB/output_com.h b/Output/pjrcUSB/output_com.h
new file mode 100644 (file)
index 0000000..07f81f5
--- /dev/null
@@ -0,0 +1,65 @@
+/* Copyright (C) 2013 by Jacob Alexander
+ * 
+ * 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:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * 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 __output_com_h
+#define __output_com_h
+
+// ----- Includes -----
+
+// Compiler Includes
+#include <stdint.h>
+
+// Local Includes
+#include <buildvars.h> // Defines USB Parameters, partially generated by CMake
+
+
+
+// ----- Defines -----
+
+// Indicator for other modules through USBKeys_MaxSize for how capable the USB module is when sending large number of keypresses
+#define USB_MAX_KEY_SEND 6
+
+
+
+// ----- Variables -----
+
+// Variables used to communciate to the usb module
+extern                       uint8_t USBKeys_Modifiers;
+extern                       uint8_t USBKeys_Array[USB_MAX_KEY_SEND];
+extern                       uint8_t USBKeys_Sent;
+extern volatile              uint8_t USBKeys_LEDs;
+
+                static const uint8_t USBKeys_MaxSize = USB_MAX_KEY_SEND;
+
+// Misc variables (XXX Some are only properly utilized using AVR)
+extern                       uint8_t USBKeys_Protocol;
+extern                       uint8_t USBKeys_Idle_Config;
+extern                       uint8_t USBKeys_Idle_Count;
+
+
+
+// ----- Functions -----
+
+void usb_setup(void);
+void usb_send(void);
+
+#endif
+
diff --git a/Output/pjrcUSB/setup.cmake b/Output/pjrcUSB/setup.cmake
new file mode 100644 (file)
index 0000000..157dfc2
--- /dev/null
@@ -0,0 +1,49 @@
+###| CMake Kiibohd Controller USB Module |###
+#
+# Written by Jacob Alexander in 2011-2013 for the Kiibohd Controller
+#
+# Released into the Public Domain
+#
+###
+
+
+###
+# Module C files
+#
+
+
+#| AVR Compiler
+if ( ${COMPILER_FAMILY} MATCHES "avr" )
+
+       set( OUTPUT_SRCS
+               output_com.c
+               avr/usb_keyboard_debug.c
+       )
+
+#| ARM Compiler
+elseif ( ${COMPILER_FAMILY} MATCHES "arm" )
+
+       set( OUTPUT_SRCS
+               output_com.c
+               arm/usb_desc.c
+               arm/usb_dev.c
+               arm/usb_keyboard.c
+               arm/usb_mem.c
+               arm/usb_serial.c
+       )
+
+endif ( ${COMPILER_FAMILY} MATCHES "avr" )
+
+
+###
+# Module Specific Options
+#
+
+###
+# Compiler Family Compatibility
+#
+set( OutputModuleCompatibility
+       arm
+       avr
+)
+
diff --git a/USB/pjrc/arm/usb_desc.c b/USB/pjrc/arm/usb_desc.c
deleted file mode 100644 (file)
index 564c79e..0000000
+++ /dev/null
@@ -1,473 +0,0 @@
-#include "usb_desc.h"
-
-
-// USB Descriptors are binary data which the USB host reads to
-// automatically detect a USB device's capabilities.  The format
-// and meaning of every field is documented in numerous USB
-// standards.  When working with USB descriptors, despite the
-// complexity of the standards and poor writing quality in many
-// of those documents, remember descriptors are nothing more
-// than constant binary data that tells the USB host what the
-// device can do.  Computers will load drivers based on this data.
-// Those drivers then communicate on the endpoints specified by
-// the descriptors.
-
-// To configure a new combination of interfaces or make minor
-// changes to existing configuration (eg, change the name or ID
-// numbers), usually you would edit "usb_desc.h".  This file
-// is meant to be configured by the header, so generally it is
-// only edited to add completely new USB interfaces or features.
-
-
-
-// **************************************************************
-//   USB Device
-// **************************************************************
-
-#define LSB(n) ((n) & 255)
-#define MSB(n) (((n) >> 8) & 255)
-
-// USB Device Descriptor.  The USB host reads this first, to learn
-// what type of device is connected.
-static uint8_t device_descriptor[] = {
-        18,                                     // bLength
-        1,                                      // bDescriptorType
-        0x00, 0x02,                             // bcdUSB
-#ifdef DEVICE_CLASS
-        DEVICE_CLASS,                           // bDeviceClass
-#else
-       0,
-#endif
-#ifdef DEVICE_SUBCLASS
-        DEVICE_SUBCLASS,                        // bDeviceSubClass
-#else
-       0,
-#endif
-#ifdef DEVICE_PROTOCOL
-        DEVICE_PROTOCOL,                        // bDeviceProtocol
-#else
-       0,
-#endif
-        EP0_SIZE,                               // bMaxPacketSize0
-        LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
-        LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
-        0x00, 0x01,                             // bcdDevice
-        1,                                      // iManufacturer
-        2,                                      // iProduct
-        3,                                      // iSerialNumber
-        1                                       // bNumConfigurations
-};
-
-// These descriptors must NOT be "const", because the USB DMA
-// has trouble accessing flash memory with enough bandwidth
-// while the processor is executing from flash.
-
-
-
-// **************************************************************
-//   HID Report Descriptors
-// **************************************************************
-
-// Each HID interface needs a special report descriptor that tells
-// the meaning and format of the data.
-
-#ifdef KEYBOARD_INTERFACE
-// Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
-static uint8_t keyboard_report_desc[] = {
-        0x05, 0x01,             //  Usage Page (Generic Desktop),
-        0x09, 0x06,             //  Usage (Keyboard),
-        0xA1, 0x01,             //  Collection (Application),
-        0x75, 0x01,             //  Report Size (1),
-        0x95, 0x08,             //  Report Count (8),
-        0x05, 0x07,             //  Usage Page (Key Codes),
-        0x19, 0xE0,             //  Usage Minimum (224),
-        0x29, 0xE7,             //  Usage Maximum (231),
-        0x15, 0x00,             //  Logical Minimum (0),
-        0x25, 0x01,             //  Logical Maximum (1),
-        0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Modifier byte
-        0x95, 0x08,             //  Report Count (8),
-        0x75, 0x01,             //  Report Size (1),
-        0x15, 0x00,             //  Logical Minimum (0),
-        0x25, 0x01,             //  Logical Maximum (1),
-        0x05, 0x0C,             //  Usage Page (Consumer),
-        0x09, 0xE9,             //  Usage (Volume Increment),
-        0x09, 0xEA,             //  Usage (Volume Decrement),
-        0x09, 0xE2,             //  Usage (Mute),
-        0x09, 0xCD,             //  Usage (Play/Pause),
-        0x09, 0xB5,             //  Usage (Scan Next Track),
-        0x09, 0xB6,             //  Usage (Scan Previous Track),
-        0x09, 0xB7,             //  Usage (Stop),
-        0x09, 0xB8,             //  Usage (Eject),
-        0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Media keys
-        0x95, 0x05,             //  Report Count (5),
-        0x75, 0x01,             //  Report Size (1),
-        0x05, 0x08,             //  Usage Page (LEDs),
-        0x19, 0x01,             //  Usage Minimum (1),
-        0x29, 0x05,             //  Usage Maximum (5),
-        0x91, 0x02,             //  Output (Data, Variable, Absolute), ;LED report
-        0x95, 0x01,             //  Report Count (1),
-        0x75, 0x03,             //  Report Size (3),
-        0x91, 0x03,             //  Output (Constant),                 ;LED report padding
-        0x95, 0x06,             //  Report Count (6),
-        0x75, 0x08,             //  Report Size (8),
-        0x15, 0x00,             //  Logical Minimum (0),
-        0x25, 0x7F,             //  Logical Maximum(104),
-        0x05, 0x07,             //  Usage Page (Key Codes),
-        0x19, 0x00,             //  Usage Minimum (0),
-        0x29, 0x7F,             //  Usage Maximum (104),
-        0x81, 0x00,             //  Input (Data, Array),                ;Normal keys
-        0xc0                    // End Collection
-};
-#endif
-
-#ifdef MOUSE_INTERFACE
-// Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
-static uint8_t mouse_report_desc[] = {
-        0x05, 0x01,                     // Usage Page (Generic Desktop)
-        0x09, 0x02,                     // Usage (Mouse)
-        0xA1, 0x01,                     // Collection (Application)
-        0x05, 0x09,                     //   Usage Page (Button)
-        0x19, 0x01,                     //   Usage Minimum (Button #1)
-        0x29, 0x03,                     //   Usage Maximum (Button #3)
-        0x15, 0x00,                     //   Logical Minimum (0)
-        0x25, 0x01,                     //   Logical Maximum (1)
-        0x95, 0x03,                     //   Report Count (3)
-        0x75, 0x01,                     //   Report Size (1)
-        0x81, 0x02,                     //   Input (Data, Variable, Absolute)
-        0x95, 0x01,                     //   Report Count (1)
-        0x75, 0x05,                     //   Report Size (5)
-        0x81, 0x03,                     //   Input (Constant)
-        0x05, 0x01,                     //   Usage Page (Generic Desktop)
-        0x09, 0x30,                     //   Usage (X)
-        0x09, 0x31,                     //   Usage (Y)
-        0x15, 0x81,                     //   Logical Minimum (-127)
-        0x25, 0x7F,                     //   Logical Maximum (127)
-        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),
-        0x81, 0x06,                     //   Input (Data, Variable, Relative)
-        0xC0                            // End Collection
-};
-#endif
-
-
-
-// **************************************************************
-//   USB Configuration
-// **************************************************************
-
-// USB Configuration Descriptor.  This huge descriptor tells all
-// of the devices capbilities.
-static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
-        // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
-        9,                                      // bLength;
-        2,                                      // bDescriptorType;
-        LSB(CONFIG_DESC_SIZE),                 // wTotalLength
-        MSB(CONFIG_DESC_SIZE),
-        NUM_INTERFACE,                          // bNumInterfaces
-        1,                                      // bConfigurationValue
-        0,                                      // iConfiguration
-        0xC0,                                   // bmAttributes
-        50,                                     // bMaxPower
-
-#ifdef CDC_IAD_DESCRIPTOR
-        // interface association descriptor, USB ECN, Table 9-Z
-        8,                                      // bLength
-        11,                                     // bDescriptorType
-        CDC_STATUS_INTERFACE,                   // bFirstInterface
-        2,                                      // bInterfaceCount
-        0x02,                                   // bFunctionClass
-        0x02,                                   // bFunctionSubClass
-        0x01,                                   // bFunctionProtocol
-        4,                                      // iFunction
-#endif
-
-#ifdef CDC_DATA_INTERFACE
-        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
-        9,                                      // bLength
-        4,                                      // bDescriptorType
-        CDC_STATUS_INTERFACE,                  // bInterfaceNumber
-        0,                                      // bAlternateSetting
-        1,                                      // bNumEndpoints
-        0x02,                                   // bInterfaceClass
-        0x02,                                   // bInterfaceSubClass
-        0x01,                                   // bInterfaceProtocol
-        0,                                      // iInterface
-        // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
-        5,                                      // bFunctionLength
-        0x24,                                   // bDescriptorType
-        0x00,                                   // bDescriptorSubtype
-        0x10, 0x01,                             // bcdCDC
-        // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
-        5,                                      // bFunctionLength
-        0x24,                                   // bDescriptorType
-        0x01,                                   // bDescriptorSubtype
-        0x01,                                   // bmCapabilities
-        1,                                      // bDataInterface
-        // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
-        4,                                      // bFunctionLength
-        0x24,                                   // bDescriptorType
-        0x02,                                   // bDescriptorSubtype
-        0x06,                                   // bmCapabilities
-        // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
-        5,                                      // bFunctionLength
-        0x24,                                   // bDescriptorType
-        0x06,                                   // bDescriptorSubtype
-        CDC_STATUS_INTERFACE,                   // bMasterInterface
-        CDC_DATA_INTERFACE,                     // bSlaveInterface0
-        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
-        7,                                      // bLength
-        5,                                      // bDescriptorType
-        CDC_ACM_ENDPOINT | 0x80,                // bEndpointAddress
-        0x03,                                   // bmAttributes (0x03=intr)
-        CDC_ACM_SIZE, 0,                        // wMaxPacketSize
-        64,                                     // bInterval
-        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
-        9,                                      // bLength
-        4,                                      // bDescriptorType
-        CDC_DATA_INTERFACE,                     // bInterfaceNumber
-        0,                                      // bAlternateSetting
-        2,                                      // bNumEndpoints
-        0x0A,                                   // bInterfaceClass
-        0x00,                                   // bInterfaceSubClass
-        0x00,                                   // bInterfaceProtocol
-        0,                                      // iInterface
-        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
-        7,                                      // bLength
-        5,                                      // bDescriptorType
-        CDC_RX_ENDPOINT,                        // bEndpointAddress
-        0x02,                                   // bmAttributes (0x02=bulk)
-        CDC_RX_SIZE, 0,                         // wMaxPacketSize
-        0,                                      // bInterval
-        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
-        7,                                      // bLength
-        5,                                      // bDescriptorType
-        CDC_TX_ENDPOINT | 0x80,                 // bEndpointAddress
-        0x02,                                   // bmAttributes (0x02=bulk)
-        CDC_TX_SIZE, 0,                         // wMaxPacketSize
-        0,                                      // bInterval
-#endif // CDC_DATA_INTERFACE
-
-#ifdef KEYBOARD_INTERFACE
-        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
-        9,                                      // bLength
-        4,                                      // bDescriptorType
-        KEYBOARD_INTERFACE,                     // bInterfaceNumber
-        0,                                      // bAlternateSetting
-        1,                                      // bNumEndpoints
-        0x03,                                   // bInterfaceClass (0x03 = HID)
-        0x01,                                   // bInterfaceSubClass (0x01 = Boot)
-        0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
-        0,                                      // iInterface
-        // HID interface descriptor, HID 1.11 spec, section 6.2.1
-        9,                                      // bLength
-        0x21,                                   // bDescriptorType
-        0x11, 0x01,                             // bcdHID
-        0,                                      // bCountryCode
-        1,                                      // bNumDescriptors
-        0x22,                                   // bDescriptorType
-        LSB(sizeof(keyboard_report_desc)),      // wDescriptorLength
-        MSB(sizeof(keyboard_report_desc)),
-        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
-        7,                                      // bLength
-        5,                                      // bDescriptorType
-        KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
-        0x03,                                   // bmAttributes (0x03=intr)
-        KEYBOARD_SIZE, 0,                       // wMaxPacketSize
-        KEYBOARD_INTERVAL,                      // bInterval
-#endif // KEYBOARD_INTERFACE
-
-#ifdef MOUSE_INTERFACE
-        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
-        9,                                      // bLength
-        4,                                      // bDescriptorType
-        MOUSE_INTERFACE,                        // bInterfaceNumber
-        0,                                      // bAlternateSetting
-        1,                                      // bNumEndpoints
-        0x03,                                   // bInterfaceClass (0x03 = HID)
-        0x01,                                   // bInterfaceSubClass (0x01 = Boot)
-        0x02,                                   // bInterfaceProtocol (0x02 = Mouse)
-        0,                                      // iInterface
-        // HID interface descriptor, HID 1.11 spec, section 6.2.1
-        9,                                      // bLength
-        0x21,                                   // bDescriptorType
-        0x11, 0x01,                             // bcdHID
-        0,                                      // bCountryCode
-        1,                                      // bNumDescriptors
-        0x22,                                   // bDescriptorType
-        LSB(sizeof(mouse_report_desc)),         // wDescriptorLength
-        MSB(sizeof(mouse_report_desc)),
-        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
-        7,                                      // bLength
-        5,                                      // bDescriptorType
-        MOUSE_ENDPOINT | 0x80,                  // bEndpointAddress
-        0x03,                                   // bmAttributes (0x03=intr)
-        MOUSE_SIZE, 0,                          // wMaxPacketSize
-        MOUSE_INTERVAL,                         // bInterval
-#endif // MOUSE_INTERFACE
-};
-
-
-
-// **************************************************************
-//   String Descriptors
-// **************************************************************
-
-// The descriptors above can provide human readable strings,
-// referenced by index numbers.  These descriptors are the
-// actual string data
-
-struct usb_string_descriptor_struct {
-        uint8_t bLength;
-        uint8_t bDescriptorType;
-        uint16_t wString[];
-};
-
-static struct usb_string_descriptor_struct string0 = {
-        4,
-        3,
-        {0x0409}
-};
-
-static struct usb_string_descriptor_struct string1 = {
-        sizeof(STR_MANUFACTURER),
-        3,
-        STR_MANUFACTURER
-};
-static struct usb_string_descriptor_struct string2 = {
-       sizeof(STR_PRODUCT),
-        3,
-        STR_PRODUCT
-};
-static struct usb_string_descriptor_struct string3 = {
-       sizeof(STR_SERIAL),
-        3,
-       STR_SERIAL
-};
-
-
-// **************************************************************
-//   Descriptors List
-// **************************************************************
-
-// This table provides access to all the descriptor data above.
-
-const usb_descriptor_list_t usb_descriptor_list[] = {
-       //wValue, wIndex, address,          length
-       {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
-       {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
-#ifdef KEYBOARD_INTERFACE
-        {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
-        {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
-#endif
-#ifdef MOUSE_INTERFACE
-        {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
-        {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
-#endif
-        {0x0300, 0x0000, (const uint8_t *)&string0, 4},
-        {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
-        {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
-        {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)},
-       {0, 0, NULL, 0}
-};
-
-
-// **************************************************************
-//   Endpoint Configuration
-// **************************************************************
-
-#if 0
-// 0x00 = not used
-// 0x19 = Recieve only
-// 0x15 = Transmit only
-// 0x1D = Transmit & Recieve
-// 
-const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] = 
-{
-       0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00, 
-       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
-};
-#endif
-
-
-const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] = 
-{
-#if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
-       ENDPOINT1_CONFIG,
-#elif (NUM_ENDPOINTS >= 1)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
-       ENDPOINT2_CONFIG,
-#elif (NUM_ENDPOINTS >= 2)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
-       ENDPOINT3_CONFIG,
-#elif (NUM_ENDPOINTS >= 3)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
-       ENDPOINT4_CONFIG,
-#elif (NUM_ENDPOINTS >= 4)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
-       ENDPOINT5_CONFIG,
-#elif (NUM_ENDPOINTS >= 5)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
-       ENDPOINT6_CONFIG,
-#elif (NUM_ENDPOINTS >= 6)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
-       ENDPOINT7_CONFIG,
-#elif (NUM_ENDPOINTS >= 7)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
-       ENDPOINT8_CONFIG,
-#elif (NUM_ENDPOINTS >= 8)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
-       ENDPOINT9_CONFIG,
-#elif (NUM_ENDPOINTS >= 9)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
-       ENDPOINT10_CONFIG,
-#elif (NUM_ENDPOINTS >= 10)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
-       ENDPOINT11_CONFIG,
-#elif (NUM_ENDPOINTS >= 11)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
-       ENDPOINT12_CONFIG,
-#elif (NUM_ENDPOINTS >= 12)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
-       ENDPOINT13_CONFIG,
-#elif (NUM_ENDPOINTS >= 13)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
-       ENDPOINT14_CONFIG,
-#elif (NUM_ENDPOINTS >= 14)
-       ENDPOINT_UNUSED,
-#endif
-#if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
-       ENDPOINT15_CONFIG,
-#elif (NUM_ENDPOINTS >= 15)
-       ENDPOINT_UNUSED,
-#endif
-};
-
diff --git a/USB/pjrc/arm/usb_desc.h b/USB/pjrc/arm/usb_desc.h
deleted file mode 100644 (file)
index f4e8dfc..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-#ifndef _usb_desc_h_
-#define _usb_desc_h_
-
-// This header is NOT meant to be included when compiling
-// user sketches in Arduino.  The low-level functions
-// provided by usb_dev.c are meant to be called only by
-// code which provides higher-level interfaces to the user.
-
-#include <stdint.h>
-#include <stddef.h>
-#include "usb_com.h"
-
-#define ENDPOINT_UNUSED                        0x00
-#define ENDPOINT_TRANSIMIT_ONLY                0x15
-#define ENDPOINT_RECEIVE_ONLY          0x19
-#define ENDPOINT_TRANSMIT_AND_RECEIVE  0x1D
-
-// Some operating systems, especially Windows, may cache USB device
-// info.  Changes to the device name may not update on the same
-// computer unless the vendor or product ID numbers change, or the
-// "bcdDevice" revision code is increased.
-
-#define DEVICE_CLASS           0xEF
-#define DEVICE_SUBCLASS        0x02
-#define DEVICE_PROTOCOL        0x01
-#define EP0_SIZE               64
-#define NUM_ENDPOINTS          15
-#define NUM_INTERFACE          4
-#define CDC_IAD_DESCRIPTOR     1
-#define CDC_STATUS_INTERFACE   0
-#define CDC_DATA_INTERFACE     1       // Serial
-#define CDC_ACM_ENDPOINT       2
-#define CDC_RX_ENDPOINT       3
-#define CDC_TX_ENDPOINT       4
-#define CDC_ACM_SIZE          16
-#define CDC_RX_SIZE           64
-#define CDC_TX_SIZE           64
-#define KEYBOARD_INTERFACE    2        // Keyboard
-#define KEYBOARD_ENDPOINT     1
-#define KEYBOARD_SIZE         8
-#define KEYBOARD_INTERVAL     1
-#define MOUSE_INTERFACE       3        // Mouse
-#define MOUSE_ENDPOINT        5
-#define MOUSE_SIZE            8
-#define MOUSE_INTERVAL        2
-#define KEYBOARD_DESC_OFFSET   (9+8 + 9+5+5+4+5+7+9+7+7 + 9)
-#define MOUSE_DESC_OFFSET      (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9)
-#define CONFIG_DESC_SIZE       (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9+9+7)
-#define ENDPOINT1_CONFIG       ENDPOINT_TRANSIMIT_ONLY
-#define ENDPOINT2_CONFIG       ENDPOINT_TRANSIMIT_ONLY
-#define ENDPOINT3_CONFIG       ENDPOINT_RECEIVE_ONLY
-#define ENDPOINT4_CONFIG       ENDPOINT_TRANSIMIT_ONLY
-#define ENDPOINT5_CONFIG       ENDPOINT_TRANSIMIT_ONLY
-
-
-
-// NUM_ENDPOINTS = number of non-zero endpoints (0 to 15)
-extern const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS];
-
-typedef struct {
-       uint16_t        wValue;
-       uint16_t        wIndex;
-       const uint8_t   *addr;
-       uint16_t        length;
-} usb_descriptor_list_t;
-
-extern const usb_descriptor_list_t usb_descriptor_list[];
-
-
-#endif
-
diff --git a/USB/pjrc/arm/usb_dev.c b/USB/pjrc/arm/usb_dev.c
deleted file mode 100644 (file)
index 72dad59..0000000
+++ /dev/null
@@ -1,867 +0,0 @@
-#include <Lib/USBLib.h>
-#include "usb_dev.h"
-#include "usb_mem.h"
-
-// buffer descriptor table
-
-typedef struct {
-       uint32_t desc;
-       void * addr;
-} bdt_t;
-
-__attribute__ ((section(".usbdescriptortable"), used))
-static bdt_t table[64];
-
-#define BDT_OWN                0x80
-#define BDT_DATA1      0x40
-#define BDT_DATA0      0x00
-#define BDT_DTS                0x08
-#define BDT_STALL      0x04
-#define BDT_PID(n)     (((n) >> 2) & 15)
-
-#define BDT_DESC(count, data)  (BDT_OWN | BDT_DTS \
-                               | ((data) ? BDT_DATA1 : BDT_DATA0) \
-                               | ((count) << 16))
-
-#define TX   1
-#define RX   0
-#define ODD  1
-#define EVEN 0
-#define DATA0 0
-#define DATA1 1
-#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
-#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
-
-
-static union {
- struct {
-  union {
-   struct {
-       uint8_t bmRequestType;
-       uint8_t bRequest;
-   };
-       uint16_t wRequestAndType;
-  };
-       uint16_t wValue;
-       uint16_t wIndex;
-       uint16_t wLength;
- };
- struct {
-       uint32_t word1;
-       uint32_t word2;
- };
-} setup;
-
-
-#define GET_STATUS             0
-#define CLEAR_FEATURE          1
-#define SET_FEATURE            3
-#define SET_ADDRESS            5
-#define GET_DESCRIPTOR         6
-#define SET_DESCRIPTOR         7
-#define GET_CONFIGURATION      8
-#define SET_CONFIGURATION      9
-#define GET_INTERFACE          10
-#define SET_INTERFACE          11
-#define SYNCH_FRAME            12
-
-// SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
-// transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
-// Status stage uses a DATA1 PID.
-
-static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
-static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
-static const uint8_t *ep0_tx_ptr = NULL;
-static uint16_t ep0_tx_len;
-static uint8_t ep0_tx_bdt_bank = 0;
-static uint8_t ep0_tx_data_toggle = 0;
-uint8_t usb_rx_memory_needed = 0;
-
-volatile uint8_t usb_configuration = 0;
-volatile uint8_t usb_reboot_timer = 0;
-
-
-static void endpoint0_stall(void)
-{
-       USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
-}
-
-
-static void endpoint0_transmit(const void *data, uint32_t len)
-{
-#if 0
-       serial_print("tx0:");
-       serial_phex32((uint32_t)data);
-       serial_print(",");
-       serial_phex16(len);
-       serial_print(ep0_tx_bdt_bank ? ", odd" : ", even");
-       serial_print(ep0_tx_data_toggle ? ", d1\n" : ", d0\n");
-#endif
-       table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
-       table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
-       ep0_tx_data_toggle ^= 1;
-       ep0_tx_bdt_bank ^= 1;
-}
-
-static uint8_t reply_buffer[8];
-
-static void usbdev_setup(void)
-{
-       const uint8_t *data = NULL;
-       uint32_t datalen = 0;
-       const usb_descriptor_list_t *list;
-       uint32_t size;
-       volatile uint8_t *reg;
-       uint8_t epconf;
-       const uint8_t *cfg;
-       int i;
-
-       switch (setup.wRequestAndType) {
-         case 0x0500: // SET_ADDRESS
-               break;
-         case 0x0900: // SET_CONFIGURATION
-               //serial_print("configure\n");
-               usb_configuration = setup.wValue;
-               reg = &USB0_ENDPT1;
-               cfg = usb_endpoint_config_table;
-               // clear all BDT entries, free any allocated memory...
-               for (i=4; i <= NUM_ENDPOINTS*4; i++) {
-                       if (table[i].desc & BDT_OWN) {
-                               usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8));
-                               table[i].desc = 0;
-                       }
-               }
-               usb_rx_memory_needed = 0;
-               for (i=1; i <= NUM_ENDPOINTS; i++) {
-                       epconf = *cfg++;
-                       *reg = epconf;
-                       reg += 4;
-                       if (epconf & USB_ENDPT_EPRXEN) {
-                               usb_packet_t *p;
-                               p = usb_malloc();
-                               if (p) {
-                                       table[index(i, RX, EVEN)].addr = p->buf;
-                                       table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
-                               } else {
-                                       table[index(i, RX, EVEN)].desc = 0;
-                                       usb_rx_memory_needed++;
-                               }
-                               p = usb_malloc();
-                               if (p) {
-                                       table[index(i, RX, ODD)].addr = p->buf;
-                                       table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
-                               } else {
-                                       table[index(i, RX, ODD)].desc = 0;
-                                       usb_rx_memory_needed++;
-                               }
-                       }
-                       table[index(i, TX, EVEN)].desc = 0;
-                       table[index(i, TX, ODD)].desc = 0;
-               }
-               break;
-         case 0x0880: // GET_CONFIGURATION
-               reply_buffer[0] = usb_configuration;
-               datalen = 1;
-               data = reply_buffer;
-               break;
-         case 0x0080: // GET_STATUS (device)
-               reply_buffer[0] = 0;
-               reply_buffer[1] = 0;
-               datalen = 2;
-               data = reply_buffer;
-               break;
-         case 0x0082: // GET_STATUS (endpoint)
-               if (setup.wIndex > NUM_ENDPOINTS) {
-                       // TODO: do we need to handle IN vs OUT here?
-                       endpoint0_stall();
-                       return;
-               }
-               reply_buffer[0] = 0;
-               reply_buffer[1] = 0;
-               if (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02) reply_buffer[0] = 1;
-               data = reply_buffer;
-               datalen = 2;
-               break;
-         case 0x0102: // CLEAR_FEATURE (endpoint)
-               i = setup.wIndex & 0x7F;
-               if (i > NUM_ENDPOINTS || setup.wValue != 0) {
-                       // TODO: do we need to handle IN vs OUT here?
-                       endpoint0_stall();
-                       return;
-               }
-               (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
-               // TODO: do we need to clear the data toggle here?
-               break;
-         case 0x0302: // SET_FEATURE (endpoint)
-               i = setup.wIndex & 0x7F;
-               if (i > NUM_ENDPOINTS || setup.wValue != 0) {
-                       // TODO: do we need to handle IN vs OUT here?
-                       endpoint0_stall();
-                       return;
-               }
-               (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
-               // TODO: do we need to clear the data toggle here?
-               break;
-         case 0x0680: // GET_DESCRIPTOR
-         case 0x0681:
-               //serial_print("desc:");
-               //serial_phex16(setup.wValue);
-               //serial_print("\n");
-               for (list = usb_descriptor_list; 1; list++) {
-                       if (list->addr == NULL) break;
-                       //if (setup.wValue == list->wValue && 
-                       //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
-                       if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
-                               data = list->addr;
-                               datalen = list->length;
-#if 0
-                               serial_print("Desc found, ");
-                               serial_phex32((uint32_t)data);
-                               serial_print(",");
-                               serial_phex16(datalen);
-                               serial_print(",");
-                               serial_phex(data[0]);
-                               serial_phex(data[1]);
-                               serial_phex(data[2]);
-                               serial_phex(data[3]);
-                               serial_phex(data[4]);
-                               serial_phex(data[5]);
-                               serial_print("\n");
-#endif
-                               goto send;
-                       }
-               }
-               //serial_print("desc: not found\n");
-               endpoint0_stall();
-               return;
-#if defined(CDC_STATUS_INTERFACE)
-         case 0x2221: // CDC_SET_CONTROL_LINE_STATE
-               usb_cdc_line_rtsdtr = setup.wValue;
-               //serial_print("set control line state\n");
-               break;
-         case 0x2021: // CDC_SET_LINE_CODING
-               //serial_print("set coding, waiting...\n");
-               return;
-#endif
-
-// TODO: this does not work... why?
-#if defined(KEYBOARD_INTERFACE)
-         case 0x0921: // HID SET_REPORT
-               //serial_print(":)\n");
-               return;
-         case 0x0A21: // HID SET_IDLE
-               break;
-         // case 0xC940:
-#endif
-         default:
-               endpoint0_stall();
-               return;
-       }
-       send:
-       //serial_print("setup send ");
-       //serial_phex32(data);
-       //serial_print(",");
-       //serial_phex16(datalen);
-       //serial_print("\n");
-
-       if (datalen > setup.wLength) datalen = setup.wLength;
-       size = datalen;
-       if (size > EP0_SIZE) size = EP0_SIZE;
-       endpoint0_transmit(data, size);
-       data += size;
-       datalen -= size;
-       if (datalen == 0 && size < EP0_SIZE) return;
-
-       size = datalen;
-       if (size > EP0_SIZE) size = EP0_SIZE;
-       endpoint0_transmit(data, size);
-       data += size;
-       datalen -= size;
-       if (datalen == 0 && size < EP0_SIZE) return;
-
-       ep0_tx_ptr = data;
-       ep0_tx_len = datalen;
-}
-
-
-
-//A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
-//experiences any configuration event (configuration events are explained in
-//Sections 9.1.1.5 and 9.4.5).
-
-//Configuring a device or changing an alternate setting causes all of the status
-//and configuration values associated with endpoints in the affected interfaces
-//to be set to their default values. This includes setting the data toggle of
-//any endpoint using data toggles to the value DATA0.
-
-//For endpoints using data toggle, regardless of whether an endpoint has the
-//Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
-//data toggle being reinitialized to DATA0.
-
-
-
-// #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
-
-static void usb_control(uint32_t stat)
-{
-       bdt_t *b;
-       uint32_t pid, size;
-       uint8_t *buf;
-       const uint8_t *data;
-
-       b = stat2bufferdescriptor(stat);
-       pid = BDT_PID(b->desc);
-       //count = b->desc >> 16;
-       buf = b->addr;
-       //serial_print("pid:");
-       //serial_phex(pid);
-       //serial_print(", count:");
-       //serial_phex(count);
-       //serial_print("\n");
-
-       switch (pid) {
-       case 0x0D: // Setup received from host
-               //serial_print("PID=Setup\n");
-               //if (count != 8) ; // panic?
-               // grab the 8 byte setup info
-               setup.word1 = *(uint32_t *)(buf);
-               setup.word2 = *(uint32_t *)(buf + 4);
-
-               // give the buffer back
-               b->desc = BDT_DESC(EP0_SIZE, DATA1);
-               //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
-               //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
-
-               // clear any leftover pending IN transactions
-               ep0_tx_ptr = NULL;
-               if (ep0_tx_data_toggle) {
-               }
-               //if (table[index(0, TX, EVEN)].desc & 0x80) {
-                       //serial_print("leftover tx even\n");
-               //}
-               //if (table[index(0, TX, ODD)].desc & 0x80) {
-                       //serial_print("leftover tx odd\n");
-               //}
-               table[index(0, TX, EVEN)].desc = 0;
-               table[index(0, TX, ODD)].desc = 0;
-               // first IN after Setup is always DATA1
-               ep0_tx_data_toggle = 1;
-
-#if 0
-               serial_print("bmRequestType:");
-               serial_phex(setup.bmRequestType);
-               serial_print(", bRequest:");
-               serial_phex(setup.bRequest);
-               serial_print(", wValue:");
-               serial_phex16(setup.wValue);
-               serial_print(", wIndex:");
-               serial_phex16(setup.wIndex);
-               serial_print(", len:");
-               serial_phex16(setup.wLength);
-               serial_print("\n");
-#endif
-               // actually "do" the setup request
-               usbdev_setup();
-               // unfreeze the USB, now that we're ready
-               USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
-               break;
-       case 0x01:  // OUT transaction received from host
-       case 0x02:
-               //serial_print("PID=OUT\n");
-#ifdef CDC_STATUS_INTERFACE
-               if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
-                       int i;
-                       uint8_t *dst = 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_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;
-                       endpoint0_transmit(NULL, 0);
-               }
-#endif
-#ifdef KEYBOARD_INTERFACE
-               if (setup.word1 == 0x02000921 && setup.word2 == ((1<<16)|KEYBOARD_INTERFACE)) {
-                       USBKeys_LEDs = buf[0];
-                       endpoint0_transmit(NULL, 0);
-               }
-#endif
-               // give the buffer back
-               b->desc = BDT_DESC(EP0_SIZE, DATA1);
-               break;
-
-       case 0x09: // IN transaction completed to host
-               //serial_print("PID=IN:");
-               //serial_phex(stat);
-               //serial_print("\n");
-
-               // send remaining data, if any...
-               data = ep0_tx_ptr;
-               if (data) {
-                       size = ep0_tx_len;
-                       if (size > EP0_SIZE) size = EP0_SIZE;
-                       endpoint0_transmit(data, size);
-                       data += size;
-                       ep0_tx_len -= size;
-                       ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
-               }
-
-               if (setup.bRequest == 5 && setup.bmRequestType == 0) {
-                       setup.bRequest = 0;
-                       //serial_print("set address: ");
-                       //serial_phex16(setup.wValue);
-                       //serial_print("\n");
-                       USB0_ADDR = setup.wValue;
-               }
-
-               break;
-       //default:
-               //serial_print("PID=unknown:");
-               //serial_phex(pid);
-               //serial_print("\n");
-       }
-       USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
-}
-
-
-
-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];
-
-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             4
-
-
-
-usb_packet_t *usb_rx(uint32_t endpoint)
-{
-       usb_packet_t *ret;
-       endpoint--;
-       if (endpoint >= NUM_ENDPOINTS) return NULL;
-       __disable_irq();
-       ret = rx_first[endpoint];
-       if (ret) rx_first[endpoint] = ret->next;
-       __enable_irq();
-       //serial_print("rx, epidx=");
-       //serial_phex(endpoint);
-       //serial_print(", packet=");
-       //serial_phex32(ret);
-       //serial_print("\n");
-       return ret;
-}
-
-static uint32_t usb_queue_byte_count(const usb_packet_t *p)
-{
-       uint32_t count=0;
-
-       __disable_irq();
-       for ( ; p; p = p->next) {
-               count += p->len;
-       }
-       __enable_irq();
-       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--;
-       if (endpoint >= NUM_ENDPOINTS) return 0;
-       return usb_queue_byte_count(tx_first[endpoint]);
-}
-
-uint32_t usb_tx_packet_count(uint32_t endpoint)
-{
-       const usb_packet_t *p;
-       uint32_t count=0;
-
-       endpoint--;
-       if (endpoint >= NUM_ENDPOINTS) return 0;
-       p = tx_first[endpoint];
-       __disable_irq();
-       for ( ; p; p = p->next) count++;
-       __enable_irq();
-       return count;
-}
-
-
-// Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
-// receive endpoints are starving for memory.  The intention is to give
-// endpoints needing receive memory priority over the user's code, which is
-// likely calling usb_malloc to obtain memory for transmitting.  When the
-// user is creating data very quickly, their consumption could starve reception
-// without this prioritization.  The packet buffer (input) is assigned to the
-// first endpoint needing memory.
-//
-void usb_rx_memory(usb_packet_t *packet)
-{
-       unsigned int i;
-       const uint8_t *cfg;
-
-       cfg = usb_endpoint_config_table;
-       //serial_print("rx_mem:");
-       __disable_irq();
-       for (i=1; i <= NUM_ENDPOINTS; i++) {
-               if (*cfg++ & USB_ENDPT_EPRXEN) {
-                       if (table[index(i, RX, EVEN)].desc == 0) {
-                               table[index(i, RX, EVEN)].addr = packet->buf;
-                               table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
-                               usb_rx_memory_needed--;
-                               __enable_irq();
-                               //serial_phex(i);
-                               //serial_print(",even\n");
-                               return;
-                       }
-                       if (table[index(i, RX, ODD)].desc == 0) {
-                               table[index(i, RX, ODD)].addr = packet->buf;
-                               table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
-                               usb_rx_memory_needed--;
-                               __enable_irq();
-                               //serial_phex(i);
-                               //serial_print(",odd\n");
-                               return;
-                       }
-               }
-       }
-       __enable_irq();
-       // we should never reach this point.  If we get here, it means
-       // usb_rx_memory_needed was set greater than zero, but no memory
-       // was actually needed.  
-       usb_rx_memory_needed = 0;
-       usb_free(packet);
-       return;
-}
-
-//#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
-//#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
-
-void usb_tx(uint32_t endpoint, usb_packet_t *packet)
-{
-       bdt_t *b = &table[index(endpoint, TX, EVEN)];
-       uint8_t next;
-
-       endpoint--;
-       if (endpoint >= NUM_ENDPOINTS) return;
-       __disable_irq();
-       //serial_print("txstate=");
-       //serial_phex(tx_state[endpoint]);
-       //serial_print("\n");
-       switch (tx_state[endpoint]) {
-         case TX_STATE_BOTH_FREE_EVEN_FIRST:
-               next = TX_STATE_ODD_FREE;
-               break;
-         case TX_STATE_BOTH_FREE_ODD_FIRST:
-               b++;
-               next = TX_STATE_EVEN_FREE;
-               break;
-         case TX_STATE_EVEN_FREE:
-               next = TX_STATE_NONE_FREE;
-               break;
-         case TX_STATE_ODD_FREE:
-               b++;
-               next = TX_STATE_NONE_FREE;
-               break;
-         default:
-               if (tx_first[endpoint] == NULL) {
-                       tx_first[endpoint] = packet;
-               } else {
-                       tx_last[endpoint]->next = packet;
-               }
-               tx_last[endpoint] = packet;
-               __enable_irq();
-               return;
-       }
-       tx_state[endpoint] = next;
-       b->addr = packet->buf;
-       b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
-       __enable_irq();
-}
-
-
-
-
-
-
-void _reboot_Teensyduino_(void)
-{
-       // TODO: initialize R0 with a code....
-       asm volatile("bkpt");
-}
-
-
-
-void usb_isr(void)
-{
-       uint8_t status, stat, t;
-
-       //serial_print("isr");
-       //status = USB0_ISTAT;
-       //serial_phex(status);
-       //serial_print("\n");
-       restart:
-       status = USB0_ISTAT;
-
-       if ((status & USB_INTEN_SOFTOKEN /* 04 */ )) {
-               if (usb_configuration) {
-                       t = usb_reboot_timer;
-                       if (t) {
-                               usb_reboot_timer = --t;
-                               if (!t) _reboot_Teensyduino_();
-                       }
-#ifdef CDC_DATA_INTERFACE
-                       t = usb_cdc_transmit_flush_timer;
-                       if (t) {
-                               usb_cdc_transmit_flush_timer = --t;
-                               if (t == 0) usb_serial_flush_callback();
-                       }
-#endif
-               }
-               USB0_ISTAT = USB_INTEN_SOFTOKEN;
-       }
-
-       if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
-               uint8_t endpoint;
-               stat = USB0_STAT;
-               //serial_print("token: ep=");
-               //serial_phex(stat >> 4);
-               //serial_print(stat & 0x08 ? ",tx" : ",rx");
-               //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
-               endpoint = stat >> 4;
-               if (endpoint == 0) {
-                       usb_control(stat);
-               } else {
-                       bdt_t *b = stat2bufferdescriptor(stat);
-                       usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
-#if 0
-                       serial_print("ep:");
-                       serial_phex(endpoint);
-                       serial_print(", pid:");
-                       serial_phex(BDT_PID(b->desc));
-                       serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
-                       serial_print(", count:");
-                       serial_phex(b->desc >> 16);
-                       serial_print("\n");
-#endif
-                       endpoint--;     // endpoint is index to zero-based arrays
-
-                       if (stat & 0x08) { // transmit
-                               usb_free(packet);
-                               packet = tx_first[endpoint];
-                               if (packet) {
-                                       //serial_print("tx packet\n");
-                                       tx_first[endpoint] = packet->next;
-                                       b->addr = packet->buf;
-                                       switch (tx_state[endpoint]) {
-                                         case TX_STATE_BOTH_FREE_EVEN_FIRST:
-                                               tx_state[endpoint] = TX_STATE_ODD_FREE;
-                                               break;
-                                         case TX_STATE_BOTH_FREE_ODD_FIRST:
-                                               tx_state[endpoint] = TX_STATE_EVEN_FREE;
-                                               break;
-                                         case TX_STATE_EVEN_FREE:
-                                         case TX_STATE_ODD_FREE:
-                                         default:
-                                               tx_state[endpoint] = TX_STATE_NONE_FREE;
-                                               break;
-                                       }
-                                       b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
-                               } else {
-                                       //serial_print("tx no packet\n");
-                                       switch (tx_state[endpoint]) {
-                                         case TX_STATE_BOTH_FREE_EVEN_FIRST:
-                                         case TX_STATE_BOTH_FREE_ODD_FIRST:
-                                               break;
-                                         case TX_STATE_EVEN_FREE:
-                                               tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
-                                               break;
-                                         case TX_STATE_ODD_FREE:
-                                               tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
-                                               break;
-                                         default:
-                                               tx_state[endpoint] = ((uint32_t)b & 8) ?
-                                                 TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
-                                               break;
-                                       }
-                               }
-                       } 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;
-                               } 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++;
-                               }
-                       }
-
-
-
-
-               }
-               USB0_ISTAT = USB_ISTAT_TOKDNE;
-               goto restart;
-       }
-
-
-
-       if (status & USB_ISTAT_USBRST /* 01 */ ) {
-               //serial_print("reset\n");
-
-               // initialize BDT toggle bits
-               USB0_CTL = USB_CTL_ODDRST;
-               ep0_tx_bdt_bank = 0;
-
-               // set up buffers to receive Setup and OUT packets
-               table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
-               table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
-               table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
-               table[index(0, RX, ODD)].addr = ep0_rx1_buf;
-               table[index(0, TX, EVEN)].desc = 0;
-               table[index(0, TX, ODD)].desc = 0;
-               
-               // activate endpoint 0
-               USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
-
-               // clear all ending interrupts
-               USB0_ERRSTAT = 0xFF;
-               USB0_ISTAT = 0xFF;
-
-               // set the address to zero during enumeration
-               USB0_ADDR = 0;
-
-               // enable other interrupts
-               USB0_ERREN = 0xFF;
-               USB0_INTEN = USB_INTEN_TOKDNEEN |
-                       USB_INTEN_SOFTOKEN |
-                       USB_INTEN_STALLEN |
-                       USB_INTEN_ERROREN |
-                       USB_INTEN_USBRSTEN |
-                       USB_INTEN_SLEEPEN;
-
-               // is this necessary?
-               USB0_CTL = USB_CTL_USBENSOFEN;
-               return;
-       }
-
-
-       if ((status & USB_ISTAT_STALL /* 80 */ )) {
-               //serial_print("stall:\n");
-               USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
-               USB0_ISTAT = USB_ISTAT_STALL;
-       }
-       if ((status & USB_ISTAT_ERROR /* 02 */ )) {
-               uint8_t err = USB0_ERRSTAT;
-               USB0_ERRSTAT = err;
-               //serial_print("err:");
-               //serial_phex(err);
-               //serial_print("\n");
-               USB0_ISTAT = USB_ISTAT_ERROR;
-       }
-
-       if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
-               //serial_print("sleep\n");
-               USB0_ISTAT = USB_ISTAT_SLEEP;
-       }
-
-}
-
-
-
-void usb_init(void)
-{
-       int i;
-
-       //serial_begin(BAUD2DIV(115200));
-       //serial_print("usb_init\n");
-
-       for (i=0; i <= NUM_ENDPOINTS*4; i++) {
-               table[i].desc = 0;
-               table[i].addr = 0;
-       }
-
-       // this basically follows the flowchart in the Kinetis
-       // Quick Reference User Guide, Rev. 1, 03/2012, page 141
-
-       // assume 48 MHz clock already running
-       // SIM - enable clock
-       SIM_SCGC4 |= SIM_SCGC4_USBOTG;
-
-       // reset USB module
-       USB0_USBTRC0 = USB_USBTRC_USBRESET;
-       while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
-
-       // set desc table base addr
-       USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
-       USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
-       USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
-
-       // clear all ISR flags
-       USB0_ISTAT = 0xFF;
-       USB0_ERRSTAT = 0xFF;
-       USB0_OTGISTAT = 0xFF;
-
-       USB0_USBTRC0 |= 0x40; // undocumented bit
-
-       // enable USB
-       USB0_CTL = USB_CTL_USBENSOFEN;
-       USB0_USBCTRL = 0;
-
-       // enable reset interrupt
-       USB0_INTEN = USB_INTEN_USBRSTEN;
-
-       // enable interrupt in NVIC...
-       NVIC_ENABLE_IRQ(IRQ_USBOTG);
-
-       // enable d+ pullup
-       USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
-}
-
-// return 0 if the USB is not configured, or the configuration
-// number selected by the HOST
-uint8_t usb_configured(void)
-{
-       return usb_configuration;
-}
-
diff --git a/USB/pjrc/arm/usb_dev.h b/USB/pjrc/arm/usb_dev.h
deleted file mode 100644 (file)
index 2ac3baa..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-#ifndef _usb_dev_h_
-#define _usb_dev_h_
-
-// This header is NOT meant to be included when compiling
-// user sketches in Arduino.  The low-level functions
-// provided by usb_dev.c are meant to be called only by
-// code which provides higher-level interfaces to the user.
-
-#include "usb_mem.h"
-#include "usb_desc.h"
-
-void usb_init(void);
-uint8_t usb_configured(void);          // is the USB port configured
-void usb_isr(void);
-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_packet_count(uint32_t endpoint);
-void usb_tx(uint32_t endpoint, usb_packet_t *packet);
-void usb_tx_isr(uint32_t endpoint, usb_packet_t *packet);
-
-extern volatile uint8_t usb_configuration;
-
-#ifdef CDC_DATA_INTERFACE
-extern uint8_t usb_cdc_line_coding[7];
-extern volatile uint8_t usb_cdc_line_rtsdtr;
-extern volatile uint8_t usb_cdc_transmit_flush_timer;
-extern void usb_serial_flush_callback(void);
-#endif
-
-
-#endif
-
diff --git a/USB/pjrc/arm/usb_keyboard.c b/USB/pjrc/arm/usb_keyboard.c
deleted file mode 100644 (file)
index ffe2293..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "usb_dev.h"
-#include "usb_keyboard.h"
-#include <Lib/USBLib.h>
-#include <string.h> // for memcpy()
-
-
-// Maximum number of transmit packets to queue so we don't starve other endpoints for memory
-#define TX_PACKET_LIMIT 4
-
-static uint8_t transmit_previous_timeout=0;
-
-// When the PC isn't listening, how long do we wait before discarding data?
-#define TX_TIMEOUT_MSEC 50
-
-#if F_CPU == 96000000
-  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
-#elif F_CPU == 48000000
-  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
-#elif F_CPU == 24000000
-  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
-#endif
-
-
-// send the contents of keyboard_keys and keyboard_modifier_keys
-uint8_t usb_keyboard_send(void)
-{
-       uint32_t wait_count=0;
-       usb_packet_t *tx_packet;
-
-       while (1) {
-               if (!usb_configuration) {
-                       return -1;
-               }
-               if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
-                       tx_packet = usb_malloc();
-                       if (tx_packet) break;
-               }
-               if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
-                       transmit_previous_timeout = 1;
-                       return -1;
-               }
-               yield();
-       }
-       *(tx_packet->buf) = USBKeys_Modifiers;
-       *(tx_packet->buf + 1) = 0;
-       memcpy(tx_packet->buf + 2, USBKeys_Array, USB_MAX_KEY_SEND);
-       tx_packet->len = 8;
-       usb_tx(KEYBOARD_ENDPOINT, tx_packet);
-
-       return 0;
-}
-
diff --git a/USB/pjrc/arm/usb_keyboard.h b/USB/pjrc/arm/usb_keyboard.h
deleted file mode 100644 (file)
index 49aa84d..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#ifndef USBkeyboard_h_
-#define USBkeyboard_h_
-
-#include <inttypes.h>
-#include "usb_com.h"
-
-uint8_t usb_keyboard_send(void);
-
-#endif // USBkeyboard_h_
-
diff --git a/USB/pjrc/arm/usb_mem.c b/USB/pjrc/arm/usb_mem.c
deleted file mode 100644 (file)
index 47956f6..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-#include <Lib/USBLib.h>
-#include "usb_dev.h"
-#include "usb_mem.h"
-
-#define NUM_BUF 30
-
-__attribute__ ((section(".usbbuffers"), used))
-//static unsigned char usb_buffer_memory[NUM_BUF * sizeof(usb_packet_t)];
-unsigned char usb_buffer_memory[NUM_BUF * sizeof(usb_packet_t)];
-
-static uint32_t usb_buffer_available = 0xFFFFFFFF;
-
-// use bitmask and CLZ instruction to implement fast free list
-// http://www.archivum.info/gnu.gcc.help/2006-08/00148/Re-GCC-Inline-Assembly.html
-// http://gcc.gnu.org/ml/gcc/2012-06/msg00015.html
-// __builtin_clz()
-
-usb_packet_t * usb_malloc(void)
-{
-       unsigned int n, avail;
-       uint8_t *p;
-
-       __disable_irq();
-       avail = usb_buffer_available;
-       n = __builtin_clz(avail); // clz = count leading zeros
-       if (n >= NUM_BUF) {
-               __enable_irq();
-               return NULL;
-       }
-       //serial_print("malloc:");
-       //serial_phex(n);
-       //serial_print("\n");
-
-       usb_buffer_available = avail & ~(0x80000000 >> n);
-       __enable_irq();
-       p = usb_buffer_memory + (n * sizeof(usb_packet_t));
-       //serial_print("malloc:");
-       //serial_phex32((int)p);
-       //serial_print("\n");
-       *(uint32_t *)p = 0;
-       *(uint32_t *)(p + 4) = 0;
-       return (usb_packet_t *)p;
-}
-
-// for the receive endpoints to request memory
-extern uint8_t usb_rx_memory_needed;
-extern void usb_rx_memory(usb_packet_t *packet);
-
-void usb_free(usb_packet_t *p)
-{
-       unsigned int n, mask;
-
-       //serial_print("free:");
-       n = ((uint8_t *)p - usb_buffer_memory) / sizeof(usb_packet_t);
-       if (n >= NUM_BUF) return;
-       //serial_phex(n);
-       //serial_print("\n");
-
-       // if any endpoints are starving for memory to receive
-       // packets, give this memory to them immediately!
-       if (usb_rx_memory_needed && usb_configuration) {
-               //serial_print("give to rx:");
-               //serial_phex32((int)p);
-               //serial_print("\n");
-               usb_rx_memory(p);
-               return;
-       }
-
-       mask = (0x80000000 >> n);
-       __disable_irq();
-       usb_buffer_available |= mask;
-       __enable_irq();
-
-       //serial_print("free:");
-       //serial_phex32((int)p);
-       //serial_print("\n");
-}
-
diff --git a/USB/pjrc/arm/usb_mem.h b/USB/pjrc/arm/usb_mem.h
deleted file mode 100644 (file)
index c3f1f8d..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-#ifndef _usb_mem_h_
-#define _usb_mem_h_
-
-#include <stdint.h>
-
-typedef struct usb_packet_struct {
-       uint16_t len;
-       uint16_t index;
-       struct usb_packet_struct *next;
-       uint8_t buf[64];
-} usb_packet_t;
-
-usb_packet_t * usb_malloc(void);
-void usb_free(usb_packet_t *p);
-
-
-
-
-#endif
diff --git a/USB/pjrc/arm/usb_serial.c b/USB/pjrc/arm/usb_serial.c
deleted file mode 100644 (file)
index a674488..0000000
+++ /dev/null
@@ -1,241 +0,0 @@
-#include "usb_dev.h"
-#include "usb_serial.h"
-#include <Lib/USBLib.h>
-
-// defined by usb_dev.h -> usb_desc.h
-#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
-
-uint8_t usb_cdc_line_coding[7];
-volatile uint8_t usb_cdc_line_rtsdtr=0;
-volatile uint8_t usb_cdc_transmit_flush_timer=0;
-
-static usb_packet_t *rx_packet=NULL;
-static usb_packet_t *tx_packet=NULL;
-static volatile uint8_t tx_noautoflush=0;
-
-#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
-int usb_serial_getchar(void)
-{
-       unsigned int i;
-       int c;
-
-       usb_serial_receive();
-       if (!rx_packet) return -1;
-       i = rx_packet->index;
-       c = rx_packet->buf[i++];
-       if (i >= rx_packet->len) {
-               usb_free(rx_packet);
-               rx_packet = NULL;
-       } else {
-               rx_packet->index = i;
-       }
-       return c;
-}
-
-// peek at the next character, or -1 if nothing received
-int usb_serial_peekchar(void)
-{
-       usb_serial_receive();
-       if (!rx_packet) return -1;
-       return rx_packet->buf[rx_packet->index];
-}
-
-// number of bytes available in the receive buffer
-int usb_serial_available(void)
-{
-       int count=0;
-
-       if (usb_configuration) {
-               count = usb_rx_byte_count(CDC_RX_ENDPOINT);
-       }
-       if (rx_packet) count += rx_packet->len - rx_packet->index;
-       return count;
-}
-
-// discard any buffered input
-void usb_serial_flush_input(void)
-{
-       usb_packet_t *rx;
-
-       if (!usb_configuration) return;
-       if (rx_packet) {
-               usb_free(rx_packet);
-               rx_packet = NULL;
-       }
-       while (1) {
-               rx = usb_rx(CDC_RX_ENDPOINT);
-               if (!rx) break;
-               usb_free(rx);
-       }
-}
-
-// Maximum number of transmit packets to queue so we don't starve other endpoints for memory
-#define TX_PACKET_LIMIT 8
-
-// When the PC isn't listening, how long do we wait before discarding data?  If this is
-// too short, we risk losing data during the stalls that are common with ordinary desktop
-// software.  If it's too long, we stall the user's program when no software is running.
-#define TX_TIMEOUT_MSEC 70
-
-#if F_CPU == 96000000
-  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
-#elif F_CPU == 48000000
-  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
-#elif F_CPU == 24000000
-  #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
-#endif
-
-// When we've suffered the transmit timeout, don't wait again until the computer
-// begins accepting data.  If no software is running to receive, we'll just discard
-// data as rapidly as Serial.print() can generate it, until there's something to
-// actually receive it.
-static uint8_t transmit_previous_timeout=0;
-
-
-// transmit a character.  0 returned on success, -1 on error
-int usb_serial_putchar(uint8_t c)
-{
-#if 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)
-{
-#if 1
-       uint32_t len;
-       uint32_t wait_count;
-       const uint8_t *src = (const uint8_t *)buffer;
-       uint8_t *dest;
-
-       tx_noautoflush = 1;
-       while (size > 0) {
-               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;
-                               }
-                               yield();
-                       }
-               }
-               transmit_previous_timeout = 0;
-               len = CDC_TX_SIZE - tx_packet->index;
-               if (len > size) len = size;
-               dest = tx_packet->buf + tx_packet->index;
-               tx_packet->index += len;
-               size -= len;
-               while (len-- > 0) *dest++ = *src++;
-               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
-#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)
-{
-       if (!usb_configuration) return;
-       //serial_print("usb_serial_flush_output\n");
-       if (tx_packet && tx_packet->index > 0) {
-               usb_cdc_transmit_flush_timer = 0;
-               tx_packet->len = tx_packet->index;
-               usb_tx(CDC_TX_ENDPOINT, tx_packet);
-               tx_packet = NULL;
-       }
-       // while (usb_tx_byte_count(CDC_TX_ENDPOINT) > 0) ; // wait
-}
-
-void usb_serial_flush_callback(void)
-{
-       if (tx_noautoflush) return;
-       //serial_print("usb_flush_callback \n");
-       tx_packet->len = tx_packet->index;
-       usb_tx(CDC_TX_ENDPOINT, tx_packet);
-       tx_packet = NULL;
-       //serial_print("usb_flush_callback end\n");
-}
-
-#endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE
-
diff --git a/USB/pjrc/arm/usb_serial.h b/USB/pjrc/arm/usb_serial.h
deleted file mode 100644 (file)
index 4dbfa05..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef USBserial_h_
-#define USBserial_h_
-
-#include <inttypes.h>
-
-// Compatibility defines from AVR
-#define PROGMEM
-#define PGM_P  const char *
-#define PSTR(str) (str)
-
-
-int usb_serial_getchar(void);
-int usb_serial_peekchar(void);
-int usb_serial_available(void);
-void usb_serial_flush_input(void);
-int usb_serial_putchar(uint8_t c);
-int usb_serial_write(const void *buffer, uint32_t size);
-void usb_serial_flush_output(void);
-extern uint8_t usb_cdc_line_coding[7];
-extern volatile uint8_t usb_cdc_line_rtsdtr;
-extern volatile uint8_t usb_cdc_transmit_flush_timer;
-extern volatile uint8_t usb_configuration;
-
-#endif // USBserial_h_
-
diff --git a/USB/pjrc/avr/usb_keyboard_debug.c b/USB/pjrc/avr/usb_keyboard_debug.c
deleted file mode 100644 (file)
index 3a792cf..0000000
+++ /dev/null
@@ -1,705 +0,0 @@
-/* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
- * http://www.pjrc.com/teensy/usb_keyboard.html
- * Copyright (c) 2009 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:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * 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.
- */
-
-// Version 1.0: Initial Release
-// Version 1.1: Add support for Teensy 2.0
-
-#define USB_SERIAL_PRIVATE_INCLUDE
-#include "usb_keyboard_debug.h"
-
-/**************************************************************************
- *
- *  Configurable Options
- *
- **************************************************************************/
-
-// USB devices are supposed to implment a halt feature, which is
-// rarely (if ever) used.  If you comment this line out, the halt
-// code will be removed, saving 102 bytes of space (gcc 4.3.0).
-// This is not strictly USB compliant, but works with all major
-// operating systems.
-#define SUPPORT_ENDPOINT_HALT
-
-
-
-/**************************************************************************
- *
- *  Endpoint Buffer Configuration
- *
- **************************************************************************/
-
-#define ENDPOINT0_SIZE         32
-
-#define KEYBOARD_INTERFACE     0
-#define KEYBOARD_ENDPOINT      3
-#define KEYBOARD_SIZE          8
-#define KEYBOARD_BUFFER                EP_DOUBLE_BUFFER
-
-#define DEBUG_INTERFACE                1
-#define DEBUG_TX_ENDPOINT      4
-#define DEBUG_TX_SIZE          32
-#define DEBUG_TX_BUFFER                EP_DOUBLE_BUFFER
-
-static const uint8_t PROGMEM endpoint_config_table[] = {
-       0,
-       0,
-       1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
-       1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER
-};
-
-
-/**************************************************************************
- *
- *  Descriptor Data
- *
- **************************************************************************/
-
-// Descriptors are the data that your computer reads when it auto-detects
-// this USB device (called "enumeration" in USB lingo).  The most commonly
-// changed items are editable at the top of this file.  Changing things
-// in here should only be done by those who've read chapter 9 of the USB
-// spec and relevant portions of any USB class specifications!
-
-
-static const uint8_t PROGMEM device_descriptor[] = {
-       18,                                     // bLength
-       1,                                      // bDescriptorType
-       0x00, 0x02,                             // bcdUSB
-       0,                                      // bDeviceClass
-       0,                                      // bDeviceSubClass
-       0,                                      // bDeviceProtocol
-       ENDPOINT0_SIZE,                         // bMaxPacketSize0
-       LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
-       LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
-       0x00, 0x01,                             // bcdDevice
-       1,                                      // iManufacturer
-       2,                                      // iProduct
-       3,                                      // iSerialNumber
-       1                                       // bNumConfigurations
-};
-
-// Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
-static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
-        0x05, 0x01,          // Usage Page (Generic Desktop),
-        0x09, 0x06,          // Usage (Keyboard),
-        0xA1, 0x01,          // Collection (Application),
-        0x75, 0x01,          //   Report Size (1),
-        0x95, 0x08,          //   Report Count (8),
-        0x05, 0x07,          //   Usage Page (Key Codes),
-        0x19, 0xE0,          //   Usage Minimum (224),
-        0x29, 0xE7,          //   Usage Maximum (231),
-        0x15, 0x00,          //   Logical Minimum (0),
-        0x25, 0x01,          //   Logical Maximum (1),
-        0x81, 0x02,          //   Input (Data, Variable, Absolute), ;Modifier byte
-        0x95, 0x01,          //   Report Count (1),
-        0x75, 0x08,          //   Report Size (8),
-        0x81, 0x03,          //   Input (Constant),                 ;Reserved byte
-        0x95, 0x05,          //   Report Count (5),
-        0x75, 0x01,          //   Report Size (1),
-        0x05, 0x08,          //   Usage Page (LEDs),
-        0x19, 0x01,          //   Usage Minimum (1),
-        0x29, 0x05,          //   Usage Maximum (5),
-        0x91, 0x02,          //   Output (Data, Variable, Absolute), ;LED report
-        0x95, 0x01,          //   Report Count (1),
-        0x75, 0x03,          //   Report Size (3),
-        0x91, 0x03,          //   Output (Constant),                 ;LED report padding
-        0x95, 0x06,          //   Report Count (6),
-        0x75, 0x08,          //   Report Size (8),
-        0x15, 0x00,          //   Logical Minimum (0),
-        0x25, 0x68,          //   Logical Maximum(104),
-        0x05, 0x07,          //   Usage Page (Key Codes),
-        0x19, 0x00,          //   Usage Minimum (0),
-        0x29, 0x68,          //   Usage Maximum (104),
-        0x81, 0x00,          //   Input (Data, Array),
-        0xc0                 // End Collection
-};
-
-static const uint8_t PROGMEM debug_hid_report_desc[] = {
-       //0x06, 0x30, 0xFF,                     // Usage Page 0xFF31 (vendor defined)
-       0x06, 0x31, 0xFF,                       // Usage Page 0xFF31 (vendor defined)
-       0x09, 0x74,                             // Usage 0x74
-       0xA1, 0x53,                             // Collection 0x53
-       0x75, 0x08,                             // report size = 8 bits
-       0x15, 0x00,                             // logical minimum = 0
-       0x26, 0xFF, 0x00,                       // logical maximum = 255
-       0x95, DEBUG_TX_SIZE,                    // report count
-       0x09, 0x75,                             // usage
-       0x81, 0x02,                             // Input (array)
-       0xC0                                    // end collection
-};
-
-#define CONFIG1_DESC_SIZE        (9+9+9+7+9+9+7)
-#define KEYBOARD_HID_DESC_OFFSET (9+9)
-#define DEBUG_HID_DESC_OFFSET    (9+9+9+7+9)
-static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
-       // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
-       9,                                      // bLength;
-       2,                                      // bDescriptorType;
-       LSB(CONFIG1_DESC_SIZE),                 // wTotalLength
-       MSB(CONFIG1_DESC_SIZE),
-       2,                                      // bNumInterfaces
-       1,                                      // bConfigurationValue
-       0,                                      // iConfiguration
-       0xC0,                                   // bmAttributes
-       50,                                     // bMaxPower
-       // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
-       9,                                      // bLength
-       4,                                      // bDescriptorType
-       KEYBOARD_INTERFACE,                     // bInterfaceNumber
-       0,                                      // bAlternateSetting
-       1,                                      // bNumEndpoints
-       0x03,                                   // bInterfaceClass (0x03 = HID)
-       0x01,                                   // bInterfaceSubClass (0x01 = Boot)
-       0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
-       0,                                      // iInterface
-       // HID interface descriptor, HID 1.11 spec, section 6.2.1
-       9,                                      // bLength
-       0x21,                                   // bDescriptorType
-       0x11, 0x01,                             // bcdHID
-       0,                                      // bCountryCode
-       1,                                      // bNumDescriptors
-       0x22,                                   // bDescriptorType
-       sizeof(keyboard_hid_report_desc),       // wDescriptorLength
-       0,
-       // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
-       7,                                      // bLength
-       5,                                      // bDescriptorType
-       KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
-       0x03,                                   // bmAttributes (0x03=intr)
-       KEYBOARD_SIZE, 0,                       // wMaxPacketSize
-       1,                                      // bInterval
-       // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
-       9,                                      // bLength
-       4,                                      // bDescriptorType
-       DEBUG_INTERFACE,                        // bInterfaceNumber
-       0,                                      // bAlternateSetting
-       1,                                      // bNumEndpoints
-       0x03,                                   // bInterfaceClass (0x03 = HID)
-       0x00,                                   // bInterfaceSubClass
-       0x00,                                   // bInterfaceProtocol
-       0,                                      // iInterface
-       // HID interface descriptor, HID 1.11 spec, section 6.2.1
-       9,                                      // bLength
-       0x21,                                   // bDescriptorType
-       0x11, 0x01,                             // bcdHID
-       0,                                      // bCountryCode
-       1,                                      // bNumDescriptors
-       0x22,                                   // bDescriptorType
-       sizeof(debug_hid_report_desc),          // wDescriptorLength
-       0,
-       // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
-       7,                                      // bLength
-       5,                                      // bDescriptorType
-       DEBUG_TX_ENDPOINT | 0x80,               // bEndpointAddress
-       0x03,                                   // bmAttributes (0x03=intr)
-       DEBUG_TX_SIZE, 0,                       // wMaxPacketSize
-       1                                       // bInterval
-};
-
-// If you're desperate for a little extra code memory, these strings
-// can be completely removed if iManufacturer, iProduct, iSerialNumber
-// in the device desciptor are changed to zeros.
-struct usb_string_descriptor_struct {
-       uint8_t bLength;
-       uint8_t bDescriptorType;
-       int16_t wString[];
-};
-static const struct usb_string_descriptor_struct PROGMEM string0 = {
-       4,
-       3,
-       {0x0409}
-};
-static const struct usb_string_descriptor_struct PROGMEM string1 = {
-       sizeof(STR_MANUFACTURER),
-       3,
-       STR_MANUFACTURER
-};
-static const struct usb_string_descriptor_struct PROGMEM string2 = {
-       sizeof(STR_PRODUCT),
-       3,
-       STR_PRODUCT
-};
-static const struct usb_string_descriptor_struct PROGMEM string3 = {
-       sizeof(STR_SERIAL),
-       3,
-       STR_SERIAL
-};
-
-// This table defines which descriptor data is sent for each specific
-// request from the host (in wValue and wIndex).
-static const struct descriptor_list_struct {
-       uint16_t        wValue;
-       uint16_t        wIndex;
-       const uint8_t   *addr;
-       uint8_t         length;
-} PROGMEM descriptor_list[] = {
-       {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
-       {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
-       {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
-       {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
-       {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
-       {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
-       {0x0300, 0x0000, (const uint8_t *)&string0, 4},
-       {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
-       {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
-       {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
-};
-#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
-
-
-/**************************************************************************
- *
- *  Variables - these are the only non-stack RAM usage
- *
- **************************************************************************/
-
-// zero when we are not configured, non-zero when enumerated
-static volatile uint8_t usb_configuration=0;
-
-// the time remaining before we transmit any partially full
-// packet, or send a zero length packet.
-static volatile uint8_t debug_flush_timer=0;
-
-
-/**************************************************************************
- *
- *  Public Functions - these are the API intended for the user
- *
- **************************************************************************/
-
-
-// initialize USB
-void usb_init(void)
-{
-       HW_CONFIG();
-       USB_FREEZE();                           // enable USB
-       PLL_CONFIG();                           // config PLL
-        while (!(PLLCSR & (1<<PLOCK))) ;       // wait for PLL lock
-        USB_CONFIG();                          // start USB clock
-        UDCON = 0;                             // enable attach resistor
-       usb_configuration = 0;
-        UDIEN = (1<<EORSTE)|(1<<SOFE);
-       sei();
-}
-
-// return 0 if the USB is not configured, or the configuration
-// number selected by the HOST
-uint8_t usb_configured(void)
-{
-       return usb_configuration;
-}
-
-// send the contents of USBKeys_Array and USBKeys_Modifiers
-int8_t usb_keyboard_send(void)
-{
-       uint8_t i, intr_state, timeout;
-
-       if (!usb_configuration) return -1;
-       intr_state = SREG;
-       cli();
-       UENUM = KEYBOARD_ENDPOINT;
-       timeout = UDFNUML + 50;
-       while (1) {
-               // are we ready to transmit?
-               if (UEINTX & (1<<RWAL)) break;
-               SREG = intr_state;
-               // has the USB gone offline?
-               if (!usb_configuration) return -1;
-               // have we waited too long?
-               if (UDFNUML == timeout) return -1;
-               // get ready to try checking again
-               intr_state = SREG;
-               cli();
-               UENUM = KEYBOARD_ENDPOINT;
-       }
-       UEDATX = USBKeys_Modifiers;
-       UEDATX = 0;
-       for (i=0; i<6; i++) {
-               UEDATX = USBKeys_Array[i];
-       }
-       UEINTX = 0x3A;
-       USBKeys_Idle_Count = 0;
-       SREG = intr_state;
-       return 0;
-}
-
-// transmit a character.  0 returned on success, -1 on error
-int8_t usb_debug_putchar(uint8_t c)
-{
-       static uint8_t previous_timeout=0;
-       uint8_t timeout, intr_state;
-
-       // if we're not online (enumerated and configured), error
-       if (!usb_configuration) return -1;
-       // interrupts are disabled so these functions can be
-       // used from the main program or interrupt context,
-       // even both in the same program!
-       intr_state = SREG;
-       cli();
-       UENUM = DEBUG_TX_ENDPOINT;
-       // if we gave up due to timeout before, don't wait again
-       if (previous_timeout) {
-               if (!(UEINTX & (1<<RWAL))) {
-                       SREG = intr_state;
-                       return -1;
-               }
-               previous_timeout = 0;
-       }
-       // wait for the FIFO to be ready to accept data
-       timeout = UDFNUML + 4;
-       while (1) {
-               // are we ready to transmit?
-               if (UEINTX & (1<<RWAL)) break;
-               SREG = intr_state;
-               // have we waited too long?
-               if (UDFNUML == timeout) {
-                       previous_timeout = 1;
-                       return -1;
-               }
-               // has the USB gone offline?
-               if (!usb_configuration) return -1;
-               // get ready to try checking again
-               intr_state = SREG;
-               cli();
-               UENUM = DEBUG_TX_ENDPOINT;
-       }
-       // actually write the byte into the FIFO
-       UEDATX = c;
-       // if this completed a packet, transmit it now!
-       if (!(UEINTX & (1<<RWAL))) {
-               UEINTX = 0x3A;
-               debug_flush_timer = 0;
-       } else {
-               debug_flush_timer = 2;
-       }
-       SREG = intr_state;
-       return 0;
-}
-
-
-// immediately transmit any buffered output.
-void usb_debug_flush_output(void)
-{
-       uint8_t intr_state;
-
-       intr_state = SREG;
-       cli();
-       if (debug_flush_timer) {
-               UENUM = DEBUG_TX_ENDPOINT;
-               while ((UEINTX & (1<<RWAL))) {
-                       UEDATX = 0;
-               }
-               UEINTX = 0x3A;
-               debug_flush_timer = 0;
-       }
-       SREG = intr_state;
-}
-
-
-
-/**************************************************************************
- *
- *  Private Functions - not intended for general user consumption....
- *
- **************************************************************************/
-
-
-
-// USB Device Interrupt - handle all device-level events
-// the transmit buffer flushing is triggered by the start of frame
-//
-ISR(USB_GEN_vect)
-{
-       uint8_t intbits, t, i;
-       static uint8_t div4=0;
-
-        intbits = UDINT;
-        UDINT = 0;
-        if (intbits & (1<<EORSTI)) {
-               UENUM = 0;
-               UECONX = 1;
-               UECFG0X = EP_TYPE_CONTROL;
-               UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
-               UEIENX = (1<<RXSTPE);
-               usb_configuration = 0;
-        }
-       if ((intbits & (1<<SOFI)) && usb_configuration) {
-               t = debug_flush_timer;
-               if (t) {
-                       debug_flush_timer = -- t;
-                       if (!t) {
-                               UENUM = DEBUG_TX_ENDPOINT;
-                               while ((UEINTX & (1<<RWAL))) {
-                                       UEDATX = 0;
-                               }
-                               UEINTX = 0x3A;
-                       }
-               }
-               if (USBKeys_Idle_Config && (++div4 & 3) == 0) {
-                       UENUM = KEYBOARD_ENDPOINT;
-                       if (UEINTX & (1<<RWAL)) {
-                               USBKeys_Idle_Count++;
-                               if (USBKeys_Idle_Count == USBKeys_Idle_Config) {
-                                       USBKeys_Idle_Count = 0;
-                                       UEDATX = USBKeys_Modifiers;
-                                       UEDATX = 0;
-                                       for (i=0; i<6; i++) {
-                                               UEDATX = USBKeys_Array[i];
-                                       }
-                                       UEINTX = 0x3A;
-                               }
-                       }
-               }
-       }
-}
-
-
-
-// Misc functions to wait for ready and send/receive packets
-static inline void usb_wait_in_ready(void)
-{
-       while (!(UEINTX & (1<<TXINI))) ;
-}
-static inline void usb_send_in(void)
-{
-       UEINTX = ~(1<<TXINI);
-}
-static inline void usb_wait_receive_out(void)
-{
-       while (!(UEINTX & (1<<RXOUTI))) ;
-}
-static inline void usb_ack_out(void)
-{
-       UEINTX = ~(1<<RXOUTI);
-}
-
-
-
-// USB Endpoint Interrupt - endpoint 0 is handled here.  The
-// other endpoints are manipulated by the user-callable
-// functions, and the start-of-frame interrupt.
-//
-ISR(USB_COM_vect)
-{
-        uint8_t intbits;
-       const uint8_t *list;
-        const uint8_t *cfg;
-       uint8_t i, n, len, en;
-       uint8_t bmRequestType;
-       uint8_t bRequest;
-       uint16_t wValue;
-       uint16_t wIndex;
-       uint16_t wLength;
-       uint16_t desc_val;
-       const uint8_t *desc_addr;
-       uint8_t desc_length;
-
-        UENUM = 0;
-       intbits = UEINTX;
-        if (intbits & (1<<RXSTPI)) {
-                bmRequestType = UEDATX;
-                bRequest = UEDATX;
-                wValue = UEDATX;
-                wValue |= (UEDATX << 8);
-                wIndex = UEDATX;
-                wIndex |= (UEDATX << 8);
-                wLength = UEDATX;
-                wLength |= (UEDATX << 8);
-                UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
-                if (bRequest == GET_DESCRIPTOR) {
-                       list = (const uint8_t *)descriptor_list;
-                       for (i=0; ; i++) {
-                               if (i >= NUM_DESC_LIST) {
-                                       UECONX = (1<<STALLRQ)|(1<<EPEN);  //stall
-                                       return;
-                               }
-                               desc_val = pgm_read_word(list);
-                               if (desc_val != wValue) {
-                                       list += sizeof(struct descriptor_list_struct);
-                                       continue;
-                               }
-                               list += 2;
-                               desc_val = pgm_read_word(list);
-                               if (desc_val != wIndex) {
-                                       list += sizeof(struct descriptor_list_struct)-2;
-                                       continue;
-                               }
-                               list += 2;
-                               desc_addr = (const uint8_t *)pgm_read_word(list);
-                               list += 2;
-                               desc_length = pgm_read_byte(list);
-                               break;
-                       }
-                       len = (wLength < 256) ? wLength : 255;
-                       if (len > desc_length) len = desc_length;
-                       do {
-                               // wait for host ready for IN packet
-                               do {
-                                       i = UEINTX;
-                               } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
-                               if (i & (1<<RXOUTI)) return;    // abort
-                               // send IN packet
-                               n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
-                               for (i = n; i; i--) {
-                                       UEDATX = pgm_read_byte(desc_addr++);
-                               }
-                               len -= n;
-                               usb_send_in();
-                       } while (len || n == ENDPOINT0_SIZE);
-                       return;
-                }
-               if (bRequest == SET_ADDRESS) {
-                       usb_send_in();
-                       usb_wait_in_ready();
-                       UDADDR = wValue | (1<<ADDEN);
-                       return;
-               }
-               if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
-                       usb_configuration = wValue;
-                       usb_send_in();
-                       cfg = endpoint_config_table;
-                       for (i=1; i<5; i++) {
-                               UENUM = i;
-                               en = pgm_read_byte(cfg++);
-                               UECONX = en;
-                               if (en) {
-                                       UECFG0X = pgm_read_byte(cfg++);
-                                       UECFG1X = pgm_read_byte(cfg++);
-                               }
-                       }
-                       UERST = 0x1E;
-                       UERST = 0;
-                       return;
-               }
-               if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
-                       usb_wait_in_ready();
-                       UEDATX = usb_configuration;
-                       usb_send_in();
-                       return;
-               }
-
-               if (bRequest == GET_STATUS) {
-                       usb_wait_in_ready();
-                       i = 0;
-                       #ifdef SUPPORT_ENDPOINT_HALT
-                       if (bmRequestType == 0x82) {
-                               UENUM = wIndex;
-                               if (UECONX & (1<<STALLRQ)) i = 1;
-                               UENUM = 0;
-                       }
-                       #endif
-                       UEDATX = i;
-                       UEDATX = 0;
-                       usb_send_in();
-                       return;
-               }
-               #ifdef SUPPORT_ENDPOINT_HALT
-               if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
-                 && bmRequestType == 0x02 && wValue == 0) {
-                       i = wIndex & 0x7F;
-                       if (i >= 1 && i <= MAX_ENDPOINT) {
-                               usb_send_in();
-                               UENUM = i;
-                               if (bRequest == SET_FEATURE) {
-                                       UECONX = (1<<STALLRQ)|(1<<EPEN);
-                               } else {
-                                       UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
-                                       UERST = (1 << i);
-                                       UERST = 0;
-                               }
-                               return;
-                       }
-               }
-               #endif
-               if (wIndex == KEYBOARD_INTERFACE) {
-                       if (bmRequestType == 0xA1) {
-                               if (bRequest == HID_GET_REPORT) {
-                                       usb_wait_in_ready();
-                                       UEDATX = USBKeys_Modifiers;
-                                       UEDATX = 0;
-                                       for (i=0; i<6; i++) {
-                                               UEDATX = USBKeys_Array[i];
-                                       }
-                                       usb_send_in();
-                                       return;
-                               }
-                               if (bRequest == HID_GET_IDLE) {
-                                       usb_wait_in_ready();
-                                       UEDATX = USBKeys_Idle_Config;
-                                       usb_send_in();
-                                       return;
-                               }
-                               if (bRequest == HID_GET_PROTOCOL) {
-                                       usb_wait_in_ready();
-                                       UEDATX = USBKeys_Protocol;
-                                       usb_send_in();
-                                       return;
-                               }
-                       }
-                       if (bmRequestType == 0x21) {
-                               if (bRequest == HID_SET_REPORT) {
-                                       usb_wait_receive_out();
-                                       USBKeys_LEDs = UEDATX;
-                                       usb_ack_out();
-                                       usb_send_in();
-                                       return;
-                               }
-                               if (bRequest == HID_SET_IDLE) {
-                                       USBKeys_Idle_Config = (wValue >> 8);
-                                       USBKeys_Idle_Count = 0;
-                                       //usb_wait_in_ready();
-                                       usb_send_in();
-                                       return;
-                               }
-                               if (bRequest == HID_SET_PROTOCOL) {
-                                       USBKeys_Protocol = wValue;
-                                       //usb_wait_in_ready();
-                                       usb_send_in();
-                                       return;
-                               }
-                       }
-               }
-               if (wIndex == DEBUG_INTERFACE) {
-                       if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
-                               len = wLength;
-                               do {
-                                       // wait for host ready for IN packet
-                                       do {
-                                               i = UEINTX;
-                                       } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
-                                       if (i & (1<<RXOUTI)) return;    // abort
-                                       // send IN packet
-                                       n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
-                                       for (i = n; i; i--) {
-                                               UEDATX = 0;
-                                       }
-                                       len -= n;
-                                       usb_send_in();
-                               } while (len || n == ENDPOINT0_SIZE);
-                               return;
-                       }
-               }
-       }
-       UECONX = (1<<STALLRQ) | (1<<EPEN);      // stall
-}
-
diff --git a/USB/pjrc/avr/usb_keyboard_debug.h b/USB/pjrc/avr/usb_keyboard_debug.h
deleted file mode 100644 (file)
index 987e4fd..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-#ifndef usb_serial_h__
-#define usb_serial_h__
-
-#include <stdint.h>
-#include "usb_com.h"
-
-void usb_init(void);                   // initialize everything
-uint8_t usb_configured(void);          // is the USB port configured
-
-int8_t usb_keyboard_send(void);
-
-
-int8_t usb_debug_putchar(uint8_t c);   // transmit a character
-void usb_debug_flush_output(void);     // immediately transmit any buffered output
-#define USB_DEBUG_HID
-
-
-// Everything below this point is only intended for usb_serial.c
-#ifdef USB_SERIAL_PRIVATE_INCLUDE
-#include <avr/io.h>
-#include <avr/pgmspace.h>
-#include <avr/interrupt.h>
-
-#define EP_TYPE_CONTROL                        0x00
-#define EP_TYPE_BULK_IN                        0x81
-#define EP_TYPE_BULK_OUT               0x80
-#define EP_TYPE_INTERRUPT_IN           0xC1
-#define EP_TYPE_INTERRUPT_OUT          0xC0
-#define EP_TYPE_ISOCHRONOUS_IN         0x41
-#define EP_TYPE_ISOCHRONOUS_OUT                0x40
-
-#define EP_SINGLE_BUFFER               0x02
-#define EP_DOUBLE_BUFFER               0x06
-
-#define EP_SIZE(s)     ((s) == 64 ? 0x30 :     \
-                       ((s) == 32 ? 0x20 :     \
-                       ((s) == 16 ? 0x10 :     \
-                                    0x00)))
-
-#define MAX_ENDPOINT           4
-
-#define LSB(n) (n & 255)
-#define MSB(n) ((n >> 8) & 255)
-
-#if defined(__AVR_AT90USB162__)
-#define HW_CONFIG() 
-#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
-#define USB_CONFIG() (USBCON = (1<<USBE))
-#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
-#elif defined(__AVR_ATmega32U4__)
-#define HW_CONFIG() (UHWCON = 0x01)
-#define PLL_CONFIG() (PLLCSR = 0x12)
-#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
-#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
-#elif defined(__AVR_AT90USB646__)
-#define HW_CONFIG() (UHWCON = 0x81)
-#define PLL_CONFIG() (PLLCSR = 0x1A)
-#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
-#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
-#elif defined(__AVR_AT90USB1286__)
-#define HW_CONFIG() (UHWCON = 0x81)
-#define PLL_CONFIG() (PLLCSR = 0x16)
-#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
-#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
-#endif
-
-// standard control endpoint request types
-#define GET_STATUS                     0
-#define CLEAR_FEATURE                  1
-#define SET_FEATURE                    3
-#define SET_ADDRESS                    5
-#define GET_DESCRIPTOR                 6
-#define GET_CONFIGURATION              8
-#define SET_CONFIGURATION              9
-#define GET_INTERFACE                  10
-#define SET_INTERFACE                  11
-// HID (human interface device)
-#define HID_GET_REPORT                 1
-#define HID_GET_IDLE                   2
-#define HID_GET_PROTOCOL               3
-#define HID_SET_REPORT                 9
-#define HID_SET_IDLE                   10
-#define HID_SET_PROTOCOL               11
-// CDC (communication class device)
-#define CDC_SET_LINE_CODING            0x20
-#define CDC_GET_LINE_CODING            0x21
-#define CDC_SET_CONTROL_LINE_STATE     0x22
-#endif
-#endif
-
diff --git a/USB/pjrc/setup.cmake b/USB/pjrc/setup.cmake
deleted file mode 100644 (file)
index b1be1d4..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-###| CMake Kiibohd Controller USB Module |###
-#
-# Written by Jacob Alexander in 2011-2013 for the Kiibohd Controller
-#
-# Released into the Public Domain
-#
-###
-
-
-###
-# Module C files
-#
-
-
-#| AVR Compiler
-if ( ${COMPILER_FAMILY} MATCHES "avr" )
-
-       set( USB_SRCS
-               usb_com.c
-               avr/usb_keyboard_debug.c
-       )
-
-#| ARM Compiler
-elseif ( ${COMPILER_FAMILY} MATCHES "arm" )
-
-       set( USB_SRCS
-               usb_com.c
-               arm/usb_desc.c
-               arm/usb_dev.c
-               arm/usb_keyboard.c
-               arm/usb_mem.c
-               arm/usb_serial.c
-       )
-
-endif ( ${COMPILER_FAMILY} MATCHES "avr" )
-
-
-###
-# Module Specific Options
-#
-
-###
-# Compiler Family Compatibility
-#
-set( USBModuleCompatibility
-       arm
-       avr
-)
-
diff --git a/USB/pjrc/usb_com.c b/USB/pjrc/usb_com.c
deleted file mode 100644 (file)
index dad641d..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-/* Copyright (C) 2011-2013 by Jacob Alexander
- * 
- * 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:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * 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.
- */
-
-// ----- Includes -----
-
-// Compiler Includes
-#include <Lib/USBLib.h>
-
-// Project Includes
-#include <scan_loop.h>
-
-// USB Includes
-#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
-#include "avr/usb_keyboard_debug.h"
-#elif defined(_mk20dx128_)
-#include "arm/usb_keyboard.h"
-#include "arm/usb_dev.h"
-#endif
-
-// Local Includes
-#include "usb_com.h"
-
-
-
-// ----- Variables -----
-
-// which modifier keys are currently pressed
-// 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
-// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
-         uint8_t USBKeys_Modifiers = 0;
-
-// which keys are currently pressed, up to 6 keys may be down at once
-         uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
-
-// The number of keys sent to the usb in the array
-         uint8_t USBKeys_Sent;
-
-// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
-volatile uint8_t USBKeys_LEDs = 0;
-
-// protocol setting from the host.  We use exactly the same report
-// either way, so this variable only stores the setting since we
-// are required to be able to report which setting is in use.
-         uint8_t USBKeys_Protocol = 1;
-
-// the idle configuration, how often we send the report to the
-// host (ms * 4) even when it hasn't changed
-         uint8_t USBKeys_Idle_Config = 125;
-
-// count until idle timeout
-         uint8_t USBKeys_Idle_Count = 0;
-
-
-
-// ----- Functions -----
-
-// USB Module Setup
-inline void usb_setup(void)
-{
-       // Initialize the USB, and then wait for the host to set configuration.
-       // If the Teensy is powered without a PC connected to the USB port,
-       // this will wait forever.
-       usb_init();
-       while ( !usb_configured() ) /* wait */ ;
-
-       // Wait an extra second for the PC's operating system to load drivers
-       // and do whatever it does to actually be ready for input
-       //_delay_ms(1000); // TODO
-}
-
-
-// USB Data Send
-inline void usb_send(void)
-{
-               // TODO undo potentially old keys
-               for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
-                       USBKeys_Array[c] = 0;
-
-               // Send keypresses
-               usb_keyboard_send();
-
-               // Clear modifiers and keys
-               USBKeys_Modifiers = 0;
-               USBKeys_Sent      = 0;
-
-               // Signal Scan Module we are finishedA
-               scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
-}
-
diff --git a/USB/pjrc/usb_com.h b/USB/pjrc/usb_com.h
deleted file mode 100644 (file)
index f62e90c..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-/* Copyright (C) 2013 by Jacob Alexander
- * 
- * 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:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * 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_com_h
-#define __usb_com_h
-
-// ----- Includes -----
-
-// Compiler Includes
-#include <stdint.h>
-
-// Local Includes
-#include <buildvars.h> // Defines USB Parameters, partially generated by CMake
-
-
-
-// ----- Defines -----
-
-// Indicator for other modules through USBKeys_MaxSize for how capable the USB module is when sending large number of keypresses
-#define USB_MAX_KEY_SEND 6
-
-
-
-// ----- Variables -----
-
-// Variables used to communciate to the usb module
-extern                       uint8_t USBKeys_Modifiers;
-extern                       uint8_t USBKeys_Array[USB_MAX_KEY_SEND];
-extern                       uint8_t USBKeys_Sent;
-extern volatile              uint8_t USBKeys_LEDs;
-
-                static const uint8_t USBKeys_MaxSize = USB_MAX_KEY_SEND;
-
-// Misc variables (XXX Some are only properly utilized using AVR)
-extern                       uint8_t USBKeys_Protocol;
-extern                       uint8_t USBKeys_Idle_Config;
-extern                       uint8_t USBKeys_Idle_Count;
-
-
-
-// ----- Functions -----
-
-void usb_setup(void);
-void usb_send(void);
-
-#endif
-
index aad36905d030e4f296c5134480528a7937c409ec..8041b08c8675007e025a4ec6dc12d5ecda2513bf 100644 (file)
@@ -26,10 +26,10 @@ set(  ScanModule  "MBC-55X" )
 set( MacroModule  "buffer"  )
 
 ##| Sends the current list of usb key codes through USB HID
-set(   USBModule  "pjrc"   )
+set( OutputModule  "pjrcUSB" )
 
 ##| Debugging source to use, each module has it's own set of defines that it sets
-set( DebugModule  "full"   )
+set( DebugModule   "full"    )
 
 
 
@@ -45,11 +45,12 @@ endif ()
 
 ###
 # Path Setup
-# 
-set(  ScanModulePath  "Scan/${ScanModule}"  )
-set( MacroModulePath "Macro/${MacroModule}" )
-set(   USBModulePath   "USB/${USBModule}"   )
-set( DebugModulePath "Debug/${DebugModule}" )
+#
+set(  ScanModulePath   "Scan/${ScanModule}"  )
+set( MacroModulePath  "Macro/${MacroModule}" )
+set( OutputModulePath "Output/${OutputModule}" )
+set(   USBModulePath    "USB/${USBModule}"   )
+set( DebugModulePath  "Debug/${DebugModule}" )
 
 #| Top-level directory adjustment
 set( HEAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
@@ -87,7 +88,7 @@ add_definitions()
 add_definitions(
        -I${HEAD_DIR}/${ScanModulePath}
        -I${HEAD_DIR}/${MacroModulePath}
-       -I${HEAD_DIR}/${USBModulePath}
+       -I${HEAD_DIR}/${OutputModulePath}
        -I${HEAD_DIR}/${DebugModulePath}
 )
 
@@ -123,9 +124,9 @@ PathPrepend(  SCAN_SRCS  ${ScanModulePath} ${SCAN_SRCS}  )
 include    (           "${MacroModulePath}/setup.cmake"  )
 PathPrepend( MACRO_SRCS ${MacroModulePath} ${MACRO_SRCS} )
 
-#| USB Module
-include    (             "${USBModulePath}/setup.cmake"  )
-PathPrepend(   USB_SRCS   ${USBModulePath} ${USB_SRCS}   )
+#| Output Module
+include    (             "${OutputModulePath}/setup.cmake"   )
+PathPrepend( OUTPUT_SRCS  ${OutputModulePath} ${OUTPUT_SRCS} )
 
 #| Debugging Module
 include    (           "${DebugModulePath}/setup.cmake"  )
@@ -138,7 +139,7 @@ message( "${SCAN_SRCS}" )
 message( STATUS "Detected Macro Module Source Files:" )
 message( "${MACRO_SRCS}" )
 message( STATUS "Detected USB Module Source Files:" )
-message( "${USB_SRCS}" )
+message( "${OUTPUT_SRCS}" )
 message( STATUS "Detected Debug Module Source Files:" )
 message( "${DEBUG_SRCS}" )