Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SPI flash memory can read ID but can't write//read

Status
Not open for further replies.

TheMartian

Junior Member level 3
Joined
Jul 6, 2018
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
322
Hey
I am stuck with Memory project.
I can read memory ID so SPI communication works.
But when I write a byte and read it back, it comes back invalid.
My code:
Code:
void SST25VF_writeByte(uint32_t adr, uint8_t dat)
{  
  //dat = 'A';

  SPI_INIT
  setFlashSSN(LOW);
  SPI1_Write(0x06);//write enable instruction
  setFlashSSN(HIGH);
  Delay_us(1);// nop();
  
  setFlashSSN(LOW);
  SPI1_Write(0x02); // Write Byte //
  SST25VF_setAddress(adr);       // 3 address cycles
  SPI1_Write(dat); // data cycle
  setFlashSSN(HIGH);
  SST25VF_waitUntilDone(); /// waitUntilDone();
}

uint8_t SST25VF_readByte(uint32_t addres)
{
    uint8_t retV;
    
    SPI_INIT
    setFlashSSN(LOW);
    SPI1_Write(0x03); // 
    SST25VF_setAddress(addres); // 3 bytes
    retV = SPI1_Read(0);
    setFlashSSN(HIGH);

    return retV;
}

I assume that SPI works because "read memory ID" command returns correct values, compared to the ones with Datasheet.
But read always returns the same value and I don't know where the problem is. It might be in write, it might be in read.
I am using PIC18.
I checked SPI read/write order with Datasheet and it's OK:


Any suggestions what might be wrong?
 

Presume you know that flash memory area must be erased before writing to it.
 

Presume you know that flash memory area must be erased before writing to it.

Good suggestion, but seems ruled out, unless I did something more wrong.

eraase code:
Code:
void SST25VF_sectorErase(uint8_t sectorAddress)
{
  SPI_INIT
  setFlashSSN(LOW);
  SPI_Write(0x06);//write enable instruction
  setFlashSSN(HIGH);
  Delay_ms(1);///nop();
  setFlashSSN(LOW);
  SPI_Write(0x20); // Erase 4KB Sector //
  SST25VF_setAddress(4096UL*((long)(sectorAddress)));
  setFlashSSN(HIGH);
  SST25VF_waitUntilDone();
 /// SPI.endTransaction();;
}

test code:
Code:
    byte0 = 123;
    adr  = 5;
    SST25VF_sectorErase(0);
               Delay_ms(10);
   SST25VF_writeByte(adr,byte0);
               Delay_ms(10);
    byte1 = SST25VF_readByte(adr);
    if(byte0 != byte1)
    {                     // BLINK TO SHOW BUG
           for(i= 0; i < 10; i++)
           {
                        PORTF ^=  1 << 2 ;                      // toggle PORTF with xor operator
                        Delay_ms(100);
           }
    }

any other suggestions or ideas how to debug it?

PS: also tried "whole chip erase", no differences
 

The fault is probably not in the part of the code you are yet showing.
 

The fault is probably not in the part of the code you are yet showing.

So I guess you should see this:

Code:
#define SPI_INIT SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_END  ,_SPI_CLK_IDLE_LOW,_SPI_LOW_2_HIGH) ;
//#define SPI_INIT SPI1_Init();

first I assumed it was OK because "read ID" commands works, but maybe here is the fault.

Or is there anything more related to the issue?


PS: I also tried checking WP pin, but it seems to make no difference in my case.
I based on schematic:
scheme_prog.png


EDIT: Fixed but I am not sure why.

and the
Serial Data Output (SO) is driven after the falling edge
of the SCK clock signal.

_SPI_LOW_2_HIGH

ah low 2 high is "rising edge" not falling


but I based on code from web, from mikroC forum, so that guy had it wrong, or that setting is different for different flash memories???
 
Last edited:

From the above picture it's not clear what board you're using, but apparently the atmega32u4 microcontroller is being powered with 5v, whereas the SST25VF memory should be powered with the maximum voltage 3.6v.
 

From the above picture it's not clear what board you're using, but apparently the atmega32u4 microcontroller is being powered with 5v, whereas the SST25VF memory should be powered with the maximum voltage 3.6v.

No, I am using PIC at 3.3v.

My problem is solved. My SPI1_Init_Advanced was wrong.

I was 100% sure it was OKAY because "READ ID" worked. That's why I didnd check it twice. I was looking for error in wrong place.

But somehow it turns out that "READ ID" works with bad edge rising setting (bad SPI1_Init_Advanced settngs), but "write memory" dont. Strange.
 

but I based on code from web, from mikroC forum, so that guy had it wrong, or that setting is different for different flash memories???
As you found out, your original setting was wrong. But it's not different for usual flash memories, all are using SPI mode 0/3 as described in the Micron datasheet. Standard mode 0 settings for PIC are CKP = 0, CKE = 1 and SMP = 0.

Mikro C SPI library is slightly confusing things by adding new descriptions for the SPI modes.
 

Why are you calling your "SPI_INIT" macro (i.e calling the initialisation function) before each operation? That is just a waste of time.
Initialise the SPI peripheral during the overall application setup and then just use it after that.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top