I have managed to interface a S32K144 and a W25Q256JV winbond external flash using LPSPI, no DMA is used.
I am able to read registers of the external flash and also the data in various addresses which return a FF as nothing has been written to the flash yet.
I am having difficulty to write to a memory address. I have ensured that the chip is write enabled in the register.
I am performing the following steps in the code
read an address, write a byte to that address, read back the same address to see whether write has been successful.
When I read an address it returns FF, when I read the same address once again it returns FF.
However when I perform a write operation between the two reads, the second read returns a 0. However no data has been written to that address.
Can some one have a look at my code to see whether the write operation is properly coded.
uint8_t LPSPI1_write_to_memory_location()
{
uint32_t receive = 0;
LPSPI1->TCR = LPSPI_TCR_CPOL(0)|LPSPI_TCR_CPHA(0)
|LPSPI_TCR_PRESCALE(2)
|LPSPI_TCR_PCS(0)
|LPSPI_TCR_FRAMESZ(47)
|LPSPI_TCR_LSBF(0);
while ((LPSPI1->SR & LPSPI_SR_TDF_MASK) >> LPSPI_SR_TDF_SHIFT == 0)
; /* Wait for Tx FIFO available */
LPSPI1->TDR = 0x12000000; /* Transmit data */
LPSPI1->SR |= LPSPI_SR_TDF_MASK; /* Clear TDF flag */
while ((LPSPI1->SR & LPSPI_SR_TDF_MASK) >> LPSPI_SR_TDF_SHIFT == 0)
LPSPI1->TDR = 0x014B; /* Transmit data */
LPSPI1->SR |= LPSPI_SR_TDF_MASK; /* Clear TDF flag */
LPSPI1->TCR = LPSPI_TCR_CPOL(0)|LPSPI_TCR_CPHA(0)
|LPSPI_TCR_PRESCALE(2)
|LPSPI_TCR_PCS(0)
|LPSPI_TCR_FRAMESZ(31)
|LPSPI_TCR_LSBF(0);
LPSPI1->RDR;
LPSPI1->RDR;
return receive;
}
uint8_t LPSPI1_read_from_memory_location(unsigned long startaddress, unsigned long numbytes)
{
uint32_t receive = 0;
LPSPI1->TCR = LPSPI_TCR_CPOL(0)|LPSPI_TCR_CPHA(0)
|LPSPI_TCR_PRESCALE(2)
|LPSPI_TCR_PCS(0)
|LPSPI_TCR_FRAMESZ(47)
|LPSPI_TCR_LSBF(0);
while ((LPSPI1->SR & LPSPI_SR_TDF_MASK) >> LPSPI_SR_TDF_SHIFT == 0)
; /* Wait for Tx FIFO available */
LPSPI1->TDR = 0x13000000; /* Transmit data */
LPSPI1->SR |= LPSPI_SR_TDF_MASK; /* Clear TDF flag */
while ((LPSPI1->SR & LPSPI_SR_TDF_MASK) >> LPSPI_SR_TDF_SHIFT == 0);
LPSPI1->TDR=0x0000;
LPSPI1->SR |= LPSPI_SR_TDF_MASK; /* Clear TDF flag */
while ((LPSPI1->SR & LPSPI_SR_RDF_MASK) >> LPSPI_SR_RDF_SHIFT == 0);
receive = LPSPI1->RDR;
while ((LPSPI1->SR & LPSPI_SR_RDF_MASK) >> LPSPI_SR_RDF_SHIFT == 0);
receive = LPSPI1->RDR;
LPSPI1->TCR = LPSPI_TCR_CPOL(0)|LPSPI_TCR_CPHA(0)
|LPSPI_TCR_PRESCALE(2)
|LPSPI_TCR_PCS(0)
|LPSPI_TCR_FRAMESZ(31)
|LPSPI_TCR_LSBF(0);
return receive;
}
Solved! Go to Solution.
I have fixed this, the issue was after writing to memory location, I was performing read operation twice, however the RDF flag doesn't get set for some time, by adding a check for RDF flag before read resolved this
I have fixed this, the issue was after writing to memory location, I was performing read operation twice, however the RDF flag doesn't get set for some time, by adding a check for RDF flag before read resolved this
Just to add to my earlier post, even if I write to a different address and read from a different address, read is returning 0 instead of FF, if read after a write operation.
Hi,
measure SPI bus with scope/analyzer and check if it follows protocol stated in the Flash datasheet.
BR, Petr