| |
| |
| <!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>Sensor Device Driver — 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"/> |
| |
| |
| <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.12.0, Apache NimBLE 1.7.0 </a> released April 4, 2024) |
| </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> / |
| |
| Sensor Device Driver |
| |
| <div class="sourcelink"> |
| <a href="https://github.com/apache/mynewt-core/edit/master/docs/os/modules/sensor_framework/sensor_driver.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_12_0" > |
| Version: 1.12.0 |
| </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" > |
| Version: 1.5.0 |
| </option> |
| <option value="/v1_4_0" selected="selected" > |
| 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> |
| <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 & 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"><a class="reference internal" href="../../os_user_guide.html">OS User Guide</a></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.4.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="sensor-device-driver"> |
| <h1>Sensor Device Driver<a class="headerlink" href="#sensor-device-driver" title="Permalink to this headline">¶</a></h1> |
| <p>A Mynewt sensor device driver uses the sensor framework abstraction and |
| API to enable applications to access sensor data from any Mynewt sensor |
| device using a common interface. The sensor device driver must also use |
| the Mynewt HAL interface to communicate with and control a sensor |
| device.</p> |
| <p>This guide describes what a sensor device driver must implement to |
| enable a sensor device within the sensor framework. For information on |
| using the HAL API to communicate with a sensor device, see the <a class="reference external" href="/os/modules/hal/hal.html">Hardware |
| Layer Abstraction Guide</a>.</p> |
| <p>The <code class="docutils literal notranslate"><span class="pre">hw/drivers/sensors/<sensorname></span></code> package implements the device |
| driver for the sensor named <code class="docutils literal notranslate"><span class="pre">SENSORNAME</span></code>.</p> |
| <p><strong>Note:</strong> All example excerpts are from the BNO055 sensor device driver |
| package.</p> |
| <div class="section" id="initializing-and-configuring-a-sensor-device"> |
| <h2>Initializing and Configuring a Sensor Device<a class="headerlink" href="#initializing-and-configuring-a-sensor-device" title="Permalink to this headline">¶</a></h2> |
| <p>A driver package for a sensor named <code class="docutils literal notranslate"><span class="pre">SENSORNAME</span></code> must define and |
| export the following data structures and functions to initialize and |
| configure a device:</p> |
| <ul> |
| <li><p><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre"><sensorname></span></code>: This data structure represents a sensor |
| device. The structure must include a <code class="docutils literal notranslate"><span class="pre">dev</span></code> field of type |
| <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">os_dev</span></code> and a <code class="docutils literal notranslate"><span class="pre">sensor</span></code> field of type <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">sensor</span></code>. |
| For example:</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct bno055 { |
| struct os_dev dev; |
| struct sensor sensor; |
| struct bno055_cfg cfg; |
| os_time_t last_read_time; |
| }; |
| </pre></div> |
| </div> |
| </li> |
| <li><p><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre"><sensorname>_cfg</span></code>: This data structure defines the |
| configuration for a sensor device. The structure fields are specific |
| to the device. This is the data structure that a BSP, the sensor |
| creator package, or an application sets to configure a sensor device. |
| For example:</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct bno055_cfg { |
| uint8_t bc_opr_mode; |
| uint8_t bc_pwr_mode; |
| uint8_t bc_units; |
| uint8_t bc_placement; |
| uint8_t bc_acc_range; |
| uint8_t bc_acc_bw; |
| |
| ... |
| |
| uint32_t bc_mask; |
| }; |
| </pre></div> |
| </div> |
| </li> |
| <li><p><code class="docutils literal notranslate"><span class="pre"><sensorname>_init()</span></code>: This is the os device initialization |
| callback of type |
| <code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">(*os_dev_init_func_t)(struct</span> <span class="pre">os_dev</span> <span class="pre">*,</span> <span class="pre">void</span> <span class="pre">*)</span></code> that the |
| <code class="docutils literal notranslate"><span class="pre">os_dev_create()</span></code> function calls to initialize the device. For |
| example, the bno055 device driver package defines the following |
| function:</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>int bno055_init(struct os_dev *dev, void *arg) |
| </pre></div> |
| </div> |
| <p>The BSP, which creates a device for an onboard sensor, and the sensor |
| creator package, which creates a device for an off-board sensor, |
| calls the <code class="docutils literal notranslate"><span class="pre">os_dev_create()</span></code> function and passes:</p> |
| <ul class="simple"> |
| <li><p>A pointer to a <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre"><sensorname></span></code> variable.</p></li> |
| <li><p>The <code class="docutils literal notranslate"><span class="pre"><sensorname>_init()</span></code> function pointer.</p></li> |
| <li><p>A pointer to a <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">sensor_itf</span></code> variable that specifies the |
| interface the driver uses to communicate with the sensor device.</p></li> |
| </ul> |
| <p>See the <a class="reference external" href="/os/modules/sensor_framework/sensor_create.html">Creating Sensor |
| Devices</a> page for |
| more details.</p> |
| <p>The <code class="docutils literal notranslate"><span class="pre">os_dev_create()</span></code> function calls the <code class="docutils literal notranslate"><span class="pre"><sensorname>_init()</span></code> |
| function with a pointer to the <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre"><sensorname></span></code> for the |
| <code class="docutils literal notranslate"><span class="pre">dev</span></code> parameter and a pointer to the <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">sensor_itf</span></code> for the |
| <code class="docutils literal notranslate"><span class="pre">arg</span></code> parameter.</p> |
| </li> |
| <li><p><code class="docutils literal notranslate"><span class="pre"><sensorname>_config()</span></code>: This is the sensor configuration function |
| that the BSP, sensor creator package, or an application calls to |
| configure the sensor device. For example:</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>int bno055_config(struct bno055 *bno055, struct bno055_cfg *cfg) |
| </pre></div> |
| </div> |
| </li> |
| </ul> |
| </div> |
| <div class="section" id="defining-functions-to-read-sensor-data-and-get-sensor-value-type"> |
| <h2>Defining Functions to Read Sensor Data and Get Sensor Value Type<a class="headerlink" href="#defining-functions-to-read-sensor-data-and-get-sensor-value-type" title="Permalink to this headline">¶</a></h2> |
| <p>A device driver must implement the following functions that the sensor |
| API uses to read sensor data and to get the configuration value type for |
| a sensor:</p> |
| <ul class="simple"> |
| <li><p>A function of type |
| <code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">(*sensor_read_func_t)(struct</span> <span class="pre">sensor</span> <span class="pre">*,</span> <span class="pre">sensor_type_t,</span> <span class="pre">sensor_data_func_t,</span> <span class="pre">void</span> <span class="pre">*,</span> <span class="pre">uint32_t)</span></code> |
| that the sensor framework uses to read a single value from a sensor |
| for the specified sensor types. The device driver must implement this |
| function such that it reads, for each sensor type set in the bit |
| mask, a single value from the sensor and calls the |
| <code class="docutils literal notranslate"><span class="pre">sensor_data_funct_t</span></code> callback with the opaque callback argument , |
| the sensor data, and the sensor type.</p></li> |
| <li><p>A function of type |
| <code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">(*sensor_get_config_func_t)(struct</span> <span class="pre">sensor</span> <span class="pre">*,</span> <span class="pre">sensor_type_t,</span> <span class="pre">struct</span> <span class="pre">sensor_cfg</span> <span class="pre">*)</span></code> |
| that returns the value type for the specified sensor type. For |
| example, the value type for a <code class="docutils literal notranslate"><span class="pre">SENSOR_VALUE_TYPE_TEMPERATURE</span></code> |
| sensor might be <code class="docutils literal notranslate"><span class="pre">SENSOR_VALUE_TYPE_FLOAT</span></code> and the value type for a |
| <code class="docutils literal notranslate"><span class="pre">SENSOR_TYPE_ACCELEROMETER</span></code> sensor might be |
| <code class="docutils literal notranslate"><span class="pre">SENSOR_VALUE_TYPE_FLOAT_TRIPLET</span></code>.</p></li> |
| </ul> |
| <p>The driver initializes a <code class="docutils literal notranslate"><span class="pre">sensor_driver</span></code> structure, shown below, with |
| the pointers to these functions:</p> |
| <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">sensor_driver</span><span class="w"> </span><span class="p">{</span> |
| <span class="w"> </span><span class="n">sensor_read_func_t</span><span class="w"> </span><span class="n">sd_read</span><span class="p">;</span> |
| <span class="w"> </span><span class="n">sensor_get_config_func_t</span><span class="w"> </span><span class="n">sd_get_config</span><span class="p">;</span> |
| <span class="p">};</span> |
| </pre></div> |
| </div> |
| <p>For example:</p> |
| <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">bno055_sensor_read</span><span class="p">(</span><span class="k">struct</span><span class="w"> </span><span class="nc">sensor</span><span class="w"> </span><span class="o">*</span><span class="p">,</span><span class="w"> </span><span class="n">sensor_type_t</span><span class="p">,</span> |
| <span class="w"> </span><span class="n">sensor_data_func_t</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="p">,</span><span class="w"> </span><span class="kt">uint32_t</span><span class="p">);</span> |
| <span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">bno055_sensor_get_config</span><span class="p">(</span><span class="k">struct</span><span class="w"> </span><span class="nc">sensor</span><span class="w"> </span><span class="o">*</span><span class="p">,</span><span class="w"> </span><span class="n">sensor_type_t</span><span class="p">,</span> |
| <span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">sensor_cfg</span><span class="w"> </span><span class="o">*</span><span class="p">);</span> |
| |
| <span class="k">static</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">sensor_driver</span><span class="w"> </span><span class="n">g_bno055_sensor_driver</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span> |
| <span class="w"> </span><span class="n">bno055_sensor_read</span><span class="p">,</span> |
| <span class="w"> </span><span class="n">bno055_sensor_get_config</span> |
| <span class="p">};</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="registering-the-sensor-in-the-sensor-framework"> |
| <h2>Registering the Sensor in the Sensor Framework<a class="headerlink" href="#registering-the-sensor-in-the-sensor-framework" title="Permalink to this headline">¶</a></h2> |
| <p>The device driver must initialize and register a <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">sensor</span></code> |
| object with the sensor manager. See the <a class="reference external" href="/os/modules/sensor_framework/sensor_api.html">Sensor |
| API</a> and the <a class="reference external" href="/os/modules/sensor_framework/sensor_manager_api.html">Sensor |
| Manager API</a> |
| pages for more details.</p> |
| <p>The device driver <code class="docutils literal notranslate"><span class="pre"><sensorname>_init()</span></code> function initializes and |
| registers a sensor object as follows:</p> |
| <ul class="simple"> |
| <li><p>Calls the <code class="docutils literal notranslate"><span class="pre">sensor_init()</span></code> function to initialize the |
| <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">sensor</span></code> object.</p></li> |
| <li><p>Calls the <code class="docutils literal notranslate"><span class="pre">sensor_set_driver()</span></code> function to specify the sensor |
| types that the sensor device supports, and the pointer to the |
| <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">sensor_driver</span></code> variable that specifies the driver functions |
| to read the sensor data and to get the value type for a sensor.</p></li> |
| <li><p>Calls the <code class="docutils literal notranslate"><span class="pre">sensor_set_interface()</span></code> function to set the interface |
| that the device driver uses to communicate with the sensor device. |
| The BSP, or sensor creator package for an off-board sensors, sets up |
| the <code class="docutils literal notranslate"><span class="pre">sensor_itf</span></code> and passes it to the <code class="docutils literal notranslate"><span class="pre"><sensorname>_init()</span></code> |
| function. The <code class="docutils literal notranslate"><span class="pre">sensor_set_interface()</span></code> functions saves this |
| information in the sensor object. The device driver uses the |
| <code class="docutils literal notranslate"><span class="pre">SENSOR_GET_ITF()</span></code> macro to retrieve the sensor_itf when it needs |
| to communicate with the sensor device.</p></li> |
| <li><p>Calls the <code class="docutils literal notranslate"><span class="pre">sensor_mgr_register()</span></code> function to register the sensor |
| with the sensor manager.</p></li> |
| </ul> |
| <p>For example:</p> |
| <p>```hl_lines=”13 20 25 31 32 33 34 35 41 46”</p> |
| <p>int bno055_init(struct os_dev <a href="#id1"><span class="problematic" id="id2">*</span></a>dev, void <a href="#id3"><span class="problematic" id="id4">*</span></a>arg) { struct bno055 |
| <a href="#id5"><span class="problematic" id="id6">*</span></a>bno055; struct sensor <a href="#id7"><span class="problematic" id="id8">*</span></a>sensor; int rc;</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>if (!arg || !dev) { |
| rc = SYS_ENODEV; |
| goto err; |
| } |
| |
| bno055 = (struct bno055 *) dev; |
| |
| rc = bno055_default_cfg(&bno055->cfg); |
| if (rc) { |
| goto err; |
| } |
| |
| sensor = &bno055->sensor; |
| |
| /* Code to setup logging and stats may go here */ |
| .... |
| |
| rc = sensor_init(sensor, dev); |
| if (rc != 0) { |
| goto err; |
| } |
| |
| /* Add the accelerometer/magnetometer driver */ |
| rc = sensor_set_driver(sensor, SENSOR_TYPE_ACCELEROMETER | |
| SENSOR_TYPE_MAGNETIC_FIELD | SENSOR_TYPE_GYROSCOPE | |
| SENSOR_TYPE_TEMPERATURE | SENSOR_TYPE_ROTATION_VECTOR | |
| SENSOR_TYPE_GRAVITY | SENSOR_TYPE_LINEAR_ACCEL | |
| SENSOR_TYPE_EULER, (struct sensor_driver *) &g_bno055_sensor_driver); |
| if (rc != 0) { |
| goto err; |
| } |
| |
| /* Set the interface */ |
| rc = sensor_set_interface(sensor, arg); |
| if (rc) { |
| goto err; |
| } |
| |
| rc = sensor_mgr_register(sensor); |
| if (rc != 0) { |
| goto err; |
| } |
| |
| return (0); |
| </pre></div> |
| </div> |
| <p>err: return (rc); }</p> |
| <p>```</p> |
| </div> |
| <div class="section" id="configuring-the-sensor-device-and-setting-the-configured-sensor-types"> |
| <h2>Configuring the Sensor Device and Setting the Configured Sensor Types<a class="headerlink" href="#configuring-the-sensor-device-and-setting-the-configured-sensor-types" title="Permalink to this headline">¶</a></h2> |
| <p>After the BSP, or the sensor creator package for an off-board sensor, |
| creates the OS device for a sensor, it calls the |
| <code class="docutils literal notranslate"><span class="pre"><sensorname>_config()</span></code> function to configure sensor device |
| information such as mode, power mode, and to set the configured sensor |
| types. The <code class="docutils literal notranslate"><span class="pre"><sensorname>_config()</span></code> function configures the settings on |
| the sensor device. It must also call the <code class="docutils literal notranslate"><span class="pre">sensor_set_type_mask()</span></code> |
| function to set the configured sensor types in the sensor object. The |
| configured sensor types are a subset of the sensor types that the sensor |
| device supports and the sensor framework only reads sensor data for |
| configured sensor types.</p> |
| <p><strong>Notes:</strong></p> |
| <ul class="simple"> |
| <li><p>The device driver uses the <code class="docutils literal notranslate"><span class="pre">SENSOR_GET_ITF()</span></code> macro to retrieve the |
| sensor interface to communicate with the sensor.</p></li> |
| <li><p>If a sensor device has a chip ID that can be queried, we recommend |
| that the device driver read and verify the chip ID with the data |
| sheet.</p></li> |
| <li><p>An application may call the <code class="docutils literal notranslate"><span class="pre"><sensorname>_config()</span></code> function to |
| configure the sensor device.</p></li> |
| </ul> |
| <p>For example:</p> |
| <p>```hl_lines=”7 9 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
| 28 29 37 38 39 40 41 42”</p> |
| <p>int bno055_config(struct bno055 <a href="#id9"><span class="problematic" id="id10">*</span></a>bno055, struct bno055_cfg <a href="#id11"><span class="problematic" id="id12">*</span></a>cfg) { |
| int rc; uint8_t id; uint8_t mode; struct sensor_itf *itf;</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>itf = SENSOR_GET_ITF(&(bno055->sensor)); |
| |
| /* Check if we can read the chip address */ |
| rc = bno055_get_chip_id(itf, &id); |
| if (rc) { |
| goto err; |
| } |
| |
| if (id != BNO055_ID) { |
| os_time_delay((OS_TICKS_PER_SEC * 100)/1000 + 1); |
| |
| rc = bno055_get_chip_id(itf, &id); |
| if (rc) { |
| goto err; |
| } |
| |
| if(id != BNO055_ID) { |
| rc = SYS_EINVAL; |
| goto err; |
| } |
| } |
| |
| .... |
| |
| /* Other code to set the configuration on the sensor device. */ |
| |
| .... |
| |
| rc = sensor_set_type_mask(&(bno055->sensor), cfg->bc_mask); |
| if (rc) { |
| goto err; |
| } |
| |
| bno055->cfg.bc_mask = cfg->bc_mask; |
| |
| return 0; |
| </pre></div> |
| </div> |
| <p>err: return rc; }</p> |
| <p>```</p> |
| </div> |
| <div class="section" id="implementing-a-sensor-device-shell-command"> |
| <h2>Implementing a Sensor Device Shell Command<a class="headerlink" href="#implementing-a-sensor-device-shell-command" title="Permalink to this headline">¶</a></h2> |
| <p>A sensor device driver package may optionally implement a sensor device |
| shell command that retrieves and sets sensor device information to aid |
| in testing and debugging. While the sensor framework <a class="reference external" href="/os/modules/sensor_framework/sensor_shell.html">sensor shell |
| command</a> reads sensor |
| data for configured sensor types and is useful for testing an |
| application, it does not access low level device information, such as |
| reading register values and setting hardware configurations, that might |
| be needed to test a sensor device or to debug the sensor device driver |
| code. A sensor device shell command implementation is device specific |
| but should minimally support reading sensor data for all the sensor |
| types that the device supports because the sensor framework <code class="docutils literal notranslate"><span class="pre">sensor</span></code> |
| shell command only reads sensor data for configured sensor types.</p> |
| <p>The package should:</p> |
| <ul class="simple"> |
| <li><p>Name the sensor device shell command <code class="docutils literal notranslate"><span class="pre"><sensorname></span></code>. For example, |
| the sensor device shell command for the BNO055 sensor device is |
| <code class="docutils literal notranslate"><span class="pre">bno055</span></code>.</p></li> |
| <li><p>Define a <code class="docutils literal notranslate"><span class="pre"><SENSORNAME>_CLI</span></code> syscfg setting to specify whether the |
| shell command is enabled and disable the setting by default.</p></li> |
| <li><p>Export a <code class="docutils literal notranslate"><span class="pre"><sensorname>_shell_init()</span></code> function that an application |
| calls to initialize the sensor shell command when the |
| <code class="docutils literal notranslate"><span class="pre">SENSORNAME_CLI</span></code> setting is enabled.</p></li> |
| </ul> |
| <p>For an example on how to implement a sensor device shell command, see |
| the <a class="reference external" href="https://github.com/apache/mynewt-core/blob/master/hw/drivers/sensors/bno055/src/bno055_shell.c">bno055 shell |
| command</a> |
| source code. See the <a class="reference external" href="/os/tutorials/sensors/sensor_nrf52_bno055.html">Enabling an Off-Board Sensor in an Existing |
| Application Tutorial</a> |
| for examples of the bno055 shell command.</p> |
| </div> |
| <div class="section" id="defining-logs"> |
| <h2>Defining Logs<a class="headerlink" href="#defining-logs" title="Permalink to this headline">¶</a></h2> |
| <p>A sensor device driver should define logs for testing purposes. See the |
| <a class="reference external" href="os/modules/logs/logs.html">Log OS Guide</a> for more details on how to |
| add logs. The driver should define a <code class="docutils literal notranslate"><span class="pre"><SENSORNAME>_LOG</span></code> syscfg setting |
| to specify whether logging is enabled and disable the setting by |
| default.</p> |
| <p>Here is an example from the BNO055 sensor driver package:</p> |
| <p>```hl_lines=”1 2 3 5 6 7 8 9 10 11 12 13 14 28 29 30”</p> |
| <div class="section" id="if-mynewt-val-bno055-log"> |
| <h3>if MYNEWT_VAL(BNO055_LOG)<a class="headerlink" href="#if-mynewt-val-bno055-log" title="Permalink to this headline">¶</a></h3> |
| </div> |
| <div class="section" id="include-log-log-h"> |
| <h3>include “log/log.h”<a class="headerlink" href="#include-log-log-h" title="Permalink to this headline">¶</a></h3> |
| </div> |
| <div class="section" id="endif"> |
| <h3>endif<a class="headerlink" href="#endif" title="Permalink to this headline">¶</a></h3> |
| <p>if MYNEWT_VAL(BNO055_LOG) #define LOG_MODULE_BNO055 (305) #define |
| BNO055_INFO(…) LOG_INFO(&_log, LOG_MODULE_BNO055, <strong>VA_ARGS</strong>) |
| #define BNO055_ERR(…) LOG_ERROR(&_log, LOG_MODULE_BNO055, |
| <strong>VA_ARGS</strong>) static struct log _log; #else #define BNO055_INFO(…) |
| #define BNO055_ERR(…) #endif</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>... |
| </pre></div> |
| </div> |
| <p>int bno055_init(struct os_dev <a href="#id13"><span class="problematic" id="id14">*</span></a>dev, void <a href="#id15"><span class="problematic" id="id16">*</span></a>arg) {</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span> ... |
| |
| rc = bno055_default_cfg(&bno055->cfg); |
| if (rc) { |
| goto err; |
| } |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="id17"> |
| <h3>if MYNEWT_VAL(BNO055_LOG)<a class="headerlink" href="#id17" title="Permalink to this headline">¶</a></h3> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>log_register(dev->od_name, &_log, &log_console_handler, NULL, LOG_SYSLEVEL); |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="id18"> |
| <h3>endif<a class="headerlink" href="#id18" title="Permalink to this headline">¶</a></h3> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>... |
| </pre></div> |
| </div> |
| <p>}</p> |
| <p>```</p> |
| </div> |
| </div> |
| <div class="section" id="defining-stats"> |
| <h2>Defining Stats<a class="headerlink" href="#defining-stats" title="Permalink to this headline">¶</a></h2> |
| <p>A sensor device driver may also define stats for the sensor. See the |
| <a class="reference external" href="os/modules/stats/stats.html">Stats OS Guide</a> for more details on how |
| to add stats. The driver should define a <code class="docutils literal notranslate"><span class="pre"><SENSORNAME>_STATS</span></code> syscfg |
| setting to specify whether stats is enabled and disable the setting by |
| default.</p> |
| <p>Here is an example from the BNO055 sensor driver package:</p> |
| <p>```hl_lines=”1 2 3 5 6 7 8 9 11 12 13 14 16 17 18 29 30 31 32 33 34 |
| 35 36 37 38 39 “</p> |
| <div class="section" id="if-mynewt-val-bno055-stats"> |
| <h3>if MYNEWT_VAL(BNO055_STATS)<a class="headerlink" href="#if-mynewt-val-bno055-stats" title="Permalink to this headline">¶</a></h3> |
| </div> |
| <div class="section" id="include-stats-stats-h"> |
| <h3>include “stats/stats.h”<a class="headerlink" href="#include-stats-stats-h" title="Permalink to this headline">¶</a></h3> |
| </div> |
| <div class="section" id="id19"> |
| <h3>endif<a class="headerlink" href="#id19" title="Permalink to this headline">¶</a></h3> |
| </div> |
| <div class="section" id="id20"> |
| <h3>if MYNEWT_VAL(BNO055_STATS)<a class="headerlink" href="#id20" title="Permalink to this headline">¶</a></h3> |
| <p>/* Define the stats section and records */ |
| STATS_SECT_START(bno055_stat_section) STATS_SECT_ENTRY(errors) |
| STATS_SECT_END</p> |
| <p>/* Define stat names for querying */ |
| STATS_NAME_START(bno055_stat_section) |
| STATS_NAME(bno055_stat_section, errors) |
| STATS_NAME_END(bno055_stat_section)</p> |
| <p>/* Global variable used to hold stats data */ |
| STATS_SECT_DECL(bno055_stat_section) g_bno055stats; #endif</p> |
| <p>…</p> |
| <p>int bno055_init(struct os_dev <a href="#id21"><span class="problematic" id="id22">*</span></a>dev, void <a href="#id23"><span class="problematic" id="id24">*</span></a>arg) {</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>... |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="id25"> |
| <h3>if MYNEWT_VAL(BNO055_STATS)<a class="headerlink" href="#id25" title="Permalink to this headline">¶</a></h3> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/* Initialise the stats entry */ |
| rc = stats_init( |
| STATS_HDR(g_bno055stats), |
| STATS_SIZE_INIT_PARMS(g_bno055stats, STATS_SIZE_32), |
| STATS_NAME_INIT_PARMS(bno055_stat_section)); |
| SYSINIT_PANIC_ASSERT(rc == 0); |
| /* Register the entry with the stats registry */ |
| rc = stats_register(dev->od_name, STATS_HDR(g_bno055stats)); |
| SYSINIT_PANIC_ASSERT(rc == 0); |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="id26"> |
| <h3>endif<a class="headerlink" href="#id26" title="Permalink to this headline">¶</a></h3> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>... |
| </pre></div> |
| </div> |
| <p>}</p> |
| <p>```</p> |
| </div> |
| </div> |
| </div> |
| |
| |
| </div> |
| </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> |