Shop OBEX P1 Docs P2 Docs Learn Events
slightly faster Simple_Serial — Parallax Forums

slightly faster Simple_Serial

lonesocklonesock Posts: 917
edited 2009-08-05 04:53 in Propeller 1
Hi, All.

OK, this won't be of too much use to most people, but I noticed that the simple_serial.spin code wasn't overly efficient in the actual sending and receiving of bits. I re-worked it a little, saved a few longs, but most importantly on an 80 MHz system, the speed limit used to be 19200 bps. With the reworked version I can do 31250 bps (but not 38400) on the same system.

Here are the relevant bits:
PUB rx: rxByte | t
{{ Receive a byte; blocks caller until byte received. }}

  if rxOkay
    dira[noparse][[/noparse]sin]~                                          ' make rx pin an input
    t := bitTime >> 1                                   ' 1/2 a bit
    waitpeq(inverted & |< sin, |< sin, 0)               ' wait for start bit
    t += cnt                                            ' sync + 1/2 bit
    repeat 8
      waitcnt(t += bitTime)                             ' wait for middle of bit
      rxByte += ina[noparse][[/noparse]sin] + rxByte                       ' sample bit 
    waitcnt(t + bitTime)                                ' allow for stop bit 

    rxByte := (rxByte >< 8) ^ inverted                  ' adjust for mode and strip off high bits


PUB tx(txByte) | t
{{ Transmit a byte; blocks caller until byte transmitted. }}

  if txOkay
    txByte := ((txByte | $100) << 2) ^ inverted         ' add stop bit, set mode
    outa[noparse][[/noparse]sout] := !inverted                             ' set idle state
    t := cnt                                            ' sync    
    dira[noparse][[/noparse]sout]~~                                        ' make tx pin an output 
    repeat 10                                           ' start + eight data bits + stop
      waitcnt(t += bitTime)                             ' wait bit time
      outa[noparse][[/noparse]sout] := (txByte >>= 1)                      ' output bit (true mode)
    dira[noparse][[/noparse]sout] := sout <> sin                           ' release to pull-up/pull-down




Jonathan

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.

Comments

  • jazzedjazzed Posts: 11,803
    edited 2009-08-05 04:06
    Good work Jonathan. I've never seen Simple_Serial.spin reliably work at 19200. Maybe this one will.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • lonesocklonesock Posts: 917
    edited 2009-08-05 04:53
    jazzed said...
    Good work Jonathan. I've never seen Simple_Serial.spin reliably work at 19200. Maybe this one will.
    Thanks. I am definitely interested in knowing if this code isn't portable...I was planning on using it in a size-limited application, and if it isn't robust I'll definitely want to check it out on the scope.

    thanks,
    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.
Sign In or Register to comment.