Skip to content

Commit cd931bb

Browse files
qianfan-Zhaogalak
authored andcommitted
usb: stm32: add usb_dc_ep_read_wait/conitinue
usb mass example need usb_dc_ep_read_wait/continue API. test usb mass storage with RAM DISK on stm32f4 series. Signed-off-by: qianfan Zhao <[email protected]>
1 parent bc6aae8 commit cd931bb

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

drivers/usb/device/usb_dc_stm32.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -488,27 +488,48 @@ int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
488488
return ret;
489489
}
490490

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)
493493
{
494494
struct usb_dc_stm32_ep_state *ep_state = usb_dc_stm32_get_ep_state(ep);
495495
u32_t read_count = ep_state->read_count;
496496

497497
SYS_LOG_DBG("ep 0x%02x, %u bytes, %u+%u, %p", ep,
498498
max_data_len, ep_state->read_offset, read_count, data);
499499

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;
502503
}
503504

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);
508511
memcpy(data, usb_dc_stm32_state.ep_buf[EP_IDX(ep)] +
509512
ep_state->read_offset, read_count);
510513
ep_state->read_count -= read_count;
511514
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;
512533
}
513534

514535
/* 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,
519540
USB_OTG_FS_MAX_PACKET_SIZE);
520541
}
521542

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;
525555
}
526556

527557
return 0;

0 commit comments

Comments
 (0)