1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Understanding a PIC FIFO

Discussion in 'Oshonsoft' started by camerart, Jun 29, 2017.

  1. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    I'm using a PIC to control a radio module SX1278. This means there will be two FIFOs, one on the PIC and one on the SX.
    I have questions about the SX FIFO here: http://www.electro-tech-online.com/...ion-receiving-using-scr-radio-modules.149198/
    This is regarding the PIC FIFO:
    Am I correct, that a serial sentence enters the PIC via the RX pin, and is stored in the FIFO, till a HSEROUT command is given? How big is the FIFO on an 18LF2520 PIC?

    Cheers, Camerart.
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Morning C,
    The Receive buffer can hold 2 bytes of incoming data, if a third byte is received an error flag is raised, so the buffer [ fifo] holds two bytes.
    E
    A01.gif
     
    Last edited: Jun 29, 2017
  3. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Morning E,
    If I recall correctly, we've done this before with NMEA sentences (Which of course is one of the uses for this set-up) I can now visualise the HSERIN/OUT Oshonsoft simulations. I think what is puzzling me is that in real life it happens a bit quicker, so a sentence comes into the PIC completely in one string.
    Where is the sentence held and how do I send it to the SX1278 at the correct timing?
    Thanks.
    C.
     
    Last edited: Jun 29, 2017
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE

    hi C,
    If you want to hold/buffer an incoming serial data string you would enable the UART's interrupt.
    When the first byte of the incoming string appears in the UART buffer it will raise an interrupt.

    Loop:
    The ISR [Interrupt service subroutine] would read the recv buffer and move the received byte into a software buffer, the program would increment the software buffer address pointer.
    If the received byte is LF [0x0a] that is the incoming string terminating byte. EXIT
    else
    keep reading the UART s RCIF and if set, read the UART recv buffer and move the byte to the software buffer.
    Loop.

    An alternative method is to use the RCIF to jump to the ISR on every byte, I prefer the former method.
    The downside is, if the LF byte never arrives the program is stuck in the ISR loop!!! , You have to use a PIC Timer to check if its time to escape from the ISR.

    It is also important that when entering the ISR due a RCIF, you test the OERR [over run error]
    If there is an Error you must clear the error flags.

    E
     
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    I use a software fifo to keep the code flowing,
    Code (text):

    #define FifoLength 32
    unsigned char RXFifoBuffer[FifoLength];
    unsigned char *RXFifoStart;
    unsigned char *RXFifoEnd;


    void InitFifo(void){
        RXFifoStart=RXFifoBuffer;
        RXFifoEnd=RXFifoBuffer;
    }

    void PutRXFifo(unsigned char chr){
        *RXFifoEnd++=chr;
        if(RXFifoEnd==RXFifoBuffer+FifoLength)      //reached end
            RXFifoEnd=RXFifoBuffer;                 //yes so wrap
        if(RXFifoStart==RXFifoEnd){                 //is head eating tail?
            //fifo full so deal with it!!
        }
    }

    unsigned char GetRXFifo(void){
    unsigned char chr;
        while(RXFifoStart==RXFifoEnd);              //if fifo empty then wait
        chr=*RXFifoStart++;
        if(RXFifoStart==RXFifoBuffer+FifoLength)    //wrapped?
            RXFifoStart=RXFifoBuffer;               //yes
        return(chr);
    }

    unsigned char RXFifoCount(void){
        if(RXFifoEnd>=RXFifoStart){
            return(RXFifoEnd-RXFifoStart);
        }else{
            return(FifoLength-(RXFifoStart-RXFifoEnd));
        }
    }
     
    Then in the interrupt I have,
    Code (text):

        if(RCIF&&RCIE)
            PutRXFifo(RCREG);
    And in the main code I can do,
    Code (text):

    if(RXFifoCount()!=0){
        var=GetRXFifo();
    }
    Just calling GetRXFifo will force it to wait until a character is received.

    Mike.
     
  7. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi M,
    Thanks for the CODE, but it's written in a foreign language for me. I only speak BASIC, sorry. Also it appears to have a FIFO size where the FIFO is limited to a couple of BYTES.

    Hi E,
    This FIFO lark isn't an easy one is it:confused:
    Can you visualise the speed of the other program here: http://www.electro-tech-online.com/...ion-receiving-using-scr-radio-modules.149198/
    having the speed to accept an incoming sentence on the fly? This would solve a lot of things. In real life, when connected to 'say' a GPS module, it would send a message at 1 to 5 sentences/sec, so I suppose there would be no need for storage.
    C.
     
  8. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    The fifo in the above code is set at 32 bytes (FifoLength) but can be any length. I actually use a fifo on both receive and transmit and find the transmit one most useful as I can send a long sentence without pausing the code.

    Maybe Eric could convert above to basic. Does OBasic have pointers?

    Mike.
     
  9. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi M,
    Ok, thanks.
    I'll finish the program here: http://www.electro-tech-online.com/...ion-receiving-using-scr-radio-modules.149198/ first to the best of my abilities, first, then try to test it and see a FIFO is necessary.

    NOTE: When referring to the SX1278 FIFO, I will colour it BLUE, for clarification. Here we are talking about the 18LF2520 FIFO.
    Eric, don't convert anything yet;)
    Ok, back to the other thread!
    C.
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    IIRC you are programming the TX as well the RX ends of the LoRa radio link, so why are the rates so high.??
    You can select a lower rate.??
    E
    A02.gif
     
  11. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi E,
    Well spotted, however, I notice that this is in the FSK/OOK not the LORA section of the DATA sheet. I would have to double check whether this is relevant. You will notice that the LORA sections are in light blue. Having said that, there are some shared sections. A DATA sheet to send you mad:arghh:
    EDIT: I searched, and don't think this is used in LORA MODE.

    C.
     
    Last edited: Jun 30, 2017

Share This Page