blob: c477777154bf8e1ae7caa0c19d95979af6931661 [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>BSP Porting &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="Porting Mynewt OS" href="port_os.html"/>
<link rel="next" title="Porting Mynewt to a new MCU" href="port_mcu.html"/>
<link rel="prev" title="Porting Mynewt OS" href="port_os.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> /
<a href="../../os_user_guide.html">OS User Guide</a> /
<a href="port_os.html">Porting Mynewt OS</a> /
BSP Porting
<div class="sourcelink">
<a href="https://github.com/apache/mynewt-core/edit/master/docs/os/core_os/porting/port_bsp.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" selected="selected" >
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" >
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="../mynewt_os.html">Kernel</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/system_modules.html">System</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/hal/hal.html">Hardware Abstraction</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/bootloader/bootloader.html">Secure Bootloader</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/split/split.html">Split Images</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="port_os.html">Porting Guide</a><ul class="current">
<li class="toctree-l3 current"><a class="current reference internal" href="#">BSP Porting</a></li>
<li class="toctree-l3"><a class="reference internal" href="port_mcu.html">Porting Mynewt to a new MCU</a></li>
<li class="toctree-l3"><a class="reference internal" href="port_cpu.html">Porting Mynewt to a new CPU Architecture</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/baselibc.html">Baselibc</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/drivers/driver.html">Drivers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/devmgmt/newtmgr.html">Device Management with Newt Manager</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/mcumgr/mcumgr.html">Device Management with MCUmgr</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/imgmgr/imgmgr.html">Image Manager</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/sysinitconfig/sysinitconfig.html">Compile-Time Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/sysinitdown/sysinitdown.html">System Initialization and Shutdown</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/extcmd/extcmd.html">Build-Time Hooks</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/fs/fs.html">File System</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/fcb/fcb.html">Flash Circular Buffer</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/sensor_framework/sensor_framework.html">Sensor Framework</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/testutil/testutil.html">Test Utilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/json/json.html">JSON</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../modules/mfg/mfg.html">Manufacturing support</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../bsp/index.html">Board support</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.9.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="bsp-porting">
<h1>BSP Porting<a class="headerlink" href="#bsp-porting" title="Permalink to this headline"></a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#introduction" id="id1">Introduction</a></p></li>
<li><p><a class="reference internal" href="#download-the-bsp-package-template" id="id2">Download the BSP package template</a></p></li>
<li><p><a class="reference internal" href="#create-a-set-of-mynewt-targets" id="id3">Create a set of Mynewt targets</a></p></li>
<li><p><a class="reference internal" href="#fill-in-the-bsp-yml-file" id="id4">Fill in the <code class="docutils literal notranslate"><span class="pre">bsp.yml</span></code> file</a></p></li>
<li><p><a class="reference internal" href="#flash-map" id="id5">Flash map</a></p></li>
<li><p><a class="reference internal" href="#add-the-mcu-dependency-to-pkg-yml" id="id6">Add the MCU dependency to <code class="docutils literal notranslate"><span class="pre">pkg.yml</span></code></a></p></li>
<li><p><a class="reference internal" href="#check-the-bsp-linker-scripts" id="id7">Check the BSP linker scripts</a></p></li>
<li><p><a class="reference internal" href="#copy-the-download-and-debug-scripts" id="id8">Copy the download and debug scripts</a></p></li>
<li><p><a class="reference internal" href="#fill-in-bsp-functions-and-defines" id="id9">Fill in BSP functions and defines</a></p></li>
<li><p><a class="reference internal" href="#add-startup-code" id="id10">Add startup code</a></p></li>
<li><p><a class="reference internal" href="#satisfy-mcu-requirements" id="id11">Satisfy MCU requirements</a></p></li>
<li><p><a class="reference internal" href="#test-it" id="id12">Test it</a></p></li>
<li><p><a class="reference internal" href="#appendix-a-bsp-files" id="id13">Appendix A: BSP files</a></p></li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id1">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline"></a></h2>
<p>The Apache Mynewt core repo contains support for several different boards. For each supported board, there is a Board
Support Package (BSP) package in the <code class="docutils literal notranslate"><span class="pre">hw/bsp</span></code> directory. If there isn’t a BSP package for your hardware, then you will need to make one
yourself. This document describes the process of creating a BSP package from scratch.</p>
<p>While creating your BSP package, the following documents will probably come in handy:</p>
<ul class="simple">
<li><p>The datasheet for the MCU you have chosen.</p></li>
<li><p>The schematic of your board.</p></li>
<li><p>The information on the CPU core within your MCU if it is not included in your MCU documentation.</p></li>
</ul>
<p>This document is applicable to any hardware, but it will often make reference to a specific board as an example. Our
example BSP has the following properties:</p>
<ul class="simple">
<li><p><strong>Name:</strong> <code class="docutils literal notranslate"><span class="pre">hw/bsp/myboard</span></code></p></li>
<li><p><strong>MCU:</strong> Nordic nRF52</p></li>
</ul>
</div>
<div class="section" id="download-the-bsp-package-template">
<h2><a class="toc-backref" href="#id2">Download the BSP package template</a><a class="headerlink" href="#download-the-bsp-package-template" title="Permalink to this headline"></a></h2>
<p>We start by downloading a BSP package template. This template will serve as a good starting point for our new BSP.</p>
<p>Execute the <code class="docutils literal notranslate"><span class="pre">newt</span> <span class="pre">pkg</span> <span class="pre">new</span></code> command, as below:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>newt<span class="w"> </span>pkg<span class="w"> </span>new<span class="w"> </span>-t<span class="w"> </span>bsp<span class="w"> </span>hw/bsp/myboard
<span class="go">Download package template for package type bsp.</span>
<span class="go">Package successfuly installed into /home/me/myproj/hw/bsp/myboard.</span>
</pre></div>
</div>
<p>Our new package has the following file structure:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>tree<span class="w"> </span>hw/bsp/myboard
<span class="go">hw/bsp/myboard</span>
<span class="go">├── README.md</span>
<span class="go">├── boot-myboard.ld</span>
<span class="go">├── bsp.yml</span>
<span class="go">├── include</span>
<span class="go">│   └── myboard</span>
<span class="go">│   └── bsp.h</span>
<span class="go">├── myboard.ld</span>
<span class="go">├── myboard_debug.sh</span>
<span class="go">├── myboard_download.sh</span>
<span class="go">├── pkg.yml</span>
<span class="go">├── src</span>
<span class="go">│   ├── hal_bsp.c</span>
<span class="go">│   └── sbrk.c</span>
<span class="go">└── syscfg.yml</span>
<span class="go">3 directories, 11 files</span>
</pre></div>
</div>
<p>We will be adding to this package throughout the remainder of this document. See <a class="reference internal" href="#appendix-a-bsp-files">Appendix A: BSP files</a> for a full
list of files typically found in a BSP package.</p>
</div>
<div class="section" id="create-a-set-of-mynewt-targets">
<h2><a class="toc-backref" href="#id3">Create a set of Mynewt targets</a><a class="headerlink" href="#create-a-set-of-mynewt-targets" title="Permalink to this headline"></a></h2>
<p>We’ll need two <a class="reference internal" href="../../../concepts.html#mynewt-target"><span class="std std-ref">targets</span></a> to test our BSP as we go:</p>
<ol class="arabic simple">
<li><p>Boot loader</p></li>
<li><p>Application</p></li>
</ol>
<p>A minimal application is best, since we are just interested in getting the BSP up and running. A good app for our
purposes is <a class="reference internal" href="../../../tutorials/blinky/blinky.html"><span class="doc">blinky</span></a>.</p>
<p>We create our targets with the following set of newt commands:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>newt target create boot-myboard &amp;&amp;
newt target set boot-myboard app=@mcuboot/boot/mynewt \
bsp=hw/bsp/myboard \
build_profile=optimized
newt target create blinky-myboard &amp;&amp;
newt target set blinky-myboard app=apps/blinky \
bsp=hw/bsp/myboard \
build_profile=debug
</pre></div>
</div>
<p>Which generates the following output:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Target targets/boot-myboard successfully created
Target targets/boot-myboard successfully set target.app to @mcuboot/boot/mynewt
Target targets/boot-myboard successfully set target.bsp to hw/bsp/myboard
Target targets/boot-myboard successfully set target.build_profile to debug
Target targets/blinky-myboard successfully created
Target targets/blinky-myboard successfully set target.app to apps/blinky
Target targets/blinky-myboard successfully set target.bsp to hw/bsp/myboard
Target targets/blinky-myboard successfully set target.build_profile to debug
</pre></div>
</div>
</div>
<div class="section" id="fill-in-the-bsp-yml-file">
<h2><a class="toc-backref" href="#id4">Fill in the <code class="docutils literal notranslate"><span class="pre">bsp.yml</span></code> file</a><a class="headerlink" href="#fill-in-the-bsp-yml-file" title="Permalink to this headline"></a></h2>
<p>The template <code class="docutils literal notranslate"><span class="pre">hw/bsp/myboard/bsp.yml</span></code> file is missing some values that need to be added. It also assumes certain
information that may not be appropriate for your BSP. We need to get this file into a usable state.</p>
<p>Missing fields are indicated by the presence of <code class="docutils literal notranslate"><span class="pre">XXX</span></code> markers. Here are the first several lines of our <code class="docutils literal notranslate"><span class="pre">bsp.yml</span></code>
file where all the incomplete fields are located:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>bsp.arch: # XXX &lt;MCU-architecture&gt;
bsp.compiler: # XXX &lt;compiler-package&gt;
bsp.linkerscript:
- &#39;hw/bsp/myboard/myboard.ld&#39;
# - XXX mcu-linker-script
bsp.linkerscript.BOOT_LOADER.OVERWRITE:
- &#39;hw/bsp/myboard/myboard/boot-myboard.ld&#39;
# - XXX mcu-linker-script
</pre></div>
</div>
<p>So we need to specify the following:</p>
<ul class="simple">
<li><p>MCU architecture</p></li>
<li><p>Compiler package</p></li>
<li><p>MCU linker script</p></li>
</ul>
<p>Our example BSP uses an nRF52 MCU, which implements the <code class="docutils literal notranslate"><span class="pre">cortex_m4</span></code> architecture. We use this information to fill in
the incomplete fields:</p>
<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="hll"><span class="w"> </span><span class="nt">bsp.arch</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">cortex_m4</span>
</span><span class="hll"><span class="w"> </span><span class="nt">bsp.compiler</span><span class="p">:</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/compiler/arm-none-eabi-m4&#39;</span>
</span><span class="w"> </span><span class="nt">bsp.linkerscript</span><span class="p">:</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;hw/bsp/myboard/myboard.ld&#39;</span>
<span class="hll"><span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/hw/mcu/nordic/nrf52xxx/nrf52.ld&#39;</span>
</span><span class="w"> </span><span class="nt">bsp.linkerscript.BOOT_LOADER.OVERWRITE</span><span class="p">:</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;hw/bsp/myboard/boot-myboard.ld&#39;</span>
<span class="hll"><span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/hw/mcu/nordic/nrf52xxx/nrf52.ld&#39;</span>
</span></pre></div>
</div>
<p>Naturally, these values must be adjusted accordingly for other MCU types.</p>
</div>
<div class="section" id="flash-map">
<h2><a class="toc-backref" href="#id5">Flash map</a><a class="headerlink" href="#flash-map" title="Permalink to this headline"></a></h2>
<p>At the bottom of the <code class="docutils literal notranslate"><span class="pre">bsp.yml</span></code> file is the flash map. The flash map partitions the BSP’s flash memory into sections
called areas. Flash areas are further categorized into two types: 1) system areas, and 2) user areas. These two area
types are defined below.</p>
<p><strong>System areas</strong></p>
<ul class="simple">
<li><p>Used by Mynewt core components.</p></li>
<li><p>BSP support is mandatory in most cases.</p></li>
<li><p>Use reserved names.</p></li>
</ul>
<p><strong>User areas</strong></p>
<ul class="simple">
<li><p>Used by application code and supplementary libraries.</p></li>
<li><p>Identified by user-assigned names.</p></li>
<li><p>Have unique user-assigned numeric identifiers for access by C code.</p></li>
</ul>
<p>The flash map in the template <code class="docutils literal notranslate"><span class="pre">bsp.yml</span></code> file is suitable for an MCU with 512kB of internal flash. You may need to
adjust the area offsets and sizes if your BSP does not have 512kB of internal flash.</p>
<p>The system flash areas are briefly described below:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 36%" />
<col style="width: 64%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Flash area</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">FLASH_AREA_BOOTLOADER</span></code></p></td>
<td><p>Contains the Mynewt boot loader.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">FLASH_AREA_IMAGE_0</span></code></p></td>
<td><p>Contains the active Mynewt application image.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">FLASH_AREA_IMAGE_1</span></code></p></td>
<td><p>Contains the secondary image; used for image upgrade.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">FLASH_AREA_IMAGE_SCRATCH</span></code></p></td>
<td><p>Used by the boot loader during image swap.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="add-the-mcu-dependency-to-pkg-yml">
<h2><a class="toc-backref" href="#id6">Add the MCU dependency to <code class="docutils literal notranslate"><span class="pre">pkg.yml</span></code></a><a class="headerlink" href="#add-the-mcu-dependency-to-pkg-yml" title="Permalink to this headline"></a></h2>
<p>A package’s dependencies are listed in its <code class="docutils literal notranslate"><span class="pre">pkg.yml</span></code> file. A BSP package always depends on its corresponding MCU
package, so let’s add that dependency to our BSP now. The <code class="docutils literal notranslate"><span class="pre">pkg.deps</span></code> section of our <code class="docutils literal notranslate"><span class="pre">hw/bsp/myboard/pkg.yml</span></code> file
currently looks like this:</p>
<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">pkg.deps</span><span class="p">:</span>
<span class="w"> </span><span class="c1"># - XXX &lt;MCU-package&gt;</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/kernel/os&#39;</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/libc/baselibc&#39;</span>
</pre></div>
</div>
<p>Continuing with our example nRF52 BSP, we replace the marked line as follows:</p>
<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="nt">pkg.deps</span><span class="p">:</span>
<span class="hll"><span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/hw/mcu/nordic/nrf52xxx&#39;</span>
</span><span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/kernel/os&#39;</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;@apache-mynewt-core/libc/baselibc&#39;</span>
</pre></div>
</div>
<p>Again, the particulars depend on the MCU that your BSP uses.</p>
</div>
<div class="section" id="check-the-bsp-linker-scripts">
<h2><a class="toc-backref" href="#id7">Check the BSP linker scripts</a><a class="headerlink" href="#check-the-bsp-linker-scripts" title="Permalink to this headline"></a></h2>
<p>Linker scripts are a key component of the BSP package. They specify how code and data are arranged in the MCU’s memory.
Our BSP package contains two linker scripts:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 32%" />
<col style="width: 68%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p><strong>Filename</strong></p></th>
<th class="head"><p><strong>Description</strong></p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">myboard.ld</span></code></p></td>
<td><p>Linker script for Mynewt application images.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">boot-myboard.ld</span></code></p></td>
<td><p>Linker script for the Mynewt boot loader.</p></td>
</tr>
</tbody>
</table>
<p>First, we will deal with the application linker script. You may have noticed that the <code class="docutils literal notranslate"><span class="pre">bsp.linkerscript</span></code> item in
<code class="docutils literal notranslate"><span class="pre">bsp.yml</span></code> actually specifies two linker scripts:</p>
<ul class="simple">
<li><p>BSP linker script (<code class="docutils literal notranslate"><span class="pre">hw/bsp/myboard.ld</span></code>)</p></li>
<li><p>MCU linker script (<code class="docutils literal notranslate"><span class="pre">&#64;apache-mynewt-core/hw/mcu/nordic/nrf52xxx/nrf52.ld</span></code>)</p></li>
</ul>
<p>Both linker scripts get used in combination when you build a Mynewt image. Typically, all the complexity is isolated to
the MCU linker script, while the BSP linker script just contains minimal size and offset information. This makes the job
of creating a BSPpackage much simpler.</p>
<p>Our <code class="docutils literal notranslate"><span class="pre">myboard.ld</span></code> file has the following contents:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">MEMORY</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">FLASH</span><span class="w"> </span><span class="p">(</span><span class="n">rx</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">ORIGIN</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">0x00008000</span><span class="p">,</span><span class="w"> </span><span class="n">LENGTH</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">0x3a000</span>
<span class="w"> </span><span class="n">RAM</span><span class="w"> </span><span class="p">(</span><span class="n">rwx</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">ORIGIN</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">0x20000000</span><span class="p">,</span><span class="w"> </span><span class="n">LENGTH</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">0x10000</span>
<span class="p">}</span>
<span class="cm">/* This linker script is used for images and thus contains an image header */</span>
<span class="n">_imghdr_size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">0x20</span><span class="p">;</span>
</pre></div>
</div>
<p>Our task is to ensure the offset (<code class="docutils literal notranslate"><span class="pre">ORIGIN</span></code>) and size (<code class="docutils literal notranslate"><span class="pre">LENGTH</span></code>) values are correct for the <code class="docutils literal notranslate"><span class="pre">FLASH</span></code> and <code class="docutils literal notranslate"><span class="pre">RAM</span></code>
regions. Note that the <code class="docutils literal notranslate"><span class="pre">FLASH</span></code> region does not specify the board’s entire internal flash; it only describes the area
of the flash dedicated to containing the running Mynewt image. The bounds of the <code class="docutils literal notranslate"><span class="pre">FLASH</span></code> region should match those of
the <code class="docutils literal notranslate"><span class="pre">FLASH_AREA_IMAGE_0</span></code> area in the BSP’s flash map.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">_imghdr_size</span></code> is always <code class="docutils literal notranslate"><span class="pre">0x20</span></code>, so it can remain unchanged.</p>
<p>The second linker script, <code class="docutils literal notranslate"><span class="pre">boot-myboard.ld</span></code>, is quite similar to the first. The important difference is the <code class="docutils literal notranslate"><span class="pre">FLASH</span></code>
region: it describes the area of flash which contains the boot loader rather than an image. The bounds of this region
should match those of the <code class="docutils literal notranslate"><span class="pre">FLASH_AREA_BOOTLOADER</span></code> area in the BSP’s flash map. For more information about the Mynewt
boot loader, see <a class="reference internal" href="../../modules/bootloader/bootloader.html"><span class="doc">this page</span></a>.</p>
</div>
<div class="section" id="copy-the-download-and-debug-scripts">
<h2><a class="toc-backref" href="#id8">Copy the download and debug scripts</a><a class="headerlink" href="#copy-the-download-and-debug-scripts" title="Permalink to this headline"></a></h2>
<p>The newt command line tool uses a set of scripts to load and run Mynewt images. It is the BSP package that provides
these scripts.</p>
<p>As with the linker scripts, most of the work done by the download and debug scripts is isolated to the MCU package. The
BSP scripts are quite simple, and you can likely get away with just copying them from another BSP. The template
<code class="docutils literal notranslate"><span class="pre">myboard_debug.sh</span></code> script indicates which BSP to copy from:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/bin/sh</span>
<span class="c1"># This script attaches a gdb session to a Mynewt image running on your BSP.</span>
<span class="c1"># If your BSP uses JLink, a good example script to copy is:</span>
<span class="c1"># repos/apache-mynewt-core/hw/bsp/nordic_pca10040/nordic_pca10040_debug.sh</span>
<span class="c1">#</span>
<span class="c1"># If your BSP uses OpenOCD, a good example script to copy is:</span>
<span class="c1"># repos/apache-mynewt-core/hw/bsp/rb-nano2/rb-nano2_debug.sh</span>
</pre></div>
</div>
<p>Our example Nordic nRF52 BSP uses JLink, so we will copy the Nordic PCA10040 (nRF52 DK) BSP’s scripts:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>cp repos/apache-mynewt-core/hw/bsp/nordic_pca10040/nordic_pca10040_debug.sh hw/bsp/myboard/myboard_debug.sh
cp repos/apache-mynewt-core/hw/bsp/nordic_pca10040/nordic_pca10040_download.sh hw/bsp/myboard/myboard_download.sh
</pre></div>
</div>
</div>
<div class="section" id="fill-in-bsp-functions-and-defines">
<h2><a class="toc-backref" href="#id9">Fill in BSP functions and defines</a><a class="headerlink" href="#fill-in-bsp-functions-and-defines" title="Permalink to this headline"></a></h2>
<p>There are a few particulars missing from the BSP’s C code. These areas are marked with <code class="docutils literal notranslate"><span class="pre">XXX</span></code> comments to make them
easier to spot. The missing pieces are summarized in the table below:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 14%" />
<col style="width: 53%" />
<col style="width: 34%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>File</p></th>
<th class="head"><p>Description</p></th>
<th class="head"><p>Notes</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">src/hal_bsp.c</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">hal_bsp_flash_dev()</span></code> needs to return a pointer to the MCU’s flash object when <code class="docutils literal notranslate"><span class="pre">id</span> <span class="pre">==</span> <span class="pre">0</span></code>.</p></td>
<td><p>The flash object is defined in MCU’s <code class="docutils literal notranslate"><span class="pre">hal_flash.c</span></code> file.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">include/bsp/bsp.h</span></code></p></td>
<td><p>Define <code class="docutils literal notranslate"><span class="pre">LED_BLINK_PIN</span></code> to the pin number of the BSP’s primary LED.</p></td>
<td><p>Required by the blinky application.</p></td>
</tr>
</tbody>
</table>
<p>For our nRF52 BSP, we modify these files as follows:</p>
<p><strong>src/hal_bsp.c:</strong></p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="hll"><span class="w"> </span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu/nrf52_hal.h&quot;</span>
</span></pre></div>
</div>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></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">hal_flash</span><span class="w"> </span><span class="o">*</span>
<span class="w"> </span><span class="n">hal_bsp_flash_dev</span><span class="p">(</span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">id</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="mi">0</span><span class="p">:</span>
<span class="w"> </span><span class="cm">/* MCU internal flash. */</span>
<span class="hll"><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="o">&amp;</span><span class="n">nrf52k_flash_dev</span><span class="p">;</span>
</span>
<span class="w"> </span><span class="k">default</span><span class="o">:</span>
<span class="w"> </span><span class="cm">/* External flash. Assume not present in this BSP. */</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
<p><strong>include/bsp/bsp.h:</strong></p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="cp">#define RAM_SIZE 0x10000</span>
<span class="w"> </span><span class="cm">/* Put additional BSP definitions here. */</span>
<span class="hll"><span class="w"> </span><span class="cp">#define LED_BLINK_PIN 17</span>
</span></pre></div>
</div>
</div>
<div class="section" id="add-startup-code">
<h2><a class="toc-backref" href="#id10">Add startup code</a><a class="headerlink" href="#add-startup-code" title="Permalink to this headline"></a></h2>
<p>Now we need to add the BSP’s assembly startup code. Among other things, this is the code that gets executed immediately
on power up, before the Mynewt OS is running. This code must perform a few basic tasks:</p>
<ul class="simple">
<li><p>Assign labels to memory region boundaries.</p></li>
<li><p>Define some interrupt request handlers.</p></li>
<li><p>Define the <code class="docutils literal notranslate"><span class="pre">Reset_Handler</span></code> function, which:</p>
<ul>
<li><p>Zeroes the <code class="docutils literal notranslate"><span class="pre">.bss</span></code> section.</p></li>
<li><p>Copies static data from the image to the <code class="docutils literal notranslate"><span class="pre">.data</span></code> section.</p></li>
<li><p>Starts the Mynewt OS.</p></li>
</ul>
</li>
</ul>
<p>This file is named according to the following pattern: <code class="docutils literal notranslate"><span class="pre">hw/bsp/myboard/src/arch/&lt;ARCH&gt;/gcc_startup_&lt;MCU&gt;.s</span></code></p>
<p>The best approach for creating this file is to copy from other BSPs. If there is another BSP that uses the same MCU, you
might be able to use most or all of its startup file.</p>
<p>For our example BSP, we’ll just copy the Nordic PCA10040 (nRF52 DK) BSP’s startup code:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>mkdir<span class="w"> </span>-p<span class="w"> </span>hw/bsp/myboard/src/arch/cortex_m4
<span class="gp">$ </span>cp<span class="w"> </span>repos/apache-mynewt-core/hw/bsp/nordic_pca10040/src/arch/cortex_m4/gcc_startup_nrf52.s<span class="w"> </span>hw/bsp/myboard/src/arch/cortex_m4/
</pre></div>
</div>
</div>
<div class="section" id="satisfy-mcu-requirements">
<h2><a class="toc-backref" href="#id11">Satisfy MCU requirements</a><a class="headerlink" href="#satisfy-mcu-requirements" title="Permalink to this headline"></a></h2>
<p>The MCU package probably requires some build-time configuration. Typically, it is the BSP which provides this configuration.
Completing this step will likely involve some trial and error as each unmet requirement gets reported as a build error.</p>
<p>Our example nRF52 BSP requires the following changes:</p>
<ol class="arabic">
<li><p>Macro indicating MCU type. We add this to our BSP’s <code class="docutils literal notranslate"><span class="pre">pkg.yml</span></code> file:</p>
<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="nt">pkg.cflags</span><span class="p">:</span>
<span class="hll"><span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="s">&#39;-DNRF52&#39;</span>
</span></pre></div>
</div>
</li>
<li><p>Enable exactly one low-frequency timer setting in our BSP’s <code class="docutils literal notranslate"><span class="pre">syscfg.yml</span></code> file. This is required by the nRF51 and nRF52 MCU packages:</p>
<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="c1"># Settings this BSP overrides.</span>
<span class="w"> </span><span class="nt">syscfg.vals</span><span class="p">:</span>
<span class="hll"><span class="w"> </span><span class="nt">XTAL_32768</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">1</span>
</span></pre></div>
</div>
</li>
</ol>
</div>
<div class="section" id="test-it">
<h2><a class="toc-backref" href="#id12">Test it</a><a class="headerlink" href="#test-it" title="Permalink to this headline"></a></h2>
<p>Now it’s finally time to test the BSP package. Build and load your boot and blinky targets as follows:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>newt<span class="w"> </span>build<span class="w"> </span>boot-myboard
<span class="gp">$ </span>newt<span class="w"> </span>load<span class="w"> </span>boot-myboard
<span class="gp">$ </span>newt<span class="w"> </span>run<span class="w"> </span>blinky-myboard<span class="w"> </span><span class="m">0</span>
</pre></div>
</div>
<p>If everything is correct, the blinky app should successfully build, and you should be presented with a gdb prompt. Type
<code class="docutils literal notranslate"><span class="pre">c</span> <span class="pre">&lt;enter&gt;</span></code> (continue) to see your board’s LED blink.</p>
</div>
<div class="section" id="appendix-a-bsp-files">
<h2><a class="toc-backref" href="#id13">Appendix A: BSP files</a><a class="headerlink" href="#appendix-a-bsp-files" title="Permalink to this headline"></a></h2>
<p>The table below lists the files required by all BSP packages. The naming scheme assumes a BSP called “myboard”.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 39%" />
<col style="width: 61%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p><strong>File Path Name</strong></p></th>
<th class="head"><p><strong>Description</strong></p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">pkg.yml</span></code></p></td>
<td><p>Defines a Mynewt package for the BSP.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">bsp.yml</span></code></p></td>
<td><p>Defines BSP-specific settings.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">include/bsp/bsp.h</span></code></p></td>
<td><p>Contains additional BSP-specific settings.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">src/hal_bsp.c</span></code></p></td>
<td><p>Contains code to initialize the BSP’s peripherals.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">src/sbrk.c</span></code></p></td>
<td><p>Low level heap management required by <code class="docutils literal notranslate"><span class="pre">malloc()</span></code>.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">src/arch/&lt;ARCH&gt;/gcc_startup_myboard.s</span></code></p></td>
<td><p>Startup assembly code to bring up Mynewt</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">myboard.ld</span></code></p></td>
<td><p>A linker script providing the memory map for a Mynewt application.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">boot-myboard.ld</span></code></p></td>
<td><p>A linker script providing the memory map for the Mynewt bootloader.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">myboard_download.sh</span></code></p></td>
<td><p>A bash script to download code onto your platform.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">myboard_debug.sh</span></code></p></td>
<td><p>A bash script to initiate a gdb session with your platform.</p></td>
</tr>
</tbody>
</table>
<p>A BSP can also contain the following optional files:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 29%" />
<col style="width: 71%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p><strong>File Path Name</strong></p></th>
<th class="head"><p><strong>Description</strong></p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">split-myboard.ld</span></code></p></td>
<td><p>A linker script providing the memory map for the “application” half of a <a class="reference internal" href="../../modules/split/split.html"><span class="doc">split image</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">no-boot-myboard.ld</span></code></p></td>
<td><p>A linker script providing the memory map for your bootloader</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">src/arch/&lt;ARCH&gt;/gcc_startup_myboard_split.s</span></code></p></td>
<td><p>Startup assembly code to bring up the “application” half of a <a class="reference internal" href="../../modules/split/split.html"><span class="doc">split image</span></a>.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">myboard_download.cmd</span></code></p></td>
<td><p>An MSDOS batch file to download code onto your platform; required for Windows support.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">myboard_debug.cmd</span></code></p></td>
<td><p>An MSDOS batch file to intiate a gdb session with your platform; required for Windows support.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="rst-footer-buttons row" role="navigation" aria-label="footer navigation">
<a href="port_mcu.html" class="btn btn-neutral float-right" title="Porting Mynewt to a new MCU" accesskey="n">Next: Porting Mynewt to a new MCU <span class="fa fa-arrow-circle-right"></span></a>
<a href="port_os.html" class="btn btn-neutral" title="Porting Mynewt OS" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous: Porting Mynewt OS</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>