Shop OBEX P1 Docs P2 Docs Learn Events
Simple Serial for Spin2 with PNut (Updated 15 MAR 2020) — Parallax Forums

Simple Serial for Spin2 with PNut (Updated 15 MAR 2020)

JonnyMacJonnyMac Posts: 9,104
edited 2020-03-15 21:40 in Propeller 2
I am very late to the P2 party, so there may be code that does this, but I couldn't find it. I wanted to do simple serial debugging of Spin2 code compiled by PNut so I cobbled this together from code by Eric Smith and Chip. It uses the core of Eric's SmartSerial (sans autobaud), with formatting routines by Chip and me. Fixed and variable width output for decimal, hex, octal, and binary are supported.

So far everything seems to work, and yet, I'm sure that even this simple code can be improved. When you make fixes/improvements, please share.

Edits:
-- fixed octal formatting
-- added quarternary formatting
-- added formatted strings
-- added demo program
-- fixed bug with fpdec()
-- added generic formatted number output
-- decimal and string fields can now be left- or right-padded
-- updated jm_nstrings library to unify int-to-string conversion with itoa()
-- last update 15 MAR 2020 @ 2:45pm PDT


Notes (discovered moving code from P1 to P2):
-- The Spin1 => and =< operators now match other languages (>= and <=).
-- The Spin2 encod operator works differently than its P1 namesake, |<.
-- The || operator in Spin1 has to be changed to abs in Spin2.

28 FEB 2020
GOOD NEWS! Chip found the issues with if/elseif/else and everything in my format() method for strings is now working as intended. You can, for example, do this:
  x := 15
  term.fstr5(string("%3d    %3x    %3o    %3q    %4b\r"), x, x, x, x, x)
...which gives you this:
 15      F     17     33    1111

Comments

  • evanhevanh Posts: 15,916
    Most snippets up till now have been in Pasm rather than Spin.
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-02-16 20:31
    I seem to be having trouble re-posting the code
    Problem fixed.
  • PublisonPublison Posts: 12,366
    edited 2020-02-16 19:10
    JonnyMac wrote: »
    I seem to be having trouble re-posting the code

    Ahh, I see attachments are missing. Will summon the web gurus.

    EDIT: I attached your original file as test.
  • JonnyMac wrote: »
    I seem to be having trouble re-posting the code

    Should be working now, Jon.
  • Should be working now, Jon.
    It is, thanks. Latest version of object and a small demo program are attached to the original post.
  • I loaded the demo file into PNut_V33L and received the following error.
    PNut
    Access violation at address 00461E54 in module 'PNut_v33L.exe'. Read of address 02793000.
    OK
    ??
  • simple_serial.spin2 compiled and loaded to the P2.
  • RaymanRayman Posts: 14,646
    edited 2020-02-20 16:59
    It's nice that we no longer need to dedicate a cog to do serial interfacing...

    I guess the one drawback here is that there is no big receive buffer, right?
    But, I'm usually doing a lot more transmitting that receiving... So, maybe doesn't matter to me...

    Still, this may be a case where using interrupts may be useful in certain cases...
    How many bytes can come in before you lose data if you don't check on receive buffer?
  • JonnyMac,

    Latest flexgui (fastspin compiler v4.1.3) fails to build the demo:
    "~/flexgui/bin/fastspin" -2b -l -O1 -I "~/flexgui/include"  "~/Documents/P2ESCode/jm_simple_serial/jm_simple_serial_demo.spin2"
    Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2020 Total Spectrum Software Inc.
    Version 4.1.3 Compiled on: Feb 20 2020
    ~/Documents/P2ESCode/jm_simple_serial/simple_serial.spin2:286: error: syntax error, unexpected ABS (||)
    jm_simple_serial_demo.spin2
    |-simple_serial.spin2
    child process exited abnormally
    Finished at Thu Feb 20 09:13:17 2020
    
    It does not like: ||=
    if (nlzflag ||= (digit := value / divisor // 10) or (divisor == 1))
          tx("0" + digit + n1flag*(divisor == 1))
    
    PNut_v34i, compiles without issue.

    An issue for fastspin?

    dgately
  • I wrote it for PNut, but being plain Spin2, it work, no? I didn't test it with FlexGUI, just PNut.
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-02-20 19:29
    I guess the one drawback here is that there is no big receive buffer, right?
    True.
    How many bytes can come in before you lose data if you don't check on receive buffer?
    I don't know. I'm very late to the P2 and wanted something for output to PST so I could begin experimenting. I may be the only person on the forums that doesn't use a VGA monitor for Propeller output. <grin>
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-02-20 19:30
    It does not like: ||=
    if (nlzflag ||= (digit := value / divisor // 10) or (divisor == 1))
          tx("0" + digit + n1flag*(divisor == 1))
    
    PNut_v34i, compiles without issue.
    || (logical or) and ||= are new to Spin2 -- perhaps Eric will adjust his compiler for them.

    That said, the segment is a mouthful of code -- typical of Chip's style <grin>

    I suppose it could be simplified to
      digit := value / divisor // 10
      nlzflag := nlzflag or ((digit) or (divisor == 1))
      if (nlzflag)
        tx("0" + digit + n1flag*(divisor == 1))
    

  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-02-22 08:12
    I replaced the guts of the dec() method with a call to this code, but there are other problems and my library is still not compatible with FlexGUI. (darn)
    pub itoa(value, base) : p_str | sign, d
    
    '' Convert value to string
    '' -- base is desired format
    '' -- returns pointer to string
    
      if (value == 0)
        nbuf[0] := "0"
        nbuf[1] := 0
        return @nbuf
    
      bytefill(@nbuf, 0, 33) 
      p_str := @nbuf + 31
    
      if ((value < 0) and (base == 10))
        sign := 1
        value := -value
      else
        sign := 0
    
      repeat
        d := value // base
        byte[--p_str] := (d < 10) ? "0" + d : "A" + (d - 10)
        value /= base
      while (value)
    
      if (sign)
        byte[--p_str] := "-"
    
  • cgraceycgracey Posts: 14,155
    Does it execute in Spin2 okay?
  • Yes. I wrote it so that I had something to do nice output in PNut (using PST). If you have a second, and could add a F12 hook to PST in PNut, I'd be grateful -- I robotically hit F10, then F12 when testing.
  • JonnyMac,
    Eric does intend to make Fastspin/FlexGui able to compile the official Spin2 syntax. It'll just take some time and Chip's documentation of Spin2's full syntax.
  • You could try this one, it is FullDuplexSerial for two ports with buffers in the LUT. Needs a COG...

    Does mimic FastSpin's serial STD but also has async handling.

    Have not tried it with spin2,works with FastSpin.

    Mike
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-02-28 23:14
    Good news! Chip found a bug in the Spin2 compiler that was wreaking havoc in my .format() method -- everything is now working as desired.
  • Found and fixed a bug today. Updated n_strings with itoa() method.
Sign In or Register to comment.