-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathusb_serial.c
206 lines (165 loc) · 5.2 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
/*
usb_serial.c - USB serial port implementation for STM32F103C8 ARM processors
Part of grblHAL
Copyright (c) 2019-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/>.
*/
#include "driver.h"
#include "serial.h"
#include "grbl/hal.h"
#include "grbl/protocol.h"
#include "main.h"
#include "usbd_cdc_if.h"
#include "usb_device.h"
static char txdata2[BLOCK_TX_BUFFER_SIZE]; // Secondary TX buffer (for double buffering)
static bool use_tx2data = false;
static stream_rx_buffer_t rxbuf = {0};
static stream_block_tx_buffer_t txbuf = {0};
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command;
//
// 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;
// Flush output buffer too.
txbuf.s = use_tx2data ? txdata2 : txbuf.data;
txbuf.length = 0;
}
//
// 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 current buffer to the USB output stream, swaps buffers
//
static inline bool usb_write (void)
{
static uint8_t dummy = 0;
txbuf.s = use_tx2data ? txdata2 : txbuf.data;
while(CDC_Transmit_FS((uint8_t *)txbuf.s, txbuf.length) == USBD_BUSY) {
if(!hal.stream_blocking_callback())
return false;
}
if(txbuf.length % 64 == 0) {
while(CDC_Transmit_FS(&dummy, 0) == USBD_BUSY) {
if(!hal.stream_blocking_callback())
return false;
}
}
use_tx2data = !use_tx2data;
txbuf.s = use_tx2data ? txdata2 : txbuf.data;
txbuf.length = 0;
return true;
}
//
// 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(CDC_Transmit_FS(buf, 1) == USBD_BUSY) {
if(!hal.stream_blocking_callback())
return false;
}
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 + txbuf.length) > txbuf.max_length) {
if(!usb_write())
return;
}
memcpy(txbuf.s, s, length);
txbuf.length += length;
txbuf.s += length;
if(s[length - 1] == ASCII_LF) {
if(!usb_write())
return;
}
}
//
// 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 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_all = usbWriteS,
.get_rx_buffer_free = usbRxFree,
.reset_read_buffer = usbRxFlush,
.cancel_read_buffer = usbRxCancel,
.suspend_read = usbSuspendInput,
.set_enqueue_rt_handler = usbSetRtHandler
};
MX_USB_DEVICE_Init();
txbuf.s = txbuf.data;
txbuf.max_length = BLOCK_TX_BUFFER_SIZE;
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
}
}