Shop OBEX P1 Docs P2 Docs Learn Events
clkfreq not the same inside cognew? — Parallax Forums

clkfreq not the same inside cognew?

bunnibunni Posts: 38
edited 2013-07-02 09:20 in Propeller 1
I've spent a good chunk of time trying to track this down with no avail, this is my first time even trying spin. Basically, I am trying to run a method in a cog, when I do the clkfreq variable seems to be screwing everything up. Basically, this works:
CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000

PUB init
  cognew(ledon(clkfreq), stack)

PUB ledon(rate)
  dira[23] := %1
  repeat
    !outa[23]
    waitcnt(rate + cnt)

While this doesnt:
CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000

PUB init
  cognew(ledon, stack)

PUB ledon
  dira[23] := %1
  repeat
    !outa[23]
    waitcnt(clkfreq + cnt)

The former code toggles the LED at .5hz, the latter is about .01hz blink.

Can anyone explain to me what I am missing here? According to the manual clkfreq is a global read only variable, while I believe the cog has to wait for the hub to access this variable, it should in no way impact the waitcnt method.

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-07-01 22:52
    Where is your stack declaration? Assuming you have some kind of long stack[n] bit somewhere you should use @stack{0} in cognew calls as using just stack evaluates to 0 (default VAR setting, address 0 is clkfreq location).
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2013-07-01 23:04
    I wondered the same thing as Kuroneko but I can see that since you have passed the value rather than the address you have effectively passed an address of zero as this is the variables initial value. So your ledon cog uses it's stack at address 0 and clobbers clkfreq (which resides at address 0) which ledon is trying to also read. What a mess. You need to pass a pointer using the @ operator.

    Did you RTM (Propeller Manual V1.2, p79)?
    Screenshot from 2013-07-02 16:03:58.jpg
  • bunnibunni Posts: 38
    edited 2013-07-01 23:18
    Ahhh Smile.

    First of all, yes, I do have a stack declaration, a stupidly huge 500 longs:
    VAR
      long  stack[500]
    

    I just accidentally chopped it out when formatting my code here for paste.

    Second, ahh Smile again. Thats what I get for trying to learn a new language after 8pm. Everything magically works now, who would have thought?! Thanks for the help guys. Can we please delete this thread to hide my blatant stupidity? :D
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2013-07-01 23:35
    bunni wrote: »
    Ahhh Smile.

    First of all, yes, I do have a stack declaration, a stupidly huge 500 longs:
    VAR
      long  stack[500]
    

    I just accidentally chopped it out when formatting my code here for paste.

    Second, ahh Smile again. Thats what I get for trying to learn a new language after 8pm. Everything magically works now, who would have thought?! Thanks for the help guys. Can we please delete this thread to hide my blatant stupidity? :D

    Delete it??? Where do you think we get our free entertainment from? 500 longs for a stack, who'd have thunk it!
    BTW, after 8pm is always a good time to learn a new language, I don't warm up until around 11pm myself.
  • SapiehaSapieha Posts: 2,964
    edited 2013-07-01 23:47
    Hi bunni.

    How You think Others can learn from Yours mistake's if You delete this thread?


    bunni wrote: »
    Ahhh Smile.

    First of all, yes, I do have a stack declaration, a stupidly huge 500 longs:
    VAR
      long  stack[500]
    

    I just accidentally chopped it out when formatting my code here for paste.

    Second, ahh Smile again. Thats what I get for trying to learn a new language after 8pm. Everything magically works now, who would have thought?! Thanks for the help guys. Can we please delete this thread to hide my blatant stupidity? :D
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-07-02 07:23
    I agree, if you made this mistake, many other would do the same.

    Can you post your working code, so we can learn from your result? The kids will likely encounter this same issue in an upcoming project.
  • bunnibunni Posts: 38
    edited 2013-07-02 09:20
    I agree, if you made this mistake, many other would do the same.
    Sapieha wrote: »
    How You think Others can learn from Yours mistake's if You delete this thread?

    I was just joking ;)


    As requested:
    CON
            _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
            _xinfreq = 5_000_000
    
    VAR
      long stack[5]
    
    PUB init
      cognew(ledon, @stack)
    
    PUB ledon
      dira[23] := %1
      repeat
        !outa[23]
        waitcnt(clkfreq + cnt)
    
Sign In or Register to comment.