SPI interface for Dataflash on SAM7x-EK, problems

Hi all,

I have trouble getting my interface working, and hope there is someone out there who point me in the right direction.

I have just started working on the SPI driver for this board, and as a test of the interface. I want to query the Dataflash on the board about its ID’s.

Here is the init code:

void AT45_init( void )
{
	unsigned long dummy;
	
	AT91C_BASE_AIC->AIC_EOICR = 0;
	
	// Disable interrupts
	AT91C_BASE_SPI0->SPI_IDR = 0xFFFFFFFF;
	
	dummy = AT91C_BASE_SPI0->SPI_SR;
	dummy = dummy;
	
	AT91C_BASE_PIOA->PIO_PPUDR = (AT91C_PA16_MISO0 | AT91C_PA17_MOSI0 | 
								AT91C_PA18_SPCK0 | AT91C_PA12_NPCS00);
	// Enable SPI0 Periph. pins
	AT91C_BASE_PIOA->PIO_PDR = (AT91C_PA16_MISO0 | AT91C_PA17_MOSI0 | 
								AT91C_PA18_SPCK0 | AT91C_PA12_NPCS00);
								
	AT91C_BASE_PIOA->PIO_ASR = (AT91C_PA16_MISO0 | AT91C_PA17_MOSI0 | 
								AT91C_PA18_SPCK0 | AT91C_PA12_NPCS00);
	
	
	// Enable Periph. clock
	AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_SPI0);
	
	AT91C_BASE_SPI0->SPI_CR  = 0x80;              // Software reset
  
	// Set master mode
	// PCS = 1110 -> (14<<16)
	AT91C_BASE_SPI0->SPI_MR = 0;	// Clear mode register
	AT91C_BASE_SPI0->SPI_MR = AT91C_SPI_MSTR | AT91C_SPI_PS_FIXED |
								AT91C_SPI_MODFDIS | (14<<16);// | AT91C_SPI_LLB;
								// AT91C_SPI_LLB is For TEST ONLY!
	
	// Setup SPI interface, mode, clock etc.
	// CPOL = 0; NCPHA = 1; <- Spi mode 0
	// Bits = 8
	// SCBR = 3 -> MCK/3 -> (3<<8)
	// Delay before SPCK = 333ns (16 << 16)
	//AT91C_BASE_SPI->SPI_CSR[0] = AT91C_SPI_NCPHA | (3<<8) | (16 << 16);
	AT91C_BASE_SPI0->SPI_CSR[0] = AT91C_SPI_NCPHA | (2<<8) | (240<<16);//(16 << 16);
	
	
	dummy = 0;
	// Enable SPI Interface
	while(!(dummy & AT91C_SPI_SPIENS))
	{
		AT91C_BASE_SPI0->SPI_CR = AT91C_SPI_SPIEN;
		dummy = AT91C_BASE_SPI0->SPI_SR;
	}
}

And i use the following functions to read the IDs from the dataflash:

void AT45_ReadIDs(unsigned long * pc)
{
	unsigned char tmpdata1, tmpdata2,
				tmpdata3, tmpdata4;
	// Read IDs
	// Opcode
	Spi_ReadWrite(0x9F);
	
	tmpdata1 = Spi_ReadWrite(0xAA);
	tmpdata2 = Spi_ReadWrite(0xAA);
	tmpdata3 = Spi_ReadWrite(0xAA);
	tmpdata4 = Spi_ReadWrite(0xAA);
	
	pc[0] = (tmpdata1 | tmpdata2 | tmpdata3 | tmpdata4);	
}

unsigned char Spi_ReadWrite(unsigned char data)
{
	unsigned int spib;
	
	while((AT91C_BASE_SPI0->SPI_SR & AT91C_SPI_TDRE) == 0);      // Wait for the transfer to complete
	AT91C_BASE_SPI0->SPI_TDR = (data & 0xFFFF);                  // Send the data
	
	while((AT91C_BASE_SPI0->SPI_SR & AT91C_SPI_RDRF) == 0);      // Wait until the character can be sent
	spib = ((AT91C_BASE_SPI0->SPI_RDR) & 0xFFFF);                // Get the data received
	return spib;
}

the result of the read operation is 0x00. But according to the datasheet of the dataflash i should be getting some values back.

What am i doing wrong here ?

regards,

Martin

I’m having similar problems with SPI Atmel DataFlash and AT91SAM7x256. Did you every get this sorted out? Paul