Shop OBEX P1 Docs P2 Docs Learn Events
Hub exec - the basics [solved] — Parallax Forums

Hub exec - the basics [solved]

wmosscropwmosscrop Posts: 409
edited 2020-03-29 02:14 in Propeller 2
I'm just trying to get my head around hub exec. To me the following code should work (both led's lit on a P2 eval board):
PUB Start()
  coginit(32 + 16, @init_hub, 0) 
  repeat

DAT
			org	0	' COG		   
init_cog
			drvl	#56
cog_loop		jmp	#cog_loop

DAT	                orgh	$400   	' HUB

init_hub
			drvl	#57
			jmp	#init_cog
But only the led on pin 57 lights.

I'm obviously missing something.

Edit:
I'm using Pnut, v 34o.

Added the repeat to the Start() method. No change in results.

Comments

  • RaymanRayman Posts: 14,646
    Is this Fastspin or Spin2?

    I think your start routine needs to either end in infinite loop or shut down the cog.
    Otherwise it may be doing random stuff...
  • He does have a repeat right after the coginit, so that should keep the first cog from shutting down.
  • RaymanRayman Posts: 14,646
    I see he added that...

    I think what you'd need to do is copy that cog code into the Spin2 cog.
    Right now, it's just in HUB memory...
  • RaymanRayman Posts: 14,646
    I'd try adding something along these lines to the Start routine, before the coginit:
    Use SETQ+RDLONG to read multiple hub longs into cog register RAM:
    
        SETQ    #x                'x = number of longs, minus 1, to read
        RDLONG  first_reg,S/#/PTRx        'read x+1 longs starting at first_reg
    
  • evanhevanh Posts: 15,916
    It looks like he's wanting the first DAT block to be inlined with the REPEAT. Chip's spin2 doc appears to be missing how to do that but there is an example with the compiler binary.
    pub go() | x
    
      repeat
    
        org
    	getrnd	wc
    	rcl	x,#1
        end
    
        pinwrite(56 addpins 7, x)
        waitms(100)
    
  • The first DAT directive ends the spin2 code block.
    The only way init_cog can execute is with another coginit or as evanh suggested use inline assembly.
  • RaymanRayman Posts: 14,646
    Oh, that's right I remember now...

    For Spin2, inline assembly somehow copies that code to lower cog ram and runs it.
  • My intent was to have both cog and hub execution in the same cog. Hub would be mainly for initialization and shutdown routines, with cog for the main routines (since I plan on using XBYTE). This needs to be a separate cog from the spin code, which must continue execution.

    So I would want the hub to execute first, loading the cog ram if needed, and then branching into the cog. Otherwise I don't see any way to copy the code into the cog before starting it. Right?

    How do I find the address of the cog's data? I've tried @ & @@. So far all I've done is made it worse.

    I have searched for an example of mixed hub/cog execution and can't find any. I think there must be some out there I just can't find it.
  • evanhevanh Posts: 15,916
    Ah, COGINIT launches a different cog altogether. So you've got two cogs running, one is running the main spin2 program and the other is running hubexec pasm2 as per your COGINIT.

  • roglohrogloh Posts: 5,787
    edited 2020-03-29 00:08
    wmosscrop wrote: »
    How do I find the address of the cog's data? I've tried @ & @@. So far all I've done is made it worse.
    The way I do it, in FastSpin, is to declare a label with ORGH before the ORG'd code. Like this... you can then use "cogcode" label (with @ if appropriate) to identify where in hub RAM your PASM code is located. Whether this works or not in the official Spin2 or if you need some other method I'm not sure as I don't run Windows, but I'd hope it would work the same there too.
    DAT
    			orgh 			   
    cogcode
    			org	0	' COG		   
    init_cog
    			drvl	#56
    cog_loop		jmp	#cog_loop
    
    DAT	                orgh	$400   	' HUB
    
    init_hub
    			drvl	#57
    			jmp	#init_cog
    
  • The way I do it, in FastSpin, is to declare a label with ORGH before the ORG'd code. Like this... you can then use "cogcode" label (with @ if appropriate) to identify where in hub RAM your PASM code is located. Whether this works or not in the official Spin2 or if you need some other method I'm not sure as I don't run Windows, but I'd hope it would work the same there too.
    DAT
    orgh
    cogcode
    org 0 ' COG
    init_cog
    drvl #56
    cog_loop jmp #cog_loop

    DAT orgh $400 ' HUB

    init_hub
    drvl #57
    jmp #init_cog

    I tried it PNut and I see 1 LED light up .
  • roglohrogloh Posts: 5,787
    edited 2020-03-29 00:39
    The example I gave was only how to use a label to identify the HUB address position of some PASM code within it (the separate quoted question). It was not a way to solve the original posted problem.
  • RaymanRayman Posts: 14,646
    edited 2020-03-29 00:41
    I guess you could try jumping into the hubexec code from inside the inline assembly.

    And then jump back, of course...
  • Try this
    PUB Start()
      coginit(32 + 16, @init_hub, @init_cog) 
      repeat
    
    DAT			
    			org	0	' COG		   
    init_cog
    			drvl	#56
    cog_loop		jmp	#cog_loop
    
    DAT	                orgh	$400   	' HUB
    
    init_hub
    			drvl	#57
    			setq	#16
    			rdlong	0,ptra
    			jmp	#init_cog
    
  • @ozpropdev
    Try this
    PUB Start()
    coginit(32 + 16, @init_hub, @init_cog)
    repeat

    DAT
    org 0 ' COG
    init_cog
    drvl #56
    cog_loop jmp #cog_loop

    DAT orgh $400 ' HUB

    init_hub
    drvl #57
    setq #16
    rdlong 0,ptra
    jmp #init_cog

    That works :)

  • AribaAriba Posts: 2,690
    Or that:
    PUB Start()
      coginit(16, @init_cog, @init_hub) 
      repeat
    
    DAT
    			org	0	' COG		   
    init_cog
    			jmp	ptra
    			drvl	#56
    cog_loop		jmp	#cog_loop
    
    DAT	                orgh	$400   	' HUB
    
    init_hub
    			drvl	#57
    			jmp	#init_cog+1
    
    It lets coginit load the cog and starts in cog-mode, but then jumps immediatly to hubexec
  • Ariba wrote: »
    It lets coginit load the cog and starts in cog-mode, but then jumps immediatly to hubexec
    Simple! :)

  • ozpropdev wrote: »
    Ariba wrote: »
    It lets coginit load the cog and starts in cog-mode, but then jumps immediatly to hubexec
    Simple! :)

    Yes, and it works for me! Thanks everyone for your input. There's so much power here, and so many new levers to push and pull to get to that power. It's knowing the lever positions that's key.

    And there really are a lot of levers. Smart pins especially. I originally was disappointed due to the omission of the P1's counters but now see how much better it is on the P2.

    Again, thanks!
Sign In or Register to comment.