<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Potential bug in micros()?</title>
		<link>http://forums.leaflabs.com/topic.php?id=9141</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:08:08 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=9141" rel="self" type="application/rss+xml" />

		<item>
			<title>palsor on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141&amp;page=2#post-20340</link>
			<pubDate>Wed, 17 Oct 2012 21:05:48 +0000</pubDate>
			<dc:creator>palsor</dc:creator>
			<guid isPermaLink="false">20340@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Yes, as gbulmer said, we're trying to avoid the 300ms loop times. Ideally we're hoping for a 20ms loop time.&#60;/p&#62;
&#60;p&#62;After some experimentation, it looks like the problem is now tied to SD card writes, not to our calls to micros(). I did several experiments, but here are the four that I thought were interesting.&#60;/p&#62;
&#60;p&#62;1. Running the simple code I posted at the top of the thread without the fix to micros() that gbulmer posted results in occasional long loop times.&#60;br /&#62;
2. Running the simple code I posted at the top of the thread with the fix that to micros() that gbulmer posted results in no long loops.&#60;br /&#62;
3. Running our full code with the fix that gbulmer posted results in long loop times.&#60;br /&#62;
4. Running our full code with the fix that gbulmer posted, but with our SD card writes commented out results in no long loops.&#60;/p&#62;
&#60;p&#62;Based on that, I'm inclined to believe that at least some of the long loops we were seeing originally were due to the bug in micros(), but now I'm convinced that I don't understand the delays that SD cards throw into the mix. i've started researching the options for DMA'ing data to the SD card, or using other card modes. &#60;/p&#62;
&#60;p&#62;Anyway, thanks for all the help guys!&#60;/p&#62;
&#60;p&#62;- Andy
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20334</link>
			<pubDate>Wed, 17 Oct 2012 07:25:22 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20334@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;crenn - AFAIK, they are not trying to wait for 300ms; the bug seems to give the effect that &#60;code&#62;micros()&#60;/code&#62; sometimes takes 300ms to return, rather than a microsecond (or less). They don't want that effect.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20319</link>
			<pubDate>Tue, 16 Oct 2012 20:40:45 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">20319@http://forums.leaflabs.com/</guid>
			<description>&#60;blockquote&#62;&#60;p&#62;@crenn, did you have any luck reproducing this? If needed, I could send you our log file so you have some data to look at.&#60;/p&#62;&#60;/blockquote&#62;
&#60;p&#62;Unfortunately not, as it's approaching the end of my university semester, I've got a lot of work due this week. I'll most likely be looking into the issue either this weekend or next week.&#60;/p&#62;
&#60;p&#62;If you're waiting for 300ms? Why not just use millis()?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>palsor on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20308</link>
			<pubDate>Mon, 15 Oct 2012 23:14:40 +0000</pubDate>
			<dc:creator>palsor</dc:creator>
			<guid isPermaLink="false">20308@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Yeah, I'll try a few experiments and post back.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20297</link>
			<pubDate>Mon, 15 Oct 2012 08:37:12 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20297@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;palsor - would you please post a minimal program that demonstrates the bug?&#60;/p&#62;
&#60;p&#62;Is the delay of 300ms or 450ms a slow call of micros() or is it where currTime and time differ by a lot?&#60;/p&#62;
&#60;p&#62;(Would you try (an irrational) thing just to set my mind at ease, and change the name of variable &#60;code&#62;time&#60;/code&#62; to prevTime or something just to ensure their isn't a weird clash? The linker should notice, but it should be easy enough to change.)&#60;/p&#62;
&#60;p&#62;When you say &#34; Interestingly, the instances of getting a 300ms loop occured on a pretty regular interval, with almost exactly 15 seconds between each instance.&#34;&#60;br /&#62;
How close to 15 seconds is that repetition rate? Could it be closer to 16.8 seconds?&#60;/p&#62;
&#60;p&#62;Edit: I should have said, 16.8 seconds could correspond to the cycle time of the underlying 24bit hardware SysTick timer, if it were counting at 1us. SysTick and&#60;code&#62;micros()&#60;/code&#62; should not be behave strangely at 16.8 seconds (IIRC it should be much faster), but I'm just trying to exclude possibilities.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>palsor on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20290</link>
			<pubDate>Sun, 14 Oct 2012 21:50:29 +0000</pubDate>
			<dc:creator>palsor</dc:creator>
			<guid isPermaLink="false">20290@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Well bad news. Even with the fix gbulmer pointed too, and without USB, the problem doesn't seem to be have resolved itself. My friend and I did a pass at logging all of our sensors while driving around his neighborhood. Our loop time is normally between 5ms and 30ms depending on what happens. However, over the course of logging for 10 minutes, we saw 5 instances where our loop time jumped up to 450ms, and 38 instances where the loop time jumped up to 300ms. Interestingly, the instances of getting a 300ms loop occured on a pretty regular interval, with almost exactly 15 seconds between each instance. Any guesses as to what would cause that?&#60;/p&#62;
&#60;p&#62;@crenn, did you have any luck reproducing this? If needed, I could send you our log file so you have some data to look at.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>palsor on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20266</link>
			<pubDate>Thu, 11 Oct 2012 19:50:42 +0000</pubDate>
			<dc:creator>palsor</dc:creator>
			<guid isPermaLink="false">20266@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;@higwoshy - Yes, thank you. Work has been all consuming this week so I haven't gotten a chance to try anything, but I'll definitiely try gbulmer's fix first.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20245</link>
			<pubDate>Tue, 09 Oct 2012 21:02:30 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20245@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;higwoshy - That is very helpful of you to follow up for the OP.&#60;br /&#62;
That result is interesting. I think that implies that the system is so deterministic that the bug is repeatable. Impressive.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>higwoshy on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20241</link>
			<pubDate>Tue, 09 Oct 2012 12:22:43 +0000</pubDate>
			<dc:creator>higwoshy</dc:creator>
			<guid isPermaLink="false">20241@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;UPDATE : It was the fix gbulmer pointed to : a systick overflow.  Edit file /Developer/MapleIDE.app/Contents/Resources/Java/hardware/leaflabs/cores/maple/wirish_time.cpp and change the code accordingly (this is under OSX, but just look for your maple core and you'll find it).
&#60;/p&#62;</description>
		</item>
		<item>
			<title>higwoshy on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20224</link>
			<pubDate>Mon, 08 Oct 2012 06:22:21 +0000</pubDate>
			<dc:creator>higwoshy</dc:creator>
			<guid isPermaLink="false">20224@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;@gblumber : Ah, I should have done the maths first, it was getting late here - I'm not fond of late night maths, even simple stuff. It seemed plausible at the time, a kind of harmonic race condition(?).  Nevertheless (curtime-time) is definitely creating large numbers. &#60;/p&#62;
&#60;p&#62;@crenn : The brackets did seem plausible, but it made no difference. Does the +1 to micros() internal reg happen in two parts? It's not possible to interrupt an addition instruction is it? That would create some obscure bugs if it did. :) I'm guessing the answer is no.&#60;/p&#62;
&#60;p&#62;@palsor : Yep, I can confirm that changing micros() to millis() doesn't cause any instability.&#60;/p&#62;
&#60;p&#62;P.S. I'm running this example in RAM - I know that is a source of some obscure bugs. Just tried it in flash memory and it's a lot more stable as in the USB doesn't effect it now, but it still &#34;times out&#34; every 12 seconds. How strange.&#60;/p&#62;
&#60;p&#62;Anyway, I'll let the experts work it out, over to you guys.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>palsor on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20213</link>
			<pubDate>Sun, 07 Oct 2012 21:06:25 +0000</pubDate>
			<dc:creator>palsor</dc:creator>
			<guid isPermaLink="false">20213@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;@higwoshy ... Yes, the code I posted was just to demonstrate the problem I'm seeing. The only thing I'll say in addition to what you guys already posted is that if I switch from micros() to millis() and change the delay from 75000 to 75 (due to the change to millis()), then I don't see the problem anymore. That makes me think the problem has to do with micros() rather than the end-of-loop code from libmaple. &#60;/p&#62;
&#60;p&#62;@higwoshy and gbulmer ... I'll try removing USB from the picture and see if that changes anything.&#60;/p&#62;
&#60;p&#62;@gbulmer ... Thanks for the link above. I'll try adding in the fix to micros() and see if that changes anything, though I agree that it doesn't look like the bug in the thread you linked to would actually cause the effect I'm seeing. I'm might try playing with the while loop in micros() and see if that causes anything interesting to happen as well.&#60;/p&#62;
&#60;p&#62;Thanks for the help guys.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20211</link>
			<pubDate>Sun, 07 Oct 2012 20:53:20 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">20211@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I'll run your code tonight to see if I can replicate your results&#60;/p&#62;
&#60;p&#62;I'd put a bracket around (curTime - time), it shouldn't affect it, but I can't remember some of the priorities in the maths (aka how logic fits into BODMAS)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20210</link>
			<pubDate>Sun, 07 Oct 2012 20:05:29 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20210@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;higwoshy - micros() should overflow when an unsigned long overflows. &#60;/p&#62;
&#60;p&#62;IIRC an unsigned long is 32bits, i.e. a maximum of a little over 4,000,000,000 &#60;/p&#62;
&#60;p&#62;When used to record microseconds, it should overflow every 4000 seconds, which is a tad more than an hour, not every 10 to 15 seconds.&#60;/p&#62;
&#60;p&#62;I agree USB is plausibly a culprit.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>higwoshy on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20209</link>
			<pubDate>Sun, 07 Oct 2012 17:35:17 +0000</pubDate>
			<dc:creator>higwoshy</dc:creator>
			<guid isPermaLink="false">20209@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Uh? What is that code meant to do? Replacing the serial print with a togglePin(13) I get a randomly flashing light.&#60;/p&#62;
&#60;p&#62;Did you mean this?&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;...

    if (curTime - time &#38;gt; 75000)
    {
        togglePin(13);      //SerialUSB.println(&#38;quot;1&#38;quot;);
        time = curTime;   // new bit
    }
...

}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Or is your code meant to be a test to show when micros() pauses? Cos I don't think your code does that, more like showing that when you exit the main loop (to loop round again), sometimes it takes longer than others to return. I might be getting the wrong end of the stick. &#60;/p&#62;
&#60;p&#62;AHH! Ok, got it. Hmmm, try running off battery, as the usb functions can screw timings up. Certainly running your original code with usb connected causes random flashing - disconnecting usb causes a flash every so often as you mention. In fact I altered the code to 375000 and I got a toggle every 11.9 - 12.1 seconds (using a stopwatch, so it might be off by .2 seconds)&#60;/p&#62;
&#60;p&#62;HOWEVER, changing the code to this to eliminate the maple-main-loop-code,&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;unsigned long time = 0;

void setup()
{
    time = micros();
    pinMode(13, OUTPUT);
}

void loop()
{

    unsigned long curTime;

    while(1)
    {
        curTime = micros();

        if (curTime - time &#38;gt; 375000)
        {
            togglePin(13);
        }

        time = curTime;
    }

}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;I get a weird effect going on... toggling like mad for 3 seconds then a pause of 9 seconds. Hmmm. 9+3 = 12. A clue I think.&#60;/p&#62;
&#60;p&#62;My head hurts, I'm going to lie down now...&#60;/p&#62;
&#60;p&#62;30 seconds later ... It's probably caused when micros() overflows back to zero, causing curTime to be 0 ish and time being a very large number, which overflows again to produce a negative number, which shows up as positive number &#38;gt; 75000 because it's unsigned. Clear? Try changing 375000 to 11375000... And the reason usb causes random flashing? Usb is connected to an expensive Mac which is randomly sending out control signals. Well, not randomly, but you know what I mean.&#60;/p&#62;
&#60;p&#62;I think you need to write a better test program :)  to make sure it is micros() that is the problem and not something else. Try using an audio out. Scopes are nice as well.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Potential bug in micros()?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9141#post-20206</link>
			<pubDate>Sun, 07 Oct 2012 12:11:19 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20206@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;palsor - there was a bug in micros(), explained here:&#60;br /&#62;
&#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=1958#post-11383&#34; rel=&#34;nofollow&#34;&#62;http://forums.leaflabs.com/topic.php?id=1958#post-11383&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;So it might be worth checking that you have the fix.&#60;/p&#62;
&#60;p&#62;But, I don't think that explains the effect you're seeing.
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
