<?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: Race condition with slow interrupt handlers and usarts</title>
		<link>http://forums.leaflabs.com/topic.php?id=1742</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:20: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=1742" rel="self" type="application/rss+xml" />

		<item>
			<title>mbolivar on "Race condition with slow interrupt handlers and usarts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1742#post-10742</link>
			<pubDate>Fri, 18 May 2012 12:40:14 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">10742@http://forums.leaflabs.com/</guid>
			<description>&#60;blockquote&#62;&#60;p&#62;
One important thing I forgot to mention here is that I am using the olimexino clone for this project - so although it shouldn't be a difference to the real maple I don't expect any support on it ;) Originally hoped to use the Native, but you ran out.. I would be able to test this error on the one I have though.
&#60;/p&#62;&#60;/blockquote&#62;
&#60;p&#62;That's cool. This sounds like a software problem rather than a hardware issue. We say libmaple works on the STM32F103RB, so we shouldn't be bothered if a bug report happens to come from another board ;).
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gauteh on "Race condition with slow interrupt handlers and usarts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1742#post-10728</link>
			<pubDate>Thu, 17 May 2012 18:40:19 +0000</pubDate>
			<dc:creator>gauteh</dc:creator>
			<guid isPermaLink="false">10728@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I have had rb_safe_insert hard coded in that function (using the define now), the lock up still happened with no apparent difference in frequency to using the regular rb_push_insert. A shipment with a debugger/adapter should arrive by tomorrow so I might get the chance to investigate a little closer then.&#60;/p&#62;
&#60;p&#62;One important thing I forgot to mention here is that I am using the olimexino clone for this project - so although it shouldn't be a difference to the real maple I don't expect any support on it ;) Originally hoped to use the Native, but you ran out.. I would be able to test this error on the one I have though.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>ala42 on "Race condition with slow interrupt handlers and usarts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1742#post-10727</link>
			<pubDate>Thu, 17 May 2012 17:19:47 +0000</pubDate>
			<dc:creator>ala42</dc:creator>
			<guid isPermaLink="false">10727@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Do you have USART_SAFE_INSERT defined ?&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;static inline int rb_safe_insert(ring_buffer *rb, uint8 element) {
    if (rb_is_full(rb)) {
        return 0;
    }
    rb_insert(rb, element);
    return 1;
}

rb_safe_insert(dev-&#38;gt;rb, (uint8)dev-&#38;gt;regs-&#38;gt;DR);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;In case the buffer is full, 'element' is not read, so the DR is never read and the interrupt is not cleared. Add a dummy read and check the assembler code so see the dummy read was not optimized out.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gauteh on "Race condition with slow interrupt handlers and usarts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1742#post-10723</link>
			<pubDate>Thu, 17 May 2012 12:57:09 +0000</pubDate>
			<dc:creator>gauteh</dc:creator>
			<guid isPermaLink="false">10723@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I tried to put a check for any of the bits described here: &#60;a href=&#34;http://www.micromouseonline.com/2009/12/31/stm32-usart-basics/#axzz1v8opplfY&#34; rel=&#34;nofollow&#34;&#62;http://www.micromouseonline.com/2009/12/31/stm32-usart-basics/#axzz1v8opplfY&#60;/a&#62;:&#60;/p&#62;
&#60;p&#62;USARTx_SR_ORE: Overrun Error&#60;br /&#62;
USARTx_SR_NE: Noise Error&#60;br /&#62;
USARTx_SR_FE: Framing Error&#60;br /&#62;
USARTx_SR_PE: Parity Error&#60;/p&#62;
&#60;p&#62;around the rb_safe_insert line, but to no help. Given I did it correct and that it would actually do any good: for what I know the 250Hz could still interrupt the flow in between. &#60;/p&#62;
&#60;p&#62;Also I did not do any reset if any of them were set, so my attempt was fairly basic..&#60;/p&#62;
&#60;p&#62;It would be worth a try from someone who has a better overview. But I guess the whole ring_buffer design along with the interrupt is somewhat hard to get right in this case (without the priority workaround).&#60;/p&#62;
&#60;p&#62;Perhaps another approach using DMA with a 'locked' and thread safe ringbuffers could be a solution. I don't know if it is possible to do atomic locking between user and interrupt space.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Race condition with slow interrupt handlers and usarts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1742#post-10722</link>
			<pubDate>Thu, 17 May 2012 11:55:54 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">10722@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;sigh. you've run into one of our terrible hacks. sorry for any problems you've been having; glad you found a workaround.&#60;/p&#62;
&#60;p&#62;the ring buffer implementation isn't thread-safe at all, but the USART RX &#60;code&#62;ring_buffer&#60;/code&#62;s are accessed concurrently by the interrupt handlers and other places (e.g. &#60;code&#62;usart_getc()&#60;/code&#62;, &#60;code&#62;usart_rx()&#60;/code&#62;). so it's totally reasonable that something like this would happen.&#60;/p&#62;
&#60;p&#62;i'm not sure what the right thing to do is.&#60;/p&#62;
&#60;p&#62;we could try to put together some sort of IRQ bottom half system in wirish, so that the IRQs could just mark the new data and some other routine could lock the RX ring_buffers in &#60;code&#62;usart_dev&#60;/code&#62; before pushing them. that seems tricky to get right, though, and it'd add yet another interrupt that would mess with people's real-time concerns in the same way that the USB interrupts already do (we could stuff it into the SysTick handler, but that'd be too surprising to people, i think).&#60;/p&#62;
&#60;p&#62;any suggestions are welcome.&#60;/p&#62;
&#60;p&#62;(edit: presuming that's the cause of the problem. i guess it could also be due to overrun error? not sure. anyway, thanks again for the report.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gauteh on "Race condition with slow interrupt handlers and usarts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1742#post-10721</link>
			<pubDate>Thu, 17 May 2012 11:06:11 +0000</pubDate>
			<dc:creator>gauteh</dc:creator>
			<guid isPermaLink="false">10721@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi,&#60;/p&#62;
&#60;p&#62;I've mentioned the problem on #leaflabs, but I thought I would post it here for reference:&#60;/p&#62;
&#60;p&#62;I experienced random, but frequent, lock ups with a setup where I used USARTS extensively along with an 250 Hz external interrupt with a rather slow ISR. The lock up was clearly related to, in my case, receiving on the USART. I have not figured out exactly what happens, but I think it is something along the lines of:&#60;/p&#62;
&#60;p&#62;1. Data arrives on USART -&#38;gt;regs-&#38;gt;DR&#60;br /&#62;
2. USART interrupt gets called&#60;br /&#62;
3. 250 Hz interrupt gets called before USART interrupt is finished or is started&#60;br /&#62;
4. 250 Hz interrupt takes so much time there occurs some error on the USART regs-&#38;gt;DR part&#60;br /&#62;
5. USART interrupt locks up.&#60;/p&#62;
&#60;p&#62;The lock up always happened on the line:&#60;br /&#62;
&#60;code&#62;&#60;br /&#62;
usart.c:usart_irq (usart_dev *dev):&#60;/p&#62;
&#60;p&#62;  rb_push/safe_insert (dev-&#38;gt;rb, (uint8)dev-&#38;gt;regs-&#38;gt;DR);&#60;br /&#62;
&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;As I said I don't know exactly what the error is, but by increasing the priority of the usart interrupt the lock up has been completely removed.&#60;/p&#62;
&#60;p&#62;My fix: &#60;a href=&#34;https://github.com/gauteh/libmaple/commit/1e10299b43e255a4029cacc18bdf248289be7092&#34; rel=&#34;nofollow&#34;&#62;https://github.com/gauteh/libmaple/commit/1e10299b43e255a4029cacc18bdf248289be7092&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;&#60;code&#62;&#60;br /&#62;
libmaple/usart.c View file @ 1e10299&#60;br /&#62;
...	 ...&#60;br /&#62;
@@ -102,6 +102,11 @@ void usart_init(usart_dev *dev) {&#60;br /&#62;
102	 102&#60;br /&#62;
     rb_init(dev-&#38;gt;rb, USART_RX_BUF_SIZE, dev-&#38;gt;rx_buf);&#60;br /&#62;
103	 103&#60;br /&#62;
     rcc_clk_enable(dev-&#38;gt;clk_id);&#60;br /&#62;
104	 104&#60;br /&#62;
     nvic_irq_enable(dev-&#38;gt;irq_num);&#60;br /&#62;
 	 105&#60;br /&#62;
+&#60;br /&#62;
 	 106&#60;br /&#62;
+    /* Set to max priority to avoid race condition where register presumably&#60;br /&#62;
 	 107&#60;br /&#62;
+     * might change before it can be picked out by USART interrupt and cause&#60;br /&#62;
 	 108&#60;br /&#62;
+     * it to lock up. */&#60;br /&#62;
 	 109&#60;br /&#62;
+    nvic_irq_set_priority (dev-&#38;gt;irq_num, 0);&#60;br /&#62;
105	 110&#60;br /&#62;
 }&#60;br /&#62;
&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;the other edit in usart.h is for good measure, I haven't had any problems with it.&#60;/p&#62;
&#60;p&#62;Regards, Gaute
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
