• AVR Freaks

Hot!Interfacing PIC18F26K40 to serial flash SST25VF512A through SPI

Author
m_alizd
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2016/02/28 02:21:37
  • Location: 0
  • Status: offline
2020/03/04 00:57:57 (permalink)
0

Interfacing PIC18F26K40 to serial flash SST25VF512A through SPI

Hi All,
 
I'm using MCC to generate SPI code on PIC18F26K40 so to interface to serial flash SST25VF512A.
 
setup: Win 10, MPLAB X IDE V5.30, XC8 V1.38 PRO, MCC v3
 
Looks like the generated code has a function SPI1_Exchange8bit to send bytes thru SPI, however, it waits and expects a byte in return!
uint8_t SPI1_Exchange8bit(uint8_t data)
{
    // Clear the Write Collision flag, to allow writing
    SSP1CON1bits.WCOL = 0;

    SSP1BUF = data;

    while(SSP1STATbits.BF == SPI_RX_IN_PROGRESS)
    {
    }

    return (SSP1BUF);
}

uint8_t SPI1_Exchange8bitBuffer(uint8_t *dataIn, uint8_t bufLen, uint8_t *dataOut)
{
    uint8_t bytesWritten = 0;

    if(bufLen != 0)
    {
        if(dataIn != NULL)
        {
            while(bytesWritten < bufLen)
            {
                if(dataOut == NULL)
                {
                    SPI1_Exchange8bit(dataIn[bytesWritten]);
                }
                else
                {
                    dataOut[bytesWritten] = SPI1_Exchange8bit(dataIn[bytesWritten]);
                }

                bytesWritten++;
            }
        }
        else
        {
            if(dataOut != NULL)
            {
                while(bytesWritten < bufLen )
                {
                    dataOut[bytesWritten] = SPI1_Exchange8bit(DUMMY_DATA);

                    bytesWritten++;
                }
            }
        }
    }

    return bytesWritten;
}

 
1. As serial flash expects command, address, dummy byte before sending out the data byte, does this mean I cannot use SPI1_Exchange8bit? Am I right? Has anyone used MCC generated code for this purpose?
 
2. If so, is there any shared library or ready sample code I could use?
     I only need to verify I can read and write to it using SPI peripheral.
 
3. I couldn't make sense of how SPI1_Exchange8bitBuffer works. Can someone shed some light?
 
Thanks a lot.
Matt
 
post edited by m_alizd - 2020/03/04 01:05:51
#1

4 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28368
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Interfacing PIC18F26K40 to serial flash SST25VF512A through SPI 2020/03/04 01:12:51 (permalink)
    5 (1)
    m_alizd
    Looks like the generated code has a function SPI1_Exchange8bit to send bytes thru SPI, however, it waits and expects a byte in return!

    That is how SPI works.
    EVERY transfer is an exchange of bytes between master and slave.

    1. As serial flash expects command, address, dummy byte before sending out the data byte, does this mean I cannot use SPI1_Exchange8bit? Am I right? Has anyone used MCC generated code for this purpose?

    You are wrong.
    Read the SPI FLASH data sheet again. It will clearly show that dummy bytes are returned initially.
    Just discard them.
     

    3. I couldn't make sense of how SPI1_Exchange8bitBuffer works. Can someone shed some light?

    Once you understand that SPI ALWAYS does exchanges, it should make sense.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    katela
    Super Member
    • Total Posts : 1861
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Interfacing PIC18F26K40 to serial flash SST25VF512A through SPI 2020/03/04 04:22:04 (permalink)
    5 (1)
    That's how it works, it always an exchange of bytes between master and slave. If you don't need to send anything but only want to read, you can send some "don't care" dummy data like all 0xFF or 0x00.
    Here is a simple example using the SPI_Exchange8but(): https://youtu.be/Y1uPY_9UVPY

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #3
    m_alizd
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2016/02/28 02:21:37
    • Location: 0
    • Status: offline
    Re: Interfacing PIC18F26K40 to serial flash SST25VF512A through SPI 2020/03/04 20:09:17 (permalink)
    0
    ric
    Read the SPI FLASH data sheet again. It will clearly show that dummy bytes are returned initially.
    Just discard them.

     
     Hi again,
     
    Thanks for your response, however, I couldn't get what you mentioned in the datasheet of serial flash:
    Datasheet: http://ww1.microchip.com/...n/devicedoc/25090a.pdf
     
    The table 6 (Page 10) from above datasheet states SO is Hi-Z in response for commands such as 03 (Read), as well as response for address bytes. Only starting from dummy byte X there is a Dout.
     
     This is further reinforced by the Read timing diagram in Figure 5 (page 11) as well.
     
    According to above table and figure, am I still wrong by thinking SPI1_Exchange8bit will stuck in infinite loop on the first byte? If not can you explain in detail how?
     Any more thoughts?
     
    Cheers
    Matt
    #4
    ric
    Super Member
    • Total Posts : 28368
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Interfacing PIC18F26K40 to serial flash SST25VF512A through SPI 2020/03/04 20:14:40 (permalink)
    0
    m_alizd
    The table 6 (Page 10) from above datasheet states SO is Hi-Z in response for commands such as 03 (Read), as well as response for address bytes. Only starting from dummy byte X there is a Dout.

    It's still a dummy byte, but random data if you don't have a pullup or pulldown on the SDI pin.
     

    According to above table and figure, am I still wrong by thinking SPI1_Exchange8bit will stuck in infinite loop on the first byte? If not can you explain in detail how?

    Yes, you are still wrong, because by its very nature, SPI is always an exchange.
    The Master clocks in whatever it sees on the SDI pin while it is sending data.
    It is absolutely impossible for it to be stuck waiting to receive data, because it ALWAYS happens at the same time as it is sending data.
    Stop thinking this is the same as a UART. It's not.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    Jump to:
    © 2020 APG vNext Commercial Version 4.5