<?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: Function test delay</title>
		<link>http://forums.leaflabs.com/topic.php?id=9450</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:08:05 +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=9450" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20890</link>
			<pubDate>Thu, 08 Nov 2012 18:33:05 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20890@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;higwoshy - Full disclosure: I am not a member of LeafLabs staff.&#60;/p&#62;
&#60;p&#62;You can get hold of stuff from leaflabs libmaple github, but I don't expect any package before the STM32F4 boards.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>higwoshy on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20864</link>
			<pubDate>Wed, 07 Nov 2012 19:17:33 +0000</pubDate>
			<dc:creator>higwoshy</dc:creator>
			<guid isPermaLink="false">20864@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;@gbulmer. Yes, agreed, unless systick is turned off - maybe two versions? Anyway, it's all simple enough to fix... &#60;/p&#62;
&#60;p&#62;but... it's been discussed before...1 year ago by the looks of it. Is there a new version coming out whereby all the fixes scattered throughout this forum are implemented? Will there ever be a version 0.0.13. Or even 0.0.125?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20861</link>
			<pubDate>Wed, 07 Nov 2012 13:16:22 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20861@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;There is a dedicated Cortex-M3 hardware timer (SysTick) which drives &#60;code&#62;millis()&#60;/code&#62; and &#60;code&#62;micros()&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;So a reasonable implementation would just use that timer, and those functions, e.g.:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;void myDelay(unsigned long ms) {
    unsigned long start_ms = millis();
    while (millis() - start_ms &#38;lt; ms) ;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;The code is assembler, and marked as &#60;code&#62;asm volatile&#60;/code&#62;:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;asm volatile(&#38;quot;   mov r0, %[us]          \n\t&#38;quot;
                 &#38;quot;1: subs r0, #1            \n\t&#38;quot;
                 &#38;quot;   bhi 1b                 \n\t&#38;quot;
                 :
                 : [us] &#38;quot;r&#38;quot; (us)
                 : &#38;quot;r0&#38;quot;);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;So the compiler shouldn't mess with it.&#60;br /&#62;
I think their is a thread on the forum where LeafLabs agree the estimate of this codes run time is just wrong.&#60;/p&#62;
&#60;p&#62;But using the systick hardware timer is better anyway because it would have greater immunity to timing inaccuracy due to interrupts.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>higwoshy on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20857</link>
			<pubDate>Wed, 07 Nov 2012 11:20:20 +0000</pubDate>
			<dc:creator>higwoshy</dc:creator>
			<guid isPermaLink="false">20857@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;@ventosus : I thought it would have been done that way anyway, but I guess they didn't want to use a valuable hardware timer on something so trivial - something I'd go along with. I'm ok with a hard coded delay as long as long as it's accurate - the Maple is not a multi-tasking OS so if you perform a delay() chances are you code isn't doing anything anyway so what's the harm. Keep it simple.&#60;/p&#62;
&#60;p&#62;I have no idea how to implement a delay_us() - which is the problem function - as I don't know enough about clock speeds vs speed of each assembly level instruction. &#60;/p&#62;
&#60;p&#62;Maybe it's the compiler being clever and 'optimising' the delay_us() code? It's a trivial bit of code so I'm surprised it's broken (?). I've only got an Olimexino so maybe the Maple native etc is different? Maybe it's measurement errors in the test code above?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>ventosus on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20855</link>
			<pubDate>Wed, 07 Nov 2012 09:15:57 +0000</pubDate>
			<dc:creator>ventosus</dc:creator>
			<guid isPermaLink="false">20855@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;@higwoshy, what about using a hardware timer instead of the delay-software-hack to get accurate timing?&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>higwoshy on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20852</link>
			<pubDate>Wed, 07 Nov 2012 07:46:59 +0000</pubDate>
			<dc:creator>higwoshy</dc:creator>
			<guid isPermaLink="false">20852@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;OMG indeed. This is why my spaceships keeping flying into the Moon rather than flying on to Mars. Now I think about it, I've noticed this before but never given it much attention, assuming it was my imagination.&#60;/p&#62;
&#60;p&#62;I wonder if this is why some Arduino hardware libraries fail to work when ported over to the Maple? Anything with &#60;code&#62;delay(100)&#60;/code&#62; etc in it for timing (say, clock pin toggling or waiting for hardware) would be out by 3/10 of a second?&#60;/p&#62;
&#60;p&#62;I thought I understood assembly but that bit of code looks like nonsense to me? &#60;code&#62;[us] &#38;quot;r&#38;quot; (us) ??! &#38;quot;r0&#38;quot; !?&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;Anyway, what's the fix? Changing that define from 12 to 15,16 produced 836, 1337 (!) - not much use.&#60;/p&#62;
&#60;p&#62;I changed the &#60;code&#62;delayMicroseconds(1000);&#60;/code&#62; in wirish_time.cpp to &#60;code&#62;delayMicroseconds(1496);&#60;/code&#62; which is clearly a fudge, but a fudge that works is better than a 1 second delay that only last .7 seconds! &#60;/p&#62;
&#60;p&#62;This still leaves delayMicroseconds as being very inaccurate - according to the Arduino docs it's meant to be very accurate above 3ms. Clearly the maple one isn't (?)&#60;/p&#62;
&#60;p&#62;Test program used (I also used the visual of clock hand to led flash, which clearly showed how bad delay(1000) actually was.&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;//#include &#38;lt;Wire.h&#38;gt;
#define Serial SerialUSB

void setup()
{
    pinMode(BOARD_LED_PIN, OUTPUT);
}

void loop()
{
  int millitest;
  int time;

  togglePin(BOARD_LED_PIN);

  time = millis();
  delay(1000);
  millitest=millis()-time;

  Serial.print(&#38;quot;millis = &#38;quot;); Serial.println( millitest);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;I'm running on an Olimexino, which returned 669ms for 1 second. With code change above, returns 1000. Which'll do until a proper fix comes along.&#60;/p&#62;
&#60;p&#62;Is the original delay_us() flawed because it does a multiplication which takes longer than 1us?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20832</link>
			<pubDate>Tue, 06 Nov 2012 16:37:27 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20832@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Bernardo769 - thanks for posting the details.&#60;/p&#62;
&#60;p&#62;OMG !&#60;/p&#62;
&#60;p&#62;The core of &#60;code&#62;delay()&#60;/code&#62; is a 'busy' loop:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;void delay(unsigned long ms) {
    uint32 i;
    for (i = 0; i &#38;lt; ms; i++) {
        delayMicroseconds(1000);
    }
}

void delayMicroseconds(uint32 us) {
    delay_us(us);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Then &#60;code&#62;delay_us()&#60;/code&#62; is just a loop which counts down:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;static inline void delay_us(uint32 us) {
    us *= STM32_DELAY_US_MULT;

    /* fudge for function call overhead  */
    us--;
    asm volatile(&#38;quot;   mov r0, %[us]          \n\t&#38;quot;
                 &#38;quot;1: subs r0, #1            \n\t&#38;quot;
                 &#38;quot;   bhi 1b                 \n\t&#38;quot;
                 :
                 : [us] &#38;quot;r&#38;quot; (us)
                 : &#38;quot;r0&#38;quot;);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Which is clearly not accurate.&#60;/p&#62;
&#60;p&#62;The easiest things would be to change the value of &#60;code&#62;STM32_DELAY_US_MULT&#60;/code&#62; from 12 to 16-ish.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Bernardo769 on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20828</link>
			<pubDate>Tue, 06 Nov 2012 15:43:42 +0000</pubDate>
			<dc:creator>Bernardo769</dc:creator>
			<guid isPermaLink="false">20828@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi,&#60;/p&#62;
&#60;p&#62;My board is a Maple-mini.&#60;br /&#62;
Here is my sketch :&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;lt;Wire.h&#38;gt;
#define Serial SerialUSB

unsigned int time = 0;

void setup() {
    // Led :
    pinMode(BOARD_LED_PIN, OUTPUT);
    time = millis();
}

void loop() {
  togglePin(BOARD_LED_PIN);
  delay(1000);
  Serial.print(&#38;quot;millis = &#38;quot;); Serial.println((millis() - time));
  time = millis();
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;The result is different than expected: for a time 1s, 669ms only!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20827</link>
			<pubDate>Tue, 06 Nov 2012 15:28:49 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">20827@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Bernardo769 - approximately what delay did you observe, and what was expected?&#60;br /&#62;
(I think it is helpful to put the details in your forum post rather than force people to read your site, which might change.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Bernardo769 on "Function test delay"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9450#post-20826</link>
			<pubDate>Tue, 06 Nov 2012 15:24:13 +0000</pubDate>
			<dc:creator>Bernardo769</dc:creator>
			<guid isPermaLink="false">20826@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi,&#60;/p&#62;
&#60;p&#62;During my first tests of the Maple mini, I found that the speed of flashing did not match the delay. So I checked it with this program:&#60;br /&#62;
&#60;a href=&#34;http://bernard.o.perso.neuf.fr/Electronic/MAPLE/testDelay/testDelay.htm&#34; rel=&#34;nofollow&#34;&#62;http://bernard.o.perso.neuf.fr/Electronic/MAPLE/testDelay/testDelay.htm&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;Is there an explanation for this difference?
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
