/* Write USB Device Endpoint Data Parameters: EPNum: Device 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  */
uint32_t USBD_WriteEP(uint32_t EPNum, uint8_t *pData, uint32_t cnt)
/* Write USB Device Endpoint Data Parameters: EPNum: Device 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  */
uint32_t USBD_WriteEP(uint32_t EPNum, uint8_t *pData, uint32_t cnt)
{
    uint32_t idx, n;
    EPNum &= 0x0F;
    idx = IDX(EPNum, TX, 0);
    BD[idx].bc = cnt;
    for (n = 0; n < cnt; n++) {
        EPBuf[idx][n] = pData[n];
    }
    if ((Data1 >> (idx / 2)) & 1) {
        BD[idx].stat = BD_OWN_MASK | BD_DTS_MASK;
    } else {
        BD[idx].stat = BD_OWN_MASK | BD_DTS_MASK | BD_DATA01_MASK;
    }
    protected_xor(&Data1, (1 << (idx / 2)));
    return (cnt);
}