Files
2023-02-13 15:47:41 +08:00

134 lines
11 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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=11"/>
<meta name="generator" content="Doxygen 1.9.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Core2 for AWS IoT Kit BSP: Speaker</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="custom.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="AWS_logo_RGB.png"/></td>
<td id="projectalign">
<div id="projectname">Core2 for AWS IoT Kit BSP<span id="projectnumber">&#160;1.5.1</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.3 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<div><div class="header">
<div class="headertitle"><div class="title">Speaker </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p >This library provides convenience functions to interact with the on-board speaker to play audio at 44100Hz sample rate at 16-bit depth on a mono (right) channel. The NS4168 amplifier powers the on-board speaker and communicates with the ESP32-D0WD microcontroller over I2S. The I2S bus requires that the initialization and use of the peripheral on the bus is assigned to a dedicated core. The reason for this is that I2S and I2C attaches an interrupt to one of 32 interrupt vectors available per core. Attempting to initialize or access the peripheral without affinity to a specific core will most likely cause a hard fault almost immediately or at some point in time. The amplifier and the microphone share a GPIO and therefore, only one of them can be used at one time. You must de-initialize one if you intend to use the other.</p>
<p ><a class="anchor" id="NS4168"></a></p>
<p ><a href="./block_diagram-ns4168.png"><img src="./block_diagram-ns4168.png" alt="Circuit Block Diagram for the speaker powered by the NS4168 1w amplifier" class="center" class="inline"/></a></p>
<p >To enable this feature using KConfig, use the command <code>pio run --environment core2foraws --target menuconfig</code> from within the root of the project directory in your <a href="/en/blinky-hello-world/prerequisites.html#open-the-platformio-cli-terminal-window">PlatformIO terminal window</a> and go to the menu <code>Component Config --&gt; Core2 for AWS features</code> to set the features you want to enable.</p>
<h1><a class="anchor" id="speaker_example"></a>
Example</h1>
<p >The following example creates a FreeRTOS task that's pinned to Core 1, which intializes and enables the speaker and plays 32 bytes of a 16-bit 44100Hz sound buffer. Once done playing the very brief sound, it deinitializes and disables the speaker. The size of the sound buffer is too short to be recognized by human ears.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;freertos/FreeRTOS.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;freertos/task.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;esp_log.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="core2for_a_w_s_8h.html">core2forAWS.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *<a class="code hl_define" href="i2c__device_8c.html#afc3d101f633a076cc1ca84b85b6224b2">TAG</a> = <span class="stringliteral">&quot;EXAMPLE&quot;</span>;</div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> speaker_task(<span class="keywordtype">void</span> *arg){</div>
<div class="line"> <span class="comment">/* If the microphone was initialized, be sure to call Microphone_Deinit() </span></div>
<div class="line"><span class="comment"> first. */</span></div>
<div class="line"> <a class="code hl_function" href="speaker_8c.html#aabdbd6c7828802c26f42d05bafe96f53">Speaker_Init</a>();</div>
<div class="line"> <a class="code hl_function" href="core2for_a_w_s_8c.html#aacc1c4dcaa8c695050dc274e81d6d188">Core2ForAWS_Speaker_Enable</a>(1);</div>
<div class="line"> </div>
<div class="line"> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> sound[32] = {0x01,0x00,0xff,0xff,0x01,0x00,0xff,</div>
<div class="line"> 0xff,0x01,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0xff,0xff,</div>
<div class="line"> 0x01,0x00,0xff,0xff,0x01,0x00,0xff,0xff,0xff,0xff,0xff,0xff};</div>
<div class="line"> <a class="code hl_function" href="speaker_8c.html#a53db8ae233bb5adfd464cb01a8549e22">Speaker_WriteBuff</a>((uint8_t*)sound, 32, portMAX_DELAY);</div>
<div class="line"> </div>
<div class="line"> <a class="code hl_function" href="core2for_a_w_s_8c.html#aacc1c4dcaa8c695050dc274e81d6d188">Core2ForAWS_Speaker_Enable</a>(0);</div>
<div class="line"> <a class="code hl_function" href="speaker_8c.html#a6964a2985bad4f4f8c1a57f3feebbcf4">Speaker_Deinit</a>();</div>
<div class="line"> vTaskDelete(NULL);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> app_main(<span class="keywordtype">void</span>){</div>
<div class="line"> <a class="code hl_function" href="core2for_a_w_s_8c.html#a3d850a3f56bbe0685b60726d8b53bece">Core2ForAWS_Init</a>();</div>
<div class="line"> </div>
<div class="line"> <span class="comment">/* Usage of the speaker must be pinned to a specific core since I2S ISR </span></div>
<div class="line"><span class="comment"> is assigned per core */</span></div>
<div class="line"> xTaskCreatePinnedToCore(speaker_task, <span class="stringliteral">&quot;play_sound&quot;</span>, 4096*2, NULL, 4, NULL, 1);</div>
<div class="line">}</div>
<div class="ttc" id="acore2for_a_w_s_8c_html_a3d850a3f56bbe0685b60726d8b53bece"><div class="ttname"><a href="core2for_a_w_s_8c.html#a3d850a3f56bbe0685b60726d8b53bece">Core2ForAWS_Init</a></div><div class="ttdeci">void Core2ForAWS_Init(void)</div><div class="ttdoc">Initializes the power chip with default values, enables battery charging, and initializes all enabled...</div><div class="ttdef"><b>Definition:</b> core2forAWS.c:32</div></div>
<div class="ttc" id="acore2for_a_w_s_8c_html_aacc1c4dcaa8c695050dc274e81d6d188"><div class="ttname"><a href="core2for_a_w_s_8c.html#aacc1c4dcaa8c695050dc274e81d6d188">Core2ForAWS_Speaker_Enable</a></div><div class="ttdeci">void Core2ForAWS_Speaker_Enable(bool state)</div><div class="ttdoc">Enables or disables the NS4168 speaker amplifier.</div><div class="ttdef"><b>Definition:</b> core2forAWS.c:153</div></div>
<div class="ttc" id="acore2for_a_w_s_8h_html"><div class="ttname"><a href="core2for_a_w_s_8h.html">core2forAWS.h</a></div><div class="ttdoc">Functions to initialize and access Core2 for AWS IoT Kit hardware features.</div></div>
<div class="ttc" id="ai2c__device_8c_html_afc3d101f633a076cc1ca84b85b6224b2"><div class="ttname"><a href="i2c__device_8c.html#afc3d101f633a076cc1ca84b85b6224b2">TAG</a></div><div class="ttdeci">#define TAG</div><div class="ttdef"><b>Definition:</b> i2c_device.c:10</div></div>
<div class="ttc" id="aspeaker_8c_html_a53db8ae233bb5adfd464cb01a8549e22"><div class="ttname"><a href="speaker_8c.html#a53db8ae233bb5adfd464cb01a8549e22">Speaker_WriteBuff</a></div><div class="ttdeci">esp_err_t Speaker_WriteBuff(uint8_t *buff, uint32_t len, uint32_t timeout)</div><div class="ttdoc">Plays buffer through the speaker.</div><div class="ttdef"><b>Definition:</b> speaker.c:48</div></div>
<div class="ttc" id="aspeaker_8c_html_a6964a2985bad4f4f8c1a57f3feebbcf4"><div class="ttname"><a href="speaker_8c.html#a6964a2985bad4f4f8c1a57f3feebbcf4">Speaker_Deinit</a></div><div class="ttdeci">esp_err_t Speaker_Deinit()</div><div class="ttdoc">De-initializes the speaker.</div><div class="ttdef"><b>Definition:</b> speaker.c:53</div></div>
<div class="ttc" id="aspeaker_8c_html_aabdbd6c7828802c26f42d05bafe96f53"><div class="ttname"><a href="speaker_8c.html#aabdbd6c7828802c26f42d05bafe96f53">Speaker_Init</a></div><div class="ttdeci">esp_err_t Speaker_Init()</div><div class="ttdoc">Initializes the speaker over I2S. The I2S bus is initialized for output with a sample rate of 44100Hz...</div><div class="ttdef"><b>Definition:</b> speaker.c:12</div></div>
</div><!-- fragment --><h1><a class="anchor" id="speaker_functions"></a>
Functions</h1>
<ul>
<li><a class="el" href="core2foraws_speaker_enable_function.html">Core2ForAWS_Speaker_Enable</a> <br />
</li>
<li><a class="el" href="speaker_init_function.html">Speaker_Init</a> <br />
</li>
<li><a class="el" href="speaker_writebuff_function.html">Speaker_WriteBuff</a> <br />
</li>
<li><a class="el" href="speaker_deinit_function.html">Speaker_Deinit</a> <br />
</li>
</ul>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.3
</small></address>
</body>
</html>