You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
16 KiB
HTML

<!-- HTML header for doxygen 1.8.7-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.6.3"/>
<!--BEGIN PROJECT_NAME--><title>avr-libc: avr-libc: &lt;avr/sleep.h&gt;: Power Management and Sleep Modes</title><!--END PROJECT_NAME-->
<!--BEGIN !PROJECT_NAME--><title>avr-libc: &lt;avr/sleep.h&gt;: Power Management and Sleep Modes</title><!--END !PROJECT_NAME-->
<link href="$relpath^tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="$relpath^jquery.js"></script>
<script type="text/javascript" src="$relpath^dynsections.js"></script>
$treeview
$search
$mathjax
<link href="$relpath^$stylesheet" rel="stylesheet" type="text/css" />
$extrastylesheet
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<!--BEGIN TITLEAREA-->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<!--BEGIN PROJECT_LOGO-->
<td id="projectlogo"><img alt="Logo" src="$relpath^$projectlogo"/></td>
<!--END PROJECT_LOGO-->
<!--BEGIN PROJECT_NAME-->
<td style="padding-left: 0.5em;">
<div id="projectname">avr-libc
<!--BEGIN PROJECT_NUMBER-->&#160;<span id="projectnumber">2.0.0</span><!--END PROJECT_NUMBER-->
</div>
<!--BEGIN PROJECT_BRIEF--><div id="projectbrief">$projectbrief</div><!--END PROJECT_BRIEF-->
</td>
<!--END PROJECT_NAME-->
<!--BEGIN !PROJECT_NAME-->
<!--BEGIN PROJECT_BRIEF-->
<td style="padding-left: 0.5em;">
<div id="projectbrief">$projectbrief</div>
</td>
<!--END PROJECT_BRIEF-->
<!--END !PROJECT_NAME-->
<!--BEGIN DISABLE_INDEX-->
<!--BEGIN SEARCHENGINE-->
<td>$searchbox</td>
<!--END SEARCHENGINE-->
<!--END DISABLE_INDEX-->
</tr>
</tbody>
</table>
<table>
<tr>
<td align="left"><a href="http://www.nongnu.org/avr-libc/"><h2>AVR Libc Home Page</h2></a></td>
<td align="center" colspan=4><img src="avrs.png" alt="AVRs" align="middle" border="0"></td>
<td align="right"><a href="https://savannah.nongnu.org/projects/avr-libc/"><h2>AVR Libc Development Pages</h2></a></td>
</tr>
<tr>
<td align="center" width="20%"><a href="index.html"><h2>Main Page</h2></a></td>
<td align="center" width="20%"><a href="pages.html"><h2>User Manual</h2></a></td>
<td align="center" width="20%"><a href="modules.html"><h2>Library Reference</h2></a></td>
<td align="center" width="20%"><a href="FAQ.html"><h2>FAQ</h2></a></td>
<td align="center" width="20%"><a href="group__demos.html"><h2>Example Projects</h2></a></td>
</tr>
</table>
</div>
<!--END TITLEAREA-->
<!-- end header part -->
<!-- Generated by Doxygen 1.6.3 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div class="contents">
<h1>&lt;avr/sleep.h&gt;: Power Management and Sleep Modes</h1><table border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="2"><h2>Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__avr__sleep.html#ga475174a7aa4eda03dfa7a4483e400a9e">sleep_enable</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__avr__sleep.html#gaeae22433a78fd8d50f915fb68c416efd">sleep_disable</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__avr__sleep.html#ga157b2578d95309c197b739f812938d94">sleep_cpu</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__avr__sleep.html#ga3775b21f297187752bcfc434a541209a">sleep_mode</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__avr__sleep.html#gabf889562cc5ea768ee80cfc8a5bb0312">sleep_bod_disable</a> (void)</td></tr>
</table>
<hr/><a name="_details"></a><h2>Detailed Description</h2>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;<a class="code" href="sleep_8h.html">avr/sleep.h</a>&gt;</span>
</pre></div><p>Use of the <code>SLEEP</code> instruction can allow an application to reduce its power comsumption considerably. AVR devices can be put into different sleep modes. Refer to the datasheet for the details relating to the device you are using.</p>
<p>There are several macros provided in this header file to actually put the device into sleep mode. The simplest way is to optionally set the desired sleep mode using <code>set_sleep_mode()</code> (it usually defaults to idle mode where the CPU is put on sleep but all peripheral clocks are still running), and then call <code><a class="el" href="group__avr__sleep.html#ga3775b21f297187752bcfc434a541209a">sleep_mode()</a></code>. This macro automatically sets the sleep enable bit, goes to sleep, and clears the sleep enable bit.</p>
<p>Example: </p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;<a class="code" href="sleep_8h.html">avr/sleep.h</a>&gt;</span>
...
set_sleep_mode(&lt;mode&gt;);
<a class="code" href="group__avr__sleep.html#ga3775b21f297187752bcfc434a541209a">sleep_mode</a>();
</pre></div><p>Note that unless your purpose is to completely lock the CPU (until a hardware reset), interrupts need to be enabled before going to sleep.</p>
<p>As the <code><a class="el" href="group__avr__sleep.html#ga3775b21f297187752bcfc434a541209a">sleep_mode()</a></code> macro might cause race conditions in some situations, the individual steps of manipulating the sleep enable (SE) bit, and actually issuing the <code>SLEEP</code> instruction, are provided in the macros <code><a class="el" href="group__avr__sleep.html#ga475174a7aa4eda03dfa7a4483e400a9e">sleep_enable()</a></code>, <code><a class="el" href="group__avr__sleep.html#gaeae22433a78fd8d50f915fb68c416efd">sleep_disable()</a></code>, and <code><a class="el" href="group__avr__sleep.html#ga157b2578d95309c197b739f812938d94">sleep_cpu()</a></code>. This also allows for test-and-sleep scenarios that take care of not missing the interrupt that will awake the device from sleep.</p>
<p>Example: </p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;<a class="code" href="interrupt_8h.html">avr/interrupt.h</a>&gt;</span>
<span class="preprocessor"> #include &lt;<a class="code" href="sleep_8h.html">avr/sleep.h</a>&gt;</span>
...
set_sleep_mode(&lt;mode&gt;);
<a class="code" href="group__avr__interrupts.html#ga68c330e94fe121eba993e5a5973c3162">cli</a>();
<span class="keywordflow">if</span> (some_condition)
{
<a class="code" href="group__avr__sleep.html#ga475174a7aa4eda03dfa7a4483e400a9e">sleep_enable</a>();
<a class="code" href="group__avr__interrupts.html#gaad5ebd34cb344c26ac87594f79b06b73">sei</a>();
<a class="code" href="group__avr__sleep.html#ga157b2578d95309c197b739f812938d94">sleep_cpu</a>();
<a class="code" href="group__avr__sleep.html#gaeae22433a78fd8d50f915fb68c416efd">sleep_disable</a>();
}
<a class="code" href="group__avr__interrupts.html#gaad5ebd34cb344c26ac87594f79b06b73">sei</a>();
</pre></div><p>This sequence ensures an atomic test of <code>some_condition</code> with interrupts being disabled. If the condition is met, sleep mode will be prepared, and the <code>SLEEP</code> instruction will be scheduled immediately after an <code>SEI</code> instruction. As the intruction right after the <code>SEI</code> is guaranteed to be executed before an interrupt could trigger, it is sure the device will really be put to sleep.</p>
<p>Some devices have the ability to disable the Brown Out Detector (BOD) before going to sleep. This will also reduce power while sleeping. If the specific AVR device has this ability then an additional macro is defined: <code><a class="el" href="group__avr__sleep.html#gabf889562cc5ea768ee80cfc8a5bb0312">sleep_bod_disable()</a></code>. This macro generates inlined assembly code that will correctly implement the timed sequence for disabling the BOD before sleeping. However, there is a limited number of cycles after the BOD has been disabled that the device can be put into sleep mode, otherwise the BOD will not truly be disabled. Recommended practice is to disable the BOD (<code><a class="el" href="group__avr__sleep.html#gabf889562cc5ea768ee80cfc8a5bb0312">sleep_bod_disable()</a></code>), set the interrupts (<code><a class="el" href="group__avr__interrupts.html#gaad5ebd34cb344c26ac87594f79b06b73">sei()</a></code>), and then put the device to sleep (<code><a class="el" href="group__avr__sleep.html#ga157b2578d95309c197b739f812938d94">sleep_cpu()</a></code>), like so:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;<a class="code" href="interrupt_8h.html">avr/interrupt.h</a>&gt;</span>
<span class="preprocessor"> #include &lt;<a class="code" href="sleep_8h.html">avr/sleep.h</a>&gt;</span>
...
set_sleep_mode(&lt;mode&gt;);
<a class="code" href="group__avr__interrupts.html#ga68c330e94fe121eba993e5a5973c3162">cli</a>();
<span class="keywordflow">if</span> (some_condition)
{
<a class="code" href="group__avr__sleep.html#ga475174a7aa4eda03dfa7a4483e400a9e">sleep_enable</a>();
<a class="code" href="group__avr__sleep.html#gabf889562cc5ea768ee80cfc8a5bb0312">sleep_bod_disable</a>();
<a class="code" href="group__avr__interrupts.html#gaad5ebd34cb344c26ac87594f79b06b73">sei</a>();
<a class="code" href="group__avr__sleep.html#ga157b2578d95309c197b739f812938d94">sleep_cpu</a>();
<a class="code" href="group__avr__sleep.html#gaeae22433a78fd8d50f915fb68c416efd">sleep_disable</a>();
}
<a class="code" href="group__avr__interrupts.html#gaad5ebd34cb344c26ac87594f79b06b73">sei</a>();
</pre></div> <hr/><h2>Function Documentation</h2>
<a class="anchor" id="gabf889562cc5ea768ee80cfc8a5bb0312"></a><!-- doxytag: member="sleep.h::sleep_bod_disable" ref="gabf889562cc5ea768ee80cfc8a5bb0312" args="(void)" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void sleep_bod_disable </td>
<td>(</td>
<td class="paramtype">void&nbsp;</td>
<td class="paramname"></td>
<td>&nbsp;)&nbsp;</td>
<td></td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Disable BOD before going to sleep. Not available on all devices. </p>
</div>
</div>
<a class="anchor" id="ga157b2578d95309c197b739f812938d94"></a><!-- doxytag: member="sleep.h::sleep_cpu" ref="ga157b2578d95309c197b739f812938d94" args="(void)" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void sleep_cpu </td>
<td>(</td>
<td class="paramtype">void&nbsp;</td>
<td class="paramname"></td>
<td>&nbsp;)&nbsp;</td>
<td></td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Put the device into sleep mode. The SE bit must be set beforehand, and it is recommended to clear it afterwards. </p>
</div>
</div>
<a class="anchor" id="gaeae22433a78fd8d50f915fb68c416efd"></a><!-- doxytag: member="sleep.h::sleep_disable" ref="gaeae22433a78fd8d50f915fb68c416efd" args="(void)" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void sleep_disable </td>
<td>(</td>
<td class="paramtype">void&nbsp;</td>
<td class="paramname"></td>
<td>&nbsp;)&nbsp;</td>
<td></td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Clear the SE (sleep enable) bit. </p>
</div>
</div>
<a class="anchor" id="ga475174a7aa4eda03dfa7a4483e400a9e"></a><!-- doxytag: member="sleep.h::sleep_enable" ref="ga475174a7aa4eda03dfa7a4483e400a9e" args="(void)" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void sleep_enable </td>
<td>(</td>
<td class="paramtype">void&nbsp;</td>
<td class="paramname"></td>
<td>&nbsp;)&nbsp;</td>
<td></td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Put the device in sleep mode. How the device is brought out of sleep mode depends on the specific mode selected with the set_sleep_mode() function. See the data sheet for your device for more details.</p>
<p>Set the SE (sleep enable) bit. </p>
</div>
</div>
<a class="anchor" id="ga3775b21f297187752bcfc434a541209a"></a><!-- doxytag: member="sleep.h::sleep_mode" ref="ga3775b21f297187752bcfc434a541209a" args="(void)" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void sleep_mode </td>
<td>(</td>
<td class="paramtype">void&nbsp;</td>
<td class="paramname"></td>
<td>&nbsp;)&nbsp;</td>
<td></td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Put the device into sleep mode, taking care of setting the SE bit before, and clearing it afterwards. </p>
</div>
</div>
</div>
<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&nbsp;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&nbsp;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&nbsp;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&nbsp;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&nbsp;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&nbsp;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&nbsp;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&nbsp;</span>Defines</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<!-- HTML footer for doxygen 1.8.7-->
<!-- start footer part -->
<!--BEGIN GENERATE_TREEVIEW-->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
$navpath
<li class="footer">$generatedby
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="$relpath^doxygen.png" alt="doxygen"/></a> 1.6.3 </li>
</ul>
</div>
<!--END GENERATE_TREEVIEW-->
<!--BEGIN !GENERATE_TREEVIEW-->
<hr class="footer"/><address class="footer"><small>
$generatedby &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="$relpath^doxygen.png" alt="doxygen"/>
</a> 1.6.3
</small></address>
<!--END !GENERATE_TREEVIEW-->
</body>
</html>