Shop OBEX P1 Docs P2 Docs Learn Events
Multiple Cogs accessing I/O pins — Parallax Forums

Multiple Cogs accessing I/O pins

hacktorioushacktorious Posts: 72
edited 2013-12-31 17:17 in Propeller 1
I'm writing a C program on a prop and am trying to figure out the best way to use multiple cogs.
I was attempting to access an IO pin in a second cog via cogstart.

In main I set a pin as an input and all is well, I can access it in main. However, if I have another method being started with cogstart I cannot create an input on a different I/O and access it. It seems I have to create the input in main as a global then access it from the other cog. Does this sound correct?

Here is a hybrid pseudo code example:

void some_method(void);

main {

static volatile int input_x;
execute cogstart for some_method here

while(1){
input_x= input(22);
}

}

void some_method(void){
while(1){

if input_x is on do something

}
}

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-12-31 09:26
    While I don't use C on the Prop (yet) I bet it's safe to assume the same rules of "or"ed pins applies.

    In general I make it a practice to only use an I/O pin from a single cog. I have the other cogs set flags to indicate if an I/O pin should be changed so the cog "in charge" of that particular I/O pin can make the needed changes.

    BTW, if you edit your post and choose "Go Advanced" it will let you edit the subject.

    While you can edit the subject, you can't edit which forum the post is in. This thread was posted to the wrong forum but only moderators can move threads.
  • hacktorioushacktorious Posts: 72
    edited 2013-12-31 09:32
    Duane Degn wrote: »
    While I don't use C on the Prop (yet) I bet it's safe to assume the same rules of "or"ed pins applies.

    In general I make it a practice to only use an I/O pin from a single cog. I have the other cogs set flags to indicate if an I/O pin should be changed so the cog "in charge" of that particular I/O pin can make the needed changes.

    BTW, if you edit your post and choose "Go Advanced" it will let you edit the subject.

    While you can edit the subject, you can't edit which forum the post is in. This thread was posted to the wrong forum but only moderators can move threads.

    Thanks. Your right, its in the wrong forum. Sorry.

    Only one cog with a flag sounds like a good idea.
  • PublisonPublison Posts: 12,366
    edited 2013-12-31 09:38
    Thread mover to Prop 1 forum
  • hacktorioushacktorious Posts: 72
    edited 2013-12-31 15:24
    jazzed wrote: »

    Unfortunately this was not helpful. DIRA, INA and OUTA do absolutely nothing when attempting to use cogstart. Maybe I'm doing something wrong, but compared it to the spin stuff and it doesn't seem to work. Here is what I tried:
    main
    cogstart

    some_method()
    DIRA |= 0 << 21; //set to input
    while(1){
    if(INA |= (1 << 21))
    //Do something....
    }

    Is there a better example which uses an INA somewhere? I cannot seem to find much. Thanks.
  • kuronekokuroneko Posts: 3,623
    edited 2013-12-31 15:54
    Don't you mean:
    some_method()
    DIRA [COLOR="#FF0000"]&= ~(1[/COLOR] << 21); //set to input
    while(1){
      if(INA [COLOR="#FF0000"]&[/COLOR] (1 << 21))
        //Do something....
    }
    
  • hacktorioushacktorious Posts: 72
    edited 2013-12-31 16:15
    kuroneko wrote: »
    Don't you mean:
    some_method()
    DIRA [COLOR="#FF0000"]&= ~(1[/COLOR] << 21); //set to input
    while(1){
      if(INA [COLOR="#FF0000"]&[/COLOR] (1 << 21))
        //Do something....
    }
    

    Yes, that may be it. Thanks.

    What is the difference between using the above or just the input(21) method? Input seems a lot cleaner.
  • jazzedjazzed Posts: 11,803
    edited 2013-12-31 16:30
    Yes, that may be it. Thanks.

    What is the difference between using the above or just the input(21) method? Input seems a lot cleaner.
    You trade ease of use for performance (and in this case, input sets the pin to an input). Function calls are expensive.
    unsigned int input(int pin)                   // input function definition
    {
      int mask = 1 << pin;                        // Set up mask
      DIRA &= ~mask;                              // AND DIRA with NOT mask
      return (INA & mask) >> pin;                 // Return input state
    }
    
    Reference: https://code.google.com/p/propsideworkspace/source/browse/Learn/Simple Libraries/Utility/libsimpletools/source/input.c
  • hacktorioushacktorious Posts: 72
    edited 2013-12-31 17:17
    jazzed wrote: »
    You trade ease of use for performance (and in this case, input sets the pin to an input). Function calls are expensive.
    unsigned int input(int pin)                   // input function definition
    {
      int mask = 1 << pin;                        // Set up mask
      DIRA &= ~mask;                              // AND DIRA with NOT mask
      return (INA & mask) >> pin;                 // Return input state
    }
    
    Reference: https://code.google.com/p/propsideworkspace/source/browse/Learn/Simple Libraries/Utility/libsimpletools/source/input.c

    Gotcha... Thanks. Nice signature by the way! :)
Sign In or Register to comment.