-
Notifications
You must be signed in to change notification settings - Fork 22
/
usb_serial.c
360 lines (289 loc) · 9.19 KB
/
usb_serial.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
/*
usb_serial.c - low level functions for transmitting bytes via the USB virtual serial port
Part of grblHAL
Copyright (c) 2017-2021 Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __usb_serial_h__
#define __usb_serial_h__
#include "driver.h"
#if USB_SERIAL_CDC
#include <string.h>
#include "chip.h"
#include "app_usbd_cfg.h"
#include "cdc_vcom.h"
#include "grbl/hal.h"
#include "grbl/protocol.h"
extern const USBD_HW_API_T hw_api;
extern const USBD_CORE_API_T core_api;
extern const USBD_CDC_API_T cdc_api;
static const USBD_API_T g_usbApi = {
&hw_api,
&core_api,
0,
0,
0,
&cdc_api,
0,
0x02221101,
};
const USBD_API_T *g_pUsbApi = &g_usbApi;
static USBD_HANDLE_T g_hUsb;
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command;
/* Initialize pin and clocks for USB0/USB1 port */
static void usb_pin_clk_init(void)
{
/* enable USB PLL and clocks */
Chip_USB_Init();
/* enable USB 1 port on the board */
/* set a bias to this pin to enable the host system to detect and begin enumeration */
Chip_IOCON_PinMux(LPC_IOCON, 2, 9, IOCON_MODE_INACT, IOCON_FUNC1); /* USB_CONNECT */
Chip_IOCON_PinMux(LPC_IOCON, 0, 29, IOCON_MODE_INACT, IOCON_FUNC1); /* P0.29 D1+, P0.30 D1- */
Chip_IOCON_PinMux(LPC_IOCON, 0, 30, IOCON_MODE_INACT, IOCON_FUNC1);
LPC_USB->USBClkCtrl = 0x12; /* Dev, AHB clock enable */
while ((LPC_USB->USBClkSt & 0x12) != 0x12);
}
/**
* @brief Handle interrupt from USB0
* @return Nothing
*/
void USB_IRQHandler(void)
{
USBD_API->hw->ISR(g_hUsb);
}
/* Find the address of interface descriptor for given class type. */
USB_INTERFACE_DESCRIPTOR *find_IntfDesc(const uint8_t *pDesc, uint32_t intfClass)
{
USB_COMMON_DESCRIPTOR *pD;
USB_INTERFACE_DESCRIPTOR *pIntfDesc = 0;
uint32_t next_desc_adr;
pD = (USB_COMMON_DESCRIPTOR *) pDesc;
next_desc_adr = (uint32_t) pDesc;
while (pD->bLength) {
/* is it interface descriptor */
if (pD->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE) {
pIntfDesc = (USB_INTERFACE_DESCRIPTOR *) pD;
/* did we find the right interface descriptor */
if (pIntfDesc->bInterfaceClass == intfClass) {
break;
}
}
pIntfDesc = 0;
next_desc_adr = (uint32_t) pD + pD->bLength;
pD = (USB_COMMON_DESCRIPTOR *) next_desc_adr;
}
return pIntfDesc;
}
#include "grbl/grbl.h"
static stream_rx_buffer_t rxbuf = {0};
static stream_block_tx_buffer_t txbuf = {0};
//
// Returns number of free characters in the input buffer
//
static uint16_t usbRxFree (void)
{
uint16_t tail = rxbuf.tail, head = rxbuf.head;
return RX_BUFFER_SIZE - BUFCOUNT(head, tail, RX_BUFFER_SIZE);
}
//
// Flushes the input buffer
//
static void usbRxFlush (void)
{
rxbuf.tail = rxbuf.head;
}
//
// Flushes and adds a CAN character to the input buffer
//
static void usbRxCancel (void)
{
rxbuf.data[rxbuf.head] = ASCII_CAN;
rxbuf.tail = rxbuf.head;
rxbuf.head = BUFNEXT(rxbuf.head, rxbuf);
}
//
// Writes a single character to the USB output stream, blocks if buffer full
//
static bool usbPutC (const char c)
{
static uint8_t buf[1];
*buf = c;
while(vcom_write(buf, 1) != 1) {
if(!hal.stream_blocking_callback())
return false;
}
return true;
}
static inline bool _usb_write()
{
size_t length = txbuf.length;
txbuf.s = txbuf.data;
if(vcom_connected()) while(length) {
txbuf.length = vcom_write((uint8_t *)txbuf.s, length > 64 ? 64 : length);
txbuf.s += txbuf.length;
length -= txbuf.length;
if(!hal.stream_blocking_callback())
return false;
}
txbuf.length = 0;
txbuf.s = txbuf.data;
return true;
}
//
// Writes a null terminated string to the USB output stream, blocks if buffer full
// Buffers string up to EOL (LF) before transmitting
//
static void usbWriteS (const char *s)
{
size_t length = strlen(s);
if(length == 0)
return;
if(txbuf.length && (txbuf.length + length) > txbuf.max_length) {
if(!_usb_write())
return;
}
while(length > txbuf.max_length) {
txbuf.length = txbuf.max_length;
memcpy(txbuf.s, s, txbuf.length);
if(!_usb_write())
return;
length -= txbuf.max_length;
s += txbuf.max_length;
}
if(length) {
memcpy(txbuf.s, s, length);
txbuf.length += length;
txbuf.s += length;
if(s[length - 1] == ASCII_LF)
_usb_write();
}
}
//
// Writes a number of characters from string to the USB output stream, blocks if buffer full
//
static void usbWrite (const char *s, uint16_t length)
{
if(length == 0)
return;
if(txbuf.length && (txbuf.length + length) > txbuf.max_length) {
if(!_usb_write())
return;
}
while(length > txbuf.max_length) {
txbuf.length = txbuf.max_length;
memcpy(txbuf.s, s, txbuf.length);
if(!_usb_write())
return;
length -= txbuf.max_length;
s += txbuf.max_length;
}
if(length) {
memcpy(txbuf.s, s, length);
txbuf.length += length;
txbuf.s += length;
_usb_write();
}
}
//
// usbGetC - returns -1 if no data available
//
static int16_t usbGetC (void)
{
uint_fast16_t tail = rxbuf.tail; // Get buffer pointer
if(tail == rxbuf.head)
return -1; // no data available
char data = rxbuf.data[tail]; // Get next character
rxbuf.tail = BUFNEXT(tail, rxbuf); // and update pointer
return (int16_t)data;
}
static bool usbSuspendInput (bool suspend)
{
return stream_rx_suspend(&rxbuf, suspend);
}
static bool usbEnqueueRtCommand (char c)
{
return enqueue_realtime_command(c);
}
static enqueue_realtime_command_ptr usbSetRtHandler (enqueue_realtime_command_ptr handler)
{
enqueue_realtime_command_ptr prev = enqueue_realtime_command;
if(handler)
enqueue_realtime_command = handler;
return prev;
}
const io_stream_t *usbInit (void)
{
static const io_stream_t stream = {
.type = StreamType_Serial,
.read = usbGetC,
.write = usbWriteS,
.write_char = usbPutC,
.write_n = usbWrite,
.write_all = usbWriteS,
.enqueue_rt_command = usbEnqueueRtCommand,
.get_rx_buffer_free = usbRxFree,
.reset_read_buffer = usbRxFlush,
.cancel_read_buffer = usbRxCancel,
.suspend_read = usbSuspendInput,
.set_enqueue_rt_handler = usbSetRtHandler
};
USBD_API_INIT_PARAM_T usb_param;
USB_CORE_DESCS_T desc;
// ErrorCode_t ret = LPC_OK;
/* enable clocks and pinmux */
usb_pin_clk_init();
/* initialize call back structures */
memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T));
usb_param.usb_reg_base = LPC_USB_BASE + 0x200;
usb_param.max_num_ep = 3;
usb_param.mem_base = USB_STACK_MEM_BASE;
usb_param.mem_size = USB_STACK_MEM_SIZE;
/* Set the USB descriptors */
desc.device_desc = (uint8_t *) &USB_DeviceDescriptor[0];
desc.string_desc = (uint8_t *) &USB_StringDescriptor[0];
/* Note, to pass USBCV test full-speed only devices should have both
descriptor arrays point to same location and device_qualifier set to 0.
*/
desc.high_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0];
desc.full_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0];
desc.device_qualifier = 0;
/* USB Initialization */
if (USBD_API->hw->Init(&g_hUsb, &desc, &usb_param) == LPC_OK) {
/* Init VCOM interface */
if (vcom_init(g_hUsb, &desc, &usb_param) == LPC_OK) {
/* enable USB interrupts */
NVIC_EnableIRQ(USB_IRQn);
/* now connect */
USBD_API->hw->Connect(g_hUsb, 1);
}
}
txbuf.s = txbuf.data;
txbuf.max_length = sizeof(txbuf.data);
return &stream;
}
void usbBufferInput (uint8_t *data, uint32_t length)
{
while(length--) {
if(!enqueue_realtime_command(*data)) { // Check and strip realtime commands...
uint16_t next_head = BUFNEXT(rxbuf.head, rxbuf); // Get and increment buffer pointer
if(next_head == rxbuf.tail) // If buffer full
rxbuf.overflow = 1; // flag overflow
else {
rxbuf.data[rxbuf.head] = *data; // if not add data to buffer
rxbuf.head = next_head; // and update pointer
}
}
data++; // next
}
}
#endif
#endif // __usb_serial_h__