Shop OBEX P1 Docs P2 Docs Learn Events
Serial Object for P2 — Parallax Forums

Serial Object for P2

Can someone point me to a good serial in / out object using the P2 with smart pins?

Thanks in advance!

Comments

  • Thanks!

  • Here's a really simple serial object that uses smart pins. It only provides basic tx and rx methods. For formatting use SEND together with a formatting object like ers_fmt.

  • pik33pik33 Posts: 2,366
    edited 2021-03-08 14:10

    There can be a problem if the sender sends too fast. In this case a circular buffer and a dedicated cog is needed.

    The simplest spin solution I used for communication with a RPi Zero:

    • a circular buffer and a stack:
    long serialbuf[32]
    long serialtail,serialfront
    long serialstack[64]
    long rr
    
    

    A serial cog functions:

    pub serialcog()|q
    
    serialfront:=0
    serialtail:=0
    
    repeat
      if serialfront<>((serialtail-1) //32)
        q:=rxcheck()
        if q>=0
         serialbuf[serialfront]:=q
         serialfront+=1
         serialfront:=serialfront // 32
    
    pub serialread():r
    
    if serialfront<>serialtail
      r:=serialbuf[serialtail]
      serialtail:=(serialtail+1)//32
    else
      r:=-1
    return r 
    
    

    and init a cog:

    cogspin(16,serialcog(),@serialstack)

    Now use serialread and if the result is >-1 then you have received a byte. No more bytes lost while a spin code processes the previous byte.

  • Yes, if the baud rate is high and your Spin code is busy then you have to be careful about reading data, but that's true even if you have a buffer (it just delays the point at which overflow will occur). The smart pin itself acts as a 1 character buffer. which is adequate for many purposes. @evanh posted a clever method for using the smartpin to buffer 3 characters by setting it in 28 bit mode instead of 8 bit mode, which is even better (but depends on the sent bytes being continuous).

    If you're going to use a cog for serial then you might as well use a multi-port serial object like @Cluso99 's.

  • Dave HeinDave Hein Posts: 6,347
    edited 2021-03-09 01:32

    Has anybody done a serial driver that uses a smart pin with an interrupt service routine to handle the buffering? I'm assuming it's possible to get an interrupt when the transmit register is empty, or the receive register is full, is that correct?

  • evanhevanh Posts: 15,910
    edited 2021-03-09 02:01

    @"Dave Hein" said:
    Has anybody done a serial driver that uses a smart pin with an interrupt service routine to handle the buffering? I'm assuming it's possible to get an interrupt when the transmit register is empty, or the receive register is full, is that correct?

    There's likely a reluctance for this, it comes with some caveats. eg: Can't use long WAITXs or REPs in the same cog.

  • I wrote a 2-port serial drive fitting completely in a COG and uses buffer in the LUT, needs some 12 HUB longs for init and then uses 8 of them longs for mailboxes.

    if i remember correctly @RossH uses it in catalina and needed to fix something.

    https://forums.parallax.com/discussion/169660/cogserial-fullduplex-smart-serial-using-interrupt#latest

    and

    https://forums.parallax.com/discussion/170101/fullduplexserial2-spin-for-use-with-fastspin-and-the-p2#latest

    Mike

  • @evanh said:

    @"Dave Hein" said:
    Has anybody done a serial driver that uses a smart pin with an interrupt service routine to handle the buffering? I'm assuming it's possible to get an interrupt when the transmit register is empty, or the receive register is full, is that correct?

    There's likely a reluctance for this, it comes with some caveats. eg: Can't use long WAITXs or REPs in the same cog.

    Yes, I understand the issue with long latency instructions and interrupts. However, it would be nice to have the option to do this. If I don't have any free cogs it would be useful to have a buffered serial port using interrupts and a smart pin. Maybe the compilers could have a low-latency option which would avoid using WAITX and REP. I recall some discussion about this.

  • evanhevanh Posts: 15,910

    I personally have only been using rx for getting key presses in my hardware testing. I haven't needed significant buffering.

  • RaymanRayman Posts: 14,632
    edited 2021-03-09 19:27

    I'm using SmartSerial right now with FlexSpin C and works great.

    Only thing I might change is to allow not using either TX or RX pin by feeding a negative pin# value to start().

  • @Rayman ,

    With the serial driver I wrote that is possible since I needed a driver for Parallax Laser Ping and TFMINI Lidar.

    Mike

  • RaymanRayman Posts: 14,632

    @iseries Where is it? Maybe I'll take a look...

Sign In or Register to comment.