-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdcacm.c
455 lines (399 loc) · 15.3 KB
/
cdcacm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/** @addtogroup 06 USB USB_Group
*
* @ingroup infrastructure_apis
*
* @file cdcacm.c USB Support routines group on STM32F4 and STM32F1
*
* @brief <b>USB Support routines group on STM32F4 and STM32F1</b>
*
* @version 1.0.0
*
* @author @htmlonly © @endhtmlonly 2022
* Evandro Souza <evandro.r.souza@gmail.com>
* @author @htmlonly © @endhtmlonly 2010
* Gareth McMullin <gareth@blacksphere.co.nz>
*
* @date 01 September 2022
*
* This library supports the USB on the STM32F4 and STM32F1
* series of ARM Cortex Microcontrollers by ST Microelectronics.
*
* LGPL License Terms ref lgpl_license
*/
/*
* This file is part of the PS/2 to MSX keyboard Converter and
* MSX Keyboard Subsystem Emulator projects, based on libopencm3 project.
*
* Copyright (C) 2022 Evandro Souza <evandro.r.souza@gmail.com>
*
* Based on CDCACM example of:
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cdcacm.h"
#include "usb_descriptors.h"
//Global variables
#if USE_USB == true
usbd_device *usb_dev;
int usb_configured;
bool nak_cleared[2][EP_UART_COMM_OUT]; //IN and OUT for first dimension and last defined OUT endpoint
uint8_t usbd_control_buffer[4 * USBD_DATA_BUFFER_SIZE]; // Buffer to be used for control requests.
/**
* Defines the struct of transmit and receive buffers
*
*/
extern struct sring con_tx_ring; //Declared on serial.c
extern struct sring con_rx_ring; //Declared on serial.c
#endif //#if USE_USB == true
extern struct sring uart_tx_ring; //Declared on serial.c
extern struct sring uart_rx_ring; //Declared on serial.c
/**
* X_ON & X_OFF control (Global variables)
*
@{*/
extern bool enable_xon_xoff; //Declared on serial.c
extern bool xon_condition; //Declared on serial.c
extern bool xoff_condition; //Declared on serial.c
extern bool xonoff_sendnow; //Declared on serial.c
extern bool ok_to_rx; //Declared on serial.c
/**@}*/
#if USE_USB == true
static void usb_cdc_set_state(usbd_device *usbd_dev, const uint16_t iface, const uint8_t ep)
{
uint8_t buf[10];
struct usb_cdc_notification *notif = (void*)buf;
/* We echo signals back to host as notification */
notif->bmRequestType = 0xA1;
notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
notif->wValue = 0;
notif->wIndex = iface;
notif->wLength = 2;
buf[8] = 3U;
buf[9] = 0U;
usbd_ep_write_packet(usbd_dev, ep, buf, sizeof(buf));
}
static enum usbd_request_return_codes
cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len,
void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
{
(void)complete;
(void)buf;
(void)usbd_dev;
switch (req->bmRequestType)
{
case SEND_ENCAPSULATED_COMMAND_bmRequestType: //0x21
if(req->bRequest == 0)
{
//send a RESPONSE_AVAILABLE notification
uint8_t loc_buf[4];
loc_buf[0] = 0x01;
loc_buf[1] = 0;
loc_buf[2] = 0;
loc_buf[3] = 0;
usbd_ep_write_packet(usbd_dev, EP_UART_COMM_IN, loc_buf, sizeof(uint32_t));
}
break; //case SEND_ENCAPSULATED_COMMAND_bmRequestType:
case GET_ENCAPSULATED_RESPONSE_bmRequestType: //0xA1
if(req->bRequest == GET_ENCAPSULATED_RESPONSE_bRequest) //0x01
{
usbd_ep_write_packet(usbd_dev, EP_UART_COMM_IN, &line_coding, sizeof(line_coding));
}
break; //case GET_ENCAPSULATED_RESPONSE_bmRequestType:
default:
break;
}
switch (req->bRequest)
{
case USB_CDC_REQ_SET_CONTROL_LINE_STATE: //0x22
/*
* This Linux cdc_acm driver requires this to be implemented
* even though it's optional in the CDC spec, and we don't
* advertise it in the ACM functional descriptor.
*/
/* We echo signals back to host as notification. */
usb_cdc_set_state(usbd_dev, INTF_UART_COMM, EP_UART_COMM_IN);
return USBD_REQ_HANDLED;
break;
case USB_CDC_REQ_SET_LINE_CODING: //0x20
if (*len < sizeof(struct usb_cdc_line_coding))
return USBD_REQ_NOTSUPP;
switch(req->wIndex)
{
case 0:
return USBD_REQ_NOTSUPP; // Ignore on CON Port
break;
case 2:
usart_update_comm_param((struct usb_cdc_line_coding*)*buf);
return USBD_REQ_HANDLED;
break;
default:
return USBD_REQ_HANDLED;
break;
}
break;
case USB_CDC_REQ_GET_LINE_CODING:
*buf = (uint8_t *)&line_coding;
usbd_ep_write_packet(usbd_dev, EP_UART_COMM_IN, &line_coding, sizeof(line_coding));
return USBD_REQ_HANDLED;
break;
default:
return USBD_REQ_NOTSUPP;
break;
}
return USBD_REQ_NOTSUPP;
}
static void cdcacm_con_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
{
(void)usbd_dev;
(void)ep;
char buf_con[USBD_DATA_BUFFER_SIZE];
uint16_t len_con, result = 0;
len_con = (uint16_t)usbd_ep_read_packet(usbd_dev, EP_CON_DATA_OUT, buf_con, USBD_DATA_BUFFER_SIZE);
if (len_con)
{
for(uint16_t i = 0; (i < len_con) && (result != 0xFFFF); i++)
while( (result = ring_put_ch(&con_rx_ring, buf_con[i])) == 0xFFFF) __asm("nop");
if(result > ((3 * (uart_tx_ring.bufSzMask + 1)) >> 2)) //X_OFF_TRIGGER
{
//Put EP in nak. On con_rx_ring read, check con_rx_ring room (X_ON_TRIGGER) to clear nak.
nak_cleared[(EP_CON_DATA_OUT & USB_REQ_TYPE_IN) >> 8][EP_CON_DATA_OUT & ~USB_REQ_TYPE_IN] = false;
usbd_ep_nak_set(usbd_dev, EP_CON_DATA_OUT, 1);
}
}
}
static void cdcacm_con_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
{
(void)usbd_dev;
(void)ep;
char buf[USBD_DATA_BUFFER_SIZE];
uint16_t i, len, max_transf, qty_accepted, local_getptr;
len = QTTY_CHAR_IN(con_tx_ring);
if(len)
{
max_transf = (len > (USBD_DATA_BUFFER_SIZE - 1)) ? (USBD_DATA_BUFFER_SIZE - 1) : len;
local_getptr = con_tx_ring.get_ptr;
CHECK_XONXOFF_SENDNOW_START_WITH_OPEN_BRACKET
// Transmit char through usb console.
buf[0] = data;
for(i = 1; i < max_transf; i++)
{
#if defined CHECK_INDEX
check_idx_u16(i, (uintptr_t)buf, USBD_DATA_BUFFER_SIZE);
#endif
buf[i] = con_tx_ring.data[local_getptr++];
local_getptr &= con_tx_ring.bufSzMask;
}
CHECK_XONXOFF_SENDNOW_CLOSE_BRACKET
else //else CHECK_XONXOFF_SENDNOW_START_WITH_OPEN_BRACKET
{
for(i = 0; i < max_transf; i++)
{
#if defined CHECK_INDEX
check_idx_u16(i, (uintptr_t)buf, USBD_DATA_BUFFER_SIZE);
#endif
buf[i] = con_tx_ring.data[local_getptr++];
local_getptr &= con_tx_ring.bufSzMask;
}
} //else CHECK_XONXOFF_SENDNOW_START_WITH_OPEN_BRACKET
qty_accepted = usbd_ep_write_packet(usb_dev, EP_CON_DATA_IN, buf, max_transf);
//This following two passes warranties that con_tx_ring.get_ptr to be an atomic update.
con_tx_ring.get_ptr = (con_tx_ring.get_ptr + qty_accepted) & con_tx_ring.bufSzMask;
}
else //if(len)
{ //As len = 0, set this EP to NAK requests, but first check XONXOFF SENDNOW
CHECK_XONXOFF_SENDNOW_START_WITH_OPEN_BRACKET
// Put char in console ring.
ring_put_ch(&con_tx_ring, data);
buf[0] = data;
usbd_ep_write_packet(usb_dev, EP_CON_DATA_IN, buf, 1);
CHECK_XONXOFF_SENDNOW_CLOSE_BRACKET //CHECK_XONXOFF_SENDNOW_START_WITH_OPEN_BRACKET
else //else CHECK_XONXOFF_SENDNOW_START_WITH_OPEN_BRACKET
usbd_ep_nak_set(usbd_dev, EP_CON_DATA_IN, 1);
}
}
static void cdcacm_uart_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
{
(void)usbd_dev;
(void)ep;
uint8_t buf_uart[USBD_DATA_BUFFER_SIZE + 1];
uint16_t result;
uint32_t len;
len = (uint16_t)usbd_ep_read_packet(usbd_dev, EP_UART_DATA_OUT, (char*)buf_uart, USBD_DATA_BUFFER_SIZE);
if (len && ok_to_rx)
{
for(uint16_t i = 0; (i < len) && (result != 0xFFFF); i++)
while( (result = ring_put_ch(&uart_tx_ring, buf_uart[i])) == 0xFFFF)
do_dma_usart_tx_ring(len);
do_dma_usart_tx_ring(len);
if(result > ((3 * (uart_tx_ring.bufSzMask + 1)) >> 2))
{
//Put EP in nak. On uart_tx_ring read, check uart_rx_ring room (X_ON_TRIGGER) to clear nak.
set_nak_endpoint(EP_UART_DATA_OUT);
} //if(result > (3 * (uart_tx_ring.bufSzMask + 1) / 4))
} //if (len)
}
static void cdcacm_uart_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
{
(void)usbd_dev;
(void)ep;
char buf[USBD_DATA_BUFFER_SIZE];
uint16_t i, len, max_transf, qty_accepted, local_getptr;
len = QTTY_CHAR_IN(uart_rx_ring);
if(len)
{
max_transf = (len > (USBD_DATA_BUFFER_SIZE - 1)) ? (USBD_DATA_BUFFER_SIZE - 1) : len;
local_getptr = uart_rx_ring.get_ptr;
for(i = 0; i < max_transf; i++)
{
#if defined CHECK_INDEX
check_idx_u16(i, (uintptr_t)buf, USBD_DATA_BUFFER_SIZE);
#endif
buf[i] = uart_rx_ring.data[local_getptr++];
local_getptr &= (uart_rx_ring.bufSzMask);
}
qty_accepted = usbd_ep_write_packet(usb_dev, EP_UART_DATA_IN, buf, max_transf);
//This following two passes warranties that uart_rx_ring.get_ptr to be an atomic update.
uart_rx_ring.get_ptr = (uart_rx_ring.get_ptr + qty_accepted) & (uart_rx_ring.bufSzMask);
}
}
static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
{
usb_configured = wValue;
//Console interface
usbd_ep_setup(usbd_dev, EP_CON_COMM_IN, USB_ENDPOINT_ATTR_INTERRUPT, COMM_PACKET_SIZE, NULL);
usbd_ep_setup(usbd_dev, EP_CON_DATA_OUT, USB_ENDPOINT_ATTR_BULK, USBD_DATA_BUFFER_SIZE, cdcacm_con_data_rx_cb);
usbd_ep_setup(usbd_dev, EP_CON_DATA_IN, USB_ENDPOINT_ATTR_BULK, USBD_DATA_BUFFER_SIZE, cdcacm_con_data_tx_cb);
//UART interface
usbd_ep_setup(usbd_dev, EP_UART_COMM_IN, USB_ENDPOINT_ATTR_INTERRUPT, COMM_PACKET_SIZE, NULL);
usbd_ep_setup(usbd_dev, EP_UART_DATA_OUT, USB_ENDPOINT_ATTR_BULK, USBD_DATA_BUFFER_SIZE, cdcacm_uart_data_rx_cb);
usbd_ep_setup(usbd_dev, EP_UART_DATA_IN, USB_ENDPOINT_ATTR_BULK, USBD_DATA_BUFFER_SIZE, cdcacm_uart_data_tx_cb);
usbd_register_control_callback(usbd_dev,
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
cdcacm_control_request);
/* Notify the host that DCD is asserted.
* Allows the use of /dev/tty* devices on *BSD/MacOS
*/
usb_cdc_set_state(usbd_dev, INTF_CON_COMM, EP_CON_DATA_OUT);
usb_cdc_set_state(usbd_dev, INTF_UART_COMM, EP_UART_DATA_OUT);
}
//It is used to start a communication pipe to allow the EP_??_DATA_IN callback continues send to host as needed.
//It returns ring->get_ptr updated.
void first_put_ring_content_onto_ep(struct sring *ring, uint8_t ep)
{
if(usb_configured)
{
char buf[USBD_DATA_BUFFER_SIZE];
uint16_t i, len, max_transf, qty_accepted, local_getptr;
len = (ring->bufSzMask + 1 - ring->get_ptr + ring->put_ptr) & ring->bufSzMask;
local_getptr = ring->get_ptr; //update ring->get_ptr only at the end of the process
if(len)
{
clear_nak_endpoint(ep); //disable nak on ep
max_transf = (len > (USBD_DATA_BUFFER_SIZE - 1)) ? (USBD_DATA_BUFFER_SIZE - 1) : len;
for(i = 0; i < max_transf; i++)
{
#if defined CHECK_INDEX
check_idx_u16(i, (uintptr_t)buf, USBD_DATA_BUFFER_SIZE);
#endif
buf[i] = ring->data[local_getptr++];
local_getptr &= ring->bufSzMask;
}
qty_accepted = usbd_ep_write_packet(usb_dev, ep, buf, max_transf);
//This following two passes warranties that ring->get_ptr to be an atomic update.
ring->get_ptr = (ring->get_ptr + qty_accepted) & ring->bufSzMask;
}
} //if(usb_configured)
}
void disable_usb(void)
{
/* USB control register (USB_CNTR)
Bit 1 PDWN: Power down
This bit is used to completely switch off all USB-related analog parts if it is required to
completely disable the USB peripheral for any reason. When this bit is set, the USB
peripheral is disconnected from the transceivers and it cannot be used.
0: Exit Power Down.
1: Enter Power down mode.*/
#if MCU == STM32F103
USB_CNTR_REG |= USB_CNTR_REG_PDWN;
#endif //#if MCU == STM32F103
# if (MCU == STM32F401)
// Explicitly disable DP pullup
OTG_FS_DCTL |= OTG_DCTL_SDIS;
rcc_periph_clock_disable(USB_RCC_CRC);
# endif //# if (MCU == STM32F401)
rcc_periph_clock_disable(USB_RCC_OTGFS);
}
static void usb_reset(void)
{
# if (MCU == STM32F103)
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12);
gpio_clear(GPIOA, GPIO12);
for (uint32_t i = 0; i < 0x500000; i++) __asm__("nop");
gpio_set(GPIOA, GPIO12);
# endif //# if (MCU == STM32F103)
# if (MCU == STM32F401)
for (uint32_t i = 0; i < 0x500000; i++) __asm__("nop");
# endif //# if (MCU == STM32F401)
}
void set_nak_endpoint(uint8_t ep)
{
usbd_ep_nak_set(usb_dev, ep, 1);
#if defined CHECK_INDEX
check_idx_u16(ep & (uint8_t)~USB_REQ_TYPE_IN, (uintptr_t)nak_cleared, sizeof(nak_cleared));
#endif
nak_cleared[(ep & USB_REQ_TYPE_IN) >> 8][ep & (uint8_t)~USB_REQ_TYPE_IN] = false;
}
void clear_nak_endpoint(uint8_t ep)
{
usbd_ep_nak_set(usb_dev, ep, 0);
#if defined CHECK_INDEX
check_idx_u16(ep & (uint8_t)~USB_REQ_TYPE_IN, (uintptr_t)nak_cleared, sizeof(nak_cleared));
#endif
nak_cleared[(ep & USB_REQ_TYPE_IN) >> 8][ep & (uint8_t)~USB_REQ_TYPE_IN] = true;
}
void cdcacm_init(void)
{
usb_reset();
#if MCU == STM32F401
// Enable peripherals
rcc_periph_clock_enable(USB_RCC_CRC);
gpio_mode_setup(OTG_FS_DM_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, OTG_FS_DM_PIN | OTG_FS_DP_PIN);
gpio_set_af(OTG_FS_DM_PORT, GPIO_AF10, OTG_FS_DM_PIN | OTG_FS_DP_PIN);
#endif //#if MCU == STM32F401
usb_dev = usbd_init(&USB_DRIVER, &dev_desc, &config, usb_strings, NUM_STRINGS,
usbd_control_buffer, sizeof(usbd_control_buffer));
#if MCU == STM32F401
//Disable VBUS sensing => https://github.com/libopencm3/libopencm3/pull/1256#
OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS | OTG_GCCFG_PWRDWN;
OTG_FS_GCCFG &= ~(OTG_GCCFG_VBUSBSEN | OTG_GCCFG_VBUSASEN);
#if USART_PORT == USART1
//Now, if STM32F401 and USART1, recover A9 and A10 back to serial 1:
gpio_set_af(GPIO_BANK_USART_TX, GPIO_AF7, GPIO_PIN_USART_TX | GPIO_PIN_USART_RX);
#endif //#if USART_PORT == USART1
#endif //#if MCU == STM32F401
usbd_register_set_config_callback(usb_dev, cdcacm_set_config);
nvic_set_priority(USB_NVIC, IRQ_PRI_USB);
nvic_enable_irq(USB_NVIC);
}
/*************************************************************************************************/
/******************************************** ISR ************************************************/
/*************************************************************************************************/
USB_ISR
{
usbd_poll(usb_dev);
} //USB_ISR
#endif //#if USE_USB == true