Hi,
I have already done these things which you told me. I am following these instructions:
1) I have sent to the flash write enable command
stData.m_pucCmd[0] = 0x06; // write enable command is 0x06
clear_cs(); // I selected the flash.
SPI3D = stData.m_pucCmd[0]; // I sent to flash write enable command.
set_cs(); // disable flash, disable chip select pin..
2) I have erased the target adress which I wanted to write:
stData.m_pucCmd[0] = 0xD8: // sector erase command is 0xD8
stData.m_pucCmd[1] = 0x00;
stData.m_pucCmd[2] = 0x00;
stData.m_pucCmd[3] = 0x00;
First byte is command and the others are target address.
stData.m_pucCmdLen = 4; // one byte command and three bytess adress.
stat_cSPITxStatus= COMMAND;
g_ucSPITimeOutCount= _20_MS;
3) I have called my transmit function. These function is like:
UINT8 ucIntFlag = CLEAR;
ucIntFlag = SPI3S;
/* Check transmit flag and SPI time out */
if((CLEAR == g_ucSPITimeOutCount) && (ucIntFlag & TRANSMIT_FLAG))
{
{
/* check SPI status and load the SPI data buffer with the data to be
* write in the sequence command,address and data to be written
*/
switch(stat_cSPITxStatus)
{
case COMMAND:
/* load the address of the NV memory to the SPI data
* buffer
*/
stat_unSPIIndex++;
if(stat_unSPIIndex >= stData.m_ucCmdLen)
{
stData.m_ucCmdLen = CLEAR;
stat_unSPIIndex = CLEAR;
/* After address transmission change SPI tx status to data
* in order to send the data bytes
*/
if(CLEAR != stData.m_unDataLen)
{
SPI3D = stData.m_pucData[stat_unSPIIndex];
/* Initialize a SPI timer to count 1 byte transmission */
g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
stat_cSPITxStatus = DATA;
}
else
{
/* Initialize a SPI timer to count 1 byte transmission */
g_ucSPITimeOutCount = THREE;
stat_cSPIStatus = SUCCESS;
}
}
else
{
SPI3D = stData.m_pucCmd[stat_unSPIIndex];
while(ERR_RXEMPTY==SPI_RecvChar(&temp));
/* Initialize a SPI timer to count 1 byte transmission */
g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
}
break;
case DATA:
stat_unSPIIndex++ ;
/* Return success after transferring all data bytes */
if(stat_unSPIIndex >= stData.m_unDataLen)
{
/* Initialize a SPI timer to count 1 byte transmission */
g_ucSPITimeOutCount = THREE;
/* Clear buffer index */
stat_unSPIIndex = CLEAR;
stat_cSPIStatus = SUCCESS;
stat_cSPITxStatus = COMMAND;
stData.m_pucData = NULL;
stData.m_unDataLen = ZERO_0;
}
else
{
SPI3D = stData.m_pucData[stat_unSPIIndex];
while(ERR_RXEMPTY==SPI_RecvChar(&temp));
/* Initialize a SPI timer to count 1 byte transmission */
g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
}
break;
default:
break;
}
}
}
4-) I am calling my read function:
void SPIReadData(UINT32 ulnAddress, UINT8 *pucData, UINT32 unNoOfBytes)
{
UINT8 ucIntFlag = CLEAR, temp;
/*
* Select prescaler value 1 and divider value 128 to achieve the 156.25khz
* clock rate
*/
CONFIGURE_SPI3_BAUD_RATE(BUSCLK_PRESCALER_8|BAUD_RATE_DIVIDER_512) ;
CONFIGURE_SPI3_CONTROLE_REG1(SPI_SYS_ENABLED);
/* Read flag register to clear the transmit flag */
ucIntFlag = SPI3S;
/* select NV memory as slave */
CLEAR_CS();
/* NV memory Read command */
SPIUpdateCmd(READ_DATA_CMD);
/* Command length including address length */
stData.m_ucCmdLen = COMMAND_LENGTH + ADDRESS_LENGTH;
/* Address of the NV memory */
SPIUpdateAddr(ulnAddress);
/* buffer store data read from NV memory */
stData.m_pucData = pucData;
/* Number of bytes of data to be read */
stData.m_unDataLen = unNoOfBytes;
/* Operation mode */
stData.m_ucMode = SPI_READ;
stat_unSPIIndex = CLEAR;
/* load read command to Tx buffer */
SPI3D = stData.m_pucCmd[COMMAND_INDEX];
/* Set the SPI status as command */
stat_cSPITxStatus = COMMAND;
/* Return the SPI status to NV driver as, read in progress */
stat_cSPIStatus = RD_INPROGRESS;
/* Initialize a SPI timer to count 1 byte transmission */
g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
/* Enable the SPI transmit interrupt */
SPI_TX_EN();
}
my ISR funciton is :
interrupt VectorNumber_Vspi3 void SPIISR(void)
{
UINT8 ucIntFlag = CLEAR;
/* Initialize a SPI timer to count 20msec */
g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
/* Read flag register to clear the transmit flag */
ucIntFlag = SPI3S;
/* check transmit buffer is empty */
if( ( ucIntFlag & TRANSMIT_FLAG ) )
{
/* check SPI status and load the SPI data buffer with the data to be
* write in the sequence command,address and data to be written
*/
switch(stat_cSPITxStatus)
{
case COMMAND:
/* load the address of the NV memory to the SPI data
* buffer
*/
stat_unSPIIndex++;
if(stat_unSPIIndex >= stData.m_ucCmdLen)
{
stData.m_ucCmdLen = CLEAR;
stat_unSPIIndex = CLEAR;
/* After address transmission change SPI tx status to data
* in order to send the data bytes
*/
if(CLEAR != stData.m_unDataLen)
{
/* write dummy data to SPI transmit buffer in order
* to receive data
*/
SPI3D = DUMMY_DATA;
// g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
stat_cSPITxStatus = DATA;
}
}
else
{
SPI3D = stData.m_pucCmd[stat_unSPIIndex];
// g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
}
break;
case DATA:
/* write dummy data to SPI transmit buffer in order
* to receive data
*/
if((SPI_READ == stData.m_ucMode) &&
(CLEAR == stData.m_ucCmdLen))
{
switch(stat_cSPITxStatus)
{
case DATA:
/* read the data from the SPI data buffer */
if(stat_unSPIIndex > CLEAR)
{
stData.m_pucData[stat_unSPIIndex - ONE] = SPI3D ;
}
else
{
ucIntFlag = SPI3D;
}
stat_unSPIIndex++ ;
if(stat_unSPIIndex > stData.m_unDataLen)
{
/* Disable the slave select pin */
SET_CS();
SPI_SYSTEM_DISABLE();
/* Disable the SPI transmit interrupt */
SPI3_TX_DIS();
/* Clear buffer index */
stat_unSPIIndex = CLEAR;
stat_cSPIStatus = SUCCESS;
stat_cSPITxStatus = COMMAND;
stData.m_pucData = NULL;
stData.m_unDataLen = ZERO_0;
/* Disable the slave select pin after completing
* read operation
*/
}
else
{
SPI3D = DUMMY_DATA;
// g_ucSPITimeOutCount = SPI_TIME_OUT_20MS;
}
break;
default:
break;
}
}
else
{
/* Read flag register to clear the transmit flag */
ucIntFlag = SPI3S;
ucIntFlag = SPI3D;
}
break;
default:
break;
}
}
}