cognew is NOT!
RS_Jim
Posts: 1,768
Ok, so I have an object that calls another and the both function correctly. When I put the second in a new cog it doesent. The hardware is a Demo board'. Leds are to trace where the code gets to. So where does the train come off the track?
CON _clkmode = xtal1 + pll16x ' use crystal x 16 _xinfreq = 5_000_000 CS = 16 ''CS: Clear Screen CE = 11 ''CE: Clear to End of line CB = 12 ''CB: Clear lines Below HM = 1 ''HM: HoMe cursor PC = 2 ''PC: Position Cursor in x,y PX = 14 ''PX: Position cursor in X PY = 15 ''PY: Position cursor in Y NL = 13 ''NL: New Line LF = 10 ''LF: Line Feed ML = 3 ''ML: Move cursor Left MR = 4 ''MR: Move cursor Right MU = 5 ''MU: Move cursor Up MD = 6 ''MD: Move cursor Down TB = 9 ''TB: TaB BS = 8 ''BS: BackSpace BP = 7 ''BP: BeeP speaker rainpin = 7 Var long Stack1[50] OBJ debug : "Parallax Serial Terminal" rain : "raingage021513a" delay : "timing" num : "simple_numbers" Pub Main | tmp debug.start(115200) 'cognew (testpulse,@stack1) tmp:=rain.Start(rainpin) ifnot tmp >0 debug.char(NL) debug.str(string("oops")) debug.char(HM) debug.str(string( " Bucket Tips ")) repeat tmp := rain.rainamt debug.CHAR(PX) debug.CHAR(14) 'debug.CHAR("%") debug.dec(tmp) Pub testpulse outa [7] ~~ dira [7] ~~ repeat outa[7]~ delay.pause1ms(1) outa [7] ~~ delay.pause1ms(1000)The obj with the cognew
Con fallingedge = %01110 << 26 risingedge = %01010 << 26 calfact = 2111 VAR long tipscount, cntrpin, testval, cntrpinmask,cog,rainstack[10] OBJ delay : "timing" Pub Start (datapin) Stop cog := cognew(getrain (datapin),@rainstack)+1 outa[18]~~ dira[18]~~ delay.pause1ms(500) outa{18}~ result := cog Pub getrain(datapin)|cntrpin_ outa [16] ~~ dira [16.17] ~~ delay.pause1ms(1000) ' wait one second outa [16] ~ cntrpin_ := datapin cntrpinmask := |< cntrpin_ dira[cntrpinmask] ~ ctra:= fallingedge + cntrpin frqa := calfact phsa := 0 PUB Stop if cog > -1 cogstop(cog -1) 'Stop previously launched cog Pub rainamt : amount outa[17]~~ delay.pause1ms(250) outa[17]~ amount := phsa return amount Pub resetrainamt phsa := 0Jim
Comments
Do you know the timing-object? In general there should be no problem in using the same object in different COGs at exactly the same time! That's one benefit of objects and each object causing trouble in this usecase either needs to have a good reason and proper ducumentation of this exception or I would call it a bad object-design!
@RS_Jim:
kuroneko already explained what is wrong with this object (it immediately stops running which also stops the counter), but there is another bug which might survive for years until it causes trouble! You don't reset the cog-variable in the stop-function and you check for the wrong range!
cognew returns values from 0-7 if a COG has been started and -1 if no COG has been started. Because of the + 1 the value in cog has a range from 0 to 8 (0=no COG started, 1-8=COGID)
Now compare this with your stop-function:
It should be "if cog>0" or even shorter "if cog"
And you should set the variable cog to 0 after doing the cogstop! Otherwise calling stop might stop other COGs which is not intended!
Actually your code tries to stop COG 7, as the variable cog is initialized with 0 when starting up the propeller. 0 > -1 is true and 0 - 1 = -1 = %11111111_11111111_11111111_11111111 which is used by cogstop just like a 7 = %111.
Never got from start to certain so I moved it to just after start to see if that would work. The object does work, just not with the cognew.
Jim
Part of the goal here is to learn how to get multiple cogs running and get predictable results from each. Some of the cogs will be pasm and I am confident a can read the results and report them back from there, but this simple task did not seem to merit asm.
Jim
If you want to share counter data from different cogs have them run some loop which occasionally samples and (optionally) resets phsx then stores it in a central hub location.