forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSBSerial.cpp
200 lines (171 loc) · 4.59 KB
/
USBSerial.cpp
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
/*
Copyright (c) 2015 Arduino LLC. All right reserved.
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 2.1 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if defined (USBCON) && defined(USBD_USE_CDC)
#include "USBSerial.h"
#include "usbd_cdc.h"
#include "usbd_cdc_if.h"
#include "usbd_desc.h"
#include "wiring.h"
extern __IO bool dtrState;
extern __IO bool rtsState;
USBSerial SerialUSB;
void serialEventUSB() __attribute__((weak));
void USBSerial::begin(void)
{
CDC_init();
}
void USBSerial::begin(uint32_t /* baud_count */)
{
// uart config is ignored in USB-CDC
begin();
}
void USBSerial::begin(uint32_t /* baud_count */, uint8_t /* config */)
{
// uart config is ignored in USB-CDC
begin();
}
void USBSerial::end()
{
CDC_deInit();
}
int USBSerial::availableForWrite()
{
// Just transmit queue size, available for write
return static_cast<int>(CDC_TransmitQueue_WriteSize(&TransmitQueue));
}
size_t USBSerial::write(uint8_t ch)
{
// Just write single-byte buffer.
return write(&ch, 1);
}
size_t USBSerial::write(const uint8_t *buffer, size_t size)
{
size_t rest = size;
while (rest > 0 && CDC_connected()) {
// Determine buffer size available for write
auto portion = (size_t)CDC_TransmitQueue_WriteSize(&TransmitQueue);
// Truncate it to content size (if rest is greater)
if (rest < portion) {
portion = rest;
}
if (portion > 0) {
// Only if some space in the buffer exists.
// TS: Only main thread calls write and writeSize methods,
// it's thread-safe since IRQ does not affects
// TransmitQueue write position
CDC_TransmitQueue_Enqueue(&TransmitQueue, buffer, portion);
rest -= portion;
buffer += portion;
// After storing data, start transmitting process
CDC_continue_transmit();
}
}
return size - rest;
}
int USBSerial::available(void)
{
// Just ReceiveQueue size, available for reading
return static_cast<int>(CDC_ReceiveQueue_ReadSize(&ReceiveQueue));
}
int USBSerial::read(void)
{
// Dequeue only one char from queue
// TS: it safe, because only main thread affects ReceiveQueue->read pos
auto ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue);
// Resume receive process, if possible
CDC_resume_receive();
return ch;
}
size_t USBSerial::readBytes(char *buffer, size_t length)
{
size_t read;
size_t rest = length;
_startMillis = millis();
do {
read = CDC_ReceiveQueue_Read(&ReceiveQueue, reinterpret_cast<uint8_t *>(buffer), rest);
CDC_resume_receive();
rest -= read;
buffer += read;
if (rest == 0) {
return length;
}
} while (millis() - _startMillis < _timeout);
return length - rest;
}
size_t USBSerial::readBytesUntil(char terminator, char *buffer, size_t length)
{
uint32_t read;
size_t rest = length;
_startMillis = millis();
do {
bool found = CDC_ReceiveQueue_ReadUntil(&ReceiveQueue, static_cast<uint8_t>(terminator),
reinterpret_cast<uint8_t *>(buffer), rest, &read);
CDC_resume_receive();
rest -= read;
buffer += read;
if (found) {
return length - rest;
}
if (rest == 0) {
return length;
}
} while (millis() - _startMillis < _timeout);
return length - rest;
}
int USBSerial::peek(void)
{
// Peek one symbol, it can't change receive avaiablity
return CDC_ReceiveQueue_Peek(&ReceiveQueue);
}
void USBSerial::flush(void)
{
// Wait for TransmitQueue read size becomes zero
// TS: safe, because it not be stopped while receive 0
while (CDC_TransmitQueue_ReadSize(&TransmitQueue) > 0) {}
}
uint32_t USBSerial::baud()
{
return 115200;
}
uint8_t USBSerial::stopbits()
{
return ONE_STOP_BIT;
}
uint8_t USBSerial::paritytype()
{
return NO_PARITY;
}
uint8_t USBSerial::numbits()
{
return 8;
}
void USBSerial::dtr(bool enable)
{
CDC_enableDTR(enable);
}
bool USBSerial::dtr(void)
{
return dtrState;
}
bool USBSerial::rts(void)
{
return rtsState;
}
USBSerial::operator bool()
{
delay(10);
return dtrState;
}
#endif // USBCON && USBD_USE_CDC