+ Post New Thread
Results 1 to 9 of 9
  1. #1
    Junior Member level 3
    Points: 236, Level: 2

    Join Date
    Jul 2018
    Posts
    28
    Helped
    0 / 0
    Points
    236
    Level
    2

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

    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?

  2. #2
    Super Moderator
    Points: 250,892, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,678
    Helped
    13284 / 13284
    Points
    250,892
    Level
    100

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

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



    •   AltAdvertisment

        
       

  3. #3
    Junior Member level 3
    Points: 236, Level: 2

    Join Date
    Jul 2018
    Posts
    28
    Helped
    0 / 0
    Points
    236
    Level
    2

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

    Quote Originally Posted by FvM View Post
    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



    •   AltAdvertisment

        
       

  4. #4
    Super Moderator
    Points: 250,892, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,678
    Helped
    13284 / 13284
    Points
    250,892
    Level
    100

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

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



    •   AltAdvertisment

        
       

  5. #5
    Junior Member level 3
    Points: 236, Level: 2

    Join Date
    Jul 2018
    Posts
    28
    Helped
    0 / 0
    Points
    236
    Level
    2

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

    Quote Originally Posted by FvM View Post
    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:
    Click image for larger version. 

Name:	scheme_prog.png 
Views:	5 
Size:	319.4 KB 
ID:	150411


    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 by TheMartian; 15th December 2018 at 15:22.



  6. #6
    Super Moderator
    Points: 28,249, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,418
    Helped
    1066 / 1066
    Points
    28,249
    Level
    40
    Blog Entries
    6

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

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  7. #7
    Junior Member level 3
    Points: 236, Level: 2

    Join Date
    Jul 2018
    Posts
    28
    Helped
    0 / 0
    Points
    236
    Level
    2

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

    Quote Originally Posted by andre_teprom View Post
    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.



  8. #8
    Super Moderator
    Points: 250,892, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,678
    Helped
    13284 / 13284
    Points
    250,892
    Level
    100

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

    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.



    •   AltAdvertisment

        
       

  9. #9
    Advanced Member level 3
    Points: 7,157, Level: 20

    Join Date
    Jan 2015
    Posts
    999
    Helped
    319 / 319
    Points
    7,157
    Level
    20

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

    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



--[[ ]]--