2024-10-15 13:03:12 +07:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2024 Ha Thach (tinyusb.org)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tusb_option.h"
|
|
|
|
|
|
|
|
#if CFG_TUH_ENABLED && defined(TUP_USBIP_DWC2)
|
|
|
|
|
|
|
|
// Debug level for DWC2
|
|
|
|
#define DWC2_DEBUG 2
|
|
|
|
|
|
|
|
#include "host/hcd.h"
|
|
|
|
#include "dwc2_common.h"
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
// Max number of endpoints application can open, can be larger than DWC2_CHANNEL_COUNT_MAX
|
2024-10-25 19:00:45 +07:00
|
|
|
#ifndef CFG_TUH_DWC2_ENDPOINT_MAX
|
2024-10-28 17:45:44 +07:00
|
|
|
#define CFG_TUH_DWC2_ENDPOINT_MAX 16
|
2024-10-25 00:20:34 +07:00
|
|
|
#endif
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
#define DWC2_CHANNEL_COUNT_MAX 16 // absolute max channel count
|
|
|
|
#define DWC2_CHANNEL_COUNT(_dwc2) tu_min8((_dwc2)->ghwcfg2_bm.num_host_ch + 1, DWC2_CHANNEL_COUNT_MAX)
|
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
TU_VERIFY_STATIC(CFG_TUH_DWC2_ENDPOINT_MAX <= 255, "currently only use 8-bit for index");
|
|
|
|
|
2024-10-17 15:56:12 +07:00
|
|
|
enum {
|
2024-10-21 17:45:40 +07:00
|
|
|
HPRT_W1C_MASK = HPRT_CONN_DETECT | HPRT_ENABLE | HPRT_ENABLE_CHANGE | HPRT_OVER_CURRENT_CHANGE
|
2024-10-17 15:56:12 +07:00
|
|
|
};
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
enum {
|
|
|
|
HCD_XFER_ERROR_MAX = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
// Host driver struct for each opened endpoint
|
2024-10-25 00:20:34 +07:00
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
uint32_t hcchar;
|
|
|
|
dwc2_channel_char_t hcchar_bm;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
uint32_t hcsplt;
|
|
|
|
dwc2_channel_split_t hcsplt_bm;
|
|
|
|
};
|
|
|
|
|
2024-10-29 21:54:21 +07:00
|
|
|
uint8_t next_pid;
|
2024-10-29 13:01:48 +07:00
|
|
|
// uint8_t resv[3];
|
|
|
|
} hcd_endpoint_t;
|
|
|
|
|
|
|
|
// Additional info for each channel when it is active
|
|
|
|
typedef struct {
|
2024-10-29 18:23:56 +07:00
|
|
|
volatile bool allocated;
|
2024-10-29 21:54:21 +07:00
|
|
|
uint8_t ep_id; // associated edpt
|
2024-10-29 18:23:56 +07:00
|
|
|
uint8_t result;
|
2024-10-29 13:01:48 +07:00
|
|
|
uint8_t err_count;
|
|
|
|
uint8_t* buffer;
|
|
|
|
uint16_t total_bytes;
|
|
|
|
} hcd_xfer_t;
|
2024-10-25 00:20:34 +07:00
|
|
|
|
|
|
|
typedef struct {
|
2024-10-29 13:01:48 +07:00
|
|
|
hcd_xfer_t xfer[DWC2_CHANNEL_COUNT_MAX];
|
|
|
|
hcd_endpoint_t edpt[CFG_TUH_DWC2_ENDPOINT_MAX];
|
2024-10-28 17:45:44 +07:00
|
|
|
} hcd_data_t;
|
2024-10-25 00:20:34 +07:00
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
hcd_data_t _hcd_data;
|
2024-10-25 00:20:34 +07:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
2024-10-21 17:45:40 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline tusb_speed_t convert_hprt_speed(uint32_t hprt_speed) {
|
|
|
|
tusb_speed_t speed;
|
|
|
|
switch(hprt_speed) {
|
|
|
|
case HPRT_SPEED_HIGH: speed = TUSB_SPEED_HIGH; break;
|
|
|
|
case HPRT_SPEED_FULL: speed = TUSB_SPEED_FULL; break;
|
|
|
|
case HPRT_SPEED_LOW : speed = TUSB_SPEED_LOW ; break;
|
2024-10-28 17:45:44 +07:00
|
|
|
default:
|
|
|
|
speed = TUSB_SPEED_INVALID;
|
|
|
|
TU_BREAKPOINT();
|
|
|
|
break;
|
2024-10-21 17:45:40 +07:00
|
|
|
}
|
|
|
|
return speed;
|
|
|
|
}
|
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool dma_host_enabled(const dwc2_regs_t* dwc2) {
|
|
|
|
(void) dwc2;
|
|
|
|
// Internal DMA only
|
|
|
|
return CFG_TUH_DWC2_DMA && dwc2->ghwcfg2_bm.arch == GHWCFG2_ARCH_INTERNAL_DMA;
|
|
|
|
}
|
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
// Find a free channel for new transfer
|
2024-10-29 13:01:48 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t channel_alloc(dwc2_regs_t* dwc2) {
|
|
|
|
const uint8_t max_channel = DWC2_CHANNEL_COUNT(dwc2);
|
2024-10-28 18:40:21 +07:00
|
|
|
for (uint8_t ch_id = 0; ch_id < max_channel; ch_id++) {
|
2024-10-29 13:01:48 +07:00
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
2024-10-29 18:23:56 +07:00
|
|
|
if (!xfer->allocated) {
|
2024-10-29 13:01:48 +07:00
|
|
|
tu_memclr(xfer, sizeof(hcd_xfer_t));
|
2024-10-29 18:23:56 +07:00
|
|
|
xfer->allocated = true;
|
2024-10-28 17:45:44 +07:00
|
|
|
return ch_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TUSB_INDEX_INVALID_8;
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void channel_dealloc(dwc2_regs_t* dwc2, uint8_t ch_id) {
|
2024-10-29 21:54:21 +07:00
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
|
|
|
xfer->allocated = false;
|
2024-10-29 16:06:44 +07:00
|
|
|
dwc2->haintmsk &= ~TU_BIT(ch_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void channel_disable(dwc2_channel_t* channel) {
|
2024-10-29 19:08:45 +07:00
|
|
|
// disable also require request queue
|
|
|
|
// request_queue_avail();
|
2024-10-29 16:06:44 +07:00
|
|
|
channel->hcintmsk |= HCINT_HALTED;
|
2024-10-29 18:23:56 +07:00
|
|
|
channel->hcchar |= HCCHAR_CHDIS | HCCHAR_CHENA; // must set both CHDIS and CHENA
|
2024-10-29 13:01:48 +07:00
|
|
|
}
|
|
|
|
|
2024-10-29 19:08:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t request_queue_avail(const dwc2_regs_t* dwc2, bool is_period) {
|
|
|
|
if (is_period) {
|
|
|
|
return dwc2->hptxsts_bm.req_queue_available;
|
|
|
|
} else {
|
|
|
|
return dwc2->hnptxsts_bm.req_queue_available;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to send IN token to receive data
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool channel_send_in_token(const dwc2_regs_t* dwc2, dwc2_channel_t* channel, bool is_period) {
|
|
|
|
TU_ASSERT(request_queue_avail(dwc2, is_period));
|
|
|
|
channel->hcchar_bm.enable = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
// Find currently enabled channel. Note: EP0 is bidirectional
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t channel_find_enabled(dwc2_regs_t* dwc2, uint8_t dev_addr, uint8_t ep_num, uint8_t ep_dir) {
|
|
|
|
const uint8_t max_channel = DWC2_CHANNEL_COUNT(dwc2);
|
2024-10-28 17:45:44 +07:00
|
|
|
for (uint8_t ch_id = 0; ch_id < max_channel; ch_id++) {
|
2024-10-29 18:23:56 +07:00
|
|
|
if (_hcd_data.xfer[ch_id].allocated) {
|
2024-10-28 17:45:44 +07:00
|
|
|
const dwc2_channel_char_t hcchar_bm = dwc2->channel[ch_id].hcchar_bm;
|
2024-10-29 13:01:48 +07:00
|
|
|
if (hcchar_bm.dev_addr == dev_addr && hcchar_bm.ep_num == ep_num && (ep_num == 0 || hcchar_bm.ep_dir == ep_dir)) {
|
2024-10-28 17:45:44 +07:00
|
|
|
return ch_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TUSB_INDEX_INVALID_8;
|
|
|
|
}
|
|
|
|
|
2024-10-29 15:07:28 +07:00
|
|
|
// Check if is periodic (interrupt/isochronous)
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool edpt_is_periodic(uint8_t ep_type) {
|
|
|
|
return ep_type == HCCHAR_EPTYPE_INTERRUPT || ep_type == HCCHAR_EPTYPE_ISOCHRONOUS;
|
|
|
|
}
|
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
// Allocate a new endpoint
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t edpt_alloc(void) {
|
|
|
|
for (uint32_t i = 0; i < CFG_TUH_DWC2_ENDPOINT_MAX; i++) {
|
|
|
|
hcd_endpoint_t* edpt = &_hcd_data.edpt[i];
|
|
|
|
if (edpt->hcchar_bm.enable == 0) {
|
|
|
|
edpt->hcchar_bm.enable = 1;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TUSB_INDEX_INVALID_8;
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
// Find a endpoint that is opened previously with hcd_edpt_open()
|
|
|
|
// Note: EP0 is bidirectional
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t edpt_find_opened(uint8_t dev_addr, uint8_t ep_num, uint8_t ep_dir) {
|
2024-10-28 17:45:44 +07:00
|
|
|
for (uint8_t i = 0; i < (uint8_t)CFG_TUH_DWC2_ENDPOINT_MAX; i++) {
|
2024-10-29 13:01:48 +07:00
|
|
|
const dwc2_channel_char_t* hcchar_bm = &_hcd_data.edpt[i].hcchar_bm;
|
2024-10-28 17:45:44 +07:00
|
|
|
if (hcchar_bm->enable && hcchar_bm->dev_addr == dev_addr &&
|
|
|
|
hcchar_bm->ep_num == ep_num && (ep_num == 0 || hcchar_bm->ep_dir == ep_dir)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TUSB_INDEX_INVALID_8;
|
|
|
|
}
|
|
|
|
|
2024-10-29 21:54:21 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t cal_next_pid(uint8_t pid, uint8_t packet_count) {
|
|
|
|
if (packet_count & 0x01) {
|
|
|
|
return pid ^ 0x02; // toggle DATA0 and DATA1
|
|
|
|
} else {
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
/* USB Data FIFO Layout
|
|
|
|
|
|
|
|
The FIFO is split up into
|
|
|
|
- EPInfo: for storing DMA metadata (check dcd_dwc2.c for more details)
|
|
|
|
- 1 RX FIFO: for receiving data
|
|
|
|
- 1 TX FIFO for non-periodic (NPTX)
|
|
|
|
- 1 TX FIFO for periodic (PTX)
|
|
|
|
|
|
|
|
We allocated TX FIFO from top to bottom (using top pointer), this to allow the RX FIFO to grow dynamically which is
|
|
|
|
possible since the free space is located between the RX and TX FIFOs.
|
|
|
|
|
|
|
|
----------------- ep_fifo_size
|
2024-10-25 19:00:45 +07:00
|
|
|
| HCDMAn |
|
2024-10-25 00:20:34 +07:00
|
|
|
|--------------|-- gdfifocfg.EPINFOBASE (max is ghwcfg3.dfifo_depth)
|
|
|
|
| Non-Periodic |
|
|
|
|
| TX FIFO |
|
|
|
|
|--------------|--- GNPTXFSIZ.addr (fixed size)
|
|
|
|
| Periodic |
|
|
|
|
| TX FIFO |
|
|
|
|
|--------------|--- HPTXFSIZ.addr (expandable downward)
|
|
|
|
| FREE |
|
|
|
|
| |
|
|
|
|
|--------------|-- GRXFSIZ (expandable upward)
|
|
|
|
| RX FIFO |
|
|
|
|
---------------- 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Programming Guide 2.1.2 FIFO RAM allocation
|
|
|
|
* RX
|
|
|
|
* - Largest-EPsize/4 + 2 (status info). recommended x2 if high bandwidth or multiple ISO are used.
|
|
|
|
* - 2 for transfer complete and channel halted status
|
|
|
|
* - 1 for each Control/Bulk out endpoint to Handle NAK/NYET (i.e max is number of host channel)
|
|
|
|
*
|
|
|
|
* TX non-periodic (NPTX)
|
|
|
|
* - At least largest-EPsize/4, recommended x2
|
|
|
|
*
|
|
|
|
* TX periodic (PTX)
|
|
|
|
* - At least largest-EPsize*MulCount/4 (MulCount up to 3 for high-bandwidth ISO/interrupt)
|
|
|
|
*/
|
|
|
|
static void dfifo_host_init(uint8_t rhport) {
|
|
|
|
const dwc2_controller_t* dwc2_controller = &_dwc2_controller[rhport];
|
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
|
|
|
|
// Scatter/Gather DMA mode is not yet supported. Buffer DMA only need 1 words per channel
|
|
|
|
const bool is_dma = dma_host_enabled(dwc2);
|
|
|
|
uint16_t dfifo_top = dwc2_controller->ep_fifo_size/4;
|
|
|
|
if (is_dma) {
|
|
|
|
dfifo_top -= dwc2->ghwcfg2_bm.num_host_ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixed allocation for now, improve later:
|
|
|
|
// - ptx_largest is limited to 256 for FS since most FS core only has 1024 bytes total
|
|
|
|
bool is_highspeed = dwc2_core_is_highspeed(dwc2, TUSB_ROLE_HOST);
|
|
|
|
uint32_t nptx_largest = is_highspeed ? TUSB_EPSIZE_BULK_HS/4 : TUSB_EPSIZE_BULK_FS/4;
|
|
|
|
uint32_t ptx_largest = is_highspeed ? TUSB_EPSIZE_ISO_HS_MAX/4 : 256/4;
|
|
|
|
|
|
|
|
uint16_t nptxfsiz = 2 * nptx_largest;
|
|
|
|
uint16_t rxfsiz = 2 * (ptx_largest + 2) + dwc2->ghwcfg2_bm.num_host_ch;
|
|
|
|
TU_ASSERT(dfifo_top >= (nptxfsiz + rxfsiz),);
|
|
|
|
uint16_t ptxfsiz = dfifo_top - (nptxfsiz + rxfsiz);
|
|
|
|
|
|
|
|
dwc2->gdfifocfg = (dfifo_top << GDFIFOCFG_EPINFOBASE_SHIFT) | dfifo_top;
|
|
|
|
|
|
|
|
dfifo_top -= rxfsiz;
|
|
|
|
dwc2->grxfsiz = rxfsiz;
|
|
|
|
|
|
|
|
dfifo_top -= nptxfsiz;
|
|
|
|
dwc2->gnptxfsiz = tu_u32_from_u16(nptxfsiz, dfifo_top);
|
|
|
|
|
|
|
|
dfifo_top -= ptxfsiz;
|
|
|
|
dwc2->hptxfsiz = tu_u32_from_u16(ptxfsiz, dfifo_top);
|
|
|
|
}
|
|
|
|
|
2024-10-15 13:03:12 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Controller API
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
// optional hcd configuration, called by tuh_configure()
|
|
|
|
bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param) {
|
|
|
|
(void) rhport;
|
|
|
|
(void) cfg_id;
|
|
|
|
(void) cfg_param;
|
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
return true;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize controller to host mode
|
|
|
|
bool hcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
|
2024-10-25 00:20:34 +07:00
|
|
|
(void) rh_init;
|
2024-10-16 13:19:28 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
2024-10-15 13:03:12 +07:00
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
tu_memclr(&_hcd_data, sizeof(_hcd_data));
|
|
|
|
|
2024-10-16 13:19:28 +07:00
|
|
|
// Core Initialization
|
2024-10-25 00:20:34 +07:00
|
|
|
const bool is_highspeed = dwc2_core_is_highspeed(dwc2, TUSB_ROLE_HOST);
|
|
|
|
const bool is_dma = dma_host_enabled(dwc2);
|
2024-10-21 11:43:37 +07:00
|
|
|
TU_ASSERT(dwc2_core_init(rhport, is_highspeed, is_dma));
|
2024-10-16 13:19:28 +07:00
|
|
|
|
2024-10-17 15:56:12 +07:00
|
|
|
//------------- 3.1 Host Initialization -------------//
|
|
|
|
|
2024-10-21 11:43:37 +07:00
|
|
|
// FS/LS PHY Clock Select
|
|
|
|
uint32_t hcfg = dwc2->hcfg;
|
|
|
|
if (is_highspeed) {
|
|
|
|
hcfg &= ~HCFG_FSLS_ONLY;
|
|
|
|
} else {
|
|
|
|
hcfg &= ~HCFG_FSLS_ONLY; // since we are using FS PHY
|
|
|
|
hcfg &= ~HCFG_FSLS_PHYCLK_SEL;
|
|
|
|
|
|
|
|
if (dwc2->ghwcfg2_bm.hs_phy_type == GHWCFG2_HSPHY_ULPI &&
|
|
|
|
dwc2->ghwcfg2_bm.fs_phy_type == GHWCFG2_FSPHY_DEDICATED) {
|
|
|
|
// dedicated FS PHY with 48 mhz
|
|
|
|
hcfg |= HCFG_FSLS_PHYCLK_SEL_48MHZ;
|
|
|
|
} else {
|
|
|
|
// shared HS PHY running at full speed
|
|
|
|
hcfg |= HCFG_FSLS_PHYCLK_SEL_30_60MHZ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dwc2->hcfg = hcfg;
|
2024-10-17 15:56:12 +07:00
|
|
|
|
2024-10-21 17:45:40 +07:00
|
|
|
// Enable HFIR reload
|
|
|
|
|
2024-10-21 11:43:37 +07:00
|
|
|
// force host mode and wait for mode switch
|
2024-10-16 13:19:28 +07:00
|
|
|
dwc2->gusbcfg = (dwc2->gusbcfg & ~GUSBCFG_FDMOD) | GUSBCFG_FHMOD;
|
2024-10-29 21:54:21 +07:00
|
|
|
while ((dwc2->gintsts & GINTSTS_CMOD) != GINTSTS_CMODE_HOST) {}
|
2024-10-16 13:19:28 +07:00
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
// configure fixed-allocated fifo scheme
|
|
|
|
dfifo_host_init(rhport);
|
|
|
|
|
2024-10-17 15:56:12 +07:00
|
|
|
dwc2->hprt = HPRT_W1C_MASK; // clear all write-1-clear bits
|
2024-10-21 11:43:37 +07:00
|
|
|
dwc2->hprt = HPRT_POWER; // turn on VBUS
|
2024-10-17 15:56:12 +07:00
|
|
|
|
2024-10-16 13:19:28 +07:00
|
|
|
// Enable required interrupts
|
2024-10-25 22:56:11 +07:00
|
|
|
dwc2->gintmsk |= GINTSTS_OTGINT | GINTSTS_CONIDSTSCHNG | GINTSTS_HPRTINT | GINTSTS_HCINT;
|
2024-10-25 19:00:45 +07:00
|
|
|
|
|
|
|
// NPTX can hold at least 2 packet, change interrupt level to half-empty
|
|
|
|
uint32_t gahbcfg = dwc2->gahbcfg & ~GAHBCFG_TX_FIFO_EPMTY_LVL;
|
|
|
|
gahbcfg |= GAHBCFG_GINT; // Enable global interrupt
|
|
|
|
dwc2->gahbcfg = gahbcfg;
|
2024-10-16 13:19:28 +07:00
|
|
|
|
|
|
|
return true;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable USB interrupt
|
|
|
|
void hcd_int_enable (uint8_t rhport) {
|
2024-10-17 15:56:12 +07:00
|
|
|
dwc2_int_set(rhport, TUSB_ROLE_HOST, true);
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disable USB interrupt
|
|
|
|
void hcd_int_disable(uint8_t rhport) {
|
2024-10-17 15:56:12 +07:00
|
|
|
dwc2_int_set(rhport, TUSB_ROLE_HOST, false);
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get frame number (1ms)
|
|
|
|
uint32_t hcd_frame_number(uint8_t rhport) {
|
2024-10-21 17:45:40 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
return dwc2->hfnum & HFNUM_FRNUM_Msk;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Port API
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
// Get the current connect status of roothub port
|
|
|
|
bool hcd_port_connect_status(uint8_t rhport) {
|
2024-10-17 15:56:12 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
return dwc2->hprt & HPRT_CONN_STATUS;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset USB bus on the port. Return immediately, bus reset sequence may not be complete.
|
|
|
|
// Some port would require hcd_port_reset_end() to be invoked after 10ms to complete the reset sequence.
|
|
|
|
void hcd_port_reset(uint8_t rhport) {
|
2024-10-17 15:56:12 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
2024-10-21 17:45:40 +07:00
|
|
|
uint32_t hprt = dwc2->hprt & ~HPRT_W1C_MASK;
|
|
|
|
hprt |= HPRT_RESET;
|
|
|
|
dwc2->hprt = hprt;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complete bus reset sequence, may be required by some controllers
|
|
|
|
void hcd_port_reset_end(uint8_t rhport) {
|
2024-10-17 15:56:12 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
uint32_t hprt = dwc2->hprt & ~HPRT_W1C_MASK; // skip w1c bits
|
|
|
|
hprt &= ~HPRT_RESET;
|
|
|
|
dwc2->hprt = hprt;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get port link speed
|
|
|
|
tusb_speed_t hcd_port_speed_get(uint8_t rhport) {
|
2024-10-21 17:45:40 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
const tusb_speed_t speed = convert_hprt_speed(dwc2->hprt_bm.speed);
|
|
|
|
return speed;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HCD closes all opened endpoints belong to this device
|
|
|
|
void hcd_device_close(uint8_t rhport, uint8_t dev_addr) {
|
|
|
|
(void) rhport;
|
2024-10-28 17:45:44 +07:00
|
|
|
for (uint8_t i = 0; i < (uint8_t) CFG_TUH_DWC2_ENDPOINT_MAX; i++) {
|
2024-10-29 13:01:48 +07:00
|
|
|
hcd_endpoint_t* edpt = &_hcd_data.edpt[i];
|
|
|
|
if (edpt->hcchar_bm.enable && edpt->hcchar_bm.dev_addr == dev_addr) {
|
|
|
|
tu_memclr(edpt, sizeof(hcd_endpoint_t));
|
2024-10-28 17:45:44 +07:00
|
|
|
}
|
|
|
|
}
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Endpoints API
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
// Open an endpoint
|
2024-10-29 13:01:48 +07:00
|
|
|
bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, const tusb_desc_endpoint_t* desc_ep) {
|
2024-10-15 13:03:12 +07:00
|
|
|
(void) rhport;
|
2024-10-25 00:20:34 +07:00
|
|
|
//dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
|
|
|
|
hcd_devtree_info_t devtree_info;
|
|
|
|
hcd_devtree_get_info(dev_addr, &devtree_info);
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
// find a free endpoint
|
2024-10-29 18:23:56 +07:00
|
|
|
const uint8_t ep_id = edpt_alloc();
|
|
|
|
TU_ASSERT(ep_id < CFG_TUH_DWC2_ENDPOINT_MAX);
|
|
|
|
hcd_endpoint_t* edpt = &_hcd_data.edpt[ep_id];
|
2024-10-15 13:03:12 +07:00
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
dwc2_channel_char_t* hcchar_bm = &edpt->hcchar_bm;
|
|
|
|
hcchar_bm->ep_size = tu_edpt_packet_size(desc_ep);
|
|
|
|
hcchar_bm->ep_num = tu_edpt_number(desc_ep->bEndpointAddress);
|
|
|
|
hcchar_bm->ep_dir = tu_edpt_dir(desc_ep->bEndpointAddress);
|
|
|
|
hcchar_bm->low_speed_dev = (devtree_info.speed == TUSB_SPEED_LOW) ? 1 : 0;
|
|
|
|
hcchar_bm->ep_type = desc_ep->bmAttributes.xfer; // ep_type matches TUSB_XFER_*
|
|
|
|
hcchar_bm->err_multi_count = 0;
|
|
|
|
hcchar_bm->dev_addr = dev_addr;
|
|
|
|
hcchar_bm->odd_frame = 0;
|
|
|
|
hcchar_bm->disable = 0;
|
|
|
|
hcchar_bm->enable = 1;
|
|
|
|
|
|
|
|
dwc2_channel_split_t* hcsplt_bm = &edpt->hcsplt_bm;
|
|
|
|
hcsplt_bm->hub_port = devtree_info.hub_port;
|
|
|
|
hcsplt_bm->hub_addr = devtree_info.hub_addr;
|
|
|
|
hcsplt_bm->xact_pos = 0;
|
|
|
|
hcsplt_bm->split_compl = 0;
|
|
|
|
hcsplt_bm->split_en = 0;
|
|
|
|
|
2024-10-29 21:54:21 +07:00
|
|
|
edpt->next_pid = HCTSIZ_PID_DATA0;
|
2024-10-29 18:23:56 +07:00
|
|
|
|
|
|
|
return true;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
|
|
|
|
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) {
|
2024-10-25 00:20:34 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
2024-10-25 19:00:45 +07:00
|
|
|
const uint8_t ep_num = tu_edpt_number(ep_addr);
|
|
|
|
const uint8_t ep_dir = tu_edpt_dir(ep_addr);
|
2024-10-29 21:54:21 +07:00
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
uint8_t ep_id = edpt_find_opened(dev_addr, ep_num, ep_dir);
|
|
|
|
TU_ASSERT(ep_id < CFG_TUH_DWC2_ENDPOINT_MAX);
|
|
|
|
hcd_endpoint_t* edpt = &_hcd_data.edpt[ep_id];
|
|
|
|
dwc2_channel_char_t* hcchar_bm = &edpt->hcchar_bm;
|
2024-10-29 22:48:58 +07:00
|
|
|
bool const is_period = edpt_is_periodic(hcchar_bm->ep_type);
|
2024-10-29 13:01:48 +07:00
|
|
|
|
|
|
|
uint8_t ch_id = channel_alloc(dwc2);
|
|
|
|
TU_ASSERT(ch_id < 16); // all channel are in used
|
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
2024-10-29 21:54:21 +07:00
|
|
|
xfer->ep_id = ep_id;
|
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
dwc2_channel_t* channel = &dwc2->channel[ch_id];
|
2024-10-29 13:01:48 +07:00
|
|
|
|
2024-10-25 19:00:45 +07:00
|
|
|
uint16_t packet_count = tu_div_ceil(buflen, hcchar_bm->ep_size);
|
|
|
|
if (packet_count == 0) {
|
|
|
|
packet_count = 1; // zero length packet still count as 1
|
|
|
|
}
|
2024-10-29 21:54:21 +07:00
|
|
|
channel->hctsiz = (edpt->next_pid << HCTSIZ_PID_Pos) | (packet_count << HCTSIZ_PKTCNT_Pos) | buflen;
|
2024-10-25 00:20:34 +07:00
|
|
|
|
2024-10-29 21:54:21 +07:00
|
|
|
// pre-calculate next PID based on packet count, adjusted in transfer complete interrupt if short packet
|
|
|
|
if (ep_num == 0) {
|
|
|
|
edpt->next_pid = HCTSIZ_PID_DATA1; // control data and status stage always start with DATA1
|
2024-10-25 00:20:34 +07:00
|
|
|
} else {
|
2024-10-29 21:54:21 +07:00
|
|
|
edpt->next_pid = cal_next_pid(edpt->next_pid, packet_count);
|
2024-10-25 00:20:34 +07:00
|
|
|
}
|
|
|
|
|
2024-10-25 19:00:45 +07:00
|
|
|
// TODO support split transaction
|
2024-10-29 13:01:48 +07:00
|
|
|
channel->hcsplt = edpt->hcsplt;
|
2024-10-25 19:00:45 +07:00
|
|
|
|
2024-10-29 22:48:58 +07:00
|
|
|
if (is_period) {
|
|
|
|
hcchar_bm->odd_frame = 1 - (dwc2->hfnum & 1); // transfer on next frame
|
|
|
|
}
|
2024-10-25 19:00:45 +07:00
|
|
|
hcchar_bm->ep_dir = ep_dir; // control endpoint can switch direction
|
2024-10-29 14:47:44 +07:00
|
|
|
channel->hcchar = (edpt->hcchar & ~HCCHAR_CHENA); // restore hcchar but don't enable yet
|
2024-10-25 19:00:45 +07:00
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
xfer->buffer = buffer;
|
|
|
|
xfer->total_bytes = buflen;
|
2024-10-29 18:23:56 +07:00
|
|
|
xfer->result = XFER_RESULT_INVALID;
|
2024-10-25 22:56:11 +07:00
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
if (dma_host_enabled(dwc2)) {
|
|
|
|
channel->hcdma = (uint32_t) buffer;
|
2024-10-29 15:07:28 +07:00
|
|
|
TU_ASSERT(false); // not yet supported
|
2024-10-25 00:20:34 +07:00
|
|
|
} else {
|
2024-10-29 18:23:56 +07:00
|
|
|
uint32_t hcintmsk = HCINT_NAK | HCINT_XACT_ERR | HCINT_STALL | HCINT_XFER_COMPLETE | HCINT_DATATOGGLE_ERR;
|
|
|
|
if (ep_dir == TUSB_DIR_IN) {
|
|
|
|
hcintmsk |= HCINT_BABBLE_ERR | HCINT_DATATOGGLE_ERR;
|
|
|
|
} else {
|
|
|
|
hcintmsk |= HCINT_NYET;
|
|
|
|
}
|
|
|
|
channel->hcintmsk = hcintmsk;
|
|
|
|
dwc2->haintmsk |= TU_BIT(ch_id);
|
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
// enable channel for slave mode:
|
2024-10-29 15:07:28 +07:00
|
|
|
// - OUT: it will enable corresponding FIFO channel
|
|
|
|
// - IN : it will write an IN request to the Non-periodic Request Queue, this will have dwc2 trying to send
|
2024-10-25 22:56:11 +07:00
|
|
|
// IN Token. If we got NAK, we have to re-enable the channel again in the interrupt. Due to the way usbh stack only
|
|
|
|
// call hcd_edpt_xfer() once, we will need to manage de-allocate/re-allocate IN channel dynamically.
|
|
|
|
if (ep_dir == TUSB_DIR_IN) {
|
2024-10-29 19:08:45 +07:00
|
|
|
channel_send_in_token(dwc2, channel, is_period);
|
2024-10-25 22:56:11 +07:00
|
|
|
} else {
|
2024-10-28 17:45:44 +07:00
|
|
|
channel->hcchar |= HCCHAR_CHENA;
|
2024-10-25 22:56:11 +07:00
|
|
|
if (buflen > 0) {
|
2024-10-29 13:01:48 +07:00
|
|
|
// To prevent conflict with other channel, we will enable periodic/non-periodic FIFO empty interrupt accordingly
|
2024-10-28 17:45:44 +07:00
|
|
|
// And write packet in the interrupt handler
|
|
|
|
dwc2->gintmsk |= (is_period ? GINTSTS_PTX_FIFO_EMPTY : GINTSTS_NPTX_FIFO_EMPTY);
|
2024-10-25 19:00:45 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 00:20:34 +07:00
|
|
|
|
|
|
|
return true;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Abort a queued transfer. Note: it can only abort transfer that has not been started
|
|
|
|
// Return true if a queued transfer is aborted, false if there is no transfer to abort
|
|
|
|
bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
|
2024-10-28 17:45:44 +07:00
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
2024-10-29 13:01:48 +07:00
|
|
|
const uint8_t ep_num = tu_edpt_number(ep_addr);
|
|
|
|
const uint8_t ep_dir = tu_edpt_dir(ep_addr);
|
|
|
|
const uint8_t ep_id = edpt_find_opened(dev_addr, ep_num, ep_dir);
|
|
|
|
TU_VERIFY(ep_id < CFG_TUH_DWC2_ENDPOINT_MAX);
|
2024-10-29 19:08:45 +07:00
|
|
|
//hcd_endpoint_t* edpt = &_hcd_data.edpt[ep_id];
|
2024-10-15 13:03:12 +07:00
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
// hcd_int_disable(rhport);
|
2024-10-28 17:45:44 +07:00
|
|
|
|
|
|
|
// Find enabled channeled and disable it, channel will be de-allocated in the interrupt handler
|
2024-10-29 13:01:48 +07:00
|
|
|
const uint8_t ch_id = channel_find_enabled(dwc2, dev_addr, ep_num, ep_dir);
|
2024-10-28 17:45:44 +07:00
|
|
|
if (ch_id < 16) {
|
|
|
|
dwc2_channel_t* channel = &dwc2->channel[ch_id];
|
2024-10-29 19:08:45 +07:00
|
|
|
channel_disable(channel);
|
2024-10-28 17:45:44 +07:00
|
|
|
}
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
// hcd_int_enable(rhport);
|
2024-10-28 17:45:44 +07:00
|
|
|
|
|
|
|
return true;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Submit a special transfer to send 8-byte Setup Packet, when complete hcd_event_xfer_complete() must be invoked
|
2024-10-25 00:20:34 +07:00
|
|
|
bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, const uint8_t setup_packet[8]) {
|
2024-10-29 13:01:48 +07:00
|
|
|
uint8_t ep_id = edpt_find_opened(dev_addr, 0, TUSB_DIR_OUT);
|
|
|
|
TU_ASSERT(ep_id < CFG_TUH_DWC2_ENDPOINT_MAX); // no opened endpoint
|
|
|
|
hcd_endpoint_t* edpt = &_hcd_data.edpt[ep_id];
|
2024-10-29 21:54:21 +07:00
|
|
|
edpt->next_pid = HCTSIZ_PID_SETUP;
|
2024-10-15 13:03:12 +07:00
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
return hcd_edpt_xfer(rhport, dev_addr, 0, (uint8_t*)(uintptr_t) setup_packet, 8);
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear stall, data toggle is also reset to DATA0
|
|
|
|
bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
|
|
|
|
(void) rhport;
|
2024-10-29 21:54:21 +07:00
|
|
|
const uint8_t ep_num = tu_edpt_number(ep_addr);
|
|
|
|
const uint8_t ep_dir = tu_edpt_dir(ep_addr);
|
|
|
|
const uint8_t ep_id = edpt_find_opened(dev_addr, ep_num, ep_dir);
|
|
|
|
TU_VERIFY(ep_id < CFG_TUH_DWC2_ENDPOINT_MAX);
|
|
|
|
hcd_endpoint_t* edpt = &_hcd_data.edpt[ep_id];
|
2024-10-15 13:03:12 +07:00
|
|
|
|
2024-10-29 21:54:21 +07:00
|
|
|
edpt->next_pid = HCTSIZ_PID_DATA0;
|
|
|
|
|
|
|
|
return true;
|
2024-10-15 13:03:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-16 13:19:28 +07:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// HCD Event Handler
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2024-10-17 15:56:12 +07:00
|
|
|
static void handle_rxflvl_irq(uint8_t rhport) {
|
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
|
|
|
|
// Pop control word off FIFO
|
2024-10-25 22:56:11 +07:00
|
|
|
const dwc2_grxstsp_t grxstsp_bm = dwc2->grxstsp_bm;
|
|
|
|
const uint8_t ch_id = grxstsp_bm.ep_ch_num;
|
|
|
|
dwc2_channel_t* channel = &dwc2->channel[ch_id];
|
|
|
|
|
|
|
|
switch (grxstsp_bm.packet_status) {
|
2024-10-28 18:40:21 +07:00
|
|
|
case GRXSTS_PKTSTS_RX_DATA: {
|
2024-10-25 22:56:11 +07:00
|
|
|
// In packet received
|
|
|
|
const uint16_t byte_count = grxstsp_bm.byte_count;
|
2024-10-29 13:01:48 +07:00
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
2024-10-25 22:56:11 +07:00
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
dfifo_read_packet(dwc2, xfer->buffer, byte_count);
|
|
|
|
xfer->buffer += byte_count;
|
2024-10-25 22:56:11 +07:00
|
|
|
|
2024-10-29 21:54:21 +07:00
|
|
|
const uint16_t remain_bytes = (uint16_t) channel->hctsiz_bm.xfer_size;
|
|
|
|
const uint16_t remain_packets = channel->hctsiz_bm.packet_count;
|
2024-10-29 19:08:45 +07:00
|
|
|
if (byte_count < channel->hcchar_bm.ep_size) {
|
|
|
|
// short packet, minus remaining bytes
|
|
|
|
xfer->total_bytes -= remain_bytes;
|
2024-10-29 21:54:21 +07:00
|
|
|
|
|
|
|
// update PID since we got short packet
|
|
|
|
TU_ASSERT(xfer->ep_id < CFG_TUH_DWC2_ENDPOINT_MAX,);
|
|
|
|
hcd_endpoint_t* edpt = &_hcd_data.edpt[xfer->ep_id]; // update PID
|
|
|
|
edpt->next_pid = cal_next_pid(edpt->next_pid, remain_packets);
|
2024-10-29 19:08:45 +07:00
|
|
|
} else {
|
|
|
|
if (remain_packets) {
|
2024-10-29 21:54:21 +07:00
|
|
|
// still more packet to send
|
2024-10-29 19:08:45 +07:00
|
|
|
bool const is_period = edpt_is_periodic(channel->hcchar_bm.ep_type);
|
|
|
|
channel_send_in_token(dwc2, channel, is_period);
|
|
|
|
}
|
2024-10-25 22:56:11 +07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-10-28 18:40:21 +07:00
|
|
|
case GRXSTS_PKTSTS_RX_COMPLETE:
|
2024-10-29 21:54:21 +07:00
|
|
|
// In transfer complete: After this entry is popped from the rx FIFO, dwc2 asserts a Transfer Completed
|
2024-10-25 22:56:11 +07:00
|
|
|
// interrupt --> handle_channel_irq()
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRXSTS_PKTSTS_HOST_DATATOGGLE_ERR:
|
|
|
|
TU_ASSERT(0, ); // maybe try to change DToggle
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRXSTS_PKTSTS_HOST_CHANNEL_HALTED:
|
|
|
|
// triggered when channel.hcchar_bm.disable is set
|
|
|
|
// TODO handle later
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break; // ignore other status
|
|
|
|
}
|
2024-10-17 15:56:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle Host Port interrupt, possible source are:
|
|
|
|
- Connection Detection
|
|
|
|
- Enable Change
|
|
|
|
- Over Current Change
|
|
|
|
*/
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void handle_hprt_irq(uint8_t rhport, bool in_isr) {
|
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
2024-10-21 17:45:40 +07:00
|
|
|
uint32_t hprt = dwc2->hprt & ~HPRT_W1C_MASK;
|
|
|
|
const dwc2_hprt_t hprt_bm = dwc2->hprt_bm;
|
2024-10-17 15:56:12 +07:00
|
|
|
|
2024-10-21 17:45:40 +07:00
|
|
|
if (dwc2->hprt & HPRT_CONN_DETECT) {
|
2024-10-17 15:56:12 +07:00
|
|
|
// Port Connect Detect
|
2024-10-21 17:45:40 +07:00
|
|
|
hprt |= HPRT_CONN_DETECT;
|
2024-10-17 15:56:12 +07:00
|
|
|
|
2024-10-21 17:45:40 +07:00
|
|
|
if (hprt_bm.conn_status) {
|
2024-10-17 15:56:12 +07:00
|
|
|
hcd_event_device_attach(rhport, in_isr);
|
|
|
|
} else {
|
|
|
|
hcd_event_device_remove(rhport, in_isr);
|
|
|
|
}
|
|
|
|
}
|
2024-10-21 17:45:40 +07:00
|
|
|
|
|
|
|
if (dwc2->hprt & HPRT_ENABLE_CHANGE) {
|
|
|
|
// Port enable change
|
|
|
|
hprt |= HPRT_ENABLE_CHANGE;
|
|
|
|
|
|
|
|
if (hprt_bm.enable) {
|
|
|
|
// Port enable
|
|
|
|
// Config HCFG FS/LS clock and HFIR for SOF interval according to link speed (value is in PHY clock unit)
|
|
|
|
const tusb_speed_t speed = convert_hprt_speed(hprt_bm.speed);
|
|
|
|
uint32_t hcfg = dwc2->hcfg & ~HCFG_FSLS_PHYCLK_SEL;
|
|
|
|
|
|
|
|
const dwc2_gusbcfg_t gusbcfg_bm = dwc2->gusbcfg_bm;
|
|
|
|
uint32_t clock = 60;
|
|
|
|
if (gusbcfg_bm.phy_sel) {
|
|
|
|
// dedicated FS is 48Mhz
|
|
|
|
clock = 48;
|
|
|
|
hcfg |= HCFG_FSLS_PHYCLK_SEL_48MHZ;
|
|
|
|
} else {
|
|
|
|
// UTMI+ or ULPI
|
|
|
|
if (gusbcfg_bm.ulpi_utmi_sel) {
|
|
|
|
clock = 60; // ULPI 8-bit is 60Mhz
|
|
|
|
} else if (gusbcfg_bm.phy_if16) {
|
|
|
|
clock = 30; // UTMI+ 16-bit is 30Mhz
|
|
|
|
} else {
|
|
|
|
clock = 60; // UTMI+ 8-bit is 60Mhz
|
|
|
|
}
|
|
|
|
hcfg |= HCFG_FSLS_PHYCLK_SEL_30_60MHZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwc2->hcfg = hcfg;
|
|
|
|
|
|
|
|
uint32_t hfir = dwc2->hfir & ~HFIR_FRIVL_Msk;
|
|
|
|
if (speed == TUSB_SPEED_HIGH) {
|
|
|
|
hfir |= 125*clock;
|
|
|
|
} else {
|
|
|
|
hfir |= 1000*clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwc2->hfir = hfir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dwc2->hprt = hprt; // clear interrupt
|
2024-10-17 15:56:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
bool handle_channel_slave_in(dwc2_regs_t* dwc2, uint8_t ch_id, bool is_period, uint32_t hcint) {
|
2024-10-29 16:06:44 +07:00
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
|
|
|
dwc2_channel_t* channel = &dwc2->channel[ch_id];
|
2024-10-29 18:23:56 +07:00
|
|
|
|
|
|
|
bool is_done = false;
|
2024-10-29 16:06:44 +07:00
|
|
|
|
|
|
|
if (hcint & HCINT_XFER_COMPLETE) {
|
2024-10-29 18:23:56 +07:00
|
|
|
xfer->result = XFER_RESULT_SUCCESS;
|
2024-10-29 16:06:44 +07:00
|
|
|
channel_disable(channel);
|
|
|
|
channel->hcintmsk &= ~HCINT_ACK;
|
|
|
|
} else if (hcint & (HCINT_XACT_ERR | HCINT_BABBLE_ERR | HCINT_STALL)) {
|
|
|
|
channel_disable(channel);
|
|
|
|
if (hcint & HCINT_XACT_ERR) {
|
|
|
|
xfer->err_count++;
|
|
|
|
channel->hcintmsk |= HCINT_ACK;
|
|
|
|
}
|
|
|
|
} else if (hcint & HCINT_HALTED) {
|
|
|
|
channel->hcintmsk &= ~HCINT_HALTED;
|
2024-10-29 18:23:56 +07:00
|
|
|
if (xfer->result != XFER_RESULT_INVALID) {
|
|
|
|
is_done = true;
|
|
|
|
} else if (channel->hcchar_bm.err_multi_count == HCD_XFER_ERROR_MAX) {
|
|
|
|
xfer->result = XFER_RESULT_FAILED;
|
|
|
|
is_done = true;
|
2024-10-29 16:06:44 +07:00
|
|
|
} else {
|
|
|
|
// Re-initialize Channel
|
|
|
|
}
|
|
|
|
} else if (hcint & HCINT_ACK) {
|
|
|
|
xfer->err_count = 0;
|
|
|
|
channel->hcintmsk &= ~HCINT_ACK;
|
|
|
|
} else if (hcint & HCINT_DATATOGGLE_ERR) {
|
|
|
|
xfer->err_count = 0;
|
|
|
|
} else if (hcint & HCINT_NAK) {
|
|
|
|
// NAK received, re-enable channel if request queue is available
|
2024-10-29 19:08:45 +07:00
|
|
|
channel_send_in_token(dwc2, channel, is_period);
|
2024-10-29 16:06:44 +07:00
|
|
|
}
|
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
return is_done;
|
2024-10-29 16:06:44 +07:00
|
|
|
}
|
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
bool handle_channel_slave_out(dwc2_regs_t* dwc2, uint8_t ch_id, bool is_period, uint32_t hcint) {
|
2024-10-29 16:06:44 +07:00
|
|
|
(void) is_period;
|
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
|
|
|
dwc2_channel_t* channel = &dwc2->channel[ch_id];
|
2024-10-29 18:23:56 +07:00
|
|
|
|
|
|
|
bool is_notify = false;
|
2024-10-29 16:06:44 +07:00
|
|
|
|
|
|
|
if (hcint & HCINT_XFER_COMPLETE) {
|
2024-10-29 18:23:56 +07:00
|
|
|
is_notify = true;
|
|
|
|
xfer->result = XFER_RESULT_SUCCESS;
|
2024-10-29 16:06:44 +07:00
|
|
|
channel->hcintmsk &= ~HCINT_ACK;
|
|
|
|
} else if (hcint & HCINT_STALL) {
|
2024-10-29 18:23:56 +07:00
|
|
|
xfer->result = XFER_RESULT_STALLED;
|
2024-10-29 16:06:44 +07:00
|
|
|
channel_disable(channel);
|
|
|
|
} else if (hcint & (HCINT_NAK | HCINT_XACT_ERR | HCINT_NYET)) {
|
|
|
|
channel_disable(channel);
|
|
|
|
if (hcint & HCINT_XACT_ERR) {
|
|
|
|
xfer->err_count++;
|
|
|
|
channel->hcintmsk |= HCINT_ACK;
|
|
|
|
} else {
|
|
|
|
xfer->err_count = 0;
|
|
|
|
}
|
|
|
|
} else if (hcint & HCINT_HALTED) {
|
|
|
|
channel->hcintmsk &= ~HCINT_HALTED;
|
2024-10-29 18:23:56 +07:00
|
|
|
if (xfer->result != XFER_RESULT_INVALID) {
|
|
|
|
is_notify = true;
|
|
|
|
} else if (channel->hcchar_bm.err_multi_count == HCD_XFER_ERROR_MAX) {
|
|
|
|
xfer->result = XFER_RESULT_FAILED;
|
|
|
|
is_notify = true;
|
2024-10-29 16:06:44 +07:00
|
|
|
} else {
|
2024-10-29 22:48:58 +07:00
|
|
|
// Got here due to NAK probably, retry channel (Do ping protocol for HS)
|
|
|
|
|
2024-10-29 16:06:44 +07:00
|
|
|
}
|
|
|
|
} else if (hcint & HCINT_ACK) {
|
|
|
|
xfer->err_count = 0;
|
|
|
|
channel->hcintmsk &= ~HCINT_ACK;
|
|
|
|
}
|
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
return is_notify;
|
2024-10-29 16:06:44 +07:00
|
|
|
}
|
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
void handle_channel_irq(uint8_t rhport, bool in_isr) {
|
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
2024-10-29 13:01:48 +07:00
|
|
|
const bool is_dma = dma_host_enabled(dwc2);
|
|
|
|
const uint8_t max_channel = DWC2_CHANNEL_COUNT(dwc2);
|
|
|
|
|
|
|
|
for (uint8_t ch_id = 0; ch_id < max_channel; ch_id++) { //
|
2024-10-25 00:20:34 +07:00
|
|
|
if (tu_bit_test(dwc2->haint, ch_id)) {
|
|
|
|
dwc2_channel_t* channel = &dwc2->channel[ch_id];
|
2024-10-29 13:01:48 +07:00
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
2024-10-29 16:06:44 +07:00
|
|
|
dwc2_channel_char_t hcchar_bm = channel->hcchar_bm;
|
|
|
|
const bool is_period = edpt_is_periodic(hcchar_bm.ep_type);
|
2024-10-29 13:01:48 +07:00
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
uint32_t hcint = channel->hcint;
|
|
|
|
hcint &= channel->hcintmsk;
|
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
if (is_dma) {
|
2024-10-28 17:45:44 +07:00
|
|
|
|
2024-10-29 13:01:48 +07:00
|
|
|
} else {
|
2024-10-29 18:23:56 +07:00
|
|
|
bool is_done;
|
2024-10-29 13:01:48 +07:00
|
|
|
|
2024-10-29 16:06:44 +07:00
|
|
|
if (hcchar_bm.ep_dir == TUSB_DIR_OUT) {
|
2024-10-29 18:23:56 +07:00
|
|
|
is_done = handle_channel_slave_out(dwc2, ch_id, is_period, hcint);
|
2024-10-29 16:06:44 +07:00
|
|
|
} else {
|
2024-10-29 18:23:56 +07:00
|
|
|
is_done = handle_channel_slave_in(dwc2, ch_id, is_period, hcint);
|
2024-10-29 16:06:44 +07:00
|
|
|
}
|
2024-10-29 13:01:48 +07:00
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
if (is_done) {
|
2024-10-29 13:01:48 +07:00
|
|
|
const uint8_t ep_addr = tu_edpt_addr(hcchar_bm.ep_num, hcchar_bm.ep_dir);
|
2024-10-29 18:23:56 +07:00
|
|
|
hcd_event_xfer_complete(hcchar_bm.dev_addr, ep_addr, xfer->total_bytes, xfer->result, in_isr);
|
|
|
|
channel_dealloc(dwc2, ch_id);
|
2024-10-28 17:45:44 +07:00
|
|
|
}
|
2024-10-25 22:56:11 +07:00
|
|
|
}
|
2024-10-25 00:20:34 +07:00
|
|
|
|
2024-10-25 22:56:11 +07:00
|
|
|
channel->hcint = hcint; // clear all interrupt flags
|
2024-10-25 00:20:34 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 14:47:44 +07:00
|
|
|
// return true if there is still pending data and need more ISR
|
2024-10-25 19:00:45 +07:00
|
|
|
bool handle_txfifo_empty(dwc2_regs_t* dwc2, bool is_periodic) {
|
2024-10-28 17:45:44 +07:00
|
|
|
// Use period txsts for both p/np to get request queue space available (1-bit difference, it is small enough)
|
|
|
|
volatile dwc2_hptxsts_t* txsts_bm = (volatile dwc2_hptxsts_t*) (is_periodic ? &dwc2->hptxsts : &dwc2->hnptxsts);
|
2024-10-25 19:00:45 +07:00
|
|
|
|
2024-10-29 14:47:44 +07:00
|
|
|
const uint8_t max_channel = DWC2_CHANNEL_COUNT(dwc2);
|
|
|
|
for (uint8_t ch_id = 0; ch_id < max_channel; ch_id++) {
|
|
|
|
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
|
2024-10-29 22:48:58 +07:00
|
|
|
dwc2_channel_t* channel = &dwc2->channel[ch_id];
|
|
|
|
const dwc2_channel_char_t hcchar_bm = channel->hcchar_bm;
|
|
|
|
if (hcchar_bm.ep_dir == TUSB_DIR_OUT) {
|
|
|
|
const uint16_t remain_packets = channel->hctsiz_bm.packet_count;
|
|
|
|
for (uint16_t i = 0; i < remain_packets; i++) {
|
|
|
|
const uint16_t remain_bytes = (uint16_t) channel->hctsiz_bm.xfer_size;
|
|
|
|
const uint16_t xact_bytes = tu_min16(remain_bytes, hcchar_bm.ep_size);
|
|
|
|
|
|
|
|
// check if there is enough space in FIFO and RequestQueue.
|
|
|
|
// Packet's last word written to FIFO will trigger a request queue
|
|
|
|
if ((xact_bytes > (txsts_bm->fifo_available << 2)) && (txsts_bm->req_queue_available > 0)) {
|
|
|
|
return true;
|
2024-10-25 19:00:45 +07:00
|
|
|
}
|
2024-10-29 22:48:58 +07:00
|
|
|
|
|
|
|
dfifo_write_packet(dwc2, ch_id, xfer->buffer, xact_bytes);
|
|
|
|
xfer->buffer += xact_bytes;
|
2024-10-25 19:00:45 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 14:47:44 +07:00
|
|
|
return false; // all data written
|
2024-10-25 19:00:45 +07:00
|
|
|
}
|
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
/* Interrupt Hierarchy
|
2024-10-28 17:45:44 +07:00
|
|
|
HCINTn HPRT
|
|
|
|
| |
|
|
|
|
HAINT.CHn |
|
|
|
|
| |
|
|
|
|
GINTSTS : HCInt PrtInt NPTxFEmp PTxFEmpp RXFLVL
|
2024-10-25 19:00:45 +07:00
|
|
|
|
|
|
|
|
|
|
|
*/
|
2024-10-16 13:19:28 +07:00
|
|
|
void hcd_int_handler(uint8_t rhport, bool in_isr) {
|
|
|
|
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
|
|
|
|
const uint32_t int_mask = dwc2->gintmsk;
|
|
|
|
const uint32_t int_status = dwc2->gintsts & int_mask;
|
|
|
|
|
2024-10-28 17:45:44 +07:00
|
|
|
// TU_LOG1_HEX(int_status);
|
2024-10-21 11:43:37 +07:00
|
|
|
|
|
|
|
if (int_status & GINTSTS_CONIDSTSCHNG) {
|
|
|
|
// Connector ID status change
|
|
|
|
dwc2->gintsts = GINTSTS_CONIDSTSCHNG;
|
|
|
|
|
|
|
|
//if (dwc2->gotgctl)
|
|
|
|
// dwc2->hprt = HPRT_POWER; // power on port to turn on VBUS
|
|
|
|
//dwc2->gintmsk |= GINTMSK_PRTIM;
|
|
|
|
// TODO wait for SRP if OTG
|
|
|
|
}
|
|
|
|
|
2024-10-16 13:19:28 +07:00
|
|
|
if (int_status & GINTSTS_HPRTINT) {
|
2024-10-25 00:20:34 +07:00
|
|
|
// Host port interrupt: source is cleared in HPRT register
|
2024-10-28 17:45:44 +07:00
|
|
|
// TU_LOG1_HEX(dwc2->hprt);
|
2024-10-17 15:56:12 +07:00
|
|
|
handle_hprt_irq(rhport, in_isr);
|
2024-10-16 13:19:28 +07:00
|
|
|
}
|
2024-10-17 15:56:12 +07:00
|
|
|
|
2024-10-29 18:23:56 +07:00
|
|
|
// RxFIFO non-empty interrupt handling, must be handled before HCINT
|
|
|
|
if (int_status & GINTSTS_RXFLVL) {
|
|
|
|
// RXFLVL bit is read-only
|
|
|
|
dwc2->gintmsk &= ~GINTMSK_RXFLVLM; // disable RXFLVL interrupt while reading
|
|
|
|
|
|
|
|
do {
|
|
|
|
handle_rxflvl_irq(rhport); // read all packets
|
|
|
|
} while(dwc2->gintsts & GINTSTS_RXFLVL);
|
|
|
|
|
|
|
|
dwc2->gintmsk |= GINTMSK_RXFLVLM;
|
|
|
|
}
|
|
|
|
|
2024-10-25 00:20:34 +07:00
|
|
|
if (int_status & GINTSTS_HCINT) {
|
|
|
|
// Host Channel interrupt: source is cleared in HCINT register
|
|
|
|
handle_channel_irq(rhport, in_isr);
|
|
|
|
}
|
|
|
|
|
2024-10-25 19:00:45 +07:00
|
|
|
if (int_status & GINTSTS_NPTX_FIFO_EMPTY) {
|
|
|
|
// NPTX FIFO empty interrupt, this is read-only and cleared by hardware when FIFO is written
|
2024-10-29 14:47:44 +07:00
|
|
|
const bool more_isr = handle_txfifo_empty(dwc2, false);
|
|
|
|
if (!more_isr) {
|
2024-10-25 19:00:45 +07:00
|
|
|
// no more pending packet, disable interrupt
|
|
|
|
dwc2->gintmsk &= ~GINTSTS_NPTX_FIFO_EMPTY;
|
|
|
|
}
|
|
|
|
}
|
2024-10-16 13:19:28 +07:00
|
|
|
}
|
|
|
|
|
2024-10-15 13:03:12 +07:00
|
|
|
#endif
|