//*----------------------------------------------------------------------------
//* \fn AT91F_CDC_Write
//* \brief Send through endpoint 2
//*----------------------------------------------------------------------------
static uint AT91F_UDP_Write( AT91PS_CDC pCdc, const char *pData, uint length )
{
AT91PS_UDP pUdp = pCdc->pUdp;
uint cpt = 0;
// Send the first packet
cpt = MIN( length, AT91C_EP_IN_SIZE );
length -= cpt;
while( cpt-- ) pUdp->UDP_FDR[AT91C_EP_IN] = *pData++;
pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY;
while( length )
{
// Fill the second bank
cpt = MIN( length, AT91C_EP_IN_SIZE );
length -= cpt;
while( cpt-- ) pUdp->UDP_FDR[AT91C_EP_IN] = *pData++;
// Wait for the the first bank to be sent
while( !( pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP ) ) // ISSUE: sometimes, it takes a long time to complete transmission
if( !AT91F_UDP_IsConfigured( pCdc ) ) return length;
pUdp->UDP_CSR[AT91C_EP_IN] &= ~( AT91C_UDP_TXCOMP );
while( pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP );
pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY;
}
// Wait for the end of transfer
while( !( pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP ) ) // ISSUE: sometimes, it takes a long time to complete transmission
if( !AT91F_UDP_IsConfigured( pCdc ) ) return length;
pUdp->UDP_CSR[AT91C_EP_IN] &= ~( AT91C_UDP_TXCOMP );
while( pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP );
return length;
}