Question About square_wave()
photomankc
Posts: 943
I am working providing a programmable clock for a couple of boards of mine. To that end I was using the square_wave function from simpletools.h and ran into something weird.
First thing was that changing the frequency on the fly seemed to only work sporadically. I was no able to nail down a pattern but in general what happens is that the frequency doesn't change every time unless I follow this pattern:
square_wave(1,0,7000000);
square_wave_stop();
square_wave(1,0,7000000);
...
square_wave(1,0,8000000);
square_wave_stop();
square_wave(1,0,8000000);
If I just do this:
square_wave(1,0,7000000);
square_wave(1,0,8000000);
Sometimes it works. But often it doesn't. The function is called but the frequency doesn't always change. However, If I use the "set it - stop it - set it again" pattern then the frequency changes properly every time. Seems like that shouldn't be required. Has anyone else run into this? My current project is a little complicated to reproduce but if if would help I can post code up.
First thing was that changing the frequency on the fly seemed to only work sporadically. I was no able to nail down a pattern but in general what happens is that the frequency doesn't change every time unless I follow this pattern:
square_wave(1,0,7000000);
square_wave_stop();
square_wave(1,0,7000000);
...
square_wave(1,0,8000000);
square_wave_stop();
square_wave(1,0,8000000);
If I just do this:
square_wave(1,0,7000000);
square_wave(1,0,8000000);
Sometimes it works. But often it doesn't. The function is called but the frequency doesn't always change. However, If I use the "set it - stop it - set it again" pattern then the frequency changes properly every time. Seems like that shouldn't be required. Has anyone else run into this? My current project is a little complicated to reproduce but if if would help I can post code up.
Comments
Here is a function you can add to squareWave.c to make it use the calling cog's counter modules. Make sure to recompile the libsimpletools.side project for the memory model you want to use after adding it. Also, keep in mind that if you use it with both of the calling cog's counter modules, it will disable pulse_in, pulse_out, rc_time and some others. They all block execution until they return, which is why they are allowed in the same cog. The "set it and forget it" counter functions like square_wave, pwm, and servo_* all have to launch into other cogs to prevent this conflict since that didn't seem like a fair caveat to throw at beginners. If it wasn't part of the Learn folder, I would have focused more on optimization over ease of use.
The square_wave_inline function in the previous post works fine now for all counter module frequencies now (and does not launch another cog).
...and there was a bug in the square_wave_cog function that was preventing the frequency changes from post #1. Here is the corrected version, and please accept my apologies for any delays this bug may have caused in your project.