From 1c2e353193d31ee3869a2417e17be89f0a6aa41c Mon Sep 17 00:00:00 2001 From: kkitayam <45088311+kkitayam@users.noreply.github.com> Date: Sat, 29 May 2021 19:09:13 +0900 Subject: [PATCH 1/7] Refactor and clean up --- src/portable/renesas/usba/dcd_usba.c | 286 ++++++++++++++------------- 1 file changed, 150 insertions(+), 136 deletions(-) diff --git a/src/portable/renesas/usba/dcd_usba.c b/src/portable/renesas/usba/dcd_usba.c index c7eb6e15..46513590 100644 --- a/src/portable/renesas/usba/dcd_usba.c +++ b/src/portable/renesas/usba/dcd_usba.c @@ -122,7 +122,7 @@ typedef union { typedef struct TU_ATTR_PACKED { - uintptr_t addr; /* the start address of a transfer data buffer */ + void *buf; /* the start address of a transfer data buffer */ uint16_t length; /* the number of bytes in the buffer */ uint16_t remaining; /* the number of bytes remaining in the buffer */ struct { @@ -236,34 +236,27 @@ static volatile uint16_t* ep_addr_to_pipectr(uint8_t rhport, unsigned ep_addr) return ctr; } -static unsigned wait_for_pipe_ready(void) +static unsigned edpt0_max_packet_size(void) { - unsigned ctr; - do { - ctr = USB0.D0FIFOCTR.WORD; - } while (!(ctr & USB_FIFOCTR_FRDY)); - return ctr; + return USB0.DCPMAXP.BIT.MXPS; } -static unsigned select_pipe(unsigned num, unsigned attr) +static unsigned edpt_max_packet_size(unsigned num) +{ + USB0.PIPESEL.WORD = num; + return USB0.PIPEMAXP.WORD; +} + +static inline void pipe_wait_for_ready(unsigned num) { - USB0.PIPESEL.WORD = num; - USB0.D0FIFOSEL.WORD = num | attr; while (USB0.D0FIFOSEL.BIT.CURPIPE != num) ; - return wait_for_pipe_ready(); + while (!USB0.D0FIFOCTR.BIT.FRDY) ; } -/* 1 less than mps bytes were written to FIFO - * 2 no bytes were written to FIFO - * 0 mps bytes were written to FIFO */ -static int fifo_write(volatile void *fifo, pipe_state_t* pipe, unsigned mps) +static void pipe_write_packet(void *buf, volatile void *fifo, unsigned len) { - unsigned rem = pipe->remaining; - if (!rem) return 2; - unsigned len = TU_MIN(rem, mps); - hw_fifo_t *reg = (hw_fifo_t*)fifo; - uintptr_t addr = pipe->addr + pipe->length - rem; + uintptr_t addr = (uintptr_t)buf; while (len >= 2) { reg->u16 = *(const uint16_t *)addr; addr += 2; @@ -273,31 +266,107 @@ static int fifo_write(volatile void *fifo, pipe_state_t* pipe, unsigned mps) reg->u8 = *(const uint8_t *)addr; ++addr; } - if (rem < mps) return 1; - return 0; } -/* 1 less than mps bytes were read from FIFO - * 2 the end of the buffer reached. - * 0 mps bytes were read from FIFO */ -static int fifo_read(volatile void *fifo, pipe_state_t* pipe, unsigned mps, size_t len) +static void pipe_read_packet(void *buf, volatile void *fifo, unsigned len) { - unsigned rem = pipe->remaining; - if (!rem) return 2; - if (rem < len) len = rem; - pipe->remaining = rem - len; - + uint8_t *p = (uint8_t*)buf; uint8_t *reg = (uint8_t*)fifo; /* byte access is always at base register address */ - uintptr_t addr = pipe->addr; - unsigned loop = len; - while (loop--) { - *(uint8_t *)addr = *reg; - ++addr; + while (len--) *p++ = *reg; +} + +static bool pipe0_xfer_in(void) +{ + pipe_state_t *pipe = &_dcd.pipe[0]; + const unsigned rem = pipe->remaining; + if (!rem) { + pipe->buf = NULL; + return true; } - pipe->addr = addr; - if (rem < mps) return 1; - if (rem == len) return 2; - return 0; + const unsigned mps = edpt0_max_packet_size(); + const unsigned len = TU_MIN(mps, rem); + void *buf = pipe->buf; + if (len) { + pipe_write_packet(buf, &USB0.CFIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } + if (len < mps) USB0.CFIFOCTR.WORD = USB_FIFOCTR_BVAL; + pipe->remaining = rem - len; + return false; +} + +static bool pipe0_xfer_out(void) +{ + pipe_state_t *pipe = &_dcd.pipe[0]; + const unsigned rem = pipe->remaining; + + const unsigned mps = edpt0_max_packet_size(); + const unsigned vld = USB0.CFIFOCTR.BIT.DTLN; + const unsigned len = TU_MIN(TU_MIN(rem, mps), vld); + void *buf = pipe->buf; + if (len) { + pipe_read_packet(buf, &USB0.CFIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } + if (len < mps) USB0.CFIFOCTR.WORD = USB_FIFOCTR_BCLR; + pipe->remaining = rem - len; + if ((len < mps) || (rem == len)) { + pipe->buf = NULL; + return true; + } + return false; +} + +static bool pipe_xfer_in(unsigned num) +{ + pipe_state_t *pipe = &_dcd.pipe[num]; + const unsigned rem = pipe->remaining; + + if (!rem) { + pipe->buf = NULL; + return true; + } + + USB0.D0FIFOSEL.WORD = num | USB_FIFOSEL_MBW_16; + const unsigned mps = edpt_max_packet_size(num); + pipe_wait_for_ready(num); + const unsigned len = TU_MIN(rem, mps); + void *buf = pipe->buf; + if (len) { + pipe_write_packet(buf, &USB0.D0FIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } + if (len < mps) USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BVAL; + USB0.D0FIFOSEL.WORD = 0; + while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ + pipe->remaining = rem - len; + return false; +} + +static bool pipe_xfer_out(unsigned num) +{ + pipe_state_t *pipe = &_dcd.pipe[num]; + const unsigned rem = pipe->remaining; + + USB0.D0FIFOSEL.WORD = num | USB_FIFOSEL_MBW_8; + const unsigned mps = edpt_max_packet_size(num); + pipe_wait_for_ready(num); + const unsigned vld = USB0.D0FIFOCTR.BIT.DTLN; + const unsigned len = TU_MIN(TU_MIN(rem, mps), vld); + void *buf = pipe->buf; + if (len) { + pipe_read_packet(buf, &USB0.D0FIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } + if (len < mps) USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BCLR; + USB0.D0FIFOSEL.WORD = 0; + while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ + pipe->remaining = rem - len; + if ((len < mps) || (rem == len)) { + pipe->buf = NULL; + return NULL != buf; + } + return false; } static void process_setup_packet(uint8_t rhport) @@ -327,11 +396,10 @@ static void process_status_completion(uint8_t rhport) dcd_event_xfer_complete(rhport, ep_addr, 0, XFER_RESULT_SUCCESS, true); } -static bool process_edpt0_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) +static bool process_pipe0_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { (void)rhport; - pipe_state_t *pipe = &_dcd.pipe[0]; /* configure fifo direction and access unit settings */ if (ep_addr) { /* IN, 2 bytes */ USB0.CFIFOSEL.WORD = USB_FIFOSEL_TX | USB_FIFOSEL_MBW_16 | (TU_BYTE_ORDER == TU_BIG_ENDIAN ? USB_FIFOSEL_BIGEND : 0); @@ -340,57 +408,25 @@ static bool process_edpt0_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, USB0.CFIFOSEL.WORD = USB_FIFOSEL_MBW_8; while (USB0.CFIFOSEL.WORD & USB_FIFOSEL_TX) ; } + + pipe_state_t *pipe = &_dcd.pipe[0]; + pipe->length = total_bytes; + pipe->remaining = total_bytes; if (total_bytes) { - pipe->addr = (uintptr_t)buffer; - pipe->length = total_bytes; - pipe->remaining = total_bytes; + pipe->buf = buffer; if (ep_addr) { /* IN */ TU_ASSERT(USB0.DCPCTR.BIT.BSTS && (USB0.USBREQ.WORD & 0x80)); - if (fifo_write((void*)&USB0.CFIFO.WORD, pipe, 64)) { - USB0.CFIFOCTR.WORD = USB_FIFOCTR_BVAL; - } + pipe0_xfer_in(); } USB0.DCPCTR.WORD = USB_PIPECTR_PID_BUF; } else { /* ZLP */ - pipe->addr = 0; - pipe->length = 0; - pipe->remaining = 0; + pipe->buf = NULL; USB0.DCPCTR.WORD = USB_PIPECTR_CCPL | USB_PIPECTR_PID_BUF; } return true; } -static void process_edpt0_bemp(uint8_t rhport) -{ - pipe_state_t *pipe = &_dcd.pipe[0]; - const unsigned rem = pipe->remaining; - if (rem > 64) { - pipe->remaining = rem - 64; - int r = fifo_write((void*)&USB0.CFIFO.WORD, &_dcd.pipe[0], 64); - if (r) USB0.CFIFOCTR.WORD = USB_FIFOCTR_BVAL; - return; - } - pipe->addr = 0; - pipe->remaining = 0; - dcd_event_xfer_complete(rhport, tu_edpt_addr(0, TUSB_DIR_IN), - pipe->length, XFER_RESULT_SUCCESS, true); -} - -static void process_edpt0_brdy(uint8_t rhport) -{ - size_t len = USB0.CFIFOCTR.BIT.DTLN; - int cplt = fifo_read((void*)&USB0.CFIFO.WORD, &_dcd.pipe[0], 64, len); - if (cplt || (len < 64)) { - if (2 != cplt) { - USB0.CFIFOCTR.WORD = USB_FIFOCTR_BCLR; - } - dcd_event_xfer_complete(rhport, tu_edpt_addr(0, TUSB_DIR_OUT), - _dcd.pipe[0].length - _dcd.pipe[0].remaining, - XFER_RESULT_SUCCESS, true); - } -} - static bool process_pipe_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { (void)rhport; @@ -401,23 +437,16 @@ static bool process_pipe_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, TU_ASSERT(num); - pipe_state_t *pipe = &_dcd.pipe[num]; - pipe->addr = (uintptr_t)buffer; + pipe_state_t *pipe = &_dcd.pipe[num]; + pipe->buf = buffer; pipe->length = total_bytes; pipe->remaining = total_bytes; - - USB0.PIPESEL.WORD = num; - const unsigned mps = USB0.PIPEMAXP.WORD; if (dir) { /* IN */ - USB0.D0FIFOSEL.WORD = num | USB_FIFOSEL_MBW_16 | (TU_BYTE_ORDER == TU_BIG_ENDIAN ? USB_FIFOSEL_BIGEND : 0); - while (USB0.D0FIFOSEL.BIT.CURPIPE != num) ; - int r = fifo_write((void*)&USB0.D0FIFO.WORD, pipe, mps); - if (r) USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BVAL; - USB0.D0FIFOSEL.WORD = 0; - while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ + pipe_xfer_in(num); } else { volatile reg_pipetre_t *pt = get_pipetre(num); if (pt) { + const unsigned mps = edpt_max_packet_size(num); volatile uint16_t *ctr = get_pipectr(num); if (*ctr & 0x3) *ctr = USB_PIPECTR_PID_NAK; pt->TRE = TU_BIT(8); @@ -430,47 +459,36 @@ static bool process_pipe_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, return true; } +static void process_pipe0_bemp(uint8_t rhport) +{ + bool completed = pipe0_xfer_in(); + if (completed) { + pipe_state_t *pipe = &_dcd.pipe[0]; + dcd_event_xfer_complete(rhport, tu_edpt_addr(0, TUSB_DIR_IN), + pipe->length, XFER_RESULT_SUCCESS, true); + } +} + static void process_pipe_brdy(uint8_t rhport, unsigned num) { - pipe_state_t *pipe = &_dcd.pipe[num]; - if (tu_edpt_dir(pipe->ep)) { /* IN */ - select_pipe(num, USB_FIFOSEL_MBW_16 | (TU_BYTE_ORDER == TU_BIG_ENDIAN ? USB_FIFOSEL_BIGEND : 0)); - const unsigned mps = USB0.PIPEMAXP.WORD; - unsigned rem = pipe->remaining; - rem -= TU_MIN(rem, mps); - pipe->remaining = rem; - if (rem) { - int r = 0; - r = fifo_write((void*)&USB0.D0FIFO.WORD, pipe, mps); - if (r) USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BVAL; - USB0.D0FIFOSEL.WORD = 0; - while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ - return; - } - USB0.D0FIFOSEL.WORD = 0; - while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ - pipe->addr = 0; - pipe->remaining = 0; - dcd_event_xfer_complete(rhport, pipe->ep, pipe->length, - XFER_RESULT_SUCCESS, true); + pipe_state_t *pipe = &_dcd.pipe[num]; + const unsigned dir = tu_edpt_dir(pipe->ep); + bool completed; + + if (dir) { /* IN */ + completed = pipe_xfer_in(num); } else { - const unsigned ctr = select_pipe(num, USB_FIFOSEL_MBW_8); - const unsigned len = ctr & USB_FIFOCTR_DTLN; - const unsigned mps = USB0.PIPEMAXP.WORD; - int cplt = fifo_read((void*)&USB0.D0FIFO.WORD, pipe, mps, len); - if (cplt || (len < mps)) { - if (2 != cplt) { - USB0.D0FIFO.WORD = USB_FIFOCTR_BCLR; - } - USB0.D0FIFOSEL.WORD = 0; - while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ - dcd_event_xfer_complete(rhport, pipe->ep, - pipe->length - pipe->remaining, - XFER_RESULT_SUCCESS, true); - return; + if (num) { + completed = pipe_xfer_out(num); + } else { + completed = pipe0_xfer_out(); } - USB0.D0FIFOSEL.WORD = 0; - while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ + } + if (completed) { + dcd_event_xfer_complete(rhport, pipe->ep, + pipe->length - pipe->remaining, + XFER_RESULT_SUCCESS, true); + // TU_LOG1("C %d %d\r\n", num, pipe->length - pipe->remaining); } } @@ -649,7 +667,7 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * ep_desc) if (dir || (xfer != TUSB_XFER_BULK)) { *ctr = USB_PIPECTR_PID_BUF; } - // TU_LOG1("O %d %x %x\r\n", USB0.PIPESEL.WORD, USB0.PIPECFG.WORD, USB0.PIPEMAXP.WORD); + // TU_LOG1("O %d %x %x\r\n", USB0.PIPESEL.WORD, USB0.PIPECFG.WORD, USB0.PIPEMAXP.WORD); dcd_int_enable(rhport); return true; @@ -677,7 +695,7 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t to const unsigned epn = tu_edpt_number(ep_addr); dcd_int_disable(rhport); if (0 == epn) { - r = process_edpt0_xfer(rhport, ep_addr, buffer, total_bytes); + r = process_pipe0_xfer(rhport, ep_addr, buffer, total_bytes); } else { r = process_pipe_xfer(rhport, ep_addr, buffer, total_bytes); } @@ -779,7 +797,7 @@ void dcd_int_handler(uint8_t rhport) const unsigned s = USB0.BEMPSTS.WORD; USB0.BEMPSTS.WORD = 0; if (s & 1) { - process_edpt0_bemp(rhport); + process_pipe0_bemp(rhport); } } if (is0 & USB_IS0_BRDY) { @@ -787,10 +805,6 @@ void dcd_int_handler(uint8_t rhport) unsigned s = USB0.BRDYSTS.WORD & m; /* clear active bits (don't write 0 to already cleared bits according to the HW manual) */ USB0.BRDYSTS.WORD = ~s; - if (s & 1) { - process_edpt0_brdy(rhport); - s &= ~1; - } while (s) { #if defined(__CCRX__) static const int Mod37BitPosition[] = { From 3f49380b378601a852ed45f2b38dd48c66b0f69d Mon Sep 17 00:00:00 2001 From: kkitayam <45088311+kkitayam@users.noreply.github.com> Date: Sun, 30 May 2021 22:49:53 +0900 Subject: [PATCH 2/7] added support for dcd_edpt_xfer_fifo --- src/portable/renesas/usba/dcd_usba.c | 86 ++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/src/portable/renesas/usba/dcd_usba.c b/src/portable/renesas/usba/dcd_usba.c index 46513590..ada18454 100644 --- a/src/portable/renesas/usba/dcd_usba.c +++ b/src/portable/renesas/usba/dcd_usba.c @@ -127,6 +127,7 @@ typedef struct TU_ATTR_PACKED uint16_t remaining; /* the number of bytes remaining in the buffer */ struct { uint32_t ep : 8; /* an assigned endpoint address */ + uint32_t ff : 1; /* `buf` is TU_FUFO or POD */ uint32_t : 0; }; } pipe_state_t; @@ -287,8 +288,12 @@ static bool pipe0_xfer_in(void) const unsigned len = TU_MIN(mps, rem); void *buf = pipe->buf; if (len) { - pipe_write_packet(buf, &USB0.CFIFO.WORD, len); - pipe->buf = (uint8_t*)buf + len; + if (pipe->ff) { + tu_fifo_read_n_const_addr_full_words(buf, (void*)&USB0.CFIFO.WORD, len); + } else { + pipe_write_packet(buf, &USB0.CFIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } } if (len < mps) USB0.CFIFOCTR.WORD = USB_FIFOCTR_BVAL; pipe->remaining = rem - len; @@ -305,8 +310,12 @@ static bool pipe0_xfer_out(void) const unsigned len = TU_MIN(TU_MIN(rem, mps), vld); void *buf = pipe->buf; if (len) { - pipe_read_packet(buf, &USB0.CFIFO.WORD, len); - pipe->buf = (uint8_t*)buf + len; + if (pipe->ff) { + tu_fifo_write_n_const_addr_full_words(buf, (void*)&USB0.CFIFO.WORD, len); + } else { + pipe_read_packet(buf, &USB0.CFIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } } if (len < mps) USB0.CFIFOCTR.WORD = USB_FIFOCTR_BCLR; pipe->remaining = rem - len; @@ -333,8 +342,12 @@ static bool pipe_xfer_in(unsigned num) const unsigned len = TU_MIN(rem, mps); void *buf = pipe->buf; if (len) { - pipe_write_packet(buf, &USB0.D0FIFO.WORD, len); - pipe->buf = (uint8_t*)buf + len; + if (pipe->ff) { + tu_fifo_read_n_const_addr_full_words(buf, (void*)&USB0.D0FIFO.WORD, len); + } else { + pipe_write_packet(buf, &USB0.D0FIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } } if (len < mps) USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BVAL; USB0.D0FIFOSEL.WORD = 0; @@ -355,8 +368,12 @@ static bool pipe_xfer_out(unsigned num) const unsigned len = TU_MIN(TU_MIN(rem, mps), vld); void *buf = pipe->buf; if (len) { - pipe_read_packet(buf, &USB0.D0FIFO.WORD, len); - pipe->buf = (uint8_t*)buf + len; + if (pipe->ff) { + tu_fifo_write_n_const_addr_full_words(buf, (void*)&USB0.D0FIFO.WORD, len); + } else { + pipe_read_packet(buf, &USB0.D0FIFO.WORD, len); + pipe->buf = (uint8_t*)buf + len; + } } if (len < mps) USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BCLR; USB0.D0FIFOSEL.WORD = 0; @@ -396,10 +413,8 @@ static void process_status_completion(uint8_t rhport) dcd_event_xfer_complete(rhport, ep_addr, 0, XFER_RESULT_SUCCESS, true); } -static bool process_pipe0_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) +static bool process_pipe0_xfer(int buffer_type, uint8_t ep_addr, void* buffer, uint16_t total_bytes) { - (void)rhport; - /* configure fifo direction and access unit settings */ if (ep_addr) { /* IN, 2 bytes */ USB0.CFIFOSEL.WORD = USB_FIFOSEL_TX | USB_FIFOSEL_MBW_16 | (TU_BYTE_ORDER == TU_BIG_ENDIAN ? USB_FIFOSEL_BIGEND : 0); @@ -410,6 +425,7 @@ static bool process_pipe0_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, } pipe_state_t *pipe = &_dcd.pipe[0]; + pipe->ff = buffer_type; pipe->length = total_bytes; pipe->remaining = total_bytes; if (total_bytes) { @@ -427,10 +443,8 @@ static bool process_pipe0_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, return true; } -static bool process_pipe_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) +static bool process_pipe_xfer(int buffer_type, uint8_t ep_addr, void* buffer, uint16_t total_bytes) { - (void)rhport; - const unsigned epn = tu_edpt_number(ep_addr); const unsigned dir = tu_edpt_dir(ep_addr); const unsigned num = _dcd.ep[dir][epn]; @@ -438,11 +452,19 @@ static bool process_pipe_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, TU_ASSERT(num); pipe_state_t *pipe = &_dcd.pipe[num]; + pipe->ff = buffer_type; pipe->buf = buffer; pipe->length = total_bytes; pipe->remaining = total_bytes; if (dir) { /* IN */ - pipe_xfer_in(num); + if (total_bytes) { + pipe_xfer_in(num); + } else { /* ZLP */ + USB0.D0FIFOSEL.WORD = num; + pipe_wait_for_ready(num); + USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BVAL; + USB0.D0FIFOSEL.WORD = 0; + } } else { volatile reg_pipetre_t *pt = get_pipetre(num); if (pt) { @@ -459,13 +481,23 @@ static bool process_pipe_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, return true; } +static bool process_edpt_xfer(int buffer_type, uint8_t ep_addr, void* buffer, uint16_t total_bytes) +{ + const unsigned epn = tu_edpt_number(ep_addr); + if (0 == epn) { + return process_pipe0_xfer(buffer_type, ep_addr, buffer, total_bytes); + } else { + return process_pipe_xfer(buffer_type, ep_addr, buffer, total_bytes); + } +} + static void process_pipe0_bemp(uint8_t rhport) { bool completed = pipe0_xfer_in(); if (completed) { pipe_state_t *pipe = &_dcd.pipe[0]; dcd_event_xfer_complete(rhport, tu_edpt_addr(0, TUSB_DIR_IN), - pipe->length, XFER_RESULT_SUCCESS, true); + pipe->length, XFER_RESULT_SUCCESS, true); } } @@ -486,8 +518,8 @@ static void process_pipe_brdy(uint8_t rhport, unsigned num) } if (completed) { dcd_event_xfer_complete(rhport, pipe->ep, - pipe->length - pipe->remaining, - XFER_RESULT_SUCCESS, true); + pipe->length - pipe->remaining, + XFER_RESULT_SUCCESS, true); // TU_LOG1("C %d %d\r\n", num, pipe->length - pipe->remaining); } } @@ -692,13 +724,19 @@ void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr) bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { bool r; - const unsigned epn = tu_edpt_number(ep_addr); dcd_int_disable(rhport); - if (0 == epn) { - r = process_pipe0_xfer(rhport, ep_addr, buffer, total_bytes); - } else { - r = process_pipe_xfer(rhport, ep_addr, buffer, total_bytes); - } + r = process_edpt_xfer(0, ep_addr, buffer, total_bytes); + dcd_int_enable(rhport); + return r; +} + +bool dcd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes) +{ + // USB buffers always work in bytes so to avoid unnecessary divisions we demand item_size = 1 + TU_ASSERT(ff->item_size == 1); + bool r; + dcd_int_disable(rhport); + r = process_edpt_xfer(1, ep_addr, ff, total_bytes); dcd_int_enable(rhport); return r; } From e7c9cf4aea1515306765eb2b43dbe7002b50ed62 Mon Sep 17 00:00:00 2001 From: kkitayam <45088311+kkitayam@users.noreply.github.com> Date: Wed, 2 Jun 2021 22:18:15 +0900 Subject: [PATCH 3/7] Change the accessing method of TU_FIFO from read/write_n_const_addr_full_words to get_write/read_info and advance_write/read_pointer pairs. --- src/portable/renesas/usba/dcd_usba.c | 34 ++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/portable/renesas/usba/dcd_usba.c b/src/portable/renesas/usba/dcd_usba.c index ada18454..09622d89 100644 --- a/src/portable/renesas/usba/dcd_usba.c +++ b/src/portable/renesas/usba/dcd_usba.c @@ -276,6 +276,30 @@ static void pipe_read_packet(void *buf, volatile void *fifo, unsigned len) while (len--) *p++ = *reg; } +static void pipe_read_write_packet_ff(tu_fifo_t *f, volatile void *fifo, unsigned len, unsigned dir) +{ + static const struct { + void (*tu_fifo_get_info)(tu_fifo_t *f, tu_fifo_buffer_info_t *info); + void (*tu_fifo_advance)(tu_fifo_t *f, uint16_t n); + void (*pipe_read_write)(void *buf, volatile void *fifo, unsigned len); + } ops[] = { + /* OUT */ {tu_fifo_get_write_info,tu_fifo_advance_write_pointer,pipe_read_packet}, + /* IN */ {tu_fifo_get_read_info, tu_fifo_advance_read_pointer, pipe_write_packet}, + }; + tu_fifo_buffer_info_t info; + ops[dir].tu_fifo_get_info(f, &info); + unsigned total_len = len; + len = TU_MIN(total_len, info.len_lin); + ops[dir].pipe_read_write(info.ptr_lin, fifo, len); + unsigned rem = total_len - len; + if (rem) { + len = TU_MIN(rem, info.len_wrap); + ops[dir].pipe_read_write(info.ptr_wrap, fifo, len); + rem -= len; + } + ops[dir].tu_fifo_advance(f, total_len - rem); +} + static bool pipe0_xfer_in(void) { pipe_state_t *pipe = &_dcd.pipe[0]; @@ -289,7 +313,7 @@ static bool pipe0_xfer_in(void) void *buf = pipe->buf; if (len) { if (pipe->ff) { - tu_fifo_read_n_const_addr_full_words(buf, (void*)&USB0.CFIFO.WORD, len); + pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.CFIFO.WORD, len, TUSB_DIR_IN); } else { pipe_write_packet(buf, &USB0.CFIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; @@ -311,7 +335,7 @@ static bool pipe0_xfer_out(void) void *buf = pipe->buf; if (len) { if (pipe->ff) { - tu_fifo_write_n_const_addr_full_words(buf, (void*)&USB0.CFIFO.WORD, len); + pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.CFIFO.WORD, len, TUSB_DIR_OUT); } else { pipe_read_packet(buf, &USB0.CFIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; @@ -343,7 +367,7 @@ static bool pipe_xfer_in(unsigned num) void *buf = pipe->buf; if (len) { if (pipe->ff) { - tu_fifo_read_n_const_addr_full_words(buf, (void*)&USB0.D0FIFO.WORD, len); + pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.D0FIFO.WORD, len, TUSB_DIR_IN); } else { pipe_write_packet(buf, &USB0.D0FIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; @@ -369,7 +393,7 @@ static bool pipe_xfer_out(unsigned num) void *buf = pipe->buf; if (len) { if (pipe->ff) { - tu_fifo_write_n_const_addr_full_words(buf, (void*)&USB0.D0FIFO.WORD, len); + pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.D0FIFO.WORD, len, TUSB_DIR_OUT); } else { pipe_read_packet(buf, &USB0.D0FIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; @@ -477,7 +501,7 @@ static bool process_pipe_xfer(int buffer_type, uint8_t ep_addr, void* buffer, ui *ctr = USB_PIPECTR_PID_BUF; } } - // TU_LOG1("X %x %d\r\n", ep_addr, total_bytes); + // TU_LOG1("X %x %d %d\r\n", ep_addr, total_bytes, buffer_type); return true; } From 6b9f8e454eaf5ac3a9474d0a09f019e700532093 Mon Sep 17 00:00:00 2001 From: kkitayam <45088311+kkitayam@users.noreply.github.com> Date: Fri, 4 Jun 2021 01:22:57 +0900 Subject: [PATCH 4/7] add a condition regarding OPT_MCU_RX63N --- src/class/audio/audio_device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/class/audio/audio_device.c b/src/class/audio/audio_device.c index 404d2881..86a00ea7 100644 --- a/src/class/audio/audio_device.c +++ b/src/class/audio/audio_device.c @@ -86,7 +86,8 @@ CFG_TUSB_MCU == OPT_MCU_STM32F4 || \ CFG_TUSB_MCU == OPT_MCU_STM32F7 || \ CFG_TUSB_MCU == OPT_MCU_STM32H7 || \ - (CFG_TUSB_MCU == OPT_MCU_STM32L4 && defined(STM32L4_SYNOPSYS)) + (CFG_TUSB_MCU == OPT_MCU_STM32L4 && defined(STM32L4_SYNOPSYS)) || \ + CFG_TUSB_MCU == OPT_MCU_RX63X #define USE_LINEAR_BUFFER 0 #else #define USE_LINEAR_BUFFER 1 From 3c3563288d643fc70ccff80c02a72d330cd65678 Mon Sep 17 00:00:00 2001 From: kkitayam <45088311+kkitayam@users.noreply.github.com> Date: Tue, 29 Jun 2021 20:41:16 +0900 Subject: [PATCH 5/7] add RX65N --- src/class/audio/audio_device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/class/audio/audio_device.c b/src/class/audio/audio_device.c index 86a00ea7..092e5e12 100644 --- a/src/class/audio/audio_device.c +++ b/src/class/audio/audio_device.c @@ -87,7 +87,8 @@ CFG_TUSB_MCU == OPT_MCU_STM32F7 || \ CFG_TUSB_MCU == OPT_MCU_STM32H7 || \ (CFG_TUSB_MCU == OPT_MCU_STM32L4 && defined(STM32L4_SYNOPSYS)) || \ - CFG_TUSB_MCU == OPT_MCU_RX63X + CFG_TUSB_MCU == OPT_MCU_RX63X || \ + CFG_TUSB_MCU == OPT_MCU_RX65X #define USE_LINEAR_BUFFER 0 #else #define USE_LINEAR_BUFFER 1 From ff20e4d6bcffc5d66666050c57719031fa2820e8 Mon Sep 17 00:00:00 2001 From: kkitayam <45088311+kkitayam@users.noreply.github.com> Date: Thu, 29 Jul 2021 20:45:51 +0900 Subject: [PATCH 6/7] add the entry for RX72N --- src/class/audio/audio_device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/class/audio/audio_device.c b/src/class/audio/audio_device.c index 092e5e12..de0bddcd 100644 --- a/src/class/audio/audio_device.c +++ b/src/class/audio/audio_device.c @@ -88,7 +88,8 @@ CFG_TUSB_MCU == OPT_MCU_STM32H7 || \ (CFG_TUSB_MCU == OPT_MCU_STM32L4 && defined(STM32L4_SYNOPSYS)) || \ CFG_TUSB_MCU == OPT_MCU_RX63X || \ - CFG_TUSB_MCU == OPT_MCU_RX65X + CFG_TUSB_MCU == OPT_MCU_RX65X || \ + CFG_TUSB_MCU == OPT_MCU_RX72N #define USE_LINEAR_BUFFER 0 #else #define USE_LINEAR_BUFFER 1 From 45e55a8ea05d1101626addb74c9a2059030f6ff2 Mon Sep 17 00:00:00 2001 From: kkitayam <45088311+kkitayam@users.noreply.github.com> Date: Sat, 31 Jul 2021 12:20:19 +0900 Subject: [PATCH 7/7] fix: D0FIFOSEL setting was incorrectly when big-endian is selected. In pipe_xfer_in(), the endianness setting of D0FIFOSEL was lacking due to refactoring. And add type cast operation to avoid warnings by CCRX. --- src/portable/renesas/usba/dcd_usba.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/portable/renesas/usba/dcd_usba.c b/src/portable/renesas/usba/dcd_usba.c index 09622d89..73986141 100644 --- a/src/portable/renesas/usba/dcd_usba.c +++ b/src/portable/renesas/usba/dcd_usba.c @@ -313,9 +313,9 @@ static bool pipe0_xfer_in(void) void *buf = pipe->buf; if (len) { if (pipe->ff) { - pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.CFIFO.WORD, len, TUSB_DIR_IN); + pipe_read_write_packet_ff((tu_fifo_t*)buf, (volatile void*)&USB0.CFIFO.WORD, len, TUSB_DIR_IN); } else { - pipe_write_packet(buf, &USB0.CFIFO.WORD, len); + pipe_write_packet(buf, (volatile void*)&USB0.CFIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; } } @@ -335,9 +335,9 @@ static bool pipe0_xfer_out(void) void *buf = pipe->buf; if (len) { if (pipe->ff) { - pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.CFIFO.WORD, len, TUSB_DIR_OUT); + pipe_read_write_packet_ff((tu_fifo_t*)buf, (volatile void*)&USB0.CFIFO.WORD, len, TUSB_DIR_OUT); } else { - pipe_read_packet(buf, &USB0.CFIFO.WORD, len); + pipe_read_packet(buf, (volatile void*)&USB0.CFIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; } } @@ -360,16 +360,16 @@ static bool pipe_xfer_in(unsigned num) return true; } - USB0.D0FIFOSEL.WORD = num | USB_FIFOSEL_MBW_16; + USB0.D0FIFOSEL.WORD = num | USB_FIFOSEL_MBW_16 | (TU_BYTE_ORDER == TU_BIG_ENDIAN ? USB_FIFOSEL_BIGEND : 0); const unsigned mps = edpt_max_packet_size(num); pipe_wait_for_ready(num); const unsigned len = TU_MIN(rem, mps); void *buf = pipe->buf; if (len) { if (pipe->ff) { - pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.D0FIFO.WORD, len, TUSB_DIR_IN); + pipe_read_write_packet_ff((tu_fifo_t*)buf, (volatile void*)&USB0.D0FIFO.WORD, len, TUSB_DIR_IN); } else { - pipe_write_packet(buf, &USB0.D0FIFO.WORD, len); + pipe_write_packet(buf, (volatile void*)&USB0.D0FIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; } } @@ -393,9 +393,9 @@ static bool pipe_xfer_out(unsigned num) void *buf = pipe->buf; if (len) { if (pipe->ff) { - pipe_read_write_packet_ff((tu_fifo_t*)buf, &USB0.D0FIFO.WORD, len, TUSB_DIR_OUT); + pipe_read_write_packet_ff((tu_fifo_t*)buf, (volatile void*)&USB0.D0FIFO.WORD, len, TUSB_DIR_OUT); } else { - pipe_read_packet(buf, &USB0.D0FIFO.WORD, len); + pipe_read_packet(buf, (volatile void*)&USB0.D0FIFO.WORD, len); pipe->buf = (uint8_t*)buf + len; } } @@ -488,6 +488,7 @@ static bool process_pipe_xfer(int buffer_type, uint8_t ep_addr, void* buffer, ui pipe_wait_for_ready(num); USB0.D0FIFOCTR.WORD = USB_FIFOCTR_BVAL; USB0.D0FIFOSEL.WORD = 0; + while (USB0.D0FIFOSEL.BIT.CURPIPE) ; /* if CURPIPE bits changes, check written value */ } } else { volatile reg_pipetre_t *pt = get_pipetre(num);