Shop OBEX P1 Docs P2 Docs Learn Events
COG "ownership" of I/O PINS — Parallax Forums

COG "ownership" of I/O PINS

DavidMDavidM Posts: 630
edited 2011-11-30 17:07 in Propeller 1
HI everyone,

I haven't posted for ages, but I can tell you I have been extremly busy for the whole year! ( and I do read this forum everyday to keep in touch)

I have a issue with accessing I/O pins and COGS that use them.

I have a main loop ( cog 0 )and that main loop needs to set ( Initialize) the direction & the state of a I/O pin, in this case an OUTPUT & LOW

During programm execution, this same pin need to be change to HIGH but within a NEW COG, then when the cog is finished , the main loop ( therefore the first COG ) will control this pin. ( I am pretty sure that never a cog wants to act upon the same pin at at the same rime, i.e they take turns)

For some reason my second cog seems to not make any differnece to the state of the PIN.

I thought I have solved this issue years ago! as I have done a similar thing before.

What I normally do I in my NEW COG routine is to "re-initialize" the direction using..

DIRA[MyPin] := Output ( so that this cog "owns" the pins direction) So I believe.

I then set the state ( hi or low ) as needed. but this does not apper to be working

Can any one shed some light on where I am going wrong?

Thanks


Dave M

Comments

  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-11-30 02:02
    The outputs of all cogs are OR'd together so if any cog makes a pin an output and high, the physical output will be high. The key is to remember to have a cog return the pin to low so that another pin can make it high.

    Based on your description you shouldn't be having problems. And FWIW, here's a little demo
    con
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000                                          ' use 5MHz crystal
    
      CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq
      MS_001   = CLK_FREQ / 1_000
      US_001   = CLK_FREQ / 1_000_000
    
    
    con
    
      LED3 = 18
      LED2 = 17
      LED1 = 16
    
    
    var
    
      long  selection 
    
      long  stack1[32]
      long  stack2[32]
      long  stack3[32]
    
    
    pub main 
    
      cognew(light(1, LED1), @stack1)
      cognew(light(2, LED2), @stack2)
      cognew(light(3, LED3), @stack3)
    
      repeat
        selection := ++selection & %11
        pause(150)  
        
    
    pub pause(ms) | t
    
    '' Delay program ms milliseconds
    
      t := cnt - 1088                                               ' sync with system counter
      repeat (ms #> 0)                                              ' delay must be > 0
        waitcnt(t += MS_001)
        
    
    pub high(pin)
    
    '' Makes pin output and high
    
      outa[pin] := 1
      dira[pin] := 1
    
    
    pub low(pin)
    
    '' Makes pin output and low
    
      outa[pin] := 0
      dira[pin] := 1
    
    
    pub toggle(pin)
    
    '' Toggles pin state
    
      !outa[pin]
      dira[pin] := 1
    
    
    pub input(pin)
    
    '' Makes pin input and returns current state
    
      dira[pin] := 0
    
      return ina[pin]
    
    
    con
    
    pri light(myflag, myled)
    
      repeat
        if (selection == myflag)
          high(myled)
        else
          low(myled)
    
  • DavidMDavidM Posts: 630
    edited 2011-11-30 02:13
    HI JonnyMac

    Thanks for replying

    So If COG 0 starts with..

    DIRA[MyPin1] := Output and OUTA[MyPin1] := High,

    Then COG 1 does..

    DIRA[MyPin1] := Output and OUTA[MyPin1] := High,

    Will the result of MyPin1 be HIGH?, AND Do I need to Declare "DIRA[MyPin1] := Output" again in the second cog??

    OR

    If COG 0 says..

    DIRA[MyPin1] := Output and OUTA[MyPin1] := High,

    Then COG 1 says..

    DIRA[MyPin1] := Output and OUTA[MyPin1] := Low,

    will the result of MyPin1 be Low ?? or do I have to get COG0 to make MyPin1 := LOW, before letting COG1 take control??


    Thanks
    Dave M
  • JLockeJLocke Posts: 354
    edited 2011-11-30 03:25
    If any of the cogs set the pin to HIGH, then the pin state will be HIGH, no matter how many other cogs try to set it to LOW. Only if ALL cogs are setting the pin LOW (or ignoring the pin entirely) will the output be LOW. So, if you set the pin to a HIGH state in a cog, when you are done you must return the pin to LOW if you want another cog to be able to set it HIGH again. All cog outputs are OR'd (as JohnnyMac says). If ANY cog sets the pin to HIGH, then it will be HIGH.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-30 06:03
    Addition to answer from JLocke to answer your DIRA-question:
    Yes, each COG has to set the output-pins in DIRA because each COG has it's own DIRA-register.
  • DavidMDavidM Posts: 630
    edited 2011-11-30 16:33
    Hi,

    Thanks for the replies well explained!,

    I can see know where i was going wrong, I have discovered that I don't actually need to control this particular pin from the second cog, I can set it just before the cog starts, and reset it back when the cog finishes.

    But sometime this type of control of a pin need to be done,

    Thanks

    Dave M
  • PublisonPublison Posts: 12,366
    edited 2011-11-30 17:07
Sign In or Register to comment.