/* Read USB Device Endpoint Data Parameters: EPNum: Device Endpoint Number EPNum.0..3: Address EPNum.7: Dir pData: Pointer to Data Buffer Return Value: Number of bytes read  */
U32 USBD_ReadEP(U32 EPNum, U8 *pData, U32 bufsz)
/* Read USB Device Endpoint Data Parameters: EPNum: Device Endpoint Number EPNum.0..3: Address EPNum.7: Dir pData: Pointer to Data Buffer Return Value: Number of bytes read  */
U32 USBD_ReadEP(U32 EPNum, U8 *pData, U32 bufsz)
{
    U32 num, cnt, *pv, n;
    num = EPNum & 0x0F;
    pv  = (U32 *)(USB_PMA_ADDR + 2 * ((pBUF_DSCR + num)->ADDR_RX));
    cnt = (pBUF_DSCR + num)->COUNT_RX & EP_COUNT_MASK;
    if (cnt > bufsz) {
        cnt = bufsz;
    }
    for (n = 0; n < (cnt + 1) / 2; n++) {
        __UNALIGNED_UINT16_WRITE(pData, *pv++);
        pData += 2;
    }
    EP_Status(EPNum, EP_RX_VALID);
    return (cnt);
}