A few questions about SPIN functions
Dizzy
Posts: 9
Hello,
I have a project where I need a few cogs to be polling something independantly. My question centers around the fullduplexserial object and a lot of the other objects included in general. I thought originally that an object that starts a new cog would have all that methods from that object loaded into that cog. It seems more like when you declare that you're using an object in the main program it loads the functions into the memory of the main cog. The reason this troubles me is because I need the serial cog to be constantly polling the serial lines for input but when I put an endless loop in one of the functions of the body for the serial object it locks up the main program. I noticed within the object that when you call fullduplexserial.start() it actually starts a cog pointing to an area of assembly that looks as though it runs its own endless loop. My question is can I get that assembly that starts in a cog to call a function in the object body? How can I make the cog that runs for the serial poll using the function I write as well as go about its usual business?
Also:
I read "global" variables everywhere. Is there anyway for me to declare a variable in an Object and make it truly global so that any other cog or object can access it without passing it?
thanks,
Diz
I have a project where I need a few cogs to be polling something independantly. My question centers around the fullduplexserial object and a lot of the other objects included in general. I thought originally that an object that starts a new cog would have all that methods from that object loaded into that cog. It seems more like when you declare that you're using an object in the main program it loads the functions into the memory of the main cog. The reason this troubles me is because I need the serial cog to be constantly polling the serial lines for input but when I put an endless loop in one of the functions of the body for the serial object it locks up the main program. I noticed within the object that when you call fullduplexserial.start() it actually starts a cog pointing to an area of assembly that looks as though it runs its own endless loop. My question is can I get that assembly that starts in a cog to call a function in the object body? How can I make the cog that runs for the serial poll using the function I write as well as go about its usual business?
Also:
I read "global" variables everywhere. Is there anyway for me to declare a variable in an Object and make it truly global so that any other cog or object can access it without passing it?
thanks,
Diz
Comments
2) An object and a cog are two separate things. You can have objects that all run in a single cog and you can have a single object that forks off several processors to do the same or different things. Most typically, an object will start a cog to handle the part of its function that runs in parallel with the "main" processor and the object supplies a set of interface methods that run on the processor of the caller and communicate with the processor (cog) that's running in parallel. As you noticed, the most common way this is done is with an assembly program that runs in another cog and sits there waiting to be told what to do, then does it, and waits again. FullDuplexSerial works that way as do most I/O drivers.
If you need a serial receiver that does something more complex than say FullDuplexSerial, like looks for a particular serial input pattern, you can either modify FullDuplexSerial to do it or take the Simple_Serial driver and modify it to run independently in its own cog and do the pattern match as part of its functioning.
You can't call a Spin routine from assembly language, partly because the whole cog memory is normally occupied by the Spin interpreter and that would replace the assembly program that "calls" it so no return is possible.
So, all you have to do is pass the address of the variable in question to the objects.· Or, you could use a predetermined value.· I believe some programs use the last few bytes of RAM for this...
·· I made an
Instead of passing the variables through the function calls, you can pass a pointer to the variable location(@X)...So you can have the function continually looping and checking the address of the variable and updating if it changes.
I believe the following would work for you:
Object 1 (Start):
**************************************
OBJ
· XY : "XY_OUTPUT"
VAR
···Long X_VALUE
PUB MAIN
·· XY.XY_OUTPUT(@X_VALUE)·· '''''NOTICE THE "@"?
·· REPEAT
***************************************
Object 2 (XY):
PUB XY_OUTPUT(POINTER)
···REPEAT
······ 'Code to output·what is located at·pointer...or the address of the variable
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔