Commit cd931bb7 authored by qianfan Zhao's avatar qianfan Zhao Committed by Kumar Gala
Browse files

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: default avatarqianfan Zhao <qianfanguijin@163.com>
parent bc6aae8d
Loading
Loading
Loading
Loading
+41 −11
Original line number Diff line number Diff line
@@ -488,8 +488,8 @@ int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
	return ret;
}

int usb_dc_ep_read(const u8_t ep, u8_t *const data, const u32_t max_data_len,
		   u32_t * const read_bytes)
int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
			u32_t *read_bytes)
{
	struct usb_dc_stm32_ep_state *ep_state = usb_dc_stm32_get_ep_state(ep);
	u32_t read_count = ep_state->read_count;
@@ -497,18 +497,39 @@ int usb_dc_ep_read(const u8_t ep, u8_t *const data, const u32_t max_data_len,
	SYS_LOG_DBG("ep 0x%02x, %u bytes, %u+%u, %p", ep,
		    max_data_len, ep_state->read_offset, read_count, data);

	if (!max_data_len) {
		goto done;
	if (!EP_IS_OUT(ep)) { /* check if OUT ep */
		SYS_LOG_ERR("Wrong endpoint direction: 0x%02x", ep);
		return -EINVAL;
	}

	/* When both buffer and max data to read are zero, just ingore reading
	 * and return available data in buffer. Otherwise, return data
	 * previously stored in the buffer.
	 */
	if (data) {
		read_count = min(read_count, max_data_len);

	/* Read data previously stored in the buffer */
	if (read_count) {
		memcpy(data, usb_dc_stm32_state.ep_buf[EP_IDX(ep)] +
		       ep_state->read_offset, read_count);
		ep_state->read_count -= read_count;
		ep_state->read_offset += read_count;
	} else if (max_data_len) {
		SYS_LOG_ERR("Wrong arguments");
	}

	if (read_bytes) {
		*read_bytes = read_count;
	}

	return 0;
}

int usb_dc_ep_read_continue(u8_t ep)
{
	struct usb_dc_stm32_ep_state *ep_state = usb_dc_stm32_get_ep_state(ep);

	if (!EP_IS_OUT(ep)) { /* Check if OUT ep */
		SYS_LOG_ERR("Not valid endpoint: %02x", ep);
		return -EINVAL;
	}

	/* 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,
				     USB_OTG_FS_MAX_PACKET_SIZE);
	}

done:
	if (read_bytes) {
		*read_bytes = read_count;
	return 0;
}

int usb_dc_ep_read(const u8_t ep, u8_t *const data, const u32_t max_data_len,
		   u32_t * const read_bytes)
{
	if (usb_dc_ep_read_wait(ep, data, max_data_len, read_bytes) != 0) {
		return -EINVAL;
	}

	if (usb_dc_ep_read_continue(ep) != 0) {
		return -EINVAL;
	}

	return 0;