@@ -488,27 +488,48 @@ int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
488
488
return ret ;
489
489
}
490
490
491
- int usb_dc_ep_read ( const u8_t ep , u8_t * const data , const u32_t max_data_len ,
492
- u32_t * const read_bytes )
491
+ int usb_dc_ep_read_wait ( u8_t ep , u8_t * data , u32_t max_data_len ,
492
+ u32_t * read_bytes )
493
493
{
494
494
struct usb_dc_stm32_ep_state * ep_state = usb_dc_stm32_get_ep_state (ep );
495
495
u32_t read_count = ep_state -> read_count ;
496
496
497
497
SYS_LOG_DBG ("ep 0x%02x, %u bytes, %u+%u, %p" , ep ,
498
498
max_data_len , ep_state -> read_offset , read_count , data );
499
499
500
- if (!max_data_len ) {
501
- goto done ;
500
+ if (!EP_IS_OUT (ep )) { /* check if OUT ep */
501
+ SYS_LOG_ERR ("Wrong endpoint direction: 0x%02x" , ep );
502
+ return - EINVAL ;
502
503
}
503
504
504
- read_count = min (read_count , max_data_len );
505
-
506
- /* Read data previously stored in the buffer */
507
- if (read_count ) {
505
+ /* When both buffer and max data to read are zero, just ingore reading
506
+ * and return available data in buffer. Otherwise, return data
507
+ * previously stored in the buffer.
508
+ */
509
+ if (data ) {
510
+ read_count = min (read_count , max_data_len );
508
511
memcpy (data , usb_dc_stm32_state .ep_buf [EP_IDX (ep )] +
509
512
ep_state -> read_offset , read_count );
510
513
ep_state -> read_count -= read_count ;
511
514
ep_state -> read_offset += read_count ;
515
+ } else if (max_data_len ) {
516
+ SYS_LOG_ERR ("Wrong arguments" );
517
+ }
518
+
519
+ if (read_bytes ) {
520
+ * read_bytes = read_count ;
521
+ }
522
+
523
+ return 0 ;
524
+ }
525
+
526
+ int usb_dc_ep_read_continue (u8_t ep )
527
+ {
528
+ struct usb_dc_stm32_ep_state * ep_state = usb_dc_stm32_get_ep_state (ep );
529
+
530
+ if (!EP_IS_OUT (ep )) { /* Check if OUT ep */
531
+ SYS_LOG_ERR ("Not valid endpoint: %02x" , ep );
532
+ return - EINVAL ;
512
533
}
513
534
514
535
/* If no more data in the buffer, start a new read transaction.
@@ -519,9 +540,18 @@ int usb_dc_ep_read(const u8_t ep, u8_t *const data, const u32_t max_data_len,
519
540
USB_OTG_FS_MAX_PACKET_SIZE );
520
541
}
521
542
522
- done :
523
- if (read_bytes ) {
524
- * read_bytes = read_count ;
543
+ return 0 ;
544
+ }
545
+
546
+ int usb_dc_ep_read (const u8_t ep , u8_t * const data , const u32_t max_data_len ,
547
+ u32_t * const read_bytes )
548
+ {
549
+ if (usb_dc_ep_read_wait (ep , data , max_data_len , read_bytes ) != 0 ) {
550
+ return - EINVAL ;
551
+ }
552
+
553
+ if (usb_dc_ep_read_continue (ep ) != 0 ) {
554
+ return - EINVAL ;
525
555
}
526
556
527
557
return 0 ;
0 commit comments