S32K144 bare metal LPSPI Unable to write to Winbond Flash

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S32K144 bare metal LPSPI Unable to write to Winbond Flash

Jump to solution
1,070 Views
Can_Do_bus
Contributor III

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

Can_Do_bus_0-1648482308999.png

 

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;
    }

 

0 Kudos
1 Solution
1,052 Views
Can_Do_bus
Contributor III

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

 

View solution in original post

0 Kudos
3 Replies
1,053 Views
Can_Do_bus
Contributor III

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

 

0 Kudos
1,067 Views
Can_Do_bus
Contributor III

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.

0 Kudos
1,057 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

measure SPI bus with scope/analyzer and check if it follows protocol stated in the Flash datasheet.

BR, Petr

0 Kudos