/* Write USB Device Endpoint Data Parameters: EPNum: Endpoint Number EPNum.0..3: Address EPNum.7: Dir pData: Pointer to Data Buffer cnt: Number of bytes to write Return Value: Number of bytes written  */
U32 USBD_WriteEP(U32 EPNum, U8 *pData, U32 cnt)
/* Write USB Device Endpoint Data Parameters: EPNum: Endpoint Number EPNum.0..3: Address EPNum.7: Dir pData: Pointer to Data Buffer cnt: Number of bytes to write Return Value: Number of bytes written  */
U32 USBD_WriteEP(U32 EPNum, U8 *pData, U32 cnt)
{
    U32 i;
    volatile U32 *ptr;
    U32 *dataptr;
    ptr = GetEpCmdStatPtr(EPNum);
    EPNum &= ~0x80;
    while (*ptr & BUF_ACTIVE);
    *ptr &= ~(0x3FFFFFF);
    *ptr |=  BUF_ADDR(EPBufInfo[EP_IN_IDX(EPNum)].buf_ptr) |
             N_BYTES(cnt);
    dataptr = (U32 *)EPBufInfo[EP_IN_IDX(EPNum)].buf_ptr;
    for (i = 0; i < (cnt + 3) / 4; i++) {
        dataptr[i] = __UNALIGNED_UINT32_READ(pData);
        pData += 4;
    }
    if (EPNum && (*ptr & EP_STALL)) {
        return (0);
    }
    *ptr |= BUF_ACTIVE;
    return (cnt);
}