I am testing this on actual PCB where flash and controller are interfaced
I am using the following code for reading and writing into flash. I have followed the instructions according to the spansions data sheet.
The code is working with lower frequencies(below 1MHz) but not working for higher frequencies
//////////////////////////////////////
#include "../SLLD/slld.h"
#include "HAL.h"
#include "SM0.h"
uint8_t wrBuffer[MAX_BUFFER_SIZE];
uint8_t Dummy_buffer[MAX_BUFFER_SIZE];
// ***************************************************************************
// FLASH_READ - HAL read function
//
// input : device_num device number to which operation will be done
// command write a single command byte to flash
// sys_addr system address to be used
// data_buffer Pointer to the data buffer where to store the read data
// Number_Of_Read_Bytes number of bytes to be read
//
// return value : status of the operation - FAIL or SUCCESS
// ***************************************************************************
SLLD_STATUS FLASH_READ(BYTE command,ADDRESS sys_addr,BYTE *data_buffer,int Number_Of_Read_Bytes )
{
uint8_t Addr1,Addr2,Addr3;
uint8_t Cmd_Size;
uint8_t AddBufferPtr[ADD_BUFFER_SIZE]; //! buffer to store address of the flash
uint8_t Dummy_buffer[DUMMY_BUFFER_SIZE]; //! spi dummy buffer
SLLD_STATUS status = SLLD_OK;
Cmd_Size=0;
AddBufferPtr[Cmd_Size++]=command;
if(sys_addr != ADDRESS_NOT_USED)
{
Addr1=sys_addr;
Addr2=sys_addr>>8;
Addr3=sys_addr>>16;
AddBufferPtr[Cmd_Size++]=Addr3;
AddBufferPtr[Cmd_Size++]=Addr2;
AddBufferPtr[Cmd_Size++]=Addr1;
}
// The AddBufferPtr contains the read command along with address of flash to be read
//make the chip select pin low
CS_SPI_ClrVal(CS_SPI_DeviceData);
// send the AddBufferPtr to flash
if(SM0_SendBlock(masterDevData,(LDD_TData*)AddBufferPtr,Cmd_Size)!=ERR_OK)
{
while(1);
}
if(SM0_ReceiveBlock(masterDevData, (LDD_TData*)Dummy_buffer,Cmd_Size)!=ERR_OK)
{
while(1);
}
// Wait till transmission and reception completes
while(Tx_CompleteFlag==0);
Tx_CompleteFlag=0;
while(Rx_CompleteFlag==0);
Rx_CompleteFlag=0;
// receive the data from flash
if(SM0_SendBlock(masterDevData, (LDD_TData*)msgDummy,Number_Of_Read_Bytes)!=ERR_OK)
{
while(1);
}
if(SM0_ReceiveBlock(masterDevData, (LDD_TData*)data_buffer,Number_Of_Read_Bytes)!=ERR_OK)
{
while(1);
}
//wait till actual data received
while(Tx_CompleteFlag==0);
Tx_CompleteFlag=0;
while(Rx_CompleteFlag==0);
Rx_CompleteFlag=0;
// disassert the chipselect
CS_SPI_SetVal(CS_SPI_DeviceData);
return(status);
}
/***************************************************************************
FLASH_WRITE - HAL write function
input : device_num device number to which operation will be done
command write a single command byte to flash
sys_addr system address to be used
data_buffer Pointer to the data buffer where to store the written data
Number_Of_Written_Bytes number of bytes to be written
return value : status of the operation - FAIL or SUCCESS
***************************************************************************/
SLLD_STATUS FLASH_WRITE(BYTE command,ADDRESS sys_addr,BYTE *data_buffer,int Number_Of_Written_Bytes)
{
SLLD_STATUS status = SLLD_OK;
uint32_t Cmd_Size;
uint8_t Addr1,Addr2,Addr3;
Cmd_Size=0;
wrBuffer[Cmd_Size++] = command;
if(sys_addr != ADDRESS_NOT_USED)
{
Addr1=sys_addr;
Addr2=sys_addr>>8;
Addr3=sys_addr>>16;
Cmd_Size = 0;
wrBuffer[Cmd_Size++]=command;
wrBuffer[Cmd_Size++]=Addr3;
wrBuffer[Cmd_Size++]=Addr2;
wrBuffer[Cmd_Size++]=Addr1;
if(data_buffer != BUFFER_NOT_USED)
{
uint16_t count;
for(count=0;count<Number_Of_Written_Bytes;count++)
{
wrBuffer[Cmd_Size++] = (uint8_t)*data_buffer;
data_buffer++;
}
}
}
CS_SPI_ClrVal(CS_SPI_DeviceData);
SM0_SendBlock(masterDevData,(LDD_TData*)wrBuffer,Cmd_Size);
SM0_ReceiveBlock(masterDevData, (LDD_TData*)Dummy_buffer,Cmd_Size);
while(Tx_CompleteFlag==0);
Tx_CompleteFlag=0;
while(Rx_CompleteFlag==0);
Rx_CompleteFlag=0;
CS_SPI_SetVal(CS_SPI_DeviceData);
return(status);
}