diff options
Diffstat (limited to 'lib/chibios-contrib/testhal/KINETIS/FRDM-K20D50M/USB_SERIAL/usbcfg.c')
-rw-r--r-- | lib/chibios-contrib/testhal/KINETIS/FRDM-K20D50M/USB_SERIAL/usbcfg.c | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/KINETIS/FRDM-K20D50M/USB_SERIAL/usbcfg.c b/lib/chibios-contrib/testhal/KINETIS/FRDM-K20D50M/USB_SERIAL/usbcfg.c new file mode 100644 index 000000000..c25a561a1 --- /dev/null +++ b/lib/chibios-contrib/testhal/KINETIS/FRDM-K20D50M/USB_SERIAL/usbcfg.c | |||
@@ -0,0 +1,337 @@ | |||
1 | /* | ||
2 | (C) 2015-2016 Jonathan Struebel | ||
3 | Based on ChibiOS USB_CDC demo - Copyright (C) 2006..2016 Giovanni Di Sirio | ||
4 | |||
5 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | you may not use this file except in compliance with the License. | ||
7 | You may obtain a copy of the License at | ||
8 | |||
9 | http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | |||
11 | Unless required by applicable law or agreed to in writing, software | ||
12 | distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | See the License for the specific language governing permissions and | ||
15 | limitations under the License. | ||
16 | */ | ||
17 | |||
18 | #include "hal.h" | ||
19 | |||
20 | /* Virtual serial port over USB.*/ | ||
21 | SerialUSBDriver SDU1; | ||
22 | |||
23 | /* | ||
24 | * Endpoints to be used for USBD1. | ||
25 | */ | ||
26 | #define USBD1_DATA_REQUEST_EP 1 | ||
27 | #define USBD1_DATA_AVAILABLE_EP 1 | ||
28 | #define USBD1_INTERRUPT_REQUEST_EP 2 | ||
29 | |||
30 | /* | ||
31 | * USB Device Descriptor. | ||
32 | */ | ||
33 | static const uint8_t vcom_device_descriptor_data[18] = { | ||
34 | USB_DESC_DEVICE (0x0110, /* bcdUSB (1.1). */ | ||
35 | 0x02, /* bDeviceClass (CDC). */ | ||
36 | 0x00, /* bDeviceSubClass. */ | ||
37 | 0x00, /* bDeviceProtocol. */ | ||
38 | 0x40, /* bMaxPacketSize. */ | ||
39 | 0x0179, /* idVendor. */ | ||
40 | 0x0001, /* idProduct. */ | ||
41 | 0x0200, /* bcdDevice. */ | ||
42 | 1, /* iManufacturer. */ | ||
43 | 2, /* iProduct. */ | ||
44 | 3, /* iSerialNumber. */ | ||
45 | 1) /* bNumConfigurations. */ | ||
46 | }; | ||
47 | |||
48 | /* | ||
49 | * Device Descriptor wrapper. | ||
50 | */ | ||
51 | static const USBDescriptor vcom_device_descriptor = { | ||
52 | sizeof vcom_device_descriptor_data, | ||
53 | vcom_device_descriptor_data | ||
54 | }; | ||
55 | |||
56 | /* Configuration Descriptor tree for a CDC.*/ | ||
57 | static const uint8_t vcom_configuration_descriptor_data[67] = { | ||
58 | /* Configuration Descriptor.*/ | ||
59 | USB_DESC_CONFIGURATION(67, /* wTotalLength. */ | ||
60 | 0x02, /* bNumInterfaces. */ | ||
61 | 0x01, /* bConfigurationValue. */ | ||
62 | 0, /* iConfiguration. */ | ||
63 | 0xC0, /* bmAttributes (self powered). */ | ||
64 | 50), /* bMaxPower (100mA). */ | ||
65 | /* Interface Descriptor.*/ | ||
66 | USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */ | ||
67 | 0x00, /* bAlternateSetting. */ | ||
68 | 0x01, /* bNumEndpoints. */ | ||
69 | 0x02, /* bInterfaceClass (Communications | ||
70 | Interface Class, CDC section | ||
71 | 4.2). */ | ||
72 | 0x02, /* bInterfaceSubClass (Abstract | ||
73 | Control Model, CDC section 4.3). */ | ||
74 | 0x01, /* bInterfaceProtocol (AT commands, | ||
75 | CDC section 4.4). */ | ||
76 | 0), /* iInterface. */ | ||
77 | /* Header Functional Descriptor (CDC section 5.2.3).*/ | ||
78 | USB_DESC_BYTE (5), /* bLength. */ | ||
79 | USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */ | ||
80 | USB_DESC_BYTE (0x00), /* bDescriptorSubtype (Header | ||
81 | Functional Descriptor. */ | ||
82 | USB_DESC_BCD (0x0110), /* bcdCDC. */ | ||
83 | /* Call Management Functional Descriptor. */ | ||
84 | USB_DESC_BYTE (5), /* bFunctionLength. */ | ||
85 | USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */ | ||
86 | USB_DESC_BYTE (0x01), /* bDescriptorSubtype (Call Management | ||
87 | Functional Descriptor). */ | ||
88 | USB_DESC_BYTE (0x00), /* bmCapabilities (D0+D1). */ | ||
89 | USB_DESC_BYTE (0x01), /* bDataInterface. */ | ||
90 | /* ACM Functional Descriptor.*/ | ||
91 | USB_DESC_BYTE (4), /* bFunctionLength. */ | ||
92 | USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */ | ||
93 | USB_DESC_BYTE (0x02), /* bDescriptorSubtype (Abstract | ||
94 | Control Management Descriptor). */ | ||
95 | USB_DESC_BYTE (0x02), /* bmCapabilities. */ | ||
96 | /* Union Functional Descriptor.*/ | ||
97 | USB_DESC_BYTE (5), /* bFunctionLength. */ | ||
98 | USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */ | ||
99 | USB_DESC_BYTE (0x06), /* bDescriptorSubtype (Union | ||
100 | Functional Descriptor). */ | ||
101 | USB_DESC_BYTE (0x00), /* bMasterInterface (Communication | ||
102 | Class Interface). */ | ||
103 | USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class | ||
104 | Interface). */ | ||
105 | /* Endpoint 2 Descriptor.*/ | ||
106 | USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80, | ||
107 | 0x03, /* bmAttributes (Interrupt). */ | ||
108 | 0x0008, /* wMaxPacketSize. */ | ||
109 | 0xFF), /* bInterval. */ | ||
110 | /* Interface Descriptor.*/ | ||
111 | USB_DESC_INTERFACE (0x01, /* bInterfaceNumber. */ | ||
112 | 0x00, /* bAlternateSetting. */ | ||
113 | 0x02, /* bNumEndpoints. */ | ||
114 | 0x0A, /* bInterfaceClass (Data Class | ||
115 | Interface, CDC section 4.5). */ | ||
116 | 0x00, /* bInterfaceSubClass (CDC section | ||
117 | 4.6). */ | ||
118 | 0x00, /* bInterfaceProtocol (CDC section | ||
119 | 4.7). */ | ||
120 | 0x00), /* iInterface. */ | ||
121 | /* Endpoint 1 Descriptor.*/ | ||
122 | USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/ | ||
123 | 0x02, /* bmAttributes (Bulk). */ | ||
124 | 0x0040, /* wMaxPacketSize. */ | ||
125 | 0x00), /* bInterval. */ | ||
126 | /* Endpoint 1 Descriptor.*/ | ||
127 | USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/ | ||
128 | 0x02, /* bmAttributes (Bulk). */ | ||
129 | 0x0040, /* wMaxPacketSize. */ | ||
130 | 0x00) /* bInterval. */ | ||
131 | }; | ||
132 | |||
133 | /* | ||
134 | * Configuration Descriptor wrapper. | ||
135 | */ | ||
136 | static const USBDescriptor vcom_configuration_descriptor = { | ||
137 | sizeof vcom_configuration_descriptor_data, | ||
138 | vcom_configuration_descriptor_data | ||
139 | }; | ||
140 | |||
141 | /* | ||
142 | * U.S. English language identifier. | ||
143 | */ | ||
144 | static const uint8_t vcom_string0[] = { | ||
145 | USB_DESC_BYTE(4), /* bLength. */ | ||
146 | USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ | ||
147 | USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */ | ||
148 | }; | ||
149 | |||
150 | /* | ||
151 | * Vendor string. | ||
152 | */ | ||
153 | static const uint8_t vcom_string1[] = { | ||
154 | USB_DESC_BYTE(2+2*7), /* bLength. */ | ||
155 | USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ | ||
156 | 'N', 0, 'o', 0, 'p', 0, 'e', 0, 'L', 0, 'a', 0, 'b', 0, | ||
157 | }; | ||
158 | |||
159 | /* | ||
160 | * Device Description string. | ||
161 | */ | ||
162 | static const uint8_t vcom_string2[] = { | ||
163 | USB_DESC_BYTE(2+5*2), /* bLength. */ | ||
164 | USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ | ||
165 | 'C', 0, 'h', 0, 'T', 0, 's', 0, 'y', 0, | ||
166 | }; | ||
167 | |||
168 | /* | ||
169 | * Serial Number string. | ||
170 | */ | ||
171 | static const uint8_t vcom_string3[] = { | ||
172 | USB_DESC_BYTE(8), /* bLength. */ | ||
173 | USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ | ||
174 | '0' + CH_KERNEL_MAJOR, 0, | ||
175 | '0' + CH_KERNEL_MINOR, 0, | ||
176 | '0' + CH_KERNEL_PATCH, 0 | ||
177 | }; | ||
178 | |||
179 | /* | ||
180 | * Strings wrappers array. | ||
181 | */ | ||
182 | static const USBDescriptor vcom_strings[] = { | ||
183 | {sizeof vcom_string0, vcom_string0}, | ||
184 | {sizeof vcom_string1, vcom_string1}, | ||
185 | {sizeof vcom_string2, vcom_string2}, | ||
186 | {sizeof vcom_string3, vcom_string3} | ||
187 | }; | ||
188 | |||
189 | /* | ||
190 | * Handles the GET_DESCRIPTOR callback. All required descriptors must be | ||
191 | * handled here. | ||
192 | */ | ||
193 | static const USBDescriptor *get_descriptor(USBDriver *usbp, | ||
194 | uint8_t dtype, | ||
195 | uint8_t dindex, | ||
196 | uint16_t lang) { | ||
197 | (void)usbp; | ||
198 | (void)lang; | ||
199 | switch (dtype) { | ||
200 | case USB_DESCRIPTOR_DEVICE: | ||
201 | return &vcom_device_descriptor; | ||
202 | case USB_DESCRIPTOR_CONFIGURATION: | ||
203 | return &vcom_configuration_descriptor; | ||
204 | case USB_DESCRIPTOR_STRING: | ||
205 | if (dindex < 4) | ||
206 | return &vcom_strings[dindex]; | ||
207 | } | ||
208 | return NULL; | ||
209 | } | ||
210 | |||
211 | /** | ||
212 | * @brief IN EP1 state. | ||
213 | */ | ||
214 | static USBInEndpointState ep1instate; | ||
215 | |||
216 | /** | ||
217 | * @brief OUT EP1 state. | ||
218 | */ | ||
219 | static USBOutEndpointState ep1outstate; | ||
220 | |||
221 | /** | ||
222 | * @brief EP1 initialization structure (both IN and OUT). | ||
223 | */ | ||
224 | static const USBEndpointConfig ep1config = { | ||
225 | USB_EP_MODE_TYPE_BULK, | ||
226 | NULL, | ||
227 | sduDataTransmitted, | ||
228 | sduDataReceived, | ||
229 | 0x0040, | ||
230 | 0x0040, | ||
231 | &ep1instate, | ||
232 | &ep1outstate, | ||
233 | 2, | ||
234 | NULL | ||
235 | }; | ||
236 | |||
237 | /** | ||
238 | * @brief IN EP2 state. | ||
239 | */ | ||
240 | static USBInEndpointState ep2instate; | ||
241 | |||
242 | /** | ||
243 | * @brief EP2 initialization structure (IN only). | ||
244 | */ | ||
245 | static const USBEndpointConfig ep2config = { | ||
246 | USB_EP_MODE_TYPE_INTR, | ||
247 | NULL, | ||
248 | sduInterruptTransmitted, | ||
249 | NULL, | ||
250 | 0x0010, | ||
251 | 0x0000, | ||
252 | &ep2instate, | ||
253 | NULL, | ||
254 | 1, | ||
255 | NULL | ||
256 | }; | ||
257 | |||
258 | /* | ||
259 | * Handles the USB driver global events. | ||
260 | */ | ||
261 | static void usb_event(USBDriver *usbp, usbevent_t event) { | ||
262 | extern SerialUSBDriver SDU1; | ||
263 | |||
264 | switch (event) { | ||
265 | case USB_EVENT_ADDRESS: | ||
266 | return; | ||
267 | case USB_EVENT_CONFIGURED: | ||
268 | chSysLockFromISR(); | ||
269 | |||
270 | /* Enables the endpoints specified into the configuration. | ||
271 | Note, this callback is invoked from an ISR so I-Class functions | ||
272 | must be used.*/ | ||
273 | usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config); | ||
274 | usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config); | ||
275 | |||
276 | /* Resetting the state of the CDC subsystem.*/ | ||
277 | sduConfigureHookI(&SDU1); | ||
278 | |||
279 | chSysUnlockFromISR(); | ||
280 | return; | ||
281 | case USB_EVENT_RESET: | ||
282 | /* Falls into. */ | ||
283 | case USB_EVENT_UNCONFIGURED: | ||
284 | /* Falls into. */ | ||
285 | case USB_EVENT_SUSPEND: | ||
286 | chSysLockFromISR(); | ||
287 | |||
288 | /* Disconnection event on suspend.*/ | ||
289 | sduSuspendHookI(&SDU1); | ||
290 | |||
291 | chSysUnlockFromISR(); | ||
292 | return; | ||
293 | case USB_EVENT_WAKEUP: | ||
294 | chSysLockFromISR(); | ||
295 | |||
296 | /* Disconnection event on suspend */ | ||
297 | sduWakeupHookI(&SDU1); | ||
298 | |||
299 | chSysUnlockFromISR(); | ||
300 | return; | ||
301 | case USB_EVENT_STALLED: | ||
302 | return; | ||
303 | } | ||
304 | return; | ||
305 | } | ||
306 | |||
307 | /* | ||
308 | * Handles the USB driver global events. | ||
309 | */ | ||
310 | static void sof_handler(USBDriver *usbp) { | ||
311 | |||
312 | (void)usbp; | ||
313 | |||
314 | osalSysLockFromISR(); | ||
315 | sduSOFHookI(&SDU1); | ||
316 | osalSysUnlockFromISR(); | ||
317 | } | ||
318 | |||
319 | /* | ||
320 | * USB driver configuration. | ||
321 | */ | ||
322 | const USBConfig usbcfg = { | ||
323 | usb_event, | ||
324 | get_descriptor, | ||
325 | sduRequestsHook, | ||
326 | sof_handler | ||
327 | }; | ||
328 | |||
329 | /* | ||
330 | * Serial over USB driver configuration. | ||
331 | */ | ||
332 | const SerialUSBConfig serusbcfg = { | ||
333 | &USBD1, | ||
334 | USBD1_DATA_REQUEST_EP, | ||
335 | USBD1_DATA_AVAILABLE_EP, | ||
336 | USBD1_INTERRUPT_REQUEST_EP | ||
337 | }; | ||