blob: 0ef9b282ff03828b59e8ce138d5bb014f533aedb [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>I2C &mdash; Apache Mynewt latest documentation</title>
<link rel="shortcut icon" href="../../../../_static/mynewt-logo-only-newt32x32.png"/>
<link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/css/sphinx_theme.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/css/bootstrap-3.0.3.min.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/css/v2.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/css/restructuredtext.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/css/overrides.css" type="text/css" />
<link rel="index" title="Index"
href="../../../../genindex.html"/>
<link rel="search" title="Search" href="../../../../search.html"/>
<link rel="top" title="Apache Mynewt latest documentation" href="../../../../index.html"/>
<link rel="up" title="Hardware Abstraction Layer" href="../hal.html"/>
<link rel="next" title="Flash" href="../hal_flash/hal_flash.html"/>
<link rel="prev" title="SPI" href="../hal_spi/hal_spi.html"/>
<script src="../../../../_static/js/modernizr.min.js"></script>
<script>
(function(i, s, o, g, r, a, m) {
i["GoogleAnalyticsObject"] = r;
(i[r] =
i[r] ||
function() {
(i[r].q = i[r].q || []).push(arguments);
}),
(i[r].l = 1 * new Date());
(a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]);
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
})(window, document, "script", "//www.google-analytics.com/analytics.js", "ga");
ga("create", "UA-72162311-1", "auto");
ga("send", "pageview");
</script>
</head>
<body class="not-front page-documentation" role="document" >
<div id="wrapper">
<div class="container">
<div id="banner" class="row v2-main-banner">
<a class="logo-cell" href="/">
<img class="logo" src="../../../../_static/img/logo.png">
</a>
<div class="tagline-cell">
<h4 class="tagline">An OS to build, deploy and securely manage billions of devices</h4>
</div>
<div class="news-cell">
<div class="well">
<h4>Latest News:</h4> <a href="/download">Apache Mynewt 1.11.0, Apache NimBLE 1.6.0 </a> released September 7, 2023)
</div>
</div>
</div>
</div>
<header>
<nav id="navbar" class="navbar navbar-inverse" role="navigation">
<div class="container">
<!-- Collapsed navigation -->
<div class="navbar-header">
<!-- Expander button -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Expanded navigation -->
<div class="navbar-collapse collapse">
<!-- Main navigation -->
<ul class="nav navbar-nav navbar-right">
<li>
<a href="/"><i class="fa fa-home" style="font-size: larger;"></i></a>
</li>
<li class="important">
<a href="/quick-start/">Quick Start</a>
</li>
<li>
<a href="/about/">About</a>
</li>
<li>
<a href="/talks/">Talks</a>
</li>
<li class="active">
<a href="/documentation/">Documentation</a>
</li>
<li>
<a href="/download/">Download</a>
</li>
<li>
<a href="/community/">Community</a>
</li>
<li>
<a href="/events/">Events</a>
</li>
</ul>
<!-- Search, Navigation and Repo links -->
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
</div>
</nav>
</header>
<!-- STARTS MAIN CONTENT -->
<div id="main-content">
<div id="breadcrumb">
<div class="container">
<a href="/documentation/">Docs</a> /
<a href="../../../os_user_guide.html">OS User Guide</a> /
<a href="../hal.html">Hardware Abstraction Layer</a> /
I2C
<div class="sourcelink">
<a href="https://github.com/apache/mynewt-core/edit/master/docs/os/modules/hal/hal_i2c/hal_i2c.rst" class="icon icon-github"
rel="nofollow"> Edit on GitHub</a>
</div>
</div>
</div>
<!-- STARTS CONTAINER -->
<div class="container">
<!-- STARTS .content -->
<div id="content" class="row">
<!-- STARTS .container-sidebar -->
<div class="container-sidebar col-xs-12 col-sm-3">
<div id="docSidebar" class="sticky-container">
<div role="search" class="sphinx-search">
<form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
<input type="text" name="q" placeholder="Search documentation" class="search-documentation" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<!-- Note: only works when deployed -->
<select class="form-control" onchange="if (this.value) window.location.href=this.value">
<option value="/latest" selected>
Version: latest
</option>
<option value="/v1_11_0" >
Version: 1.11.0
</option>
<option value="/v1_10_0" >
Version: 1.10.0
</option>
<option value="/v1_9_0" >
Version: 1.9.0
</option>
<option value="/v1_8_0" >
Version: 1.8.0
</option>
<option value="/v1_7_0" >
Version: 1.7.0
</option>
<option value="/v1_6_0" >
Version: 1.6.0
</option>
<option value="/v1_5_0" selected="selected" >
Version: 1.5.0
</option>
<option value="/v1_4_0" >
Version: 1.4.0
</option>
<option value="/v1_3_0/os/introduction" >
Version: 1.3.0
</option>
<option value="/v1_2_0/os/introduction" >
Version: 1.2.0
</option>
<option value="/v1_1_0/os/introduction" >
Version: 1.1.0
</option>
<option value="/v1_0_0/os/introduction" >
Version: 1.0.0
</option>
<option value="/v0_9_0/os/introduction" >
Version: 0.9.0
</option>
</select>
<div class="region region-sidebar">
<div class="docs-menu">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../../../index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../get_started/index.html">Setup &amp; Get Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../concepts.html">Concepts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorials/tutorials.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../external_links.html">Third-party Resources</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../../../os_user_guide.html">OS User Guide</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../../../core_os/mynewt_os.html">Kernel</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../system_modules.html">System</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../hal.html">Hardware Abstraction</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../hal_timer/hal_timer.html">Timer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../hal_gpio/hal_gpio.html">GPIO</a></li>
<li class="toctree-l3"><a class="reference internal" href="../hal_uart/hal_uart.html">UART</a></li>
<li class="toctree-l3"><a class="reference internal" href="../hal_spi/hal_spi.html">SPI</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">I2C</a></li>
<li class="toctree-l3"><a class="reference internal" href="../hal_flash/hal_flash.html">Flash</a></li>
<li class="toctree-l3"><a class="reference internal" href="../hal_watchdog/hal_watchdog.html">Watchdog</a></li>
<li class="toctree-l3"><a class="reference internal" href="../hal_bsp/hal_bsp.html">BSP</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../bootloader/bootloader.html">Secure Bootloader</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../split/split.html">Split Images</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../core_os/porting/port_os.html">Porting Guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../baselibc.html">Baselibc</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../drivers/driver.html">Drivers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../devmgmt/newtmgr.html">Device Management with Newt Manager</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../imgmgr/imgmgr.html">Image Manager</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../sysinitconfig/sysinitconfig.html">Compile-Time Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../fs/fs.html">File System</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../fcb/fcb.html">Flash Circular Buffer</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../sensor_framework/sensor_framework.html">Sensor Framework</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../testutil/testutil.html">Test Utilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../json/json.html">JSON</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../../network/index.html">BLE User Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../newt/index.html">Newt Tool Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../newtmgr/index.html">Newt Manager Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../mynewt_faq/index.html">Mynewt FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../misc/index.html">Appendix</a></li>
</ul>
</div>
</div>
</div>
<!-- ENDS STICKY CONTAINER -->
</div>
<!-- ENDS .container-sidebar -->
<div class="col-xs-12 col-sm-9">
<div class="alert alert-warning">
<p>
Version 1.5.0 is not the most recent version of the
Apache Mynewt documentation. Click <a href="/latest">here</a> to
read the latest version.
</p>
</div>
<div class="">
<div class="rst-content">
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="i2c">
<h1>I2C<a class="headerlink" href="#i2c" title="Permalink to this headline"></a></h1>
<p>The hardware independent interface to I2C Devices.</p>
<div class="section" id="description">
<h2>Description<a class="headerlink" href="#description" title="Permalink to this headline"></a></h2>
<p>An Inter-Integrated Circuit (I²C ] I-squared-C) bus is a multi-master,
multi-save serial interface used to connect components on a circuit
board and often peripherals devices located off the circuit board.</p>
<p>I2C is often though of as a 2-wire protocol because it uses two wires
(SDA, SCL) to send data between devices.</p>
<p>For a detailed description of I2C, see the <a class="reference external" href="https://en.wikipedia.org/wiki/I²C">I²C wikipedia
page</a></p>
</div>
<div class="section" id="hal-i2c-theory-of-operation">
<h2>HAL_I2C Theory Of Operation<a class="headerlink" href="#hal-i2c-theory-of-operation" title="Permalink to this headline"></a></h2>
<p>An I²C transaction typically involves acquiring the bus, sending and/or
receiving data and release the bus. The bus acquisition portion is
important because the bus is typically multi-master so other devices may
be trying to read/write the same peripheral.</p>
<p>HAL_I2C implements a master interface to the I²C bus. Typical usage of
the interface would involve the following steps.</p>
<p>Initialize an i2c device with: hal_i2c_init()</p>
<p>When you wish to perform an i2c transaction, you call one or both of:
hal_i2c_master_write(); hal_i2c_master_read();</p>
<p>These functions will issue a START condition, followed by the device’s
7-bit I2C address, and then send or receive the payload based on the
data provided. This will cause a repeated start on the bus, which is
valid in I2C specification, and the decision to use repeated starts was
made to simplify the I2C HAL. To set the STOP condition at an
appropriate moment, you set the <code class="docutils literal notranslate"><span class="pre">last_op</span></code> field to a <code class="docutils literal notranslate"><span class="pre">1</span></code> in either
function.</p>
<p>For example, in an I2C memory access you might write a register address
and then read data back via: hal_i2c_write(); – write to a specific
register on the device hal_i2c_read(); — read back data, setting
‘last_op’ to ‘1’</p>
<p>An addition API was added called <code class="docutils literal notranslate"><span class="pre">hal_i2c_probe</span></code>. This command
combines <code class="docutils literal notranslate"><span class="pre">hal_i2c_begin()</span></code>, <code class="docutils literal notranslate"><span class="pre">hal_i2c_read</span></code>, and <code class="docutils literal notranslate"><span class="pre">hal_i2c_end()</span></code> to
try to read 0-bytes from a specific bus address. its intended to provide
an easy way to probe the bus for a specific device. NOTE: if the device
is write-only, it will not appear with this command.</p>
<p>A slave API is pending for further release.</p>
</div>
<div class="section" id="hal-i2c-data">
<h2>HAL_I2C Data<a class="headerlink" href="#hal-i2c-data" title="Permalink to this headline"></a></h2>
<p>Data to read/write is passed to the hal_i2c APIs via the</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct hal_i2c_master_data {
uint8_t address; /* destination address */
uint16_t len; /* number of bytes to transmit or receive */
uint8_t *buffer; /* data buffer for transmit or receive */
};
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">buffer</span></code> is a pointer to the data to send. <code class="docutils literal notranslate"><span class="pre">len</span></code> is the number of
bytes to send over the bus. <code class="docutils literal notranslate"><span class="pre">address</span></code> is a 7-bit bus address of the
device.</p>
<p>When I²C builds its address, it uses the 7-bit address plus a 1-bit R/W
(read/write) indicator to identify the device and direction of the
transaction. Thus when using this API, you should use a 7-bit address in
the data structure and ensure that address is a value between 0-127.</p>
<p>As an example, consider an I²C device address that looks like this:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 14%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 14%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>B7</p></th>
<th class="head"><p>B6</p></th>
<th class="head"><p>B5</p></th>
<th class="head"><p>B4</p></th>
<th class="head"><p>B3</p></th>
<th class="head"><p>B2</p></th>
<th class="head"><p>B1</p></th>
<th class="head"><p>B0</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>1</p></td>
<td><p>0</p></td>
<td><p>0</p></td>
<td><p>0</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>0</p></td>
<td><p>R/W</p></td>
</tr>
<tr class="row-odd"><td><p>MSB</p></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><p>LSB</p></td>
</tr>
</tbody>
</table>
<p>In the HAL_I2C API you would communicate with this device with address
<code class="docutils literal notranslate"><span class="pre">0b1000110</span></code>, which is hex 0x46 or decimal 70. The I²C drive would add
the R/W bit and transmit it as hex 0x8C (binary 10001100) or 0x8D
(binary 10001101) depending whether it was a read or write command.</p>
</div>
<div class="section" id="api">
<h2>API<a class="headerlink" href="#api" title="Permalink to this headline"></a></h2>
<dl class="c function">
<dt id="c.hal_i2c_init">
<span class="target" id="group___h_a_l_i2c_1ga168e312fdccc376294a2f537a25e06d8"></span><span class="pre">int</span> <code class="sig-name descname"><span class="pre">hal_i2c_init</span></code><span class="sig-paren">(</span><span class="pre">uint8_t</span> <em><span class="pre">i2c_num</span></em>, <span class="pre">void</span> <span class="pre">*</span><em><span class="pre">cfg</span></em><span class="sig-paren">)</span><a class="headerlink" href="#c.hal_i2c_init" title="Permalink to this definition"></a><br /></dt>
<dd><p>Initialize a new i2c device with the I2C number. </p>
<p><dl class="simple">
<dt><strong>Return</strong></dt><dd><p>0 on success, and non-zero error code on failure </p>
</dd>
<dt><strong>Parameters</strong></dt><dd><ul class="breatheparameterlist simple">
<li><p><code class="docutils literal notranslate"><span class="pre">i2c_num</span></code>: The number of the I2C device being initialized </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">cfg</span></code>: The hardware specific configuration structure to configure the I2C with. This includes things like pin configuration.</p></li>
</ul>
</dd>
</dl>
</p>
</dd></dl>
<dl class="c function">
<dt id="c.hal_i2c_master_write">
<span class="target" id="group___h_a_l_i2c_1ga647137bb25fef80383b3c58691c6e173"></span><span class="pre">int</span> <code class="sig-name descname"><span class="pre">hal_i2c_master_write</span></code><span class="sig-paren">(</span><span class="pre">uint8_t</span> <em><span class="pre">i2c_num</span></em>, <em class="property"><span class="pre">struct</span></em> <a class="reference internal" href="#c.hal_i2c_master_data" title="hal_i2c_master_data"><span class="pre">hal_i2c_master_data</span></a> <span class="pre">*</span><em><span class="pre">pdata</span></em>, <span class="pre">uint32_t</span> <em><span class="pre">timeout</span></em>, <span class="pre">uint8_t</span> <em><span class="pre">last_op</span></em><span class="sig-paren">)</span><a class="headerlink" href="#c.hal_i2c_master_write" title="Permalink to this definition"></a><br /></dt>
<dd><p>Sends a start condition and writes &lt;len&gt; bytes of data on the i2c bus. </p>
<p>This API does NOT issue a stop condition unless <code class="docutils literal notranslate"><span class="pre">last_op</span></code> is set to <code class="docutils literal notranslate"><span class="pre">1</span></code>. You must stop the bus after successful or unsuccessful write attempts. This API is blocking until an error or NaK occurs. Timeout is platform dependent.</p>
<p><dl class="simple">
<dt><strong>Return</strong></dt><dd><p>0 on success, and non-zero error code on failure </p>
</dd>
<dt><strong>Parameters</strong></dt><dd><ul class="breatheparameterlist simple">
<li><p><code class="docutils literal notranslate"><span class="pre">i2c_num</span></code>: The number of the I2C device being written to </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">pdata</span></code>: The data to write to the I2C bus </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">timeout</span></code>: How long to wait for transaction to complete in ticks </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">last_op</span></code>: Master should send a STOP at the end to signify end of transaction.</p></li>
</ul>
</dd>
</dl>
</p>
</dd></dl>
<dl class="c function">
<dt id="c.hal_i2c_master_read">
<span class="target" id="group___h_a_l_i2c_1gab19941d920a74650ebd9f9790f48fa62"></span><span class="pre">int</span> <code class="sig-name descname"><span class="pre">hal_i2c_master_read</span></code><span class="sig-paren">(</span><span class="pre">uint8_t</span> <em><span class="pre">i2c_num</span></em>, <em class="property"><span class="pre">struct</span></em> <a class="reference internal" href="#c.hal_i2c_master_data" title="hal_i2c_master_data"><span class="pre">hal_i2c_master_data</span></a> <span class="pre">*</span><em><span class="pre">pdata</span></em>, <span class="pre">uint32_t</span> <em><span class="pre">timeout</span></em>, <span class="pre">uint8_t</span> <em><span class="pre">last_op</span></em><span class="sig-paren">)</span><a class="headerlink" href="#c.hal_i2c_master_read" title="Permalink to this definition"></a><br /></dt>
<dd><p>Sends a start condition and reads &lt;len&gt; bytes of data on the i2c bus. </p>
<p>This API does NOT issue a stop condition unless <code class="docutils literal notranslate"><span class="pre">last_op</span></code> is set to <code class="docutils literal notranslate"><span class="pre">1</span></code>. You must stop the bus after successful or unsuccessful write attempts. This API is blocking until an error or NaK occurs. Timeout is platform dependent.</p>
<p><dl class="simple">
<dt><strong>Return</strong></dt><dd><p>0 on success, and non-zero error code on failure </p>
</dd>
<dt><strong>Parameters</strong></dt><dd><ul class="breatheparameterlist simple">
<li><p><code class="docutils literal notranslate"><span class="pre">i2c_num</span></code>: The number of the I2C device being written to </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">pdata</span></code>: The location to place read data </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">timeout</span></code>: How long to wait for transaction to complete in ticks </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">last_op</span></code>: Master should send a STOP at the end to signify end of transaction.</p></li>
</ul>
</dd>
</dl>
</p>
</dd></dl>
<dl class="c function">
<dt id="c.hal_i2c_master_probe">
<span class="target" id="group___h_a_l_i2c_1ga53de5cf24903b7984a226276dd2a117e"></span><span class="pre">int</span> <code class="sig-name descname"><span class="pre">hal_i2c_master_probe</span></code><span class="sig-paren">(</span><span class="pre">uint8_t</span> <em><span class="pre">i2c_num</span></em>, <span class="pre">uint8_t</span> <em><span class="pre">address</span></em>, <span class="pre">uint32_t</span> <em><span class="pre">timeout</span></em><span class="sig-paren">)</span><a class="headerlink" href="#c.hal_i2c_master_probe" title="Permalink to this definition"></a><br /></dt>
<dd><p>Probes the i2c bus for a device with this address. </p>
<p>THIS API issues a start condition, probes the address using a read command and issues a stop condition.</p>
<p><dl class="simple">
<dt><strong>Return</strong></dt><dd><p>0 on success, non-zero error code on failure </p>
</dd>
<dt><strong>Parameters</strong></dt><dd><ul class="breatheparameterlist simple">
<li><p><code class="docutils literal notranslate"><span class="pre">i2c_num</span></code>: The number of the I2C to probe </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">address</span></code>: The address to probe for </p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">timeout</span></code>: How long to wait for transaction to complete in ticks</p></li>
</ul>
</dd>
</dl>
</p>
</dd></dl>
<dl class="c macro">
<dt id="c.HAL_I2C_ERR_UNKNOWN">
<span class="target" id="group___h_a_l_i2c_1ga3ae4680b7bab1e59e0971e89628c7d41"></span><code class="sig-name descname"><span class="pre">HAL_I2C_ERR_UNKNOWN</span></code><a class="headerlink" href="#c.HAL_I2C_ERR_UNKNOWN" title="Permalink to this definition"></a><br /></dt>
<dd><p>This is the API for an i2c bus. </p>
<p>Currently, this is a master API allowing the mynewt device to function as an I2C master.</p>
<p>A slave API is pending for future release</p>
<p>Typical usage of this API is as follows:</p>
<p>Initialize an i2c device with: :c:func:<code class="docutils literal notranslate"><a class="reference internal" href="#group___h_a_l_i2c_1ga168e312fdccc376294a2f537a25e06d8"><span class="std std-ref"><span class="pre">hal_i2c_init()</span></span></a></code></p>
<p>When you wish to perform an i2c transaction, you call one or both of: :c:func:<code class="docutils literal notranslate"><a class="reference internal" href="#group___h_a_l_i2c_1ga647137bb25fef80383b3c58691c6e173"><span class="std std-ref"><span class="pre">hal_i2c_master_write()</span></span></a></code>; :c:func:<code class="docutils literal notranslate"><a class="reference internal" href="#group___h_a_l_i2c_1gab19941d920a74650ebd9f9790f48fa62"><span class="std std-ref"><span class="pre">hal_i2c_master_read()</span></span></a></code>;</p>
<p>These functions will issue a START condition, followed by the device’s 7-bit I2C address, and then send or receive the payload based on the data provided. This will cause a repeated start on the bus, which is valid in I2C specification, and the decision to use repeated starts was made to simplify the I2C HAL. To set the STOP condition at an appropriate moment, you set the <code class="docutils literal notranslate"><span class="pre">last_op</span></code> field to a <code class="docutils literal notranslate"><span class="pre">1</span></code> in either function.</p>
<p>For example, in an I2C memory access you might write a register address and then read data back via: :c:func:<code class="docutils literal notranslate"><span class="pre">hal_i2c_write()</span></code>; write to a specific register on the device :c:func:<code class="docutils literal notranslate"><span class="pre">hal_i2c_read()</span></code>; read back data, setting ‘last_op’ to ‘1’ Unknown error. </p>
</dd></dl>
<dl class="c macro">
<dt id="c.HAL_I2C_ERR_INVAL">
<span class="target" id="group___h_a_l_i2c_1gad168d98e6d009b9d37bffe86de381037"></span><code class="sig-name descname"><span class="pre">HAL_I2C_ERR_INVAL</span></code><a class="headerlink" href="#c.HAL_I2C_ERR_INVAL" title="Permalink to this definition"></a><br /></dt>
<dd><p>Invalid argument. </p>
</dd></dl>
<dl class="c macro">
<dt id="c.HAL_I2C_ERR_TIMEOUT">
<span class="target" id="group___h_a_l_i2c_1ga7fbb0bb75d30973f0762a5610c60a821"></span><code class="sig-name descname"><span class="pre">HAL_I2C_ERR_TIMEOUT</span></code><a class="headerlink" href="#c.HAL_I2C_ERR_TIMEOUT" title="Permalink to this definition"></a><br /></dt>
<dd><p>MCU failed to report result of I2C operation. </p>
</dd></dl>
<dl class="c macro">
<dt id="c.HAL_I2C_ERR_ADDR_NACK">
<span class="target" id="group___h_a_l_i2c_1gacd14136f01b7aa85970dfaed4bdddbf6"></span><code class="sig-name descname"><span class="pre">HAL_I2C_ERR_ADDR_NACK</span></code><a class="headerlink" href="#c.HAL_I2C_ERR_ADDR_NACK" title="Permalink to this definition"></a><br /></dt>
<dd><p>Slave responded to address with NACK. </p>
</dd></dl>
<dl class="c macro">
<dt id="c.HAL_I2C_ERR_DATA_NACK">
<span class="target" id="group___h_a_l_i2c_1gadc2527d6d99811e7553d6d0d379235d7"></span><code class="sig-name descname"><span class="pre">HAL_I2C_ERR_DATA_NACK</span></code><a class="headerlink" href="#c.HAL_I2C_ERR_DATA_NACK" title="Permalink to this definition"></a><br /></dt>
<dd><p>Slave responded to data byte with NACK. </p>
</dd></dl>
<dl class="c struct">
<dt id="c.hal_i2c_master_data">
<span class="target" id="structhal__i2c__master__data"></span><em class="property"><span class="pre">struct</span> </em><code class="sig-name descname"><span class="pre">hal_i2c_master_data</span></code><a class="headerlink" href="#c.hal_i2c_master_data" title="Permalink to this definition"></a><br /></dt>
<dd><em>#include &lt;hal_i2c.h&gt;</em><p>When sending a packet, use this structure to pass the arguments. </p>
<div class="breathe-sectiondef docutils container">
<p class="breathe-sectiondef-title rubric" id="breathe-section-title-public-members">Public Members</p>
<dl class="c var">
<dt id="c.hal_i2c_master_data.address">
<span class="target" id="structhal__i2c__master__data_1a4d8e02692037b21eaf2a4528b24c03ca"></span><span class="pre">uint8_t</span> <code class="sig-name descname"><span class="pre">address</span></code><a class="headerlink" href="#c.hal_i2c_master_data.address" title="Permalink to this definition"></a><br /></dt>
<dd><p>Destination address An I2C address has 7 bits. </p>
<p>In the protocol these 7 bits are combined with a 1 bit R/W bit to specify read or write operation in an 8-bit address field sent to the remote device. This API accepts the 7-bit address as its argument in the 7 LSBs of the address field above. For example if I2C was writing a 0x81 in its protocol, you would pass only the top 7-bits to this function as 0x40 </p>
</dd></dl>
<dl class="c var">
<dt id="c.hal_i2c_master_data.len">
<span class="target" id="structhal__i2c__master__data_1af035be10a4a7702e303e4640e8b9938f"></span><span class="pre">uint16_t</span> <code class="sig-name descname"><span class="pre">len</span></code><a class="headerlink" href="#c.hal_i2c_master_data.len" title="Permalink to this definition"></a><br /></dt>
<dd><p>Number of buffer bytes to transmit or receive. </p>
</dd></dl>
<dl class="c var">
<dt id="c.hal_i2c_master_data.buffer">
<span class="target" id="structhal__i2c__master__data_1ad26ebbf5cce03225f2275d83c5de70b1"></span><span class="pre">uint8_t</span> <span class="pre">*</span><code class="sig-name descname"><span class="pre">buffer</span></code><a class="headerlink" href="#c.hal_i2c_master_data.buffer" title="Permalink to this definition"></a><br /></dt>
<dd><p>Buffer space to hold the transmit or receive. </p>
</dd></dl>
</div>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="rst-footer-buttons row" role="navigation" aria-label="footer navigation">
<a href="../hal_flash/hal_flash.html" class="btn btn-neutral float-right" title="Flash" accesskey="n">Next: Flash <span class="fa fa-arrow-circle-right"></span></a>
<a href="../hal_spi/hal_spi.html" class="btn btn-neutral" title="SPI" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous: SPI</a>
</div>
</div>
</div>
</div>
<!-- ENDS CONTENT SECTION -->
</div>
<!-- ENDS .content -->
</div>
</div>
<footer>
<div class="container">
<div class="row">
<div class="col-xs-12">
<p class="copyright">Apache Mynewt is available under Apache License, version 2.0.</p>
</div>
<div class="col-xs-12">
<div class="logos">
<img src="../../../../_static/img/asf_logo_wide_small.png" alt="Apache" title="Apache">
<small class="footnote">
Apache Mynewt, Mynewt, Apache, the Apache feather logo, and the Apache Mynewt project logo are either
registered trademarks or trademarks of the Apache Software Foundation in the United States and other countries.
</small>
<a href="">
<img src="../../../../_static/img/add_to_slack.png" alt="Slack Icon" title="Join our Slack Community" />
</a>
</div>
</div>
</div>
</div>
</footer>
</div>
<!-- ENDS #wrapper -->
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../../../../',
VERSION:'latest',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt',
LINK_SUFFIX: '.html'
};
</script>
<script type="text/javascript" src="../../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../../_static/doctools.js"></script>
<script type="text/javascript" src="../../../../_static/js/bootstrap-3.0.3.min.js"></script>
<script type="text/javascript" src="../../../../_static/js/affix.js"></script>
<script type="text/javascript" src="../../../../_static/js/main.js"></script>
</body>
</html>