Hot!Check Serial EEPROM Status Hangs SPI2STATbits.SPIRBF

Author
Seanj
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2014/11/28 02:29:59
  • Location: 0
  • Status: offline
2018/01/06 04:11:41 (permalink)
0

Check Serial EEPROM Status Hangs SPI2STATbits.SPIRBF

Hi All,
 
I'm returning to PIC after a hiatus of several years!
 
I'm working my way through "Programming 16 bit microcontrollers in C" and have encountered an issue with the SPI example.
 
I am using the explorer 16 board, PIC24FJ128GA010 and the following code: 
 
/*
** SPI2
*/
#include <config.h>

// I/O definitions
#define CSEE _RD12 // select line for Serial EEPROM
#define TCSEE _TRISD12 // tris control for CSEE pin

// peripheral configurations
#define SPI_MASTER 0x0120 // select 8-bit master mode, CKE=1, CKP =0
#define SPI_ENABLE 0x8000 // enable SPI port, clear status

// 25LC256 Serial EEPROM commands
#define SEE_WRSR 1 // write status register
#define SEE_WRITE 2 // write command
#define SEE_READ 3 // read command
#define SEE_WDI 4 // write disable
#define SEE_STAT 5 // read status register
#define SEE_WEN 6 // write enable


// send one byte of data and receive one back at the same time
int WriteSPI2( int data)
{
    SPI2BUF = data; // write to buffer for TX
    while( !SPI2STATbits.SPIRBF); // wait transfer completion
    return SPI2BUF; // read the received value
} // WriteSPI2


main()
{
    int i;
    
    // 1. init the SPI peripheral
    TCSEE = 0; // make SSEE pin output
    CSEE = 1; // de-select the Serial EEPROM
    SPI2CON1 = SPI_MASTER; // select mode
    SPI2STAT = SPI_ENABLE; // enable the peripheral

    // 2. main loop
    while( 1)
    {
        // 2.1 send a Write Enable command
        CSEE = 0; // select the Serial EEPROM
        WriteSPI2( SEE_WEN); // write enable command
        CSEE = 1; // deselect, terminate command
        
        // 2.2 Check the Serial EEPROM status
        CSEE = 0; // select the Serial EEPROM
        WriteSPI2( SEE_STAT); // send a READ STATUS COMMAND
        i = WriteSPI2( 0); // send/receive
        CSEE = 1; // deselect, terminate command
    } // main loop
    
} // main
    

 
When the above code runs 
 WriteSPI2( SEE_STAT);

the code enters 
WriteSPI2
and hangs on:
while( !SPI2STATbits.SPIRBF);

 
Do I have a hardware failure or is there something I have potentially missed?
 
Also, on the same topic, I have been hunting for information on SPI2STATbits.  SPIxSTAT is on the data sheet but I'm interested to find out where the keywords such as bits are listed.  
 
Thanks,
Seán
post edited by Seanj - 2018/01/06 05:46:43
#1

4 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Check Serial EEPROM Status Hangs SPI2STATbits.SPIRBF 2018/01/06 04:23:07 (permalink)
    0
    Do you have a scope to see if the SCK2 pin toggles when you write to SSPBUF?
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #2
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2745
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Check Serial EEPROM Status Hangs SPI2STATbits.SPIRBF 2018/01/06 05:54:20 (permalink)
    0
    Init spi prescaler.
    Did you make the spi ports available? - pps
     
    Yes, from Microchip.
    Section 18. Serial Peripheral Interface (SPI)
    post edited by Gort2015 - 2018/01/06 05:57:21

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    u/chip
    New Member
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2017/03/27 12:14:09
    • Location: 0
    • Status: offline
    Re: Check Serial EEPROM Status Hangs SPI2STATbits.SPIRBF 2018/04/04 09:49:46 (permalink)
    0
    Hi, 
     
    Did you figure out why your code hangs while waiting for SPIRBF to be set?
    I have the same issue, but it only happens every once in a while.
     
    Thanks,
    Ed
    #4
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2745
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Check Serial EEPROM Status Hangs SPI2STATbits.SPIRBF 2018/04/04 13:44:54 (permalink)
    0
    Shouldn't happen.
    Start a new thread.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #5
    Jump to:
    © 2018 APG vNext Commercial Version 4.5