annotate Output/pjrcUSB/arm/usb_dev.c @ 346:1beec5f17e64

Revert "Merge pull request #27 from smasher816/wakeup-devel" This reverts commit 622ea5d85f94be4b1694fdf08e30fa76eeb18fe4, reversing changes made to c21439cb48daec7514da4250c41962205fa96624.
author Jacob Alexander <haata@kiibohd.com>
date Tue, 23 Jun 2015 08:00:16 -0700
parents f7b14e25ca5b
children 136e47478441
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1 /* Teensyduino Core Library
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
2 * http://www.pjrc.com/teensy/
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
3 * Copyright (c) 2013 PJRC.COM, LLC.
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
4 * Modifications by Jacob Alexander (2013-2014)
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
5 *
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
6 * Permission is hereby granted, free of charge, to any person obtaining
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
7 * a copy of this software and associated documentation files (the
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
8 * "Software"), to deal in the Software without restriction, including
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
9 * without limitation the rights to use, copy, modify, merge, publish,
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
10 * distribute, sublicense, and/or sell copies of the Software, and to
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
11 * permit persons to whom the Software is furnished to do so, subject to
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
12 * the following conditions:
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
13 *
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
14 * 1. The above copyright notice and this permission notice shall be
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
15 * included in all copies or substantial portions of the Software.
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
16 *
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
17 * 2. If the Software is incorporated into a build system that allows
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
18 * selection among a list of target devices, then similar target
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
19 * devices manufactured by PJRC.COM must be included in the list of
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
20 * target devices and selectable in the same manner.
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
21 *
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
29 * SOFTWARE.
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
30 */
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
31
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
32 // ----- Includes -----
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
33
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
34 // Project Includes
118
b61ca96b7c24 File and macro modifications for supporting Teensy 3.1
Jacob Alexander <haata@kiibohd.com>
parents: 117
diff changeset
35 #include <Lib/OutputLib.h>
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
36 #include <print.h>
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
37
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
38 // Local Includes
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
39 #include "usb_dev.h"
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
40 #include "usb_mem.h"
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
41
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
42
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
43
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
44 // ----- Defines -----
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
45
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
46 // DEBUG Mode
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
47 // XXX - Only use when using usbMuxUart Module
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
48 // Delay causes issues initializing more than 1 hid device (i.e. NKRO keyboard)
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
49 //#define UART_DEBUG 1
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
50 // Debug Unknown USB requests, usually what you want to debug USB issues
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
51 //#define UART_DEBUG_UNKNOWN 1
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
52
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
53
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
54 #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
55 #define TX_STATE_BOTH_FREE_ODD_FIRST 1
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
56 #define TX_STATE_EVEN_FREE 2
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
57 #define TX_STATE_ODD_FREE 3
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
58 #define TX_STATE_NONE_FREE_EVEN_FIRST 4
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
59 #define TX_STATE_NONE_FREE_ODD_FIRST 5
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
60
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
61 #define BDT_OWN 0x80
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
62 #define BDT_DATA1 0x40
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
63 #define BDT_DATA0 0x00
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
64 #define BDT_DTS 0x08
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
65 #define BDT_STALL 0x04
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
66
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
67 #define TX 1
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
68 #define RX 0
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
69 #define ODD 1
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
70 #define EVEN 0
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
71 #define DATA0 0
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
72 #define DATA1 1
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
73
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
74
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
75 #define GET_STATUS 0
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
76 #define CLEAR_FEATURE 1
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
77 #define SET_FEATURE 3
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
78 #define SET_ADDRESS 5
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
79 #define GET_DESCRIPTOR 6
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
80 #define SET_DESCRIPTOR 7
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
81 #define GET_CONFIGURATION 8
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
82 #define SET_CONFIGURATION 9
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
83 #define GET_INTERFACE 10
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
84 #define SET_INTERFACE 11
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
85 #define SYNCH_FRAME 12
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
86
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
87 #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
88 #define TX_STATE_BOTH_FREE_ODD_FIRST 1
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
89 #define TX_STATE_EVEN_FREE 2
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
90 #define TX_STATE_ODD_FREE 3
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
91 #define TX_STATE_NONE_FREE 4
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
92
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
93
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
94
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
95
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
96
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
97 // ----- Macros -----
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
98
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
99 #define BDT_PID(n) (((n) >> 2) & 15)
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
100
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
101 #define BDT_DESC(count, data) (BDT_OWN | BDT_DTS \
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
102 | ((data) ? BDT_DATA1 : BDT_DATA0) \
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
103 | ((count) << 16))
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
104
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
105 #define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
106 #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
107
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
108
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
109
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
110 // ----- Structs -----
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
111
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
112 // buffer descriptor table
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
113
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
114 typedef struct {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
115 uint32_t desc;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
116 void * addr;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
117 } bdt_t;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
118
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
119 static union {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
120 struct {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
121 union {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
122 struct {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
123 uint8_t bmRequestType;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
124 uint8_t bRequest;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
125 };
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
126 uint16_t wRequestAndType;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
127 };
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
128 uint16_t wValue;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
129 uint16_t wIndex;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
130 uint16_t wLength;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
131 };
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
132 struct {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
133 uint32_t word1;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
134 uint32_t word2;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
135 };
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
136 } setup;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
137
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
138
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
139
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
140 // ----- Variables -----
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
141
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
142 __attribute__ ((section(".usbdescriptortable"), used))
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
143 static bdt_t table[ (NUM_ENDPOINTS + 1) * 4 ];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
144
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
145 static usb_packet_t *rx_first [ NUM_ENDPOINTS ];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
146 static usb_packet_t *rx_last [ NUM_ENDPOINTS ];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
147 static usb_packet_t *tx_first [ NUM_ENDPOINTS ];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
148 static usb_packet_t *tx_last [ NUM_ENDPOINTS ];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
149 uint16_t usb_rx_byte_count_data[ NUM_ENDPOINTS ];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
150
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
151 static uint8_t tx_state[NUM_ENDPOINTS];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
152
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
153 // SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
154 // transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
155 // Status stage uses a DATA1 PID.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
156
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
157 static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
158 static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
159 static const uint8_t *ep0_tx_ptr = NULL;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
160 static uint16_t ep0_tx_len;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
161 static uint8_t ep0_tx_bdt_bank = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
162 static uint8_t ep0_tx_data_toggle = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
163 uint8_t usb_rx_memory_needed = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
164
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
165 volatile uint8_t usb_configuration = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
166 volatile uint8_t usb_reboot_timer = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
167
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
168 static uint8_t reply_buffer[8];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
169
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
170
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
171
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
172 // ----- Functions -----
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
173
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
174 static void endpoint0_stall()
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
175 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
176 USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
177 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
178
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
179 static void endpoint0_transmit( const void *data, uint32_t len )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
180 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
181 table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
182 table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
183 ep0_tx_data_toggle ^= 1;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
184 ep0_tx_bdt_bank ^= 1;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
185 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
186
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
187 static void usb_setup()
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
188 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
189 const uint8_t *data = NULL;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
190 uint32_t datalen = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
191 const usb_descriptor_list_t *list;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
192 uint32_t size;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
193 volatile uint8_t *reg;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
194 uint8_t epconf;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
195 const uint8_t *cfg;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
196 int i;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
197
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
198 switch ( setup.wRequestAndType )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
199 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
200 case 0x0500: // SET_ADDRESS
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
201 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
202 case 0x0900: // SET_CONFIGURATION
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
203 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
204 print("CONFIGURE - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
205 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
206 usb_configuration = setup.wValue;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
207 reg = &USB0_ENDPT1;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
208 cfg = usb_endpoint_config_table;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
209 // clear all BDT entries, free any allocated memory...
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
210 for ( i = 4; i < ( NUM_ENDPOINTS + 1) * 4; i++ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
211 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
212 if ( table[i].desc & BDT_OWN )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
213 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
214 usb_free( (usb_packet_t *)((uint8_t *)(table[ i ].addr) - 8) );
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
215 }
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
216 }
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
217 // free all queued packets
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
218 for ( i = 0; i < NUM_ENDPOINTS; i++ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
219 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
220 usb_packet_t *p, *n;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
221 p = rx_first[i];
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
222 while ( p )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
223 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
224 n = p->next;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
225 usb_free(p);
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
226 p = n;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
227 }
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
228 rx_first[ i ] = NULL;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
229 rx_last[ i ] = NULL;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
230 p = tx_first[i];
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
231 while (p)
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
232 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
233 n = p->next;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
234 usb_free(p);
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
235 p = n;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
236 }
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
237 tx_first[ i ] = NULL;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
238 tx_last[ i ] = NULL;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
239 usb_rx_byte_count_data[i] = 0;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
240
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
241 switch ( tx_state[ i ] )
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
242 {
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
243 case TX_STATE_EVEN_FREE:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
244 case TX_STATE_NONE_FREE_EVEN_FIRST:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
245 tx_state[ i ] = TX_STATE_BOTH_FREE_EVEN_FIRST;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
246 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
247 case TX_STATE_ODD_FREE:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
248 case TX_STATE_NONE_FREE_ODD_FIRST:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
249 tx_state[ i ] = TX_STATE_BOTH_FREE_ODD_FIRST;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
250 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
251 default:
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
252 break;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
253 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
254 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
255 usb_rx_memory_needed = 0;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
256 for ( i = 1; i <= NUM_ENDPOINTS; i++ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
257 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
258 epconf = *cfg++;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
259 *reg = epconf;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
260 reg += 4;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
261 if ( epconf & USB_ENDPT_EPRXEN )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
262 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
263 usb_packet_t *p;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
264 p = usb_malloc();
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
265 if ( p )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
266 {
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
267 table[ index( i, RX, EVEN ) ].addr = p->buf;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
268 table[ index( i, RX, EVEN ) ].desc = BDT_DESC( 64, 0 );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
269 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
270 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
271 {
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
272 table[ index( i, RX, EVEN ) ].desc = 0;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
273 usb_rx_memory_needed++;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
274 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
275 p = usb_malloc();
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
276 if ( p )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
277 {
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
278 table[ index( i, RX, ODD ) ].addr = p->buf;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
279 table[ index( i, RX, ODD ) ].desc = BDT_DESC( 64, 1 );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
280 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
281 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
282 {
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
283 table[ index( i, RX, ODD ) ].desc = 0;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
284 usb_rx_memory_needed++;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
285 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
286 }
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
287 table[ index( i, TX, EVEN ) ].desc = 0;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
288 table[ index( i, TX, ODD ) ].desc = 0;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
289 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
290 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
291 case 0x0880: // GET_CONFIGURATION
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
292 reply_buffer[0] = usb_configuration;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
293 datalen = 1;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
294 data = reply_buffer;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
295 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
296 case 0x0080: // GET_STATUS (device)
346
1beec5f17e64 Revert "Merge pull request #27 from smasher816/wakeup-devel"
Jacob Alexander <haata@kiibohd.com>
parents: 327
diff changeset
297 reply_buffer[0] = 0;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
298 reply_buffer[1] = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
299 datalen = 2;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
300 data = reply_buffer;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
301 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
302 case 0x0082: // GET_STATUS (endpoint)
302
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
303 if ( setup.wIndex > NUM_ENDPOINTS )
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
304 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
305 // TODO: do we need to handle IN vs OUT here?
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
306 endpoint0_stall();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
307 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
308 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
309 reply_buffer[0] = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
310 reply_buffer[1] = 0;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
311 if ( *(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
312 reply_buffer[0] = 1;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
313 data = reply_buffer;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
314 datalen = 2;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
315 break;
302
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
316 case 0x0100: // CLEAR_FEATURE (device)
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
317 case 0x0101: // CLEAR_FEATURE (interface)
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
318 // TODO: Currently ignoring, perhaps useful? -HaaTa
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
319 endpoint0_stall();
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
320 return;
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
321 case 0x0102: // CLEAR_FEATURE (interface)
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
322 i = setup.wIndex & 0x7F;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
323 if ( i > NUM_ENDPOINTS || setup.wValue != 0 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
324 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
325 endpoint0_stall();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
326 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
327 }
301
c8605539efef Fixing Mac OSX freeze on wake-up bug
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
328 //(*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
329 // TODO: do we need to clear the data toggle here?
301
c8605539efef Fixing Mac OSX freeze on wake-up bug
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
330 //break;
c8605539efef Fixing Mac OSX freeze on wake-up bug
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
331
c8605539efef Fixing Mac OSX freeze on wake-up bug
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
332 // FIXME: Clearing causes keyboard to freeze, likely an invalid clear
c8605539efef Fixing Mac OSX freeze on wake-up bug
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
333 // XXX: Ignoring seems to work, though this may not be the ideal behaviour -HaaTa
c8605539efef Fixing Mac OSX freeze on wake-up bug
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
334 endpoint0_stall();
c8605539efef Fixing Mac OSX freeze on wake-up bug
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
335 return;
302
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
336 case 0x0300: // SET_FEATURE (device)
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
337 case 0x0301: // SET_FEATURE (interface)
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
338 // TODO: Currently ignoring, perhaps useful? -HaaTa
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
339 endpoint0_stall();
ac842066568a Adding additional case statements for SET_FEATURE and CLEAR_FEATURE
Jacob Alexander <haata@kiibohd.com>
parents: 301
diff changeset
340 return;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
341 case 0x0302: // SET_FEATURE (endpoint)
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
342 i = setup.wIndex & 0x7F;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
343 if ( i > NUM_ENDPOINTS || setup.wValue != 0 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
344 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
345 // TODO: do we need to handle IN vs OUT here?
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
346 endpoint0_stall();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
347 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
348 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
349 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
350 // TODO: do we need to clear the data toggle here?
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
351 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
352 case 0x0680: // GET_DESCRIPTOR
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
353 case 0x0681:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
354 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
355 print("desc:");
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
356 printHex( setup.wValue );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
357 print( NL );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
358 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
359 for ( list = usb_descriptor_list; 1; list++ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
360 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
361 if ( list->addr == NULL )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
362 break;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
363 if ( setup.wValue == list->wValue && setup.wIndex == list->wIndex )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
364 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
365 data = list->addr;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
366 if ( (setup.wValue >> 8) == 3 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
367 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
368 // for string descriptors, use the descriptor's
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
369 // length field, allowing runtime configured
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
370 // length.
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
371 datalen = *(list->addr);
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
372 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
373 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
374 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
375 datalen = list->length;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
376 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
377 #if UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
378 print("Desc found, ");
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
379 printHex32( (uint32_t)data );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
380 print(",");
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
381 printHex( datalen );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
382 print(",");
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
383 printHex_op( data[0], 2 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
384 printHex_op( data[1], 2 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
385 printHex_op( data[2], 2 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
386 printHex_op( data[3], 2 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
387 printHex_op( data[4], 2 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
388 printHex_op( data[5], 2 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
389 print( NL );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
390 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
391 goto send;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
392 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
393 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
394 #ifdef UART_DEBUG
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
395 print( "desc: not found" NL );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
396 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
397 endpoint0_stall();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
398 return;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
399
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
400 case 0x2221: // CDC_SET_CONTROL_LINE_STATE
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
401 usb_cdc_line_rtsdtr = setup.wValue;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
402 //serial_print("set control line state\n");
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
403 endpoint0_stall();
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
404 return;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
405
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
406 case 0x21A1: // CDC_GET_LINE_CODING
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
407 data = (uint8_t*)usb_cdc_line_coding;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
408 datalen = sizeof( usb_cdc_line_coding );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
409 goto send;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
410
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
411 case 0x2021: // CDC_SET_LINE_CODING
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
412 // XXX Needed?
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
413 //serial_print("set coding, waiting...\n");
251
d3ac199593d6 Fixing NKRO on Windows 8.1
Jacob Alexander <haata@kiibohd.com>
parents: 250
diff changeset
414 endpoint0_stall();
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
415 return; // Cannot stall here (causes issues)
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
416
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
417 case 0x0921: // HID SET_REPORT
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
418 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
419 print("SET_REPORT - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
420 printHex( setup.wValue );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
421 print(" - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
422 printHex( setup.wValue & 0xFF );
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
423 print( NL );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
424 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
425 USBKeys_LEDs = setup.wValue & 0xFF;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
426 endpoint0_stall();
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
427 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
428
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
429 case 0x01A1: // HID GET_REPORT
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
430 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
431 print("GET_REPORT - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
432 printHex( USBKeys_LEDs );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
433 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
434 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
435 data = (uint8_t*)&USBKeys_LEDs;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
436 datalen = 1;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
437 goto send;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
438
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
439 case 0x0A21: // HID SET_IDLE
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
440 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
441 print("SET_IDLE - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
442 printHex( setup.wValue );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
443 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
444 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
445 USBKeys_Idle_Config = (setup.wValue >> 8);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
446 USBKeys_Idle_Count = 0;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
447 endpoint0_stall();
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
448 return;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
449
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
450 case 0x0B21: // HID SET_PROTOCOL
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
451 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
452 print("SET_PROTOCOL - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
453 printHex( setup.wValue );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
454 print(" - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
455 printHex( setup.wValue & 0xFF );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
456 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
457 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
458 USBKeys_Protocol = setup.wValue & 0xFF; // 0 - Boot Mode, 1 - NKRO Mode
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
459 endpoint0_stall();
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
460 return;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
461
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
462 // case 0xC940:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
463 default:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
464 #ifdef UART_DEBUG_UNKNOWN
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
465 print("UNKNOWN");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
466 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
467 endpoint0_stall();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
468 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
469 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
470
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
471 send:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
472 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
473 print("setup send ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
474 printHex32((uint32_t)data);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
475 print(",");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
476 printHex(datalen);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
477 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
478 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
479
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
480 if ( datalen > setup.wLength )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
481 datalen = setup.wLength;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
482
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
483 size = datalen;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
484 if ( size > EP0_SIZE )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
485 size = EP0_SIZE;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
486
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
487 endpoint0_transmit(data, size);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
488 data += size;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
489 datalen -= size;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
490
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
491 // See if transmit has finished
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
492 if ( datalen == 0 && size < EP0_SIZE )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
493 return;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
494
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
495 size = datalen;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
496 if ( size > EP0_SIZE )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
497 size = EP0_SIZE;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
498 endpoint0_transmit(data, size);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
499 data += size;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
500 datalen -= size;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
501
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
502 // See if transmit has finished
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
503 if ( datalen == 0 && size < EP0_SIZE )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
504 return;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
505
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
506 // Save rest of transfer for later? XXX
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
507 ep0_tx_ptr = data;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
508 ep0_tx_len = datalen;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
509 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
510
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
511
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
512 //A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
513 //experiences any configuration event (configuration events are explained in
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
514 //Sections 9.1.1.5 and 9.4.5).
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
515
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
516 //Configuring a device or changing an alternate setting causes all of the status
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
517 //and configuration values associated with endpoints in the affected interfaces
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
518 //to be set to their default values. This includes setting the data toggle of
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
519 //any endpoint using data toggles to the value DATA0.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
520
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
521 //For endpoints using data toggle, regardless of whether an endpoint has the
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
522 //Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
523 //data toggle being reinitialized to DATA0.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
524
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
525 static void usb_control( uint32_t stat )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
526 {
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
527 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
528 print("CONTROL - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
529 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
530 bdt_t *b;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
531 uint32_t pid, size;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
532 uint8_t *buf;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
533 const uint8_t *data;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
534
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
535 b = stat2bufferdescriptor( stat );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
536 pid = BDT_PID( b->desc );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
537 buf = b->addr;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
538 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
539 print("pid:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
540 printHex(pid);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
541 print(", count:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
542 printHex32(b->desc);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
543 print(" - ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
544 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
545
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
546 switch (pid)
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
547 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
548 case 0x0D: // Setup received from host
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
549 //serial_print("PID=Setup\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
550 //if (count != 8) ; // panic?
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
551 // grab the 8 byte setup info
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
552 setup.word1 = *(uint32_t *)(buf);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
553 setup.word2 = *(uint32_t *)(buf + 4);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
554
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
555 // give the buffer back
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
556 b->desc = BDT_DESC( EP0_SIZE, DATA1 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
557 //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
558 //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
559
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
560 // clear any leftover pending IN transactions
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
561 ep0_tx_ptr = NULL;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
562 if ( ep0_tx_data_toggle )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
563 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
564 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
565 //if (table[index(0, TX, EVEN)].desc & 0x80) {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
566 //serial_print("leftover tx even\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
567 //}
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
568 //if (table[index(0, TX, ODD)].desc & 0x80) {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
569 //serial_print("leftover tx odd\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
570 //}
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
571 table[index(0, TX, EVEN)].desc = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
572 table[index(0, TX, ODD)].desc = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
573 // first IN after Setup is always DATA1
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
574 ep0_tx_data_toggle = 1;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
575
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
576 #ifdef UART_DEBUG_UNKNOWN
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
577 print("bmRequestType:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
578 printHex(setup.bmRequestType);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
579 print(", bRequest:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
580 printHex(setup.bRequest);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
581 print(", wValue:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
582 printHex(setup.wValue);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
583 print(", wIndex:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
584 printHex(setup.wIndex);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
585 print(", len:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
586 printHex(setup.wLength);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
587 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
588 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
589 // actually "do" the setup request
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
590 usb_setup();
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
591 // unfreeze the USB, now that we're ready
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
592 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
593 break;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
594 case 0x01: // OUT transaction received from host
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
595 case 0x02:
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
596 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
597 print("PID=OUT"NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
598 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
599 // CDC Interface
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
600 if ( setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
601 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
602 int i;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
603 uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
604 //serial_print("set line coding ");
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
605 for ( i = 0; i < 7; i++ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
606 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
607 //serial_phex(*buf);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
608 *dst++ = *buf++;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
609 }
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
610 //serial_phex32(usb_cdc_line_coding[0]);
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
611 //serial_print("\n");
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
612 if ( usb_cdc_line_coding[0] == 134 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
613 usb_reboot_timer = 15;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
614 endpoint0_transmit( NULL, 0 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
615 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
616
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
617 // Keyboard Interface
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
618 if ( setup.word1 == 0x02000921 && setup.word2 == ( (1<<16) | KEYBOARD_INTERFACE ) )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
619 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
620 USBKeys_LEDs = buf[0];
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
621 endpoint0_transmit( NULL, 0 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
622 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
623 // NKRO Keyboard Interface
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
624 if ( setup.word1 == 0x02000921 && setup.word2 == ( (1<<16) | NKRO_KEYBOARD_INTERFACE ) )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
625 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
626 USBKeys_LEDs = buf[0];
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
627 endpoint0_transmit( NULL, 0 );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
628 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
629
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
630 // give the buffer back
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
631 b->desc = BDT_DESC( EP0_SIZE, DATA1 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
632 break;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
633
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
634 case 0x09: // IN transaction completed to host
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
635 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
636 print("PID=IN:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
637 printHex(stat);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
638 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
639 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
640
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
641 // send remaining data, if any...
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
642 data = ep0_tx_ptr;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
643 if ( data )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
644 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
645 size = ep0_tx_len;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
646 if (size > EP0_SIZE) size = EP0_SIZE;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
647 endpoint0_transmit(data, size);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
648 data += size;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
649 ep0_tx_len -= size;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
650 ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
651 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
652
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
653 if ( setup.bRequest == 5 && setup.bmRequestType == 0 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
654 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
655 setup.bRequest = 0;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
656 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
657 print("set address: ");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
658 printHex(setup.wValue);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
659 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
660 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
661 USB0_ADDR = setup.wValue;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
662 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
663
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
664 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
665 default:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
666 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
667 print("PID=unknown:");
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
668 printHex(pid);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
669 print(NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
670 #endif
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
671 break;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
672 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
673 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
674 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
675
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
676 usb_packet_t *usb_rx( uint32_t endpoint )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
677 {
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
678 //print("USB RX");
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
679 usb_packet_t *ret;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
680 endpoint--;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
681 if ( endpoint >= NUM_ENDPOINTS )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
682 return NULL;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
683 __disable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
684 ret = rx_first[endpoint];
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
685 if ( ret )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
686 rx_first[ endpoint ] = ret->next;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
687 usb_rx_byte_count_data[ endpoint ] -= ret->len;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
688 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
689 //serial_print("rx, epidx=");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
690 //serial_phex(endpoint);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
691 //serial_print(", packet=");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
692 //serial_phex32(ret);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
693 //serial_print("\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
694 return ret;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
695 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
696
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
697 static uint32_t usb_queue_byte_count( const usb_packet_t *p )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
698 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
699 uint32_t count=0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
700
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
701 __disable_irq();
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
702 for ( ; p; p = p->next )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
703 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
704 count += p->len;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
705 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
706 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
707 return count;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
708 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
709
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
710 uint32_t usb_tx_byte_count( uint32_t endpoint )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
711 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
712 endpoint--;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
713 if ( endpoint >= NUM_ENDPOINTS )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
714 return 0;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
715 return usb_queue_byte_count( tx_first[ endpoint ] );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
716 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
717
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
718 uint32_t usb_tx_packet_count( uint32_t endpoint )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
719 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
720 const usb_packet_t *p;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
721 uint32_t count=0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
722
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
723 endpoint--;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
724 if ( endpoint >= NUM_ENDPOINTS )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
725 return 0;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
726 __disable_irq();
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
727 for ( p = tx_first[ endpoint ]; p; p = p->next )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
728 count++;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
729 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
730 return count;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
731 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
732
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
733
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
734 // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
735 // receive endpoints are starving for memory. The intention is to give
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
736 // endpoints needing receive memory priority over the user's code, which is
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
737 // likely calling usb_malloc to obtain memory for transmitting. When the
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
738 // user is creating data very quickly, their consumption could starve reception
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
739 // without this prioritization. The packet buffer (input) is assigned to the
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
740 // first endpoint needing memory.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
741 //
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
742 void usb_rx_memory( usb_packet_t *packet )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
743 {
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
744 //print("USB RX MEMORY");
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
745 unsigned int i;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
746 const uint8_t *cfg;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
747
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
748 cfg = usb_endpoint_config_table;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
749 //serial_print("rx_mem:");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
750 __disable_irq();
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
751 for ( i = 1; i <= NUM_ENDPOINTS; i++ )
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
752 {
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
753 if ( *cfg++ & USB_ENDPT_EPRXEN )
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
754 {
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
755 if ( table[ index( i, RX, EVEN ) ].desc == 0 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
756 {
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
757 table[ index( i, RX, EVEN ) ].addr = packet->buf;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
758 table[ index( i, RX, EVEN ) ].desc = BDT_DESC( 64, 0 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
759 usb_rx_memory_needed--;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
760 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
761 //serial_phex(i);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
762 //serial_print(",even\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
763 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
764 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
765 if ( table[ index( i, RX, ODD ) ].desc == 0 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
766 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
767 table[ index( i, RX, ODD ) ].addr = packet->buf;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
768 table[ index( i, RX, ODD ) ].desc = BDT_DESC( 64, 1 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
769 usb_rx_memory_needed--;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
770 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
771 //serial_phex(i);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
772 //serial_print(",odd\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
773 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
774 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
775 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
776 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
777 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
778 // we should never reach this point. If we get here, it means
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
779 // usb_rx_memory_needed was set greater than zero, but no memory
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
780 // was actually needed.
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
781 usb_rx_memory_needed = 0;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
782 usb_free( packet );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
783 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
784 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
785
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
786 //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
787 //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
788
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
789 void usb_tx( uint32_t endpoint, usb_packet_t *packet )
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
790 {
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
791 bdt_t *b = &table[ index( endpoint, TX, EVEN ) ];
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
792 uint8_t next;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
793
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
794 endpoint--;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
795 if ( endpoint >= NUM_ENDPOINTS )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
796 return;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
797 __disable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
798 //serial_print("txstate=");
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
799 //serial_phex(tx_state[ endpoint ]);
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
800 //serial_print("\n");
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
801 switch ( tx_state[ endpoint ] )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
802 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
803 case TX_STATE_BOTH_FREE_EVEN_FIRST:
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
804 next = TX_STATE_ODD_FREE;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
805 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
806 case TX_STATE_BOTH_FREE_ODD_FIRST:
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
807 b++;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
808 next = TX_STATE_EVEN_FREE;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
809 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
810 case TX_STATE_EVEN_FREE:
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
811 next = TX_STATE_NONE_FREE_ODD_FIRST;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
812 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
813 case TX_STATE_ODD_FREE:
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
814 b++;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
815 next = TX_STATE_NONE_FREE_EVEN_FIRST;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
816 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
817 default:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
818 if (tx_first[ endpoint ] == NULL)
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
819 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
820 tx_first[ endpoint ] = packet;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
821 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
822 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
823 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
824 tx_last[ endpoint ]->next = packet;
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
825 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
826 tx_last[ endpoint ] = packet;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
827 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
828 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
829 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
830
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
831 tx_state[ endpoint ] = next;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
832 b->addr = packet->buf;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
833 b->desc = BDT_DESC( packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
834 __enable_irq();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
835 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
836
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
837
109
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 104
diff changeset
838 void usb_device_reload()
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 104
diff changeset
839 {
177
3420e018ffbc Adding McHCK flash reload function and some cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 175
diff changeset
840 // MCHCK
3420e018ffbc Adding McHCK flash reload function and some cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 175
diff changeset
841 #if defined(_mk20dx128vlf5_)
186
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
842
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
843 // MCHCK Kiibohd Variant
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
844 // Check to see if PTA3 (has a pull-up) is connected to GND (usually via jumper)
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
845 // Only allow reload if the jumper is present (security)
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
846 GPIOA_PDDR &= ~(1<<3); // Input
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
847 PORTA_PCR3 = PORT_PCR_PFE | PORT_PCR_MUX(1); // Internal pull-up
177
3420e018ffbc Adding McHCK flash reload function and some cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 175
diff changeset
848
186
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
849 // Check for jumper
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
850 if ( GPIOA_PDIR & (1<<3) )
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
851 {
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
852 print( NL );
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
853 warn_print("Security jumper not present, cancelling firmware reload...");
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
854 info_msg("Replace jumper on middle 2 pins, or manually press the firmware reload button.");
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
855 }
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
856 else
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
857 {
192
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 186
diff changeset
858 // Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
859 for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
860 (&VBAT)[ pos ] = sys_reset_to_loader_magic[ pos ];
186
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
861 SOFTWARE_RESET();
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
862 }
50702debf278 Initial MatrixARM implementation
Jacob Alexander <haata@kiibohd.com>
parents: 177
diff changeset
863
177
3420e018ffbc Adding McHCK flash reload function and some cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 175
diff changeset
864 // Teensy 3.0 and 3.1
3420e018ffbc Adding McHCK flash reload function and some cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 175
diff changeset
865 #else
109
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 104
diff changeset
866 asm volatile("bkpt");
177
3420e018ffbc Adding McHCK flash reload function and some cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 175
diff changeset
867 #endif
109
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 104
diff changeset
868 }
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
869
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
870
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
871 void usb_isr()
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
872 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
873 uint8_t status, stat, t;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
874
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
875 //serial_print("isr");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
876 //status = USB0_ISTAT;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
877 //serial_phex(status);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
878 //serial_print("\n");
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
879 restart:
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
880 status = USB0_ISTAT;
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
881 /*
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
882 print("USB ISR STATUS: ");
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
883 printHex( status );
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
884 print( NL );
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
885 */
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
886
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
887 if ( (status & USB_INTEN_SOFTOKEN /* 04 */ ) )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
888 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
889 if ( usb_configuration )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
890 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
891 t = usb_reboot_timer;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
892 if ( t )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
893 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
894 usb_reboot_timer = --t;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
895 if ( !t )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
896 usb_device_reload();
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
897 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
898
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
899 // CDC Interface
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
900 t = usb_cdc_transmit_flush_timer;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
901 if ( t )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
902 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
903 usb_cdc_transmit_flush_timer = --t;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
904 if ( t == 0 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
905 usb_serial_flush_callback();
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
906 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
907
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
908 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
909 USB0_ISTAT = USB_INTEN_SOFTOKEN;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
910 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
911
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
912 if ( (status & USB_ISTAT_TOKDNE /* 08 */ ) )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
913 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
914 uint8_t endpoint;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
915 stat = USB0_STAT;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
916 //serial_print("token: ep=");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
917 //serial_phex(stat >> 4);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
918 //serial_print(stat & 0x08 ? ",tx" : ",rx");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
919 //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
920 endpoint = stat >> 4;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
921 if ( endpoint == 0 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
922 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
923 usb_control( stat );
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
924 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
925 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
926 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
927 bdt_t *b = stat2bufferdescriptor(stat);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
928 usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
929 #if 0
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
930 serial_print("ep:");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
931 serial_phex(endpoint);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
932 serial_print(", pid:");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
933 serial_phex(BDT_PID(b->desc));
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
934 serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
935 serial_print(", count:");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
936 serial_phex(b->desc >> 16);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
937 serial_print("\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
938 #endif
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
939 endpoint--; // endpoint is index to zero-based arrays
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
940
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
941 if ( stat & 0x08 )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
942 { // transmit
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
943 usb_free( packet );
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
944 packet = tx_first[ endpoint ];
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
945 if ( packet )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
946 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
947 //serial_print("tx packet\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
948 tx_first[endpoint] = packet->next;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
949 b->addr = packet->buf;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
950 switch ( tx_state[ endpoint ] )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
951 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
952 case TX_STATE_BOTH_FREE_EVEN_FIRST:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
953 tx_state[ endpoint ] = TX_STATE_ODD_FREE;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
954 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
955 case TX_STATE_BOTH_FREE_ODD_FIRST:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
956 tx_state[ endpoint ] = TX_STATE_EVEN_FREE;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
957 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
958 case TX_STATE_EVEN_FREE:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
959 tx_state[ endpoint ] = TX_STATE_NONE_FREE_ODD_FIRST;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
960 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
961 case TX_STATE_ODD_FREE:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
962 tx_state[ endpoint ] = TX_STATE_NONE_FREE_EVEN_FIRST;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
963 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
964 default:
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
965 break;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
966 }
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
967 b->desc = BDT_DESC( packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
968 } else {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
969 //serial_print("tx no packet\n");
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
970 switch ( tx_state[ endpoint ] )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
971 {
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
972 case TX_STATE_BOTH_FREE_EVEN_FIRST:
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
973 case TX_STATE_BOTH_FREE_ODD_FIRST:
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
974 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
975 case TX_STATE_EVEN_FREE:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
976 tx_state[ endpoint ] = TX_STATE_BOTH_FREE_EVEN_FIRST;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
977 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
978 case TX_STATE_ODD_FREE:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
979 tx_state[ endpoint ] = TX_STATE_BOTH_FREE_ODD_FIRST;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
980 break;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
981 default:
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
982 tx_state[ endpoint ] = ((uint32_t)b & 8)
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
983 ? TX_STATE_ODD_FREE
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
984 : TX_STATE_EVEN_FREE;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
985 break;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
986 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
987 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
988 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
989 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
990 { // receive
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
991 packet->len = b->desc >> 16;
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
992 if ( packet->len > 0 )
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
993 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
994 packet->index = 0;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
995 packet->next = NULL;
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
996 if ( rx_first[ endpoint ] == NULL )
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
997 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
998 //serial_print("rx 1st, epidx=");
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
999 //serial_phex(endpoint);
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1000 //serial_print(", packet=");
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1001 //serial_phex32((uint32_t)packet);
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1002 //serial_print("\n");
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1003 rx_first[ endpoint ] = packet;
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1004 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1005 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1006 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1007 //serial_print("rx Nth, epidx=");
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1008 //serial_phex(endpoint);
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1009 //serial_print(", packet=");
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1010 //serial_phex32((uint32_t)packet);
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1011 //serial_print("\n");
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1012 rx_last[ endpoint ]->next = packet;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1013 }
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1014 rx_last[ endpoint ] = packet;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1015 usb_rx_byte_count_data[ endpoint ] += packet->len;
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1016 // TODO: implement a per-endpoint maximum # of allocated packets
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1017 // so a flood of incoming data on 1 endpoint doesn't starve
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1018 // the others if the user isn't reading it regularly
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1019 packet = usb_malloc();
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1020 if ( packet )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1021 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1022 b->addr = packet->buf;
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1023 b->desc = BDT_DESC( 64, ((uint32_t)b & 8) ? DATA1 : DATA0 );
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1024 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1025 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1026 {
117
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1027 //serial_print("starving ");
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1028 //serial_phex(endpoint + 1);
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1029 //serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1030 b->desc = 0;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1031 usb_rx_memory_needed++;
5ffefdf89b37 Adding in USB module updates from Teensyduino 1.18rc2.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
1032 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1033 }
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1034 else
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1035 {
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1036 b->desc = BDT_DESC( 64, ((uint32_t)b & 8) ? DATA1 : DATA0 );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1037 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1038 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1039
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1040
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1041
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1042
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1043 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1044 USB0_ISTAT = USB_ISTAT_TOKDNE;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1045 goto restart;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1046 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1047
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1048
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1049 if ( status & USB_ISTAT_USBRST /* 01 */ )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1050 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1051 //serial_print("reset\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1052
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1053 // initialize BDT toggle bits
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1054 USB0_CTL = USB_CTL_ODDRST;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1055 ep0_tx_bdt_bank = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1056
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1057 // set up buffers to receive Setup and OUT packets
250
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1058 table[index( 0, RX, EVEN ) ].desc = BDT_DESC( EP0_SIZE, 0 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1059 table[index( 0, RX, EVEN ) ].addr = ep0_rx0_buf;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1060 table[index( 0, RX, ODD ) ].desc = BDT_DESC( EP0_SIZE, 0 );
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1061 table[index( 0, RX, ODD ) ].addr = ep0_rx1_buf;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1062 table[index( 0, TX, EVEN ) ].desc = 0;
afe96322d0c5 Code formatting cleanup.
Jacob Alexander <haata@kiibohd.com>
parents: 231
diff changeset
1063 table[index( 0, TX, ODD ) ].desc = 0;
170
85d74e5e1a95 More work on mk20dx128vlf5 port.
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
1064
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1065 // activate endpoint 0
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1066 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1067
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1068 // clear all ending interrupts
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1069 USB0_ERRSTAT = 0xFF;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1070 USB0_ISTAT = 0xFF;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1071
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1072 // set the address to zero during enumeration
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1073 USB0_ADDR = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1074
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1075 // enable other interrupts
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1076 USB0_ERREN = 0xFF;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1077 USB0_INTEN = USB_INTEN_TOKDNEEN |
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1078 USB_INTEN_SOFTOKEN |
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1079 USB_INTEN_STALLEN |
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1080 USB_INTEN_ERROREN |
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1081 USB_INTEN_USBRSTEN |
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1082 USB_INTEN_SLEEPEN;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1083
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1084 // is this necessary?
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1085 USB0_CTL = USB_CTL_USBENSOFEN;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1086 return;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1087 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1088
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1089
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1090 if ( (status & USB_ISTAT_STALL /* 80 */ ) )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1091 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1092 //serial_print("stall:\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1093 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1094 USB0_ISTAT = USB_ISTAT_STALL;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1095 }
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1096 if ( (status & USB_ISTAT_ERROR /* 02 */ ) )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1097 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1098 uint8_t err = USB0_ERRSTAT;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1099 USB0_ERRSTAT = err;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1100 //serial_print("err:");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1101 //serial_phex(err);
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1102 //serial_print("\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1103 USB0_ISTAT = USB_ISTAT_ERROR;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1104 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1105
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1106 if ( (status & USB_ISTAT_SLEEP /* 10 */ ) )
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1107 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1108 //serial_print("sleep\n");
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1109 USB0_ISTAT = USB_ISTAT_SLEEP;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1110 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1111 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1112
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1113
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1114
275
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 251
diff changeset
1115 uint8_t usb_init()
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1116 {
231
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1117 #ifdef UART_DEBUG
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1118 print("USB INIT"NL);
4afb170978e9 More preparation for NKRO USB for arm
Jacob Alexander <haata@kiibohd.com>
parents: 192
diff changeset
1119 #endif
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1120
275
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 251
diff changeset
1121 // If no USB cable is attached, do not initialize usb
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 251
diff changeset
1122 // XXX Test -HaaTa
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 251
diff changeset
1123 //if ( USB0_OTGISTAT & USB_OTGSTAT_ID )
326
05a09eda53fb Inital Remote Wakeup Support
Rowan Decker <Smasher816@gmail.com>
parents: 302
diff changeset
1124 // return 0;
275
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 251
diff changeset
1125
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
1126 // Clear out endpoints table
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
1127 for ( int i = 0; i <= NUM_ENDPOINTS * 4; i++ )
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
1128 {
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1129 table[i].desc = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1130 table[i].addr = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1131 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1132
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1133 // this basically follows the flowchart in the Kinetis
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1134 // Quick Reference User Guide, Rev. 1, 03/2012, page 141
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1135
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1136 // assume 48 MHz clock already running
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1137 // SIM - enable clock
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1138 SIM_SCGC4 |= SIM_SCGC4_USBOTG;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1139
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1140 // reset USB module
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1141 USB0_USBTRC0 = USB_USBTRC_USBRESET;
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
1142 while ( (USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0 ); // wait for reset to end
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1143
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1144 // set desc table base addr
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1145 USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1146 USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1147 USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1148
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1149 // clear all ISR flags
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1150 USB0_ISTAT = 0xFF;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1151 USB0_ERRSTAT = 0xFF;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1152 USB0_OTGISTAT = 0xFF;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1153
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1154 USB0_USBTRC0 |= 0x40; // undocumented bit
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1155
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1156 // enable USB
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1157 USB0_CTL = USB_CTL_USBENSOFEN;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1158 USB0_USBCTRL = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1159
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1160 // enable reset interrupt
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1161 USB0_INTEN = USB_INTEN_USBRSTEN;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1162
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1163 // enable interrupt in NVIC...
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
1164 NVIC_SET_PRIORITY( IRQ_USBOTG, 112 );
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
1165 NVIC_ENABLE_IRQ( IRQ_USBOTG );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1166
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1167 // enable d+ pullup
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1168 USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
275
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 251
diff changeset
1169
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 251
diff changeset
1170 return 1;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1171 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1172
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1173 // return 0 if the USB is not configured, or the configuration
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1174 // number selected by the HOST
175
c70f641581b5 McHCK USB WORKS!!
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
1175 uint8_t usb_configured()
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1176 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1177 return usb_configuration;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1178 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1179