Shop OBEX P1 Docs P2 Docs Learn Events
Have spin main wait until a newcog pasm is finished. — Parallax Forums

Have spin main wait until a newcog pasm is finished.

JkaneJkane Posts: 113
edited 2014-05-24 12:31 in Propeller 1
Hello,

I have some pasm code, one main routine and about 10 subroutines, all is working fine,

in my main pasm routine i exit with

/CODE

mov ctra, #0
cogid cog_label
cogstop cog_label
CODE/

in my spin code i have:

/CODE

pst.Start(115_200)

waitcnt((clkfreq*4) + cnt)

pst.Str(String(" ",pst#NL))
pst.Str(String("Starting Head firing",pst#NL))

cog := cognew(@Head_interface,0) 'launch new cog for pasm

'do nothing while the cog is working, then continue

pst.Str(String("Stopping Head firing",pst#NL))


PUB Stop''Stops the Cog and the PID controller
if Cog
pst.Str(String("Stopping Head firing",pst#NL))
cogstop(Cog~ - 1)

/CODE

What I am trying to do is =====>

In the spin code, I want to do nothing until the cog (pasm exits), then continue

but as expected, the pst.string "stopping" displays right after the start, which makes sense

but i want the newcog to act as a subroutine, block the current cog while the other cogs executes, then continue

regards

Jeff

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-20 12:34
    attachment.php?attachmentid=78421&d=1297987572

    It would also be helpful if you posted your entire program.

    -Phil
  • JkaneJkane Posts: 113
    edited 2014-05-20 12:47
    Ok, here is the code, not pretty,but functional
    CON
      'Set up the clock mode
    '  _clkmode = xtal1 + pll16x
    '  _xinfreq = 5_000_000  '5 MHz clock * 16x PLL = 80 MHz system clock speed
       _clkmode = xtal1 + pll16x   'Set up the system clock mode  with 16x PLL
      _xinfreq = 6_250_000             '6.25 MHz clock * 16x PLL = 100 MHz system clock
    
     Frequency3 = 30_000_000    
    VAR
         long cog
    OBJ
         pst        : "Parallax Serial Terminal"
    PUB Main {launch cog }
         pst.Start(115_200)
         waitcnt((clkfreq*4) + cnt)
         pst.Str(String(" ",pst#NL))
         pst.Str(String("Starting Head firing",pst#NL))
         cog := cognew(@Head_interface,0)   'launch new cog
        
         
    PUB Stop''Stops the Cog and the PID controller
          if Cog
            pst.Str(String("Stopping Head firing",pst#NL)) 
           cogstop(Cog~ - 1)
    DAT
                  ORG 0            'Begin at Cog Ram addr 0
    Head_interface
                  mov dira, dira_mode
                  'mov outa, dira_mode
     
    '============================================ leds ===============
                   or  outa, Pin13_Power         'Turn on Power Pin
     '              or  outa, Pin15_Trap1         'Turn on trap led
    '============================================= turn on traps =====
    'Turns on trap one 
    '              andn outa, Pin12_Pin36  ' enable low  turns on traps signal 
    '              andn outa, Pin14_Pin22  ' enable low, turns on traps signal
    'Turns off trap one 
                    or outa, Pin12_Pin36  ' enable high  turns off traps signal 
                    or outa, Pin14_Pin22  ' enable high, turns off traps signal
    'Turns on trap two 
    '              andn outa, Pin16_Pin34  ' enable low  turns on traps signal 
    '              andn outa, Pin18_Pin20  ' enable low, turns on traps signal                          
    'Turns off trap two                 
                    or outa, Pin16_Pin34  ' enable high, turns off traps signal
                    or outa, Pin18_Pin20  ' enable high, turns off traps signal
     'Turns on trap three 
     '             andn outa, Pin20_Pin32  ' enable low  turns on traps signal 
     '             andn outa, Pin22_Pin18  ' enable low, turns on traps signal              
    'Turns off trap three                
                    or outa, Pin20_Pin32  ' enable high, turns off traps signal 
                    or outa, Pin22_Pin18  ' enable high, turns off traps signal
    'Turns on trap four
    '               andn outa, Pin24_Pin30  ' enable low  turns on traps signal 
    '               andn outa, Pin26_Pin16  ' enable low, turns on traps signal               
    'Turns off trap four                
                    or outa, Pin24_Pin30  ' enable high, turns off traps signal     
                    or outa, Pin26_Pin16  ' enable high, turns off traps signal             
    
    '========================================= init for tx transmission
                    
                 
                 'mov time,cnt
                 'add time, long_wait5   '5 second wait
                 'waitcnt    Time,Period200us     'wait for 5 seconds
                          
                 ' mov time, cnt
                  'add time, Period200us  ' 200 us
    '=============================================
    ' testing io pin 15 thru propeller
                 'or  outa, Pin3_Pin15         'set pin 15  high  next time is 500ns 
                 'jmpret cp_Pin15_ret, #cp_Pin15  
    '============================================
                  jmpret  run_init39_ret,#run_init39
                  jmpret  run_traps_ret,#run_traps
                  mov ctra, #0
                  cogid cog_label
                  cogstop cog_label
    run_init39
                  'xor  outa, Pin10_Pin39        'Make pin 39 high TX transmission
                 jmpret Init_P39_ret, #Init_P39
                 jmpret Init_P39_ret, #Init_P39
                 jmpret Init_P39_ret, #Init_P39
    run_init39_ret   jmp# 0 
                 
                                                        
    
    '========================================== init for data send
                  'xor  outa, Pin0_Pin37         'make pin 37 high
                  'xor  outa, Pin1_Pin38         'make pin 38 high
    '              jmpret  run_traps_ret,#run_traps
    '              cogid cog_label
    '              cogstop cog_label
    run_traps             
    '===================================================================================
    '=============================== send data subroutine ==============================
    '===================================================================================
    '================================================ turn on trap1 led and run routine
    'Turns on trap one
                   mov  Transmit_t,#0
                   add  Transmit_t,Transmit_r     
                   andn outa, Pin12_Pin36  ' enable low  turns on traps signal 
                   andn outa, Pin14_Pin22  ' enable low, turns on traps signal
    '====================================================================================
    'Turns on led and sends data, then turns off led          
                   or    outa, Pin15_Trap1         'Turn on trap led
                   jmpret  Send_data_ret,#Send_data
                   andn  outa, Pin15_Trap1        'Turn off trap led
    'now Turn off trap one 
                    or outa, Pin12_Pin36  ' enable high  turns off traps signal 
                    or outa, Pin14_Pin22  ' enable high, turns off traps signal
    '===================================================================================
    '===================================================================================
    '=================================================================================== 
    '================================================ turn on trap2 led and run routine
    'Turns on trap two
                    mov  Transmit_t,#0     
                   add  Transmit_t,Transmit_r      
                   andn outa, Pin16_Pin34  ' enable low  turns on traps signal
                   andn outa, Pin18_Pin20  ' enable low, turns on traps signal  
    '====================================================================================
    'Turns on led and sends data, then turns off led          
                   or  outa, Pin17_Trap2         'Turn on trap led
                   jmpret  Send_data_ret,#Send_data
                   andn  outa, Pin17_Trap2        'Turn off trap led
    'Turns off trap two                 
                    or outa, Pin16_Pin34  ' enable high, turns off traps signal
                    or outa, Pin18_Pin20  ' enable high, turns off traps signal
    '===================================================================================
    '===================================================================================
    '=================================================================================== 
    '================================================ turn on trap3 led and run routine
     'Turns on trap three
                    mov  Transmit_t,#0     
                   add  Transmit_t,Transmit_r          
                   andn outa, Pin20_Pin32  ' enable low  turns on traps signal 
                   andn outa, Pin22_Pin18  ' enable low, turns on traps signal   
    '====================================================================================
    'Turns on led and sends data, then turns off led          
                   or   outa, Pin19_Trap3         'Turn on trap led
                   jmpret  Send_data_ret,#Send_data
                   andn  outa, Pin19_Trap3        'Turn off trap led
    'Turns off trap three                
                    or outa, Pin20_Pin32  ' enable high, turns off traps signal 
                    or outa, Pin22_Pin18  ' enable high, turns off traps signal
    '===================================================================================
    '===================================================================================
    '=================================================================================== 
    '================================================ turn on trap4 led and run routine
    'Turns on trap four
                    mov  Transmit_t,#0    
                   add  Transmit_t,Transmit_r     
                   andn outa, Pin24_Pin30  ' enable low  turns on traps signal 
                   andn outa, Pin26_Pin16  ' enable low, turns on traps signal       
    '====================================================================================
    'Turns on led and sends data, then turns off led          
                   or  outa, Pin21_Trap4         'Turn on trap led
                   jmpret  Send_data_ret,#Send_data
                   andn  outa, Pin21_Trap4        'Turn off trap led
    'Turns off trap four                
                    or outa, Pin24_Pin30  ' enable high, turns off traps signal     
                    or outa, Pin26_Pin16  ' enable high, turns off traps signal 
    '===================================================================================
    '===================================================================================
                    andn   outa, Pin13_Power         'Turn off run light      
    run_traps_ret   jmp# 0            
                   'add  Transmit_t,Transmit_r
    '               or  outa, Pin15_Trap1         'Turn on trap led
    '               jmpret  Send_data_ret,#Send_data
    '               andn  outa, Pin15_Trap1         'Turn off trap led    
                   
    '====================================================
                  
    Send_data
                  mov time, cnt
                  add time, Pin15_20us  'zero_job
    
    '========================================== Start of data send                  
    Send_data_L
                 
                  'xor  outa, Pin3_Pin15        'toggle pin  15  next time is 20us
                  or  outa, Pin3_Pin15         'set pin 15  high  next time is 500ns    
                  waitcnt    Time,Pin37_38_500ns  'wait on pin 15 20us
                  'xor  outa, Pin3_Pin15         'toggle pin  37  next time is 500ns
                 ' or  outa, Pin3_Pin15         'set pin 15  high  next time is 500ns  
                   andn outa, Pin3_Pin15         'set pin 15 low next time is 500ns
                  'delay between pin 15 and pin 37
                  waitcnt    Time, Pin37_38_1us  'wait 1 us
                  ' waiting over turn on pin 37
                  xor  outa, Pin0_Pin37         'toggle pin  37  next time is 1 us
                  waitcnt    Time, Pin37_38_500ns  'wait 1 us
                  xor  outa, Pin0_Pin37         'turn off pin 37 and wait for ping 38 500ns
                  ' turn off pin 37 and wait 500 ns before turning on pin 38 
                  waitcnt    Time, Pin37_38_1us  'waiting for 500ns
                  'waiting is over turn on ping 38 and wait for 1 us
                  xor  outa, Pin1_Pin38         'toggle pin  37  next time is 1 us
                  waitcnt    Time, Period       'setting up for next loop
                  xor  outa, Pin1_Pin38         'now turn off pin 38 and wait for next cycle
                             
                  waitcnt    Time,Pin15_20us    'after waiting get ready to turn on pin 15 20us next loop   
    '              jmp #:Send_data
                  djnz Transmit_t,#Send_data_L
                 Send_data_ret  jmp# 0 
    '========================================================== init subroutine
    Init_P39
                  mov time,cnt
                  add time, Wait_620ms          
                  waitcnt    Time,Period200us     'wait for 5 seconds
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period105ms  
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period200us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period105ms
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period200us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period105ms 
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period640us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period210us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period320us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period540us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period280us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period105ms
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period220us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period640us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period200us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period200us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period420us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period300us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period100us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period210us
                  xor outa, Pin10_Pin39
                  waitcnt    Time,Period200us
                  xor outa, Pin10_Pin39
    Init_P39_ret  jmp# 0
                  
    cp_Pin15
                      mov dira, dira_modeIO
                    'pin 6 is output
                    'pin 5 is input 
    :loop_15
                    test inmask, ina wc
                    waitpeq Pin5_pin15I,inmask
                    muxz OUTA,#outmask
                  ' xor  OUTA,Pin6_Pin15O 
                                    
                    jmp #:loop_15
                    
    cp_Pin15_ret    jmp# 0
                    
              
     '=====================================================================             
    Pin3_Pin15     long |< 3
    Pin0_Pin37     long |< 0
    Pin1_Pin38     long |< 1
    Pin10_Pin39    long |< 10
    '=================================== data pins
    Pin12_Pin36     long |< 12
    Pin14_Pin22     long |< 14
    Pin16_Pin34     long |< 16
    Pin18_Pin20     long |< 18
    Pin20_Pin32     long |< 20
    Pin22_Pin18     long |< 22
    Pin24_Pin30     long |< 24
    Pin26_Pin16     long |< 26
    '==================================================
    '============================= led pins ===========
    Pin13_Power     long |< 13
    Pin15_Trap1     long |< 15
    Pin17_Trap2     long |< 17
    Pin19_Trap3     long |< 19
    Pin21_Trap4     long |< 21
    '===================================================
    Pin5_Pin15I    long |< 5
    Pin6_Pin15O    long |< 6
    long_wait5     long 50000000 ' 5 second wait
    Wait_620ms     long 62000000 ' 1 second wait
    tmp            long 0
    
    'Transmit_t     long %00100111_00010000 ' 10,000 cycles
    'Transmit_r     long %00100111_00010000 ' reset to 10,000
    'Transmit_t     long %10011100_01000000 ' 40,000 cycles
    Transmit_t     long %00000000_00000000 ' 00,000 cycles 
    Transmit_r     long %10011100_01000000 ' reset to 40,000 cycles   
    'Wait_620ms     long  6200000 ' 1 second wait 
    'Period        long 100000 ' 1.25  ms
    'Period        long  50000 '  500  us
    'Period        long  10000 '  100  us
    'Period        long   5000 '   60  us
    'Period        long   2500 '   28  us
    'Period        long   2300 '   24  us
    'Period15_on   long   2000 '   20  us
    'Period15_onb   long   5000 '   20  us  
    'Period15_off  long  50000 '  500  us
    'Period15_offb  long 60000 '  500  us 
    'Period15_offa long  40000 '  500  us   
    'Period37_on    long   2000 '   20  us
    'Period37_off   long   2000 '   20  us    
    'Period38_on    long   2000 '   20  us    
    'Period_38off   long   2000 '   20  us
    'Delay37_38     long   1000 '   10  us
    'Delay37_38_1     long   100 '   1  us
    'Delay37_38_2     long   50 '   500ns
    'zero_job      long    10
    '====================================== init cycles ==============
    Period100us     long  10000 '  100  us
    Period200us     long  20000 '  200  us
    Period205us     long  20500 '  205  us
    Period105ms     long  104000  ' 1.05 ms
    Period150ms     long  150000  ' 2  ms 
    Period640us     long  64000  '  640  us
    Period220us     long  22000  '  220  us
    Period210us     long  21000  '  210  us 
    Period280us     long  28000  '  280  us
    Period300us     long  30000  '  300  us
    Period320us     long  32000  '  300  us 
    Period510us     long  51000  '  510  us
    Period540us     long  54000   ' 520 us
    Period104ms     long  100000  ' 1.04 ms
    Period420us     long  42000   ' 420  us
    '======================================= delays ==================
    Period          long  50000 '  500  us   
    Pin15_20us      long   2000    ' 20  us
    Pin37_38_500ns  long   50      ' 500ns
    Pin37_38_1us    long   100     ' us
    Pin15_next_C    long   50000   ' 500us
    '=================================================================
    dira_mode     long  %00001111_11111111_11111111_11111111
    
    dira_modeIO   long   %00000000_00000000_11111111_11011111
    test_modeIO   long  %0010_0000
    inmask        long  %0010_0000
    outmask       long  %0100_0000 
    
    outa_mode     long  %00000000_00000000_00000000_00000000
    outa_P3_1     long  %00000000_00000000_00000000_00001000
    outa_P3_0     long  %00000000_00000000_00000000_00000000
    outa_P0_1     long  %00000000_00000000_00000000_00000001
    outa_P0_0     long  %00000000_00000000_00000000_00000000
    outa_P1_1     long  %00000000_00000000_00000000_00000010
    outa_P1_0     long  %00000000_00000000_00000000_00000000  
    Time          res 1
    cog_label     res  1
    
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-20 13:09
    The first thing that comes to mind:
    cog := cognew(@Head_interface,0)

    should read
    cog := cognew(@Head_interface,0) + 1

    -Phil
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-05-20 15:32
    At the moment you're not passing anything in the par register. What I would do (have done), is pass the hub address of a variable that you can use as a flag. Have your Spin code set the flag to a non-zero value (true) just before calling your PASM routine.
    flag := true
      cog := cognew(@head_interface, @flag) + 1
    
      ' wait for cog to stop
    
      repeat
      while (flag == true)
    


    Before you have the cog commit hari-kari, write zero to this variable
    pasm_exit               mov     r1, #0
                            wrlong  r1, par                 ' alert Spin cog
    
                            cogid   r1                      ' get this cog id
                            cogstop r1                      ' stop it
    
                            jmp     #$
    


    The only thing the cog variable is telling you is that you did in fact launch the cog -- you don't need this to shut-down as that's being done in PASM.
  • JkaneJkane Posts: 113
    edited 2014-05-20 20:42
    Jon,

    thanks,

    makes sense, I'll set it up tomorrow,

    regards

    Jeff
  • JkaneJkane Posts: 113
    edited 2014-05-21 07:12
    JonnyMac wrote: »
    At the moment you're not passing anything in the par register. What I would do (have done), is pass the hub address of a variable that you can use as a flag. Have your Spin code set the flag to a non-zero value (true) just before calling your PASM routine.
    flag := true
      cog := cognew(@head_interface, @flag) + 1
    
      ' let cog start
      
      waitcnt(cnt + (clkfreq / 1000))
    
      ' wait for cog to stop
    
      repeat
      while (flag == true)
    


    Before you have the cog commit hari-kari, write zero to this variable
    pasm_exit               mov     r1, #0
                            wrlong  r1, par                 ' flag Spin cog
    
                            cogid   r1                      ' get this cog id
                            cogstop r1                      ' stop it
    
                            jmp     #$
    


    The only thing the cog variable is telling you is that you did in fact launch the cog -- you don't need this to shut-down as that's being done in PASM.
  • JkaneJkane Posts: 113
    edited 2014-05-21 07:12
    Jon,

    works very well, thanks

    Jeff
  • __red____red__ Posts: 470
    edited 2014-05-24 06:08
    Could you use an io pin as a semaphore so the spin cog could sleep until pin state change as a power optimization?
  • kuronekokuroneko Posts: 3,623
    edited 2014-05-24 06:36
    __red__ wrote: »
    Could you use an io pin as a semaphore so the spin cog could sleep until pin state change as a power optimization?
    If a pin is available, sure.
  • __red____red__ Posts: 470
    edited 2014-05-24 10:08
    Could you use one of those mythical portb pins?
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-05-24 12:31
    I don't think so -- the registers are implemented but not connected to physical circuitry. This little test fails:
    var
    
      long  stack[16]
    
    
    pub main 
    
      term.start(RX1, TX1, %0000, 115_200)
      waitcnt(cnt + (clkfreq >> 3))       
    
      cognew(toggle_portb, @stack)
    
      repeat
        term.tx("0" + inb[16])
        waitcnt(cnt + (clkfreq >> 3))
       
    
    pri toggle_portb
    
      dirb[16] := 1
    
      repeat
        !outb[16]
        waitcnt(cnt + (clkfreq >> 1))
    


    Yes, I did try it with the A port and it works fine.
Sign In or Register to comment.