MODM7AE70 - SPI EEPROM

Discussion to talk about software related topics only.
Post Reply
BryanJS
Posts: 10
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;
}
Post Reply