Accessing SPI Memory

Discussion to talk about software related topics only.
Post Reply
SeeCwriter
Posts: 630
Joined: Mon May 12, 2008 10:55 am

Accessing SPI Memory

Post by SeeCwriter »

I'm using the NANO54415 with NNDK v2.7.1. I am trying read and write 578 bytes
to an EEPROM on the SPI bus. I see the right number of data being written to
memory, but when I read it only reads 70-bytes.

EEPROMs have to be written by pages, and wait for the write-cycle to complete
between page writes. The page size is 256 for this EEPROM. But you can read the
entire EEPROM with a single read.
On a scope I can see each page being written (2.258 pages), but the single read
is only active for the first 70-bytes. Is there something with the setup that may
be causing the read to be short?

Here is the relevant SPI code:

Code: Select all

#define SPI_BUF_SIZE	1024

BYTE 	    spiTxBuf[SPI_BUF_SIZE];
volatile BYTE spiRxBuf[SPI_BUF_SIZE];
OS_SEM DSPI_SEM;
DSPIModule SPIM( DEFAULT_DSPI_MODULE );

spiInit();

spiTxBuf[0] = 3;	// Read Command
spiTxBuf[1] = 0;	// Address bits 16-23
spiTxBuf[2] = 0;	// Address bits  8-15
spiTxBuf[3] = 0;	// Address bits  0-7

WORD transferCount = 582; // write 4-bytes + read 578-bytes;

SPIM.SetCS( 0x04 ); // First SPI Memory.

spiTransfer( spiTxBuf, spiRxBuf, transferCount );

BYTE spiTransfer( PBYTE transmitBufferPtr, volatile BYTE* receiveBufferPtr, WORD byteCount)
{
	BYTE result = SPIM.Start( transmitBufferPtr, receiveBufferPtr, (DWORD)byteCount, DEASSERT_AFTER_LAST );

	OSSemPend( &DSPI_SEM, 0 );  // Wait for DSPI to complete

	return result;
}

void spiInit()
{
	memset( spiTxBuf, 0, SPI_BUF_SIZE );
	memset( (void *)spiRxBuf, 0, SPI_BUF_SIZE );

	// Setup I/O pins for SPI bus.
	Pins[SPI_CLK  ].function( PIN_31_DSPI1_SCK	);	// SPI Clock
	Pins[SPI_SIN  ].function( PIN_33_DSPI1_SIN	);	// SPI Data In
	Pins[SPI_SOUT ].function( PIN_35_DSPI1_SOUT	);	// SPI Data Out
	Pins[SPI_CS0  ].function( PIN_15_DSPI1_PCS0	);	// SPI Chip-Select 0
	Pins[SPI_CS1  ].function( PIN_37_DSPI1_PCS1	); 	// SPI Chip-Select 1
	Pins[SPI_CS2  ].function( PIN_39_DSPI1_PCS2	); 	// SPI Chip-Select 2

	OSSemInit( &DSPI_SEM, 0 );	// Initialize Semiphore for SPI bus.

	SPIM.Init( 2000000,	// Baudrate
			0x08,	// BitSz
			0x00,	// CS
			0x0F,		// CSPol
			0x01,	// ClkPol
			0x01,	// ClkPhase
			TRUE,	// DoutHiz
			0x00,	// csToClockDelay
			0x00		// delayAfterTransfer
		);

	SPIM.RegisterSem( &DSPI_SEM );
}
SeeCwriter
Posts: 630
Joined: Mon May 12, 2008 10:55 am

Re: Accessing SPI Memory

Post by SeeCwriter »

Some testing today shows that an SPI read operation is terminated after receiving byte 66. Don't know what that means
yet. I've been going through dspi.cpp to try figure out why, but so far no luck.

Just to be clear, whether reading or writing, the same function spiTransfer() is used. The only difference is the value of receiveBufferPtr. It is either a pointer to a buffer to read data, or a NULL pointer when doing only a write. The TX buffer is always valid because I have send a command byte and starting address to the EEPROM, 4-bytes.
SeeCwriter
Posts: 630
Joined: Mon May 12, 2008 10:55 am

Re: Accessing SPI Memory

Post by SeeCwriter »

Found the problem. It was all me. My pseudo shows:

Code: Select all

WORD transferCount = 582; // write 4-bytes + read 578-bytes; 
The reality was:

Code: Select all

BYTE transferCount = 582; // write 4-bytes + read 578-bytes; 
User avatar
dciliske
Posts: 624
Joined: Mon Feb 06, 2012 9:37 am
Location: San Diego, CA
Contact:

Re: Accessing SPI Memory

Post by dciliske »

Code: Select all

$ hex 582

582 => 0x246

$ decc 46

46 =>  70
Yup. I was really confused for a bit there.
Dan Ciliske
Project Engineer
Netburner, Inc
Post Reply