/* Reads data from a SPI peripheral until the provided buffer is filled. This method does NOT need to be called after SPI_Write or SPI_WriteBuffer.  */
unsigned char SPI_ReadBuffer(AT91S_SPI *spi, void *buffer, unsigned int length)
/* Reads data from a SPI peripheral until the provided buffer is filled. This method does NOT need to be called after SPI_Write or SPI_WriteBuffer.  */
unsigned char SPI_ReadBuffer(AT91S_SPI *spi, void *buffer, unsigned int length)
{
    if (spi->SPI_RCR == 0) {
        spi->SPI_RPR = (unsigned int) buffer;
        spi->SPI_RCR = length;
        spi->SPI_PTCR = AT91C_PDC_RXTEN;
        return 1;
    }
    else if (spi->SPI_RNCR == 0) {
        spi->SPI_RNPR = (unsigned int) buffer;
        spi->SPI_RNCR = length;
        return 1;
    }
    return 0;
}