<?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: bug in micros()</title>
		<link>http://forums.leaflabs.com/topic.php?id=1958</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:15:48 +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=1958" rel="self" type="application/rss+xml" />

		<item>
			<title>mbolivar on "bug in micros()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1958#post-11434</link>
			<pubDate>Wed, 27 Jun 2012 22:42:38 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">11434@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for making sure I didn't botch the fix forever ;)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>ala42 on "bug in micros()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1958#post-11427</link>
			<pubDate>Wed, 27 Jun 2012 18:16:37 +0000</pubDate>
			<dc:creator>ala42</dc:creator>
			<guid isPermaLink="false">11427@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Right. I caught the bug in the initial version and posted a fix, my fix was messed up when added to the libmaple code and I caught it again :)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "bug in micros()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1958#post-11423</link>
			<pubDate>Wed, 27 Jun 2012 12:56:51 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">11423@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Actually, looks like ala42 caught this in &#60;a href=&#34;https://github.com/leaflabs/libmaple/commit/f7e384a5eaa1e1f9f6bf315a8860d9d61284edc3&#34;&#62;f7e384a5&#60;/a&#62;. This patch will be part of the next release.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "bug in micros()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1958#post-11398</link>
			<pubDate>Tue, 26 Jun 2012 22:31:04 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">11398@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for the report; will fix.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>magnuskarlsson on "bug in micros()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1958#post-11383</link>
			<pubDate>Tue, 26 Jun 2012 09:47:07 +0000</pubDate>
			<dc:creator>magnuskarlsson</dc:creator>
			<guid isPermaLink="false">11383@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The code in wirish_time.h for micros() has a bug - it sometimes return a value 1000 uS more than the actual microsecond count due to an unprotected systick overflow.&#60;/p&#62;
&#60;p&#62;Original code:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;static inline uint32 micros(void) {
    uint32 ms;
    uint32 cycle_cnt;
    uint32 res;

    do {
        cycle_cnt = systick_get_count();
        ms = millis();
    } while (ms != millis());

    /* SYSTICK_RELOAD_VAL is 1 less than the number of cycles it
       actually takes to complete a SysTick reload */
    res = (fms * US_PER_MS) +
        (SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND;

    return res;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Fixed code:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;static inline uint32 micros(void) {
    uint32 fms, lms;
    uint32 cycle_cnt;
    uint32 res;

    do {
        // make sure millis() return the same value before and after
        // getting the systick count
        fms = millis();
        cycle_cnt = systick_get_count();
        lms = millis();
    } while (lms != fms);

    /* SYSTICK_RELOAD_VAL is 1 less than the number of cycles it
       actually takes to complete a SysTick reload */
    res = (fms * US_PER_MS) +
        (SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND;

    return res;
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>

	</channel>
</rss>
