@@ -165,6 +165,8 @@ void Serial_::end(void)
165165
166166void 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
203205int 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
209213int 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
223229int 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
242250void Serial_::flush (void )
243251{
252+ if (!Is_otg_enabled ()) return ;
253+
244254 USBD_Flush (CDC_TX);
245255}
246256
247257size_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