Picaxe M2 FIFO buffer

jims

Senior Member
This recent thread "Emic2 & PICAXE" made me realize that I need to understand how the 2 byte FIFO buffer on the M2 works. I don't want to mess-up the other thread so I'm starting this one.
If 2 data bytes have been received in the M2 background FIFO buffer; what happens to the next byte that comes along? Is it ignored, or does it go into the buffer and kick-out the oldest byte? Which bytes stay in the buffer? Only the first 2; or does the new byte push out the oldest byte? I believe that a single hserin command will return only the oldest byte in the buffer: while 2 consecutive hserin commands will return both the oldest byte and the newest byte. Does the hsersetup command reset the buffer? In my mind I picture a length of pipe that will hold two ping pong balls. If a third ball is stuffed into the pipe, the ball that's been there the longest is pushed out. Is this how it works?
I'll appreciate comments to help me so that I can program my Emic2 when it arrives in a week or two.
Also, does anyone know if there is anyplace in "code explorer" (or wherever) to observe the contents of the 2 deep M2 FIFO buffer for background received data?
Thank you, Jims
 
Last edited:

hippy

Technical Support
Staff member
The first two bytes received will be put into the internal buffer, any subsequent bytes will be lost, until HSERIN is executed and the first is taken and put into the variable. This allows another byte to be put into the buffer, subsequent bytes being lost.

So it's a like a short ( two vehicle capacity ) road into a car park with a barrier from a no waiting road.

For your ping pong balls and pipe analogy; the third one won't go in, falls to the floor, is gobbled up by alligators, never to be seen again :)

Because it's background receive, a single PICAXE can demonstrate this. Easiest for an 18M2 or 20M2 because HSEROUT isn't the SERTXD output as well. With HSEROUT pin connected to HSERIN pin, this will show A, B, <empty> repeatedly, the C and D sent are lost ...

Code:
[color=Navy]#Picaxe [/color][color=Black]18M2[/color]
[color=Navy]#Terminal 4800[/color]
[color=Blue]HserSetup B4800_4[/color][color=Black], [/color][color=Navy]%000[/color]
[color=Blue]Do
  HserOut [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"ABCD"[/color][color=Blue])
  Pause [/color][color=Navy]1000
  [/color][color=Blue]Do 
    [/color][color=Purple]w0 [/color][color=DarkCyan]= -[/color][color=Navy]1 
    [/color][color=Blue]HSerIn [/color][color=Purple]w0
    [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]Then
      SerTxd( [/color][color=Purple]b0[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF )
    End If
 Loop Until [/color][color=Purple]b1 [/color][color=DarkCyan]<> [/color][color=Navy]0
 [/color][color=Blue]SerTxd( [/color][color=Red]"<Empty>"[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF )
Loop[/color]
 
Last edited:

hippy

Technical Support
Staff member
Does HSERSETUP reset the buffer ? Yes. The following shows W, X, <empty>, not the A, B, <empty> above -

Code:
[color=Navy]#Picaxe [/color][color=Black]18M2[/color]
[color=Navy]#Terminal 4800[/color]
[color=Blue]HSerSetup B4800_4[/color][color=Black], [/color][color=Navy]%000[/color]
[color=Blue]Do
  HserOut [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"ABCD"[/color][color=Blue])
  HSerSetup B4800_4[/color][color=Black], [/color][color=Navy]%000
  [/color][color=Blue]HserOut [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"WXYZ"[/color][color=Blue])
  Pause [/color][color=Navy]1000
  [/color][color=Blue]Do 
    [/color][color=Purple]w0 [/color][color=DarkCyan]= -[/color][color=Navy]1 
    [/color][color=Blue]HSerIn [/color][color=Purple]w0
    [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]Then
      SerTxd( [/color][color=Purple]b0[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF )
    End If
 Loop Until [/color][color=Purple]b1 [/color][color=DarkCyan]<> [/color][color=Navy]0
 [/color][color=Blue]SerTxd( [/color][color=Red]"<Empty>"[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF )
Loop[/color]
As to being able to see the bytes in the internal buffer; that's not possible, and may be impossible as the buffer is internal to the chip. Accessing that would require reading the buffer and that would lose the data one wanted to receive.
 

jims

Senior Member
Hippy...I like your description about how this works. So I copied it, printed it out and put the printout in my copy of Manual 2 so that I'll remember how this works next time I try to use it. (2 months from now I'll probably have forgotten it works).
Thank ou, Jims
 

hippy

Technical Support
Staff member
Glad it helped jims.

As to why there's a two-byte buffer ...

That is the minimum one needs to receive serial bytes sent back-to-back without losing any data. After a first byte has been fully received, another may be immediately incoming, before the user program can be told there is a first byte available and can take it out of the buffer. The second byte of buffer allows the second data byte to be received while waiting for the first to be taken out.
 

techElder

Well-known member
I guess I never really thought about it being only 2-bytes long. I always imagined a serial buffer being much longer, but then is this buffer only for HserIn?
 

Goeytex

Senior Member
The Hardware EUSART has a 2-byte FIFO receive buffer. This true for both M2 and X2 Picaxe chips as they have the same EUSART. M2 firmware does not implement a scratchpad and only moves two bytes (which must be read one at a time via 2 hserin commands) into user ram. X2 firmware quickly moves the bytes out of the FIFO via the RCREGx register into a scratchpad memory which is really a 128 or 1024 byte array setup in ram.

The PIC Datasheet explains operation of the EUSART.
 
Top