Shop OBEX P1 Docs P2 Docs Learn Events
How to call one cog from another — Parallax Forums

How to call one cog from another

mynet43mynet43 Posts: 644
edited 2010-09-06 10:32 in Propeller 1
I have an assembly language routine that needs to call another assembly language routine in another cog.

Can you tell me how this can be done?

In the main program I have:
OBJ
adc : "MCP3208_fast" ' Read ADC Ports
xmotor: "Motor Controller-001" ' Stepper Motor Control Routine
ymotor: "Motor Controller-001" ' Stepper Motor Control Routine

The assembly code in the motor routine needs to call the adc input routine in the adc module.

If it were called from the main program, the call would be:
limit_1 := adc.in(4) ' detect limit switch setting

I'm trying to make an equivalent assembly language call from the motor code.

Thank you for your help.

Jim

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-04 10:47
    There is no way for assembly code to "call" Spin code or for one cog to call code in another cog. The cogs have to cooperate by sharing hub memory.

    The ADC object works by having its assembly routine communicate with the Spin code through a shared long in main memory. Look at the do_cmd and in methods in the ADC object for details. Your motor control routine can be passed the address of the shared long and it can do the same things as the do_cmd and the in methods in the ADC object. If you're using the ADC object for other things, you will also have to use semaphores (LOCKxxx) to make sure that the cogs using the ADC object's assembly cog share the shared long nicely.
  • mynet43mynet43 Posts: 644
    edited 2010-09-04 11:00
    Thanks Mike,

    That's what I was afraid of. I was hoping...

    For what I'm doing, I think the easiest way is to copy the adc routine to the motor module. The adc code I'm using isn't Chip's continuous loop, it's just a call with a port input and a value output, so I think it should work.

    The motor routine is the only thing running when it's told to move the motor, so I don't think I'll need the semaphore.

    Thanks again for the great help.

    Jim
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-09-06 06:13
    Mynet43, you can't call the routine from one cog to another in the manner that Mike described, but you can copy it and turn it on and off. Just copy the code from cog0 for example, into any cog of your choice and either by switching or by parameter passing, to decide what parts to run. The code can contain PASM or Spin code.

    Humanoido
  • mynet43mynet43 Posts: 644
    edited 2010-09-06 09:26
    I have two instances of the same routine running at the same time, to control two stepper motors independently. I think I've solved the problem by copying the adc code into the module and creating a common busy flag to avoid accessing the adc hardware from two cogs at the same time.

    See attachment. Question: is there a way to insert code in the new forum?

    Thanks for the feedback.

    Jim
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-09-06 09:46
    mynet43 wrote: »
    I have two instances of the same routine running at the same time, to control two stepper motors independently. I think I've solved the problem by copying the adc code into the module and creating a common busy flag to avoid accessing the adc hardware from two cogs at the same time. See attachment. Question: is there a way to insert code in the new forum? Thanks for the feedback. Jim
    Jim, yes you can insert code into a Forum post by simple copy and paste from the Propeller Software Tool, and surround it with the word code in brackets at the top and slash code in brackets at the bottom, like this:
    '************************************
    go_get_adc              rdlong  adc_busy,adc_busy_adr wz' see if other instance is using adc routine
                  if_nz     jmp     #go_get_adc             ' if busy, wait until cleared
                            wrlong  one,adc_busy_adr        ' if clear, set it, to reserve the adc routine
                            call    #get_adc                ' get adc value of stepper limit switch
                            wrlong  zero,adc_busy_adr       ' clear the busy lock so other instance can use it
    go_get_adc_ret          ret
    
    Humanoido
  • mynet43mynet43 Posts: 644
    edited 2010-09-06 10:01
    Thanks,

    I tried to do it from memory and got the format wrong...
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-09-06 10:21
    mynet43 wrote: »
    Thanks, I tried to do it from memory and got the format wrong...
    Take a look at this photo showing how.

    Humanoido

    attachment.php?attachmentid=73038&d=1283793678
    673 x 358 - 27K
    30.jpg 27.4K
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-09-06 10:26
    You can also use the test forum for trying out the different formatting commands in your posts.

    http://forums.parallax.com/forumdisplay.php?f=45

    Humanoido
  • mynet43mynet43 Posts: 644
    edited 2010-09-06 10:32
    No problem, I know how to do it. I just typed it wrong.

    I was used to using the buttons in the old forum...

    Thanks
Sign In or Register to comment.