more btable set/get clean up

This commit is contained in:
hathach
2024-07-30 11:17:55 +07:00
parent 75d3a3be84
commit 6771ef35d9
2 changed files with 19 additions and 59 deletions

View File

@@ -320,12 +320,8 @@ static void dcd_ep_ctr_tx_handler(uint32_t wIstr) {
return; return;
} }
xfer->iso_in_sending = false; xfer->iso_in_sending = false;
uint8_t buf_id = (wEPRegVal & USB_EP_DTOG_TX) ? 0 : 1;
if (wEPRegVal & USB_EP_DTOG_TX) { btable_set_count(EPindex, buf_id, 0);
pcd_set_ep_tx_dbuf0_cnt(USB, EPindex, 0);
} else {
pcd_set_ep_tx_dbuf1_cnt(USB, EPindex, 0);
}
} }
if ((xfer->total_len != xfer->queued_len)) { if ((xfer->total_len != xfer->queued_len)) {
@@ -406,21 +402,16 @@ static void dcd_ep_ctr_rx_handler(uint32_t wIstr)
if (ep_addr != 0u) { if (ep_addr != 0u) {
pcd_clear_rx_ep_ctr(USB, EPindex); pcd_clear_rx_ep_ctr(USB, EPindex);
} }
uint32_t count;
uint16_t addr; uint8_t buf_id;
/* Read from correct register when ISOCHRONOUS (double buffered) */
if ((wEPRegVal & USB_EP_TYPE_MASK) == USB_EP_ISOCHRONOUS) { if ((wEPRegVal & USB_EP_TYPE_MASK) == USB_EP_ISOCHRONOUS) {
if (wEPRegVal & USB_EP_DTOG_RX) { // ISO endpoints are double buffered
count = pcd_get_ep_dbuf0_cnt(USB, EPindex); buf_id = (wEPRegVal & USB_EP_DTOG_RX) ? 0 : 1;
addr = pcd_get_ep_dbuf0_address(USB, EPindex);
} else {
count = pcd_get_ep_dbuf1_cnt(USB, EPindex);
addr = pcd_get_ep_dbuf1_address(USB, EPindex);
}
} else { } else {
count = pcd_get_ep_rx_cnt(USB, EPindex); buf_id = 1;
addr = pcd_get_ep_rx_address(USB, EPindex);
} }
uint32_t count = btable_get_count(EPindex, buf_id);
uint16_t addr = (uint16_t) btable_get_addr(EPindex, buf_id);
TU_ASSERT(count <= xfer->max_packet_size, /**/); TU_ASSERT(count <= xfer->max_packet_size, /**/);
@@ -630,7 +621,6 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_ep) {
uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const dir = tu_edpt_dir(ep_addr);
const uint16_t packet_size = tu_edpt_packet_size(desc_ep); const uint16_t packet_size = tu_edpt_packet_size(desc_ep);
const uint16_t buffer_size = pcd_aligned_buffer_size(packet_size); const uint16_t buffer_size = pcd_aligned_buffer_size(packet_size);
uint16_t pma_addr;
uint32_t wType; uint32_t wType;
TU_ASSERT(ep_idx < FSDEV_EP_COUNT); TU_ASSERT(ep_idx < FSDEV_EP_COUNT);
@@ -658,7 +648,7 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_ep) {
pcd_set_ep_address(USB, ep_idx, tu_edpt_number(ep_addr)); pcd_set_ep_address(USB, ep_idx, tu_edpt_number(ep_addr));
/* Create a packet memory buffer area. */ /* Create a packet memory buffer area. */
pma_addr = dcd_pma_alloc(buffer_size, false); uint16_t pma_addr = dcd_pma_alloc(buffer_size, false);
if (dir == TUSB_DIR_IN) { if (dir == TUSB_DIR_IN) {
pcd_set_ep_tx_address(USB, ep_idx, pma_addr); pcd_set_ep_tx_address(USB, ep_idx, pma_addr);
@@ -732,11 +722,9 @@ bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet
uint32_t pma_addr = dcd_pma_alloc(buffer_size, true); uint32_t pma_addr = dcd_pma_alloc(buffer_size, true);
uint16_t pma_addr2 = pma_addr; uint16_t pma_addr2 = pma_addr;
#endif #endif
pcd_set_ep_tx_address(USB, ep_idx, pma_addr); btable_set_addr(ep_idx, 0, pma_addr);
pcd_set_ep_rx_address(USB, ep_idx, pma_addr2); btable_set_addr(ep_idx, 1, pma_addr2);
pcd_set_eptype(USB, ep_idx, USB_EP_ISOCHRONOUS); pcd_set_eptype(USB, ep_idx, USB_EP_ISOCHRONOUS);
xfer_ctl_ptr(ep_addr)->ep_idx = ep_idx; xfer_ctl_ptr(ep_addr)->ep_idx = ep_idx;
return true; return true;
@@ -770,8 +758,7 @@ bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const *p_endpoin
} }
// Currently, single-buffered, and only 64 bytes at a time (max) // Currently, single-buffered, and only 64 bytes at a time (max)
static void dcd_transmit_packet(xfer_ctl_t *xfer, uint16_t ep_ix) static void dcd_transmit_packet(xfer_ctl_t *xfer, uint16_t ep_ix) {
{
uint16_t len = (uint16_t)(xfer->total_len - xfer->queued_len); uint16_t len = (uint16_t)(xfer->total_len - xfer->queued_len);
if (len > xfer->max_packet_size) { if (len > xfer->max_packet_size) {
len = xfer->max_packet_size; len = xfer->max_packet_size;
@@ -779,20 +766,15 @@ static void dcd_transmit_packet(xfer_ctl_t *xfer, uint16_t ep_ix)
uint16_t ep_reg = pcd_get_endpoint(USB, ep_ix); uint16_t ep_reg = pcd_get_endpoint(USB, ep_ix);
bool const is_iso = (ep_reg & USB_EP_TYPE_MASK) == USB_EP_ISOCHRONOUS; bool const is_iso = (ep_reg & USB_EP_TYPE_MASK) == USB_EP_ISOCHRONOUS;
uint16_t addr_ptr;
uint8_t buf_id;
if (is_iso) { if (is_iso) {
if (ep_reg & USB_EP_DTOG_TX) { buf_id = (ep_reg & USB_EP_DTOG_TX) ? 1 : 0;
addr_ptr = pcd_get_ep_dbuf1_address(USB, ep_ix);
pcd_set_ep_tx_dbuf1_cnt(USB, ep_ix, len);
} else {
addr_ptr = pcd_get_ep_dbuf0_address(USB, ep_ix);
pcd_set_ep_tx_dbuf0_cnt(USB, ep_ix, len);
}
} else { } else {
addr_ptr = pcd_get_ep_tx_address(USB, ep_ix); buf_id = 0;
pcd_set_ep_tx_cnt(USB, ep_ix, len);
} }
uint16_t addr_ptr = (uint16_t) btable_get_addr(ep_ix, buf_id);
btable_set_count(ep_ix, buf_id, len);
if (xfer->ff) { if (xfer->ff) {
dcd_write_packet_memory_ff(xfer->ff, addr_ptr, len); dcd_write_packet_memory_ff(xfer->ff, addr_ptr, len);

View File

@@ -69,7 +69,7 @@ TU_VERIFY_STATIC(FSDEV_BTABLE_BASE % 8 == 0, "BTABLE base must be aligned to 8 b
// 16-bit or 32-bit depending on FSDEV_BUS_32BIT. // 16-bit or 32-bit depending on FSDEV_BUS_32BIT.
typedef struct { typedef struct {
union { union {
// 0: tx, 1: rx // 0: TX (IN), 1: RX (OUT)
// strictly 16-bit access (could be 32-bit aligned) // strictly 16-bit access (could be 32-bit aligned)
struct { struct {
@@ -239,9 +239,6 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t pcd_get_ep_rx_cnt(USB_TypeDef * USB
return btable_get_count(bEpIdx, 1); return btable_get_count(bEpIdx, 1);
} }
#define pcd_get_ep_dbuf0_cnt pcd_get_ep_tx_cnt
#define pcd_get_ep_dbuf1_cnt pcd_get_ep_rx_cnt
TU_ATTR_ALWAYS_INLINE static inline uint32_t pcd_get_ep_tx_address(USB_TypeDef * USBx, uint32_t bEpIdx) { TU_ATTR_ALWAYS_INLINE static inline uint32_t pcd_get_ep_tx_address(USB_TypeDef * USBx, uint32_t bEpIdx) {
(void) USBx; (void) USBx;
return btable_get_addr(bEpIdx, 0); return btable_get_addr(bEpIdx, 0);
@@ -252,10 +249,6 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t pcd_get_ep_rx_address(USB_TypeDef *
return btable_get_addr(bEpIdx, 1); return btable_get_addr(bEpIdx, 1);
} }
#define pcd_get_ep_dbuf0_address pcd_get_ep_tx_address
#define pcd_get_ep_dbuf1_address pcd_get_ep_rx_address
TU_ATTR_ALWAYS_INLINE static inline void btable_set_addr(uint32_t ep_id, uint8_t is_rx, uint16_t addr) { TU_ATTR_ALWAYS_INLINE static inline void btable_set_addr(uint32_t ep_id, uint8_t is_rx, uint16_t addr) {
#ifdef FSDEV_BUS_32BIT #ifdef FSDEV_BUS_32BIT
uint32_t count_addr = FSDEV_BTABLE->ep32[ep_id][is_rx].count_addr; uint32_t count_addr = FSDEV_BTABLE->ep32[ep_id][is_rx].count_addr;
@@ -288,26 +281,11 @@ TU_ATTR_ALWAYS_INLINE static inline void pcd_set_ep_rx_address(USB_TypeDef * USB
btable_set_addr(bEpIdx, 1, addr); btable_set_addr(bEpIdx, 1, addr);
} }
#define pcd_set_ep_dbuf0_address pcd_set_ep_tx_address
#define pcd_set_ep_dbuf1_address pcd_set_ep_rx_address
TU_ATTR_ALWAYS_INLINE static inline void pcd_set_ep_tx_cnt(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wCount) { TU_ATTR_ALWAYS_INLINE static inline void pcd_set_ep_tx_cnt(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wCount) {
(void) USBx; (void) USBx;
btable_set_count(bEpIdx, 0, wCount); btable_set_count(bEpIdx, 0, wCount);
} }
#define pcd_set_ep_tx_dbuf0_cnt pcd_set_ep_tx_cnt
TU_ATTR_ALWAYS_INLINE static inline void pcd_set_ep_tx_dbuf1_cnt(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wCount) {
#ifdef FSDEV_BUS_32BIT
(void) USBx;
pma32[2*bEpIdx + 1] = (pma32[2*bEpIdx + 1] & ~0x03FF0000u) | ((wCount & 0x3FFu) << 16);
#else
volatile uint16_t * reg = pcd_ep_rx_cnt_ptr(USBx, bEpIdx);
*reg = (uint16_t) (*reg & (uint16_t) ~0x3FFU) | (wCount & 0x3FFU);
#endif
}
TU_ATTR_ALWAYS_INLINE static inline void pcd_set_ep_blsize_num_blocks(USB_TypeDef * USBx, uint32_t rxtx_idx, TU_ATTR_ALWAYS_INLINE static inline void pcd_set_ep_blsize_num_blocks(USB_TypeDef * USBx, uint32_t rxtx_idx,
uint32_t blocksize, uint32_t numblocks) { uint32_t blocksize, uint32_t numblocks) {
/* Encode into register. When BLSIZE==1, we need to subtract 1 block count */ /* Encode into register. When BLSIZE==1, we need to subtract 1 block count */