MODM7AE70 - SPI EEPROM

Discussion to talk about software related topics only.
Post Reply
BryanJS
Posts: 11
Joined: Sun Jun 03, 2018 10:14 am

MODM7AE70 - SPI EEPROM

Post by BryanJS »

I am trying to read and write to an EEPROM 25LC1024. I have successfully done this on other netburner V1 products but not V3, so I am basing this off successful code.

I am not able to read or write and I am clearly doing something basic wrong. Any help advice or examples would be appreciated.

My current code is below and I am using CS1 for the EEPROM.



// Initialize SPI
void SPI_Services::Initialize()
{
// Define the IO
CS_FLASH.function(PINP2_30_SPI0_NPCS2); // Chip Select 2
CS_EEPROM.function(PINP2_40_SPI0_NPCS1); // Chip Select 1
CS_AOUT.function(PINP2_26_SPI0_NPCS3); // Chip Select 3


SPI_CLK.function(PINP2_25_SPI0_SPCK); // CLOCK
SPI_DIN .function(PINP2_27_SPI0_MISO); // SPI MISO
SPI_DOUT.function(PINP2_28_SPI0_MOSI); // SPI MOSI

CS_FLASH = 1;
CS_EEPROM = 1;
CS_AOUT = 1;



iprintf("SPI Initialized. \r\n");
}


// EEPROM OPERATIONS
// Write a block to the EEPROM
void SPI_Services::EEPROM_WriteBlock(int iPage, uint8_t * pData, int iLength)
{
EEPROM_Initialize();


iprintf("Write Block. \r\n");

int iTxLength = iLength + 4; // The data plus command bytes
uint16_t Address = iPage * 256; // Pages in 256 bytes/block
uint8_t TxBuffer[iTxLength];

uint8_t Buffer[1];
Buffer[0] = SPI_WREN;
uint8_t iState = QSPIStart(Buffer, NULL,1 , &SPI_SEM);
SPI_SEM.Pend(0); // Wait for QSPI to complete



if(!CheckStatus(iState, " EEPROM Write Enabled")) return ;


// Set the command and address
TxBuffer[0] = SPI_WRITE;
TxBuffer[1] = (Address >> 16);
TxBuffer[2] = (Address >> 8);
TxBuffer[3] = (Address);

// Copy the data to the tx buffer
int iTxPosition = 4 ;
for(int i = 0 ; i < iLength ; i++)
{
TxBuffer[iTxPosition++] = i; //pData;
}


iState = QSPIStart(TxBuffer, NULL, iTxLength , &SPI_SEM);
SPI_SEM.Pend(0); // Wait for QSPI to complete

if(!CheckStatus(iState, " EEPROM Write Block")) return ;

iprintf("Write Block Ended. \r\n");

}



// Read a Block from the EEPROM
void SPI_Services::EEPROM_ReadBlock(int iPage, uint8_t * pData , int iLength)
{

EEPROM_Initialize();

int iMsgLength = iLength + 4;
int iAddress = iPage * 256; // Pages in 256 blocks

uint8_t Rx_Buffer[iMsgLength];
uint8_t Tx_Buffer[4];

memset( (void *)Rx_Buffer, 0, iMsgLength ); // Clear the Rx buffer

// Set the command bytes
Tx_Buffer[0] = SPI_READ;
Tx_Buffer[1] = (iAddress >> 16);
Tx_Buffer[2] = (iAddress >> 8);
Tx_Buffer[3] = (iAddress);


uint8_t iState = QSPIStart( Tx_Buffer, Rx_Buffer, iMsgLength , &SPI_SEM); // Output the data
SPI_SEM.Pend(0); // Wait for QSPI to complete


CheckStatus(iState, "EEPROM Read Block");

int iPointer = 4; // Rx_Buffer Pointer

for(int i = 0 ; i < iLength ; i++)
{
iprintf(" Data : %d. " , Rx_Buffer[iPointer] );
pData = Rx_Buffer[iPointer++];


}
iprintf("Read Block Ended. \r\n");
}


bool SPI_Services::EEPROM_Initialize()
{
uint8_t iState = QSPIInit( 2000000, 0x08, 0x1, 0x1, 0x0, 0x0, TRUE, 0x0, 0x0 );
return CheckStatus(iState, "EEPROM Initialization ");
}


// Helper functions
// Check the SPI status
bool SPI_Services::CheckStatus(uint8_t iState, char * msg)
{
bool blnOK = iState == 0;
if(!blnOK)
{
iprintf(msg);
iprintf(" SPI Error : %d. \r\n" , iState);
}
else
{
iprintf(msg);
iprintf(" SPI OK : %d. \r\n" , iState);
}
return blnOK;
}
User avatar
TomNB
Posts: 609
Joined: Tue May 10, 2016 8:22 am

Re: MODM7AE70 - SPI EEPROM

Post by TomNB »

Have you looked at the PicKitI2C example for comparison? It reads/writes an EEPROM. The example docs are here:

https://www.netburner.com/NBDocs/Develo ... m_e70.html

Might be good to compare the two and see if there are any significant differences, especially if you had been using an older hardware platform.
BryanJS
Posts: 11
Joined: Sun Jun 03, 2018 10:14 am

Re: MODM7AE70 - SPI EEPROM

Post by BryanJS »

Hi Tom,

Thanks, but the demo is I2C and I have having issues with SPI. The message format looks the same as SPI
User avatar
pbreed
Posts: 1096
Joined: Thu Apr 24, 2008 3:58 pm

Re: MODM7AE70 - SPI EEPROM

Post by pbreed »

CS_FLASH.function(PINP2_30_SPI0_NPCS2); // Chip Select 2
CS_EEPROM.function(PINP2_40_SPI0_NPCS1); // Chip Select 1
CS_AOUT.function(PINP2_26_SPI0_NPCS3); // Chip Select 3

The above all tells the hardware that the SPI hardware is managing the pin.

Below puts the pin back in GPIO mode....
CS_FLASH = 1;
CS_EEPROM = 1;
CS_AOUT = 1;

So your half in half out... do you want to manually run the CS, or want the SPI system to run it?
Choose one and delete the other three entries in your init function.
Post Reply