Shop OBEX P1 Docs P2 Docs Learn Events
PASM 101 - Manipulation I/O Pins — Parallax Forums

PASM 101 - Manipulation I/O Pins

MJHanaganMJHanagan Posts: 189
edited 2014-01-29 11:31 in Propeller 1
About a year ago I bumped up against the speed limitations in SPIN when I needed to make some high speed ADC measurements using the MCP3202. That limitation forced me into learning how to program in PASM. My only exposure to assembly lanuguage was back in the late 1970's when I took an "Introduction to Computers" class in my senior year of high school. From that experience I gained a quick appreciation for higher level languages like FORTRAN and thought I would never ventured back to assembly. Ugh, I'm back!

Learning PASM has been a lot more difficult than leaning SPIN, mostly because it is a lower level language. At the assembly programming level the commands available do only limited things. The Propeller Manual lists each PASM command in a style reminiscent of those thick orange binders holding the VAX Operating Language. If you already have a working understanding of the language they were a great resource. If you were a beginner they were virtually useless.

There are not many resources available to the early learners of PASM. I did read potatohead's and desilva's short treatises on PASM but these did not prepare me sufficiently for writing my own code for the MCP3202 chip. I stared at several PASM programs specific to the MCP320x IC but these were difficult to understand mostly because they were quite compact/efficient and the commenting was generally limited and geared to the PASM savvy programmer.

The main challenge to using the MCP320x IC's is negotiating the SPI communication protocol using three I/O pins on the Propeller. Along the way I struggled to developed the necessary understanding of how to manipulate these I/O pins using PASM commands. I collected notes from various OBEX code examples and forum postings and assembled a short text summarizing the key I/O concepts and PASM commands.

I attached my notes below hoping that some of the PASM gurus out there would review them for accuracy and completeness.

I look forward to your feedback and comments.

Attached here is the latest version of the notes:
PASM 101 - Manipulating Input and Output Pins V1.1.pdf
«1

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-01-12 15:07
    Wow, this is fantastic!

    You're just three years too late. I could have really used this when I was starting with PASM.

    I'm far from a PASM guru but I'll continue to read through the document. My initial perusal didn't find anything that looked wrong.

    This paper would have saved be from having to translate the Propeller Manual's PASM examples into binary.

    I also found PASM a lot harder to understand than Spin.

    One of the sources I did find for PASM instruction was JonnyMac's Spin Zone articles. I finally had things start to click when working through one of Jon's examples of controlling a bi-color LED.

    Thanks for posting this. I'll add a link to this thread in my "How to Program the Propeller" section of my index.

    Edit: I don't see your name anywhere in the document. I think it would be good to let people know who wrote the paper in case it gets posted elsewhere on the internet.
  • cavelambcavelamb Posts: 720
    edited 2014-01-12 15:41
    I've been diggin into it and I agree with Duane - fantastic work.
    Thanks.
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2014-01-12 20:29
    From my perspective, it is jam-packed full of useful and well laid out information.


    Thanks

    Bill M.
  • ErlendErlend Posts: 612
    edited 2014-01-13 04:17
    As a guy who has always struggled to get a grip on assembly - and is struggleling to learn PASM - this tutorial is just perfect. I only wish you would go on to write the whole book. Your style of writing/teaching is excellent - reading it really makes me eager to try out some more PASM right away.
    Thanks!

    Erlend
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-01-13 04:31
    Duane Degn wrote: »
    Wow, this is fantastic!

    You're just three years too late. I could have really used this when I was starting with PASM.

    I'm far from a PASM guru but I'll continue to read through the document. My initial perusal didn't find anything that looked wrong.

    This paper would have saved be from having to translate the Propeller Manual's PASM examples into binary.

    I also found PASM a lot harder to understand than Spin.

    One of the sources I did find for PASM instruction was JonnyMac's Spin Zone articles. I finally had things start to click when working through one of Jon's examples of controlling a bi-color LED.

    Thanks for posting this. I'll add a link to this thread in my "How to Program the Propeller" section of my index.

    Edit: I don't see your name anywhere in the document. I think it would be good to let people know who wrote the paper in case it gets posted elsewhere on the internet.

    Not too late for some. There are always new users and this is likely to apply to PASM on the Propeller TWO as well as The ONE.
  • T ChapT Chap Posts: 4,223
    edited 2014-01-13 04:33
    Agreed, I have been putting off learning PASM and this is a nice tool. This should be linked somewhere for future readers instead of fading away with the hope of a chance search finding it.
  • RS_JimRS_Jim Posts: 1,766
    edited 2014-01-13 05:47
    I love your style! I agree with previous poster who suggested you write the whole book!Jim
  • MJHanaganMJHanagan Posts: 189
    edited 2014-01-13 08:47
    Duane Degn wrote: »

    You're just three years too late. I could have really used this when I was starting with PASM.

    Sorry about the timing, but I'm late for just about everything in my life!

    I forgot to mention JonnyMac's name. I looked through several of his OBEX programs written in PASM and found them to be helpful and generally enlightening.

    I would wait a bit before posting a link to this document since it is in the unvetted state. If/when the PASM gurus give it a good look over and I get the inevitable errors fixed and the formatting adjusted then you are welcome to post the revision or link to it.

    Let me know what errors you find or if you have suggestions on formatting/style and what sections need better clarification.
  • MJHanaganMJHanagan Posts: 189
    edited 2014-01-13 09:11
    Erlend wrote: »
    I only wish you would go on to write the whole book.

    Well, I am not a big fan of writing and my grasp of PASM is very limited so authoring a book is not likely. But thank you for the flattering suggestion!

    I did start another section regarding the various ways of reading and writing data to main RAM. It seems like there are a few ways to get this done. I am still in the learning mode on this topic so it will take some time to pull it together.

    I would like to see Parallax write a PASM 101 book. I bet if someone at Parallax provided an outline and a basic style guide they could get a bunch of the forum contributors to each take a section and get the bulk of the writing done. Parallax would only need to assemble these section writings and apply a dose of editing for style and continuity and it will be done in a fairly short period of time. A nice way to share the burden of writing such a tome. Perhaps Parallax would consider giving each contributor a token store credit for their contribution.

    Who would we get to do the accompanying UTube video lecture series? :lol:
  • Mark_TMark_T Posts: 1,981
    edited 2014-01-13 12:44
    Nice document.

    I'm a bit worried by the use of ON/OFF though - to me this is ambiguous - if an LED/resistor is connected to a pin
    and to ground, that language is perfectly reasonable.

    But if an LED/resistor is connected to 3.3V rail instead of ground, ON means LED off, OFF means LED on!

    Furthermore OFF might mean the pin is in the high-Z state, ON means its actively driving as an output.

    This is why the words HIGH and LOW are so commonly used for I/O pins since they are completely unambigous,
    whether or not negative logic or open-drain or whatever is being used.

    Typo:
    The DIRA and OUTA for each cog are ORed together and then these eight results are then ORed together to ultimately determine which output pins are high and low
    The DIRA and OUTA registers are ANDed together, then the eight results are ORed.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-01-13 13:48
    I forgot to mention JonnyMac's name. I looked through several of his OBEX programs written in PASM and found them to be helpful and generally enlightening.

    That's very kind of you to say.

    And well-done getting more information out. I try to do things in my objects and my column using practical projects, as I've found that tends to resonate with readers and get concepts to stick. That said, writing (well) is darned difficult, and it's impossible to make everybody happy with that writing. Some months ago the editor at Nuts & Volts phoned me because a reader wanted to speak directly with me (after some drunk dials, my telephone number is no longer included in my contact information). I took his number and called him. He was grateful for he call, and then proceeded to lay into me about writing above his head. The actor in me wanted to to tell him where to stuff his cell phone, but I hung in and helped him with a simple trick -- a technique he claimed to have spent two years searching for.

    Should you continue and do project-oriented material, you may want to show and explain different ways to accomplish the same task. For example, in my latest IR object I manipulate the DIRA bit for the pin instead of the OUTA bit. This allows the code to work with either circuit configuration (pin to anode, or pin to cathode). Manipulating the DIRA bit is useful in half-duplex communications (serial, 1-wire, I2C, etc) where the idle state of the line is handled with a pull-up. This was the trick that I shared with that caller and once he understood, he became far more friendly.
  • tonyp12tonyp12 Posts: 1,951
    edited 2014-01-13 13:58
    >The DIRA and OUTA registers are ANDed together, then the eight results are ORed.
    It's confusing for beginners to use those terms.
    1: Each cog have its own DIRA & OUTA registers.
    2: A write to the cogs OUTA only affect the pins that have that specific cogs DIRA also set.
    3: Sharing the same pin between cogs/Spin:
    If any cog (including Spin) have that pin set as DIRA=1 and also set as high with OUTA=1 it will be a high.
    even if 7 cogs have it set as a low and one have it set as high, it will stay high.
    4: Reading a cogs OUTA register will not reveal the true electrical state of the pin, but INA will.
  • MJHanaganMJHanagan Posts: 189
    edited 2014-01-13 16:14
    JonnyMac wrote: »
    That's very kind of you to say.

    And well-done getting more information out. I try to do things in my objects and my column using practical projects, as I've found that tends to resonate with readers and get concepts to stick. That said, writing (well) is darned difficult, and it's impossible to make everybody happy with that writing. Some months ago the editor at Nuts & Volts phoned me because a reader wanted to speak directly with me (after some drunk dials, my telephone number is no longer included in my contact information). I took his number and called him. He was grateful for he call, and then proceeded to lay into me about writing above his head. The actor in me wanted to to tell him where to stuff his cell phone, but I hung in and helped him with a simple trick -- a technique he claimed to have spent two years searching for.

    Should you continue and do project-oriented material, you may want to show and explain different ways to accomplish the same task. For example, in my latest IR object I manipulate the DIRA bit for the pin instead of the OUTA bit. This allows the code to work with either circuit configuration (pin to anode, or pin to cathode). Manipulating the DIRA bit is useful in half-duplex communications (serial, 1-wire, I2C, etc) where the idle state of the line is handled with a pull-up. This was the trick that I shared with that caller and once he understood, he became far more friendly.

    I agree that project based articles work well when trying to teach the basic programming concepts. I was originally thinking of writing this based on my experience with the MCP3202 but that would have been a much greater effort. I wanted to get this little snippet written and out there in hopes it might help a few struggling beginners like myself. If I ever get around to finishing the RAM read/write section then I might attempt writing something that brings together these two concepts. A likely project candidate is the ubiquitous MCP320x series of ADC's. Fast SPI communication, bit shifting, pin direction altering, RAM read/write, it seem to encompass most of the things PASM does well and fast. Maybe... someday... :nerd:
  • MJHanaganMJHanagan Posts: 189
    edited 2014-01-13 16:19
    tonyp12 wrote: »
    >The DIRA and OUTA registers are ANDed together, then the eight results are ORed.
    It's confusing for beginners to use those terms.
    1: Each cog have its own DIRA & OUTA registers.
    2: A write to the cogs OUTA only affect the pins that have that specific cogs DIRA also set.
    3: Sharing the same pin between cogs/Spin:
    If any cog (including Spin) have that pin set as DIRA=1 and also set as high with OUTA=1 it will be a high.
    even if 7 cogs have it set as a low and one have it set as high, it will stay high.
    4: Reading a cogs OUTA register will not reveal the true electrical state of the pin, but INA will.

    I like your explanation, much clearer and succinct. OK if I incorporate it into the primer?
  • Heater.Heater. Posts: 21,230
    edited 2014-01-13 16:35
    MJHanagan,

    I'm sure you should do that. Don't forget to reference the architecture diagrams in the Propeller manual that spell this out in detail diagrammatically.

    These details of how logic works, AND, OR, NOT etc, and the symbols used to describe it should be part of any assembler language primer. it clears up a lot of confusion.
  • MJHanaganMJHanagan Posts: 189
    edited 2014-01-13 16:43
    Mark_T wrote: »
    Nice document.

    I'm a bit worried by the use of ON/OFF though - to me this is ambiguous - if an LED/resistor is connected to a pin
    and to ground, that language is perfectly reasonable.

    But if an LED/resistor is connected to 3.3V rail instead of ground, ON means LED off, OFF means LED on!

    Furthermore OFF might mean the pin is in the high-Z state, ON means its actively driving as an output.

    This is why the words HIGH and LOW are so commonly used for I/O pins since they are completely unambigous,
    whether or not negative logic or open-drain or whatever is being used.

    Typo:

    The DIRA and OUTA registers are ANDed together, then the eight results are ORed.

    Thank you for catching the ANDed (not ORed) error - I will correct it in the text.

    You make a good point with the potential confusion with output high/on low/off. I will need to address this too. Thank you.
  • tonyp12tonyp12 Posts: 1,951
    edited 2014-01-13 19:19
    >I like your explanation, much clearer and succinct. OK if I incorporate it into the primer?
    Sure, one day I would like to make a web flash version so people can click on different bits and see the result.
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2014-01-14 21:34
    I was a bit confused with this after reading it the first time, and I am still uncertain.
    PASM 101 wrote:
    If any active cog sets a pin to be an output it supersedes any input setting in all other active cogs. However,
    the contents of the other cog's DIRAs are not altered, only the final ORed DIRA for the chip is affected.

    Is this controlled by rule #3?
    1) A pin is an input only if no active cog sets it to an output.

    2) A pin outputs low only if all active cogs that set it to output also set it to low.

    3) A pin outputs high if any active cog sets it to an output and also sets it high.


    The Propeller manual states:
    It is up to the application developer to ensure that no two cogs try to use the same I/O pin for conflicting
    purposes during run-time.
    PASM 101 wrote:
    a pin to be an output it supersedes any input setting in all other active cogs

    So basically that means that in order to reuse a pin as an input, the last cog to use the pin must shut down (rule #1?), or declare it
    an input before the original cog can reuse it as an input again (rule #1?)


    Thanks
  • tonyp12tonyp12 Posts: 1,951
    edited 2014-01-15 11:28
    If any cog have set its DIRA for that pin as an output, it will connect that pin to Vcc or Gnd (which one is by OUTA selection)
    If this pin was only meant to be a input-pin this would create a override of the input state, or maybe even a electrical short if the load to the pin does not have series resistance of at least 500+ Ohm
    You can always read INA from any cog, even if that cog is DIRA set as output.
    Setting DIRA from one cog, does not make it an output from another cog as OUTA is first ANDed with that cogs DIRA before this cogs output stage.
  • MJHanaganMJHanagan Posts: 189
    edited 2014-01-15 16:40
    Seems odd there is no built-in final/uber read-only DIRA register that reflects the ORed result of all 8 DIRA registers. Perhaps in the upcoming Prop 2 chip??
  • prof_brainoprof_braino Posts: 4,313
    edited 2014-01-16 08:14
    So basically that means that in order to reuse a pin as an input, the last cog to use the pin must shut down (rule #1?), or declare it
    an input before the original cog can reuse it as an input again (rule #1?)

    N00b question - In what case would you reuse a pin? Once its soldered into a circuit, its usually hooked up to a specific part that is an input or an output. Is this a case of "can, but doesn't usually happen"?
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-01-16 09:52
    Usage is rare, but there are circuits that allow a button input and LED output on the same pin.
  • MJHanaganMJHanagan Posts: 189
    edited 2014-01-16 17:13
    The other common reason to switch from input to output is when using one Propeller pin to do double duty for SPI data in and data out on an IC device.
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2014-01-17 22:45
    N00b question - In what case would you reuse a pin? Once its soldered into a circuit, its usually hooked up to a specific part that is an input or an output. Is this a case of "can, but doesn't usually happen"?

    I was thinking about ADC chips that have have Data-In and Data-Out wired together, and
    used by two cogs.



    Bill M.
  • skylightskylight Posts: 1,915
    edited 2014-01-18 13:39
    I've not foraged into pasm yet but found the document vey informative, one thing I noticed and perhaps I have it all wrong but in the first table is ANDN the equivalent of NAND ie ANDNOT?
    if so shouldn't the results of ANDN be opposites to the AND results ie 1=0 and 0=1?
  • kuronekokuroneko Posts: 3,623
    edited 2014-01-18 15:41
    skylight wrote: »
    ... but in the first table is ANDN the equivalent of NAND ie ANDNOT?
    if so shouldn't the results of ANDN be opposites to the AND results ie 1=0 and 0=1?
    ANDN and NAND are two different beasts, i.e. the former is A&!B while the latter is !(A&B).
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2014-01-18 16:35
    Tonyp12's explanation really helped me with ANDN
    tonyp12 wrote: »
    ANDN inverts the Source mask before it ANDs the destination,
    if you want to clear a specific bit (you don't care if current state of it's a 0 or 1)

    If your AND mask is all 1s except the bit you want to go zero then all the 1 ones in destination
    will stay 1 as 1&1 =1 and a 0 &1 =0 so zeros will stay 0

    The bit location that is a 0 in the mask (after inversion) will result in a forced 0 as 1&0=0 and 0&0=0
  • skylightskylight Posts: 1,915
    edited 2014-01-18 18:24
    Not quite getting it, I understand if A=1 and B=1 that A&!B =0 because B was inverted but what if B was originally =0 does the inversion create a 1? Therefore 1+1 =1 not a cleared bit but a set bit?

    Sorry for my ignorance but it is probably clear to most here,myself it's been a long time since I messed with logic

    Thank you both for your help.
  • tonyp12tonyp12 Posts: 1,951
    edited 2014-01-18 18:34
    1 & 1 = 1
    0 & 1 = 0
    1 & 0 = 0
    0 & 0 = 0

    The N in ANDN change (inverts) the value in left column,
    you can use same mask for a pin both with OR and ANDN this way.

    AND and ANDN can not set a bit, it can only keep an existing bit a 1 or clear it.
  • skylightskylight Posts: 1,915
    edited 2014-01-18 19:24
    So if i'm understanding this correctly the right column is affected by the mask? therefore the only true result "1&1" (first line) would be cleared to a zero hence ANDN always clears if the mask is zero?
Sign In or Register to comment.