two cogs reading mutiple variables.
Dgswaner
Posts: 795
I'm trying to have a main loop running constantly updateing 5 variables based on ping readings. and a second cog, reading these 5 variable and communicating them to a PINK. I have seen the code about passing a value to a cog, but can a seperate cog read the same variables the main cog is using, or would I need to pass all 5 variables to the second cog.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
DGSwaner
"When in doubt, use C4" - Jamie Hyneman, Myth Buster
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
DGSwaner
"When in doubt, use C4" - Jamie Hyneman, Myth Buster
Comments
The only question is how to synchronize both COGs. It's propably not your intention to read the values in one COG while the other one is just writing. It's very likely that write and read is done at different speeds because write depends on hardware speed of whatever hardware you read (eg. ADC, simply port pin states...) and read in your case depends on how fast you can send data out. There are several ways to synchronize. You can use the lockxxx instructions, you can use the buffer itself or you can use pins to synchronize.
The lockxxx instructions are build for exactly that purpose. Currently I can't tell you in detail how these work because I did not use them yet. I guess you would have to do a locknew in the main program which starts both COGs and pass the lockid to both. Then they can use the lockset and lockclear to synchronize. Please have a look at the manual. In the SPIN-part the handling is described.
The same you can do by yourself by implementing a 'access protocol' using the buffer itself. COG 1 only writes in the buffer if a certain buffer element is zero. It writes exactly that buffer element as last (and of course zero should not be a valid value for that element). COG 2 checks that element for a value not equal to zero and starts reading the rest only in this case. When it's done it resets the value to zero. -> COG 1 now can start writing again.....
The disadvantage is the extra HUB access that is needed inside of a 'check value' loop for both COGs. If you have some free pins it's faster to do COG to COG signalling using 2 port pins - one for each COG involved which has write access to this one flag. COG 1 writes the values to the buffer and sets it's flag to 1. COG 2 did a waitpeq to watch this flag an IMMEDIATELY continues with the program when the flag is set. Meanwhile COG 1 clears the flag again and is doing a waitpeq on the other flag. When COG 2 is done with reading it sets its own flag to 1 and back to 0 and starts waiting again.
Post Edited (MagIO2) : 4/27/2009 7:19:00 AM GMT
However, MagIO2 is correct in saying that your PINK code may well be reading/sending these variables more quickly than your PING code can update them. This is only a problem if the receiver needs to be synchronised with the PING's updates though.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
You'll always have as many take-offs as landings, the trick is to be sure you can take-off again