dwc2: refactor bitfields.
Signed-off-by: HiFiPhile <admin@hifiphile.com>
This commit is contained in:
@@ -44,7 +44,7 @@
|
||||
#if TU_CHECK_MCU(OPT_MCU_GD32VF103)
|
||||
#define DWC2_EP_COUNT(_dwc2) DWC2_EP_MAX
|
||||
#else
|
||||
#define DWC2_EP_COUNT(_dwc2) ((_dwc2)->ghwcfg2_bm.num_dev_ep + 1)
|
||||
#define DWC2_EP_COUNT(_dwc2) ({const dwc2_ghwcfg2_t ghwcfg2 = {.value = (_dwc2)->ghwcfg2}; ghwcfg2.num_dev_ep + 1;})
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -102,7 +102,8 @@ bool dcd_dcache_clean_invalidate(const void* addr, uint32_t data_size) {
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool dma_device_enabled(const dwc2_regs_t* dwc2) {
|
||||
(void) dwc2;
|
||||
// Internal DMA only
|
||||
return CFG_TUD_DWC2_DMA_ENABLE && dwc2->ghwcfg2_bm.arch == GHWCFG2_ARCH_INTERNAL_DMA;
|
||||
const dwc2_ghwcfg2_t ghwcfg2 = {.value = dwc2->ghwcfg2};
|
||||
return CFG_TUD_DWC2_DMA_ENABLE && ghwcfg2.arch == GHWCFG2_ARCH_INTERNAL_DMA;
|
||||
}
|
||||
|
||||
static void dma_setup_prepare(uint8_t rhport) {
|
||||
@@ -250,20 +251,15 @@ static void edpt_activate(uint8_t rhport, const tusb_desc_endpoint_t* p_endpoint
|
||||
xfer->interval = p_endpoint_desc->bInterval;
|
||||
|
||||
// Endpoint control
|
||||
union {
|
||||
uint32_t value;
|
||||
dwc2_depctl_t bm;
|
||||
} depctl;
|
||||
depctl.value = 0;
|
||||
|
||||
depctl.bm.mps = xfer->max_size;
|
||||
depctl.bm.active = 1;
|
||||
depctl.bm.type = p_endpoint_desc->bmAttributes.xfer;
|
||||
dwc2_depctl_t depctl = {.value = 0};
|
||||
depctl.mps = xfer->max_size;
|
||||
depctl.active = 1;
|
||||
depctl.type = p_endpoint_desc->bmAttributes.xfer;
|
||||
if (p_endpoint_desc->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS) {
|
||||
depctl.bm.set_data0_iso_even = 1;
|
||||
depctl.set_data0_iso_even = 1;
|
||||
}
|
||||
if (dir == TUSB_DIR_IN) {
|
||||
depctl.bm.tx_fifo_num = epnum;
|
||||
depctl.tx_fifo_num = epnum;
|
||||
}
|
||||
|
||||
dwc2_dep_t* dep = &dwc2->ep[dir == TUSB_DIR_IN ? 0 : 1][epnum];
|
||||
@@ -343,31 +339,22 @@ static void edpt_schedule_packets(uint8_t rhport, const uint8_t epnum, const uin
|
||||
}
|
||||
|
||||
// transfer size: A full OUT transfer (multiple packets, possibly) triggers XFRC.
|
||||
union {
|
||||
uint32_t value;
|
||||
dwc2_ep_tsize_t bm;
|
||||
} deptsiz;
|
||||
deptsiz.value = 0;
|
||||
deptsiz.bm.xfer_size = total_bytes;
|
||||
deptsiz.bm.packet_count = num_packets;
|
||||
|
||||
dwc2_ep_tsize_t deptsiz = {.value = 0};
|
||||
deptsiz.xfer_size = total_bytes;
|
||||
deptsiz.packet_count = num_packets;
|
||||
dep->tsiz = deptsiz.value;
|
||||
|
||||
// control
|
||||
union {
|
||||
dwc2_depctl_t bm;
|
||||
uint32_t value;
|
||||
} depctl;
|
||||
depctl.value = dep->ctl;
|
||||
|
||||
depctl.bm.clear_nak = 1;
|
||||
depctl.bm.enable = 1;
|
||||
if (depctl.bm.type == DEPCTL_EPTYPE_ISOCHRONOUS && xfer->interval == 1) {
|
||||
const uint32_t odd_now = (dwc2->dsts_bm.frame_number & 1u);
|
||||
dwc2_depctl_t depctl = {.value = dep->ctl};
|
||||
depctl.clear_nak = 1;
|
||||
depctl.enable = 1;
|
||||
if (depctl.type == DEPCTL_EPTYPE_ISOCHRONOUS && xfer->interval == 1) {
|
||||
const dwc2_dsts_t dsts = {.value = dwc2->dsts};
|
||||
const uint32_t odd_now = dsts.frame_number & 1u;
|
||||
if (odd_now) {
|
||||
depctl.bm.set_data0_iso_even = 1;
|
||||
depctl.set_data0_iso_even = 1;
|
||||
} else {
|
||||
depctl.bm.set_data1_iso_odd = 1;
|
||||
depctl.set_data1_iso_odd = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,7 +397,8 @@ bool dcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
|
||||
|
||||
// XCVRDLY: transceiver delay between xcvr_sel and txvalid during device chirp is required
|
||||
// when using with some PHYs such as USB334x (USB3341, USB3343, USB3346, USB3347)
|
||||
if (dwc2->ghwcfg2_bm.hs_phy_type == GHWCFG2_HSPHY_ULPI) {
|
||||
const dwc2_ghwcfg2_t ghwcfg2 = {.value = dwc2->ghwcfg2};
|
||||
if (ghwcfg2.hs_phy_type == GHWCFG2_HSPHY_ULPI) {
|
||||
dcfg |= DCFG_XCVRDLY;
|
||||
}
|
||||
} else {
|
||||
@@ -671,7 +659,9 @@ static void handle_bus_reset(uint8_t rhport) {
|
||||
dfifo_device_init(rhport);
|
||||
|
||||
// 5. Reset device address
|
||||
dwc2->dcfg_bm.address = 0;
|
||||
dwc2_dcfg_t dcfg = {.value = dwc2->dcfg};
|
||||
dcfg.address = 0;
|
||||
dwc2->dcfg = dcfg.value;
|
||||
|
||||
// Fixed both control EP0 size to 64 bytes
|
||||
dwc2->epin[0].ctl &= ~(0x03 << DIEPCTL_MPSIZ_Pos);
|
||||
@@ -691,8 +681,9 @@ static void handle_bus_reset(uint8_t rhport) {
|
||||
|
||||
static void handle_enum_done(uint8_t rhport) {
|
||||
dwc2_regs_t *dwc2 = DWC2_REG(rhport);
|
||||
const dwc2_dsts_t dsts = {.value = dwc2->dsts};
|
||||
tusb_speed_t speed;
|
||||
switch (dwc2->dsts_bm.enum_speed) {
|
||||
switch (dsts.enum_speed) {
|
||||
case DCFG_SPEED_HIGH:
|
||||
speed = TUSB_SPEED_HIGH;
|
||||
break;
|
||||
@@ -737,12 +728,12 @@ static void handle_rxflvl_irq(uint8_t rhport) {
|
||||
const volatile uint32_t* rx_fifo = dwc2->fifo[0];
|
||||
|
||||
// Pop control word off FIFO
|
||||
const dwc2_grxstsp_t grxstsp_bm = dwc2->grxstsp_bm;
|
||||
const uint8_t epnum = grxstsp_bm.ep_ch_num;
|
||||
const dwc2_grxstsp_t grxstsp = {.value = dwc2->grxstsp};
|
||||
const uint8_t epnum = grxstsp.ep_ch_num;
|
||||
|
||||
dwc2_dep_t* epout = &dwc2->epout[epnum];
|
||||
|
||||
switch (grxstsp_bm.packet_status) {
|
||||
switch (grxstsp.packet_status) {
|
||||
case GRXSTS_PKTSTS_GLOBAL_OUT_NAK:
|
||||
// Global OUT NAK: do nothing
|
||||
break;
|
||||
@@ -764,7 +755,7 @@ static void handle_rxflvl_irq(uint8_t rhport) {
|
||||
|
||||
case GRXSTS_PKTSTS_RX_DATA: {
|
||||
// Out packet received
|
||||
const uint16_t byte_count = grxstsp_bm.byte_count;
|
||||
const uint16_t byte_count = grxstsp.byte_count;
|
||||
xfer_ctl_t* xfer = XFER_CTL_BASE(epnum, TUSB_DIR_OUT);
|
||||
|
||||
if (byte_count) {
|
||||
@@ -778,7 +769,8 @@ static void handle_rxflvl_irq(uint8_t rhport) {
|
||||
|
||||
// short packet, minus remaining bytes (xfer_size)
|
||||
if (byte_count < xfer->max_size) {
|
||||
xfer->total_len -= epout->tsiz_bm.xfer_size;
|
||||
const dwc2_ep_tsize_t tsiz = {.value = epout->tsiz};
|
||||
xfer->total_len -= tsiz.xfer_size;
|
||||
if (epnum == 0) {
|
||||
xfer->total_len -= _dcd_data.ep0_pending[TUSB_DIR_OUT];
|
||||
_dcd_data.ep0_pending[TUSB_DIR_OUT] = 0;
|
||||
@@ -840,11 +832,13 @@ static void handle_epin_slave(uint8_t rhport, uint8_t epnum, dwc2_diepint_t diep
|
||||
// - 64 bytes or
|
||||
// - Half/Empty of TX FIFO size (configured by GAHBCFG.TXFELVL)
|
||||
if (diepint_bm.txfifo_empty && (dwc2->diepempmsk & (1 << epnum))) {
|
||||
const uint16_t remain_packets = epin->tsiz_bm.packet_count;
|
||||
dwc2_ep_tsize_t tsiz = {.value = epin->tsiz};
|
||||
const uint16_t remain_packets = tsiz.packet_count;
|
||||
|
||||
// Process every single packet (only whole packets can be written to fifo)
|
||||
for (uint16_t i = 0; i < remain_packets; i++) {
|
||||
const uint16_t remain_bytes = (uint16_t) epin->tsiz_bm.xfer_size;
|
||||
tsiz.value = epin->tsiz;
|
||||
const uint16_t remain_bytes = (uint16_t) tsiz.xfer_size;
|
||||
const uint16_t xact_bytes = tu_min16(remain_bytes, xfer->max_size);
|
||||
|
||||
// Check if dtxfsts has enough space available
|
||||
@@ -863,7 +857,8 @@ static void handle_epin_slave(uint8_t rhport, uint8_t epnum, dwc2_diepint_t diep
|
||||
}
|
||||
|
||||
// Turn off TXFE if all bytes are written.
|
||||
if (epin->tsiz_bm.xfer_size == 0) {
|
||||
tsiz.value = epin->tsiz;
|
||||
if (tsiz.xfer_size == 0) {
|
||||
dwc2->diepempmsk &= ~(1 << epnum);
|
||||
}
|
||||
}
|
||||
@@ -894,7 +889,8 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
|
||||
xfer_ctl_t* xfer = XFER_CTL_BASE(epnum, TUSB_DIR_OUT);
|
||||
|
||||
// determine actual received bytes
|
||||
const uint16_t remain = epout->tsiz_bm.xfer_size;
|
||||
const dwc2_ep_tsize_t tsiz = {.value = epout->tsiz};
|
||||
const uint16_t remain = tsiz.xfer_size;
|
||||
xfer->total_len -= remain;
|
||||
|
||||
// this is ZLP, so prepare EP0 for next setup
|
||||
|
||||
Reference in New Issue
Block a user