Skip to content

Commit 90ef7f1

Browse files
committed
fix for USB deactivation/restore (power-saving)
1 parent 65e1b36 commit 90ef7f1

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Diff for: sam/1.0.4/cores/arduino/USB/CDC.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ void Serial_::end(void)
165165

166166
void Serial_::accept(void)
167167
{
168+
if (!Is_otg_enabled()) return;
169+
168170
static uint32_t guard = 0;
169171

170172
// synchronized access to guard
@@ -202,12 +204,16 @@ void Serial_::accept(void)
202204

203205
int Serial_::available(void)
204206
{
207+
if (!Is_otg_enabled()) return 0;
208+
205209
ring_buffer *buffer = &cdc_rx_buffer;
206210
return (unsigned int)(CDC_SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % CDC_SERIAL_BUFFER_SIZE;
207211
}
208212

209213
int Serial_::peek(void)
210214
{
215+
if (!Is_otg_enabled()) return -1;
216+
211217
ring_buffer *buffer = &cdc_rx_buffer;
212218

213219
if (buffer->head == buffer->tail)
@@ -222,6 +228,8 @@ int Serial_::peek(void)
222228

223229
int Serial_::read(void)
224230
{
231+
if (!Is_otg_enabled()) return -1;
232+
225233
ring_buffer *buffer = &cdc_rx_buffer;
226234

227235
// if the head isn't ahead of the tail, we don't have any characters
@@ -241,11 +249,15 @@ int Serial_::read(void)
241249

242250
void Serial_::flush(void)
243251
{
252+
if (!Is_otg_enabled()) return;
253+
244254
USBD_Flush(CDC_TX);
245255
}
246256

247257
size_t Serial_::write(const uint8_t *buffer, size_t size)
248258
{
259+
if (!Is_otg_enabled()) return 0;
260+
249261
/* only try to send bytes if the high-level CDC connection itself
250262
is open (not just the pipe) - the OS should set lineState when the port
251263
is opened and clear lineState when the port is closed.

0 commit comments

Comments
 (0)