Starting & Stopping a New Cog, clearing the stack ...
JMLStamp2p
Posts: 259
Good Morning,
I want to start a new cog to run the Recieve Method in my program and need help understanding a few things. I want the new Cog to monitor Pin "1"·in a constant loop where I have a tranciever in-putting Serial data. I understand that I need to be concerned about Stack space but un-clear how to code. Once I recieve a byte of data and compare that byte to "$01" in my PUB Recieve Method I want to call a·Transmit Method to output some data.
Other Questions:
1) Does rxbyte "located in FullDuplexSerials PUB rx Method " hold the Hex·· byte recieved and pass that byte back to my calling routine so that It can be compared ?
NOTE: See Recieve·Method below.
2) Once I call the Stop Method does this clear the Stack space that the "Recieve" Cog uses ?
........................................................................
My Code Below:
........................................................................
CON
·_CLKMODE = XTAL1 + PLL1X
·_CLKFREQ = 5_000_000·········
.........................................................................
OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"
..........................................................................
VAR
·long Stack [noparse][[/noparse]9]
·byte rxbyte
...........................................................................
PUB MAIN
·dira[noparse][[/noparse]0]~~
·dira[noparse][[/noparse]17]~~
·dira[noparse][[/noparse]1]~
·data.start(1,0,2,9600)
·Stop
·Success := (Cog := cognew(Recieve(Pin), @Stack) + 1)
·Recieve
............................................................................
PUB Stop
··if Cog
·· cogstop(Cog~ - 1)
·Main
............................................................................
PUB Recieve
·data.rx············
·· If rxbyte :=··$01
·····Transmit
·· Recieve
................................................................................................
PUB Transmit
·data.tx ($22)
·!outa[noparse][[/noparse]17]
·Stop····················· 'Calls the Stop Method to stop the "Recieve" Cog.
.................................................................................................
Any suggestions on how to condense this code would be appreciated.
Regards.
John.
·
I want to start a new cog to run the Recieve Method in my program and need help understanding a few things. I want the new Cog to monitor Pin "1"·in a constant loop where I have a tranciever in-putting Serial data. I understand that I need to be concerned about Stack space but un-clear how to code. Once I recieve a byte of data and compare that byte to "$01" in my PUB Recieve Method I want to call a·Transmit Method to output some data.
Other Questions:
1) Does rxbyte "located in FullDuplexSerials PUB rx Method " hold the Hex·· byte recieved and pass that byte back to my calling routine so that It can be compared ?
NOTE: See Recieve·Method below.
2) Once I call the Stop Method does this clear the Stack space that the "Recieve" Cog uses ?
........................................................................
My Code Below:
........................................................................
CON
·_CLKMODE = XTAL1 + PLL1X
·_CLKFREQ = 5_000_000·········
.........................................................................
OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"
..........................................................................
VAR
·long Stack [noparse][[/noparse]9]
·byte rxbyte
...........................................................................
PUB MAIN
·dira[noparse][[/noparse]0]~~
·dira[noparse][[/noparse]17]~~
·dira[noparse][[/noparse]1]~
·data.start(1,0,2,9600)
·Stop
·Success := (Cog := cognew(Recieve(Pin), @Stack) + 1)
·Recieve
............................................................................
PUB Stop
··if Cog
·· cogstop(Cog~ - 1)
·Main
............................................................................
PUB Recieve
·data.rx············
·· If rxbyte :=··$01
·····Transmit
·· Recieve
................................................................................................
PUB Transmit
·data.tx ($22)
·!outa[noparse][[/noparse]17]
·Stop····················· 'Calls the Stop Method to stop the "Recieve" Cog.
.................................................................................................
Any suggestions on how to condense this code would be appreciated.
Regards.
John.
·
Comments
Realized it would have caused an unwanted loop.
PUB Stop
if Cog
cogstop(Cog~ - 1)
1) There is no way for your program to refer to the variables in FullDuplexSerial. You can reference constants declared in FullDuplexSerial and you can call methods in FullDuplexSerial. You could, if you needed this, add your own method to FullDuplexSerial that simply returns the address of a variable and then access that variable by using the address.
2) When you do a COGSTOP on a cog that you've started, that cog gets completely reset. It's not executing instructions. The counters are not running. The video generator is turned off. It's drawing minimal power. The space that it may have been using for a stack is no longer associated with the cog and that space contains the data that was in use when the cog was stopped.
....................................................
PUB rx : rxbyte
'' Receive byte (may wait for byte)
'' returns $00..$FF
· repeat while (rxbyte := rxcheck) < 0
.....................................................
Does rxbyte not hold the byte that was recieved, I guess I'm confused about how we referance the value recieved. Could you please give me a sample code to accomplish this. If you don't mind, change the code below in my Recieve Method the way that it should be.
Thank You !
........................................
My Code Below:
.........................................
{{ Run Program.Spin }}
.........................................
CON
·_CLKMODE = XTAL1 + PLL16X
·_CLKFREQ = 5_000_000
OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"
·GO: "Sloss Timer Program"
PUB Main
·GO.Start
·GO.Recieve
...............................................
{{ Sloss Timer Program.Spin }}
...............................................
OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"
..................................
PUB Start
·dira[noparse][[/noparse]0]~~
·dira[noparse][[/noparse]1]~
·dira[noparse][[/noparse]17]~~
·data.start(1,0,2,9600)
............................................................................................
Note: When I try and compile this program it Highlights the opening parenthesis before rxbyte and tell me that an "end of line is expected" Could you please explain this.
.............................................................................................
PUB Recieve
·· data.rx(rxbyte)············
·· If rxbyte :=· $01··········
···· Transmit
......................
PUB Transmit
·data.tx($05)
.....................
John.
When you say that it returns the next char. from the buffer, how do I access it from within my program ? Could you give me a sample bit of code that would accomplish this.
Thanks,
John.
Thank You so much for your help. Everything is working fine now, I will post the working code this afternoon as I would like to get your opinion on the way the rest code is structured.
Thanks again,
JMLStamp2p