Interesting discovery about programming for multiple cogs
4x5n
Posts: 745
I know that everyone that's spent any amount of time programming the propeller already knows this but I just tripped over it yesterday and thought I'd pass the info on to others that are new to programming the propeller (like I am)!!
First a bit of background. I'm working on a program that among other things runs a bipolar stepper motor using a stepper driver that takes a pulse to step and an input on a pin to determine the direction of rotation. I got the code to run the stepper working and although the number of counts isn't exactly right I've got it working well enough for my needs. I then decided that this routine would make a nice object to be run in it's own cog since ultimately I will be needing to do more then run a stepper motor.
The code has a number of variables that need to get initialized for step counts etc and I decided to move all of that into the "start" method prior to starting the code to spin (no pun intended) the motor. To my horror when I ran the program nothing appeared to happen!! After a lot of head scratching, gnashing of teeth and a bit of yelling at my cat I discovered the problem!!!
As it turns out each cog has it's own direction register for the pins!! When setting up the object I moved the code that set the direction of the output pins in the Start method prior to starting up the new cog. In that new cog of course the default for the pins are that of input and although I had the "outa" statements the pins in that cog were inputs and nothing was output!!
Although global variable can be passed between cogs while in the same object the direction of each pin needs to be set on the cog that uses that pin!!! After moving the "dira" statements into the method launched onto the newcog all ran well. A lesson I won't soon forget. :-)
First a bit of background. I'm working on a program that among other things runs a bipolar stepper motor using a stepper driver that takes a pulse to step and an input on a pin to determine the direction of rotation. I got the code to run the stepper working and although the number of counts isn't exactly right I've got it working well enough for my needs. I then decided that this routine would make a nice object to be run in it's own cog since ultimately I will be needing to do more then run a stepper motor.
The code has a number of variables that need to get initialized for step counts etc and I decided to move all of that into the "start" method prior to starting the code to spin (no pun intended) the motor. To my horror when I ran the program nothing appeared to happen!! After a lot of head scratching, gnashing of teeth and a bit of yelling at my cat I discovered the problem!!!
As it turns out each cog has it's own direction register for the pins!! When setting up the object I moved the code that set the direction of the output pins in the Start method prior to starting up the new cog. In that new cog of course the default for the pins are that of input and although I had the "outa" statements the pins in that cog were inputs and nothing was output!!
Although global variable can be passed between cogs while in the same object the direction of each pin needs to be set on the cog that uses that pin!!! After moving the "dira" statements into the method launched onto the newcog all ran well. A lesson I won't soon forget. :-)
Comments
Actually I never knew that either.
I found a really good explanation on page 23 of the manual - actually there are 16 registers local to a cog; PAR, CNT, INA, INB, OUTA, OUTB, DIRA, DIRB, CTRA, CTRB, FRQA, FRQB, PHSA, PHSB, VCFG, VSCL.
It explains something else - the reason why there are 496 longs available to a cog, where to those of us who think and dream in binary (all of us, right?) , 512 would be the logical number. 496 plus the 16 registers above is 512.
Thanks for pointing this out!
This was probably the cause of most of my bugs when I first started programming the Propeller. Now I try to limit the control of any single IO pin to one cog.
Duane