FreeBSD/sys/boot/arm/at91/libat91/sd-card.c
//*----------------------------------------------------------------------------
//* \fn    MCIDeviceWaitReady
//* \brief Wait for MCI Device ready
//*----------------------------------------------------------------------------
\fn    MCIDeviceWaitReady
\brief Wait for MCI Device ready
\ fn MCIDeviceWaitReady \ brief Wait for MCI Device ready
static unsigned int
MCIDeviceWaitReady(unsigned int timeout)
{
	volatile unsigned int status;
	int waitfor;
	if (MCI_Device.state == AT91C_MCI_RX_SINGLE_BLOCK)
		waitfor = AT91C_MCI_RXBUFF;
	else
		waitfor = AT91C_MCI_NOTBUSY;
	do
	{
		status = AT91C_BASE_MCI->MCI_SR;
		timeout--;
	}
	while( !(status & waitfor) && (timeout>0) );	
	status = AT91C_BASE_MCI->MCI_SR;
	if (MCI_Device.state == AT91C_MCI_TX_SINGLE_BLOCK && status & AT91C_MCI_TXBUFE) {
		AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_TXBUFE;
 		AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTDIS;
		MCI_Device.state = AT91C_MCI_IDLE;
	}	
	if (MCI_Device.state == AT91C_MCI_RX_SINGLE_BLOCK && status & AT91C_MCI_RXBUFF) {
		AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_RXBUFF;
 		AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTDIS;
		MCI_Device.state = AT91C_MCI_IDLE;
	}	
	return status;
}
static unsigned int MCIDeviceWaitReady ( unsigned int timeout ) { volatile unsigned int status ; int waitfor ; if ( MCI_Device . state == AT91C_MCI_RX_SINGLE_BLOCK ) waitfor = AT91C_MCI_RXBUFF ; else waitfor = AT91C_MCI_NOTBUSY ; do { status = AT91C_BASE_MCI -> MCI_SR ; timeout -- ; } while ( ! ( status & waitfor ) && ( timeout > 0 ) ) ; status = AT91C_BASE_MCI -> MCI_SR ; if ( MCI_Device . state == AT91C_MCI_TX_SINGLE_BLOCK && status & AT91C_MCI_TXBUFE ) { AT91C_BASE_MCI -> MCI_IDR = AT91C_MCI_TXBUFE ; AT91C_BASE_PDC_MCI -> PDC_PTCR = AT91C_PDC_TXTDIS ; MCI_Device . state = AT91C_MCI_IDLE ; } if ( MCI_Device . state == AT91C_MCI_RX_SINGLE_BLOCK && status & AT91C_MCI_RXBUFF ) { AT91C_BASE_MCI -> MCI_IDR = AT91C_MCI_RXBUFF ; AT91C_BASE_PDC_MCI -> PDC_PTCR = AT91C_PDC_RXTDIS ; MCI_Device . state = AT91C_MCI_IDLE ; } return status ; }
\fn    MCIDeviceWaitReady
\brief Wait for MCI Device ready
\ fn MCIDeviceWaitReady \ brief Wait for MCI Device ready
// If End of Tx Buffer Empty interrupt occurred // End of if AT91C_MCI_TXBUFF // If End of Rx Buffer Full interrupt occurred // End of if AT91C_MCI_RXBUFF //printf("WaitReady returning status %x\n", status);