rtic/2/api/imxrt_ral/macro.read_reg.html
2024-12-06 13:35:18 +00:00

80 lines
No EOL
9.8 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Read the value from a RORegister, RWRegister, UnsafeRORegister, or UnsafeRWRegister."><title>read_reg in imxrt_ral - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-b0742ba02757f159.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="imxrt_ral" data-themes="" data-resource-suffix="" data-rustdoc-version="1.83.0 (90b35a623 2024-11-26)" data-channel="1.83.0" data-search-js="search-f0d225181b97f9a4.js" data-settings-js="settings-805db61a62df4bd2.js" ><script src="../static.files/storage-1d39b6787ed640ff.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-f070b9041d14864c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-0111fcff984fae8f.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../imxrt_ral/index.html">imxrt_<wbr>ral</a><span class="version">0.5.3</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">read_<wbr>reg</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#usage" title="Usage">Usage</a></li><li><a href="#safety" title="Safety">Safety</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate imxrt_<wbr>ral</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">imxrt_ral</a></span><h1>Macro <span class="macro">read_reg</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/ral_registers/lib.rs.html#544">source</a> </span></div><pre class="rust item-decl"><span class="macro">macro_rules!</span> read_reg {
( <span class="macro-nonterminal">$periph</span>:path, <span class="macro-nonterminal">$instance</span>:expr, <span class="macro-nonterminal">$reg</span>:ident $([<span class="macro-nonterminal">$offset</span>:expr])<span class="kw-2">*</span>, $( <span class="macro-nonterminal">$field</span>:ident ),+ $(,)<span class="question-mark">? </span>) =&gt; { ... };
( <span class="macro-nonterminal">$periph</span>:path, <span class="macro-nonterminal">$instance</span>:expr, <span class="macro-nonterminal">$reg</span>:ident $([<span class="macro-nonterminal">$offset</span>:expr])<span class="kw-2">*</span>, <span class="macro-nonterminal">$field</span>:ident $(<span class="macro-nonterminal">$cmp</span>:tt)* ) =&gt; { ... };
( <span class="macro-nonterminal">$periph</span>:path, <span class="macro-nonterminal">$instance</span>:expr, <span class="macro-nonterminal">$reg</span>:ident $([<span class="macro-nonterminal">$offset</span>:expr])* ) =&gt; { ... };
}</pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Read the value from a RORegister, RWRegister, UnsafeRORegister, or UnsafeRWRegister.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Safely acquire the peripheral instance (will panic if already acquired)
</span><span class="kw">let </span>gpioa = stm32ral::gpio::GPIOA::take().unwrap();
<span class="comment">// Read the whole register.
</span><span class="kw">let </span>val = <span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR);
<span class="comment">// Read one field from the register.
</span><span class="kw">let </span>val = <span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR, IDR2);
<span class="comment">// Read multiple fields from the register.
</span><span class="kw">let </span>(val1, val2, val3) = <span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR, IDR0, IDR1, IDR2);
<span class="comment">// Check if one field is equal to a specific value, with the field's named values in scope.
</span><span class="kw">while </span><span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR, IDR2 == High) {}
<span class="comment">// Unsafe access without requiring you to first `take()` the instance
</span><span class="kw">let </span>val = <span class="kw">unsafe </span>{ <span class="macro">read_reg!</span>(stm32ral::gpio, GPIOA, IDR) };</code></pre></div>
<p>To support register arrays, each macro form also supports one or more array indices after the
register. For example, <code>read_reg!(stm32ral::gpio, gpioa, ODR[2]);</code> reads from the third
register of an <code>ODR</code> register array.</p>
<h2 id="usage"><a class="doc-anchor" href="#usage">§</a>Usage</h2>
<p>Like <code>write_reg!</code>, this macro can be used multiple ways, either reading the entire register or
reading a one or more fields from it and potentially performing a comparison with one field.</p>
<p>In all cases, the first arguments are:</p>
<ul>
<li>the path to the peripheral module: <code>stm32ral::gpio</code>,</li>
<li>a reference to the instance of that peripheral: gpioa (anything which dereferences to
<code>RegisterBlock</code>, such as <code>Instance</code>, <code>&amp;Instance</code>, <code>&amp;RegisterBlock</code>, or
<code>*const RegisterBlock</code>),</li>
<li>the register (and offset, for arrays) you wish to access: <code>IDR</code> (a field on the
<code>RegisterBlock</code>).</li>
</ul>
<p>In the whole-register usage, the macro simply returns the registers value:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Read the entire value of GPIOA.IDR into `val`.
</span><span class="kw">let </span>val = <span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR);</code></pre></div>
<p>For reading individual fields, the macro masks and shifts appropriately:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Read just the value of the field GPIOA.IDR2 into `val`.
</span><span class="kw">let </span>val = <span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR, IDR2);
<span class="comment">// As above, but expanded for exposition:
</span><span class="kw">let </span>val = ((<span class="kw-2">*</span>gpioa).IDR.read() &amp; stm32ral::gpio::IDR::IDR2::mask)
&gt;&gt; stm32ral::gpio::IDR::IDR2::offset;
<span class="comment">// Read multiple fields
</span><span class="kw">let </span>(val1, val2) = <span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR, IDR2, IDR3);
<span class="comment">// As above, but expanded for exposition:
</span><span class="kw">let </span>(val1, val2) = { <span class="kw">let </span>val = (<span class="kw-2">*</span>gpioa).IDR.read();
((val &amp; stm32ral::gpio::IDR::IDR2::mask) &gt;&gt; stm32ral::gpio::IDR::IDR2::offset,
(val &amp; stm32ral::gpio::IDR::IDR3::mask) &gt;&gt; stm32ral::gpio::IDR::IDR3::offset,
)};</code></pre></div>
<p>For comparing a single field, the macro masks and shifts and then performs the comparison:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Loop while PA2 is High.
</span><span class="kw">while </span><span class="macro">read_reg!</span>(stm32ral::gpio, gpioa, IDR, IDR2 == High) {}
<span class="comment">// Only proceed if the clock is not the HSI.
</span><span class="kw">if </span><span class="macro">read_reg!</span>(stm32ral::rcc, rcc, CFGR, SWS != HSI) { }
<span class="comment">// Equivalent expansion:
</span><span class="kw">if </span>(((<span class="kw-2">*</span>rcc).CFGR.read() &amp; stm32ral::rcc::CFGR::SWS::mask)
&gt;&gt; stm32ral::rcc::CFGR::SWS::offset) != stm32ral::rcc::CFGR::SWS::R::HSI { }</code></pre></div>
<h2 id="safety"><a class="doc-anchor" href="#safety">§</a>Safety</h2>
<p>This macro will require an unsafe function or block when used with an UnsafeRWRegister or
UnsafeRORegister, but not if used with RWRegister, or RORegister.</p>
<p>When run in an unsafe context, peripheral instances are directly accessible without requiring
having called <code>take()</code> beforehand:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>val = <span class="kw">unsafe </span>{ <span class="macro">read_reg!</span>(stm32ral::gpio, GPIOA, MODER) };</code></pre></div>
<p>This works because <code>GPIOA</code> is a <code>*const RegisterBlock</code> in the <code>stm32ral::gpio</code> module;
and the macro brings such constants into scope and then dereferences the provided reference.</p>
</div></details></section></div></main></body></html>