<?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: cli() and sei() for Maple 5</title>
		<link>http://forums.leaflabs.com/topic.php?id=2617</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:06:07 +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=2617" rel="self" type="application/rss+xml" />

		<item>
			<title>mbolivar on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13077</link>
			<pubDate>Sun, 02 Sep 2012 00:34:07 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">13077@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;heh, that's definitely one way of going about it.&#60;/p&#62;
&#60;p&#62;throwing in every fence available on the architecture is the definitely thermonuclear option, though. i suppose that's the safest thing to do given that we haven't heard any feedback on the semantics of the memory barriers implied by cli()/sei().
&#60;/p&#62;</description>
		</item>
		<item>
			<title>x893 on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13040</link>
			<pubDate>Sat, 01 Sep 2012 03:06:11 +0000</pubDate>
			<dc:creator>x893</dc:creator>
			<guid isPermaLink="false">13040@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;If i add&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;static inline void __ISB() { __asm volatile (&#38;quot;isb&#38;quot;); }
static inline void __DSB() { __asm volatile (&#38;quot;dsb&#38;quot;); }
static inline void __DMB() { __asm volatile (&#38;quot;dmb&#38;quot;); }&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;and modify&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;void CriticalSectionEnter(void)
{
  if (CriticalSectionCount == 0)
  {
    __disable_irq();
    __DMB();
    __DSB();
    __ISB();
  }
  CriticalSectionCount++;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13033</link>
			<pubDate>Fri, 31 Aug 2012 19:05:19 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">13033@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;x893,&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;
this not work ?&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;no; that enables/disables interrupts, but neither implies a memory barrier. so it might work like cli()/sei() in some situations, but not others. please read my above message in this thread discussing cli()/sei() and memory barriers:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=2617#post-13004&#34; rel=&#34;nofollow&#34;&#62;http://forums.leaflabs.com/topic.php?id=2617#post-13004&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>x893 on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13031</link>
			<pubDate>Fri, 31 Aug 2012 18:34:44 +0000</pubDate>
			<dc:creator>x893</dc:creator>
			<guid isPermaLink="false">13031@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;For atomic i use very simple construction (thread safe)&#60;br /&#62;
volatile uint32_t CriticalSectionCount = 0;&#60;br /&#62;
void CriticalSectionEnter(void)&#60;br /&#62;
{&#60;br /&#62;
  if (CriticalSectionCount++ == 0) __disable_irq();&#60;br /&#62;
}&#60;br /&#62;
void CriticalSectionExit(void)&#60;br /&#62;
{&#60;br /&#62;
  if (--CriticalSectionCount == 0) __enable_irq();&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;and in code&#60;br /&#62;
...&#60;br /&#62;
CriticalSectionEnter();&#60;br /&#62;
// execute with disable irq, also can call other critical codes&#60;br /&#62;
CriticalSectionExit();
&#60;/p&#62;</description>
		</item>
		<item>
			<title>x893 on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13030</link>
			<pubDate>Fri, 31 Aug 2012 18:28:13 +0000</pubDate>
			<dc:creator>x893</dc:creator>
			<guid isPermaLink="false">13030@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I see core_cm3.h from ST library and see next&#60;/p&#62;
&#60;p&#62;static inline void __enable_irq()               { __asm volatile (&#34;cpsie i&#34;); }&#60;br /&#62;
static inline void __disable_irq()              { __asm volatile (&#34;cpsid i&#34;); }&#60;/p&#62;
&#60;p&#62;this not work ?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13027</link>
			<pubDate>Fri, 31 Aug 2012 15:49:16 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">13027@http://forums.leaflabs.com/</guid>
			<description>&#60;blockquote&#62;&#60;p&#62;
FWIW, the version of the port I am currently testing compiles and assembles without complaint&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;Just because it builds doesn't mean the memory ordering constraints it implies are the same. If this is going to go into the core library, it's important to get those right.&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;
Perhaps a &#34;belts and braces&#34; way to implement a memory barrier/execution order safe cli() would be something like this:&#60;/p&#62;
&#60;p&#62;__asm__ volatile (&#34;CPSID i\n\t&#34;&#34;dmb\n\t&#34; ::: &#34;memory&#34;)&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;That seems pretty reasonable. Are you confident that &#60;code&#62;dmb&#60;/code&#62; provides the same semantics as cli() (versus &#60;code&#62;dsb&#60;/code&#62;, etc.)? The AVR docs for cli() are a little underspecified for my taste.&#60;/p&#62;
&#60;p&#62;Next question -- where should these go? I was thinking of adding a Wirish-level avr_compat.h header for things like this. People can then include it as a quick-and-dirty mechanism when porting code. Seem reasonable?&#60;/p&#62;
&#60;p&#62;By the way, &#60;a href=&#34;http://leaflabs.com/docs/lang/api/nointerrupts.html#lang-nointerrupts&#34;&#62;noInterrupts()&#60;/a&#62; and &#60;a href=&#34;http://leaflabs.com/docs/lang/api/interrupts.html#lang-interrupts&#34;&#62;interrupts()&#60;/a&#62; should both compile down to the single machine instruction they imply (&#60;code&#62;cpsid i&#60;/code&#62;/&#60;code&#62;cpsie i&#60;/code&#62;), so it's nice to use them instead of assembly for readability.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>pico on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13026</link>
			<pubDate>Fri, 31 Aug 2012 12:10:57 +0000</pubDate>
			<dc:creator>pico</dc:creator>
			<guid isPermaLink="false">13026@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Having read a little more about &#34;memory barriers&#34;, I see that there are _CPU_ memory barrier instructions to conttrol out-of-sequence execution of instructions due to e.g., pipelining, and there are also _compiler_ memory barrier directives, such as the &#34;&#34;:::&#34;memory&#34; directive GCC uses to control reording due to optimisations.&#60;/p&#62;
&#60;p&#62;Perhaps a &#34;belts and braces&#34; way to implement a memory barrier/execution order safe cli() would be something like this:&#60;br /&#62;
&#60;code&#62;&#60;br /&#62;
__asm__ volatile (&#34;CPSID i\n\t&#34;&#34;dmb\n\t&#34; ::: &#34;memory&#34;)&#60;br /&#62;
&#60;/code&#62;&#60;br /&#62;
What do you think?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>pico on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13021</link>
			<pubDate>Fri, 31 Aug 2012 01:44:00 +0000</pubDate>
			<dc:creator>pico</dc:creator>
			<guid isPermaLink="false">13021@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Not sure if talking about exactly the same thing, but I've started porting over the ATOMIC_BLOCK macros to see if I can get equivalents working under ARM, and I've come across this construct:&#60;br /&#62;
&#60;code&#62;&#60;br /&#62;
static __inline__ void    __iRestore(const  uint32_t *__s) \&#60;br /&#62;
{ __set_primask(*__s); __asm__ volatile (&#34;&#34; ::: &#34;memory&#34;); }&#60;br /&#62;
&#60;/code&#62;&#60;br /&#62;
where the asm &#34;memory&#34; directive  is in the &#34;clobber list&#34; position in an otherwise empty line of assembly code, which I understand constrains any reordering optimisations at that point the compiler might otherwise consider.&#60;/p&#62;
&#60;p&#62;Along with &#34;volatile&#34;, these are directives that basically close off optimisations to prevent these instruction being moved to unexpected places.&#60;/p&#62;
&#60;p&#62;Is this what you mean by a &#34;memory barrier&#34;? &#60;/p&#62;
&#60;p&#62;FWIW, the version of the port I am currently testing compiles and assembles without complaint with these directives, so perhaps there is nothing going on that isn't portable across the AVR and ARM versions of GCC.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-13004</link>
			<pubDate>Thu, 30 Aug 2012 14:48:56 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">13004@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I've thought about doing something like that. The only part I worry about is that apparently cli()/sei() imply memory barriers:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html#ga68c330e94fe121eba993e5a5973c3162&#34; rel=&#34;nofollow&#34;&#62;http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html#ga68c330e94fe121eba993e5a5973c3162&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;To faithfully emulate them, then, we'd need to do the same thing. I'm not sure which of the &#60;a href=&#34;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0211i/Babfdddg.html&#34;&#62;ARM memory barriers&#60;/a&#62; is equivalent to the AVR version. Can anyone provide some advice here?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>pico on "cli() and sei() for Maple 5"</title>
			<link>http://forums.leaflabs.com/topic.php?id=2617#post-12983</link>
			<pubDate>Wed, 29 Aug 2012 10:14:51 +0000</pubDate>
			<dc:creator>pico</dc:creator>
			<guid isPermaLink="false">12983@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;After some reading, I found the asm instructions to disable/enable interrupts by setting PRIMASK for the Cortex-M3, so I'm assuming this should work for a Maple 5:&#60;br /&#62;
&#60;code&#62;&#60;br /&#62;
#define _cli asm(&#34;CPSID i\n\t&#34;)&#60;/p&#62;
&#60;p&#62;#define _sei  asm(&#34;CPSIE i\n\t&#34;)&#60;br /&#62;
&#60;/code&#62;&#60;br /&#62;
Any reason why this might be problematic? Any reason why they weren't included in the libs for greater compatibility with Arduino code?&#60;/p&#62;
&#60;p&#62;FWIW, they seem to be compiling and working as expected so far in my programs I'm porting to Maple 5 from AVR Arduino.&#60;/p&#62;
&#60;p&#62;But thought I'd ask in case I'm missing something basic (or even not so basic) here...
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
