<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pojiro</title>
    <description>The latest articles on DEV Community by pojiro (@pojiro).</description>
    <link>https://dev.to/pojiro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F358667%2F88b3d113-a850-4cca-b72b-9081ff9b1f73.jpg</url>
      <title>DEV Community: pojiro</title>
      <link>https://dev.to/pojiro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pojiro"/>
    <language>en</language>
    <item>
      <title>Elixir on Industrial Hardware, porting Nerves to e-RT3</title>
      <dc:creator>pojiro</dc:creator>
      <pubDate>Thu, 10 Jun 2021 12:44:57 +0000</pubDate>
      <link>https://dev.to/pojiro/elixir-on-industrial-hardware-porting-nerves-to-e-rt3-7k1</link>
      <guid>https://dev.to/pojiro/elixir-on-industrial-hardware-porting-nerves-to-e-rt3-7k1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The title says it all, I just tried porting Nerves and got it working!&lt;/p&gt;

&lt;p&gt;So, for someone who are going to try porting like me, here is a summary of the key points of the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Target machine
&lt;/h2&gt;

&lt;p&gt;The target machine is &lt;a href="https://www.yokogawa.com/solutions/products-platforms/control-system/ert3-embedded-controller/ert3-products/ert3-products-cpu/" rel="noopener noreferrer"&gt;OS-free CPU module&lt;/a&gt; of &lt;a href="https://www.yokogawa.com/solutions/products-platforms/control-system/ert3-embedded-controller/ert3-products/" rel="noopener noreferrer"&gt;e-RT3 Products&lt;/a&gt; which is made by Yokogawa Electric Corp.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://qiita.com/myasu" rel="noopener noreferrer"&gt;myasu&lt;/a&gt; has been writing several articles about it on &lt;a href="https://qiita.com/tags/e-rt3" rel="noopener noreferrer"&gt;Qiita&lt;/a&gt;, one of Japanese tech web sites.&lt;/p&gt;

&lt;p&gt;One day he gave me the idea of porting, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we could port Nerves onto this, we can get the &lt;strong&gt;industrial robust hardware Nerves&lt;/strong&gt; 💡&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It Sounds very nice!! So this hardware became the target machine for us.&lt;/p&gt;

&lt;h3&gt;
  
  
  About e-RT3
&lt;/h3&gt;

&lt;p&gt;This &lt;a href="https://www.yokogawa.com/solutions/products-platforms/control-system/ert3-embedded-controller/ert3-products/ert3-products-cpu/" rel="noopener noreferrer"&gt;OS-free CPU module&lt;/a&gt; is a very unique controller that allows users to freely select the OS they want to run. For this reason, its booting specifications docs and the Linux kernel source are provided.&lt;/p&gt;

&lt;p&gt;The specifications/sources are open so that users can freely select the OS they want to run, which made this porting successful. &lt;strong&gt;It's cool the info is open.&lt;/strong&gt;😉&lt;/p&gt;

&lt;p&gt;In this article, I will refer to it as "e-RT3" instead of "OS-free CPU module"(cause little bit long).&lt;/p&gt;

&lt;h2&gt;
  
  
  Porting outline
&lt;/h2&gt;

&lt;p&gt;The porting procedure of Nerves is described in &lt;a href="https://github.com/nerves-project/nerves_system_br/blob/0229532e3843bc3b4e534807636c767c380ae4b4/README.md" rel="noopener noreferrer"&gt;README of nerves_system_br&lt;/a&gt; as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Create a minimal Buildroot &lt;code&gt;defconfig&lt;/code&gt; that boots and runs on the board. This doesn't use Nerves at all.&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;defconfig&lt;/code&gt; requires a writable root filesystem, figure out how to make it read-only. This should be pretty easy unless you're using &lt;code&gt;systemd&lt;/code&gt;. Since Nerves uses a custom init system, keep in mind for later that &lt;code&gt;systemd&lt;/code&gt; may be helping initialize something on the board that will need to be done manually later.&lt;/li&gt;
&lt;li&gt;Take a look at the Flash memory layout and compare that to the layouts used in one of the supported systems. We use  &lt;a href="https://github.com/fhunleth/fwup" rel="noopener noreferrer"&gt;fwup&lt;/a&gt; to create images. There's a lot of variety in how one can lay out Flash memory and deal with things like failbacks. At this point, just see if you can get &lt;code&gt;fwup&lt;/code&gt; to create an image.&lt;/li&gt;
&lt;li&gt;Clone one of the official systems that seems close for your board. Update the &lt;code&gt;nerves_defconfig&lt;/code&gt; based on the Buildroot &lt;code&gt;defconfig&lt;/code&gt; that works.&lt;/li&gt;
&lt;li&gt;Build the system using &lt;code&gt;mix&lt;/code&gt; or manually by running the &lt;code&gt;create-build.sh&lt;/code&gt; script.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;I went through the steps in this order, but in retrospect, the first thing I needed to do was to understand the &lt;strong&gt;how the target machine boots&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once I understood this, I was able to finish step 2 above by learning how to use Buildroot and creating the kernel and dtb. Steps 3, 4, and 5, I use &lt;a href="https://github.com/nerves-project/nerves_system_rpi3" rel="noopener noreferrer"&gt;nerves_system_rpi3&lt;/a&gt; as base and made modifications. This is a difficult part to describe what I did.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, understand how the target machine(e-RT3) boots
&lt;/h2&gt;

&lt;p&gt;When e-RT3 is turned on, it reads the boot loader, U-Boot, in its internal ROM and starts up.&lt;/p&gt;

&lt;p&gt;The U-Boot checks if there is a uEnv.txt in the SD slots 1 and 2, and if there is, e-RT3 operates according to the file. The uEnv.txt is like a script for U-boot.&lt;/p&gt;

&lt;p&gt;The provided Ubuntu image has a uEnv.txt, which was very helpful for me as a working reference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key points of uEnv.txt
&lt;/h3&gt;

&lt;p&gt;The key points of uEnv.txt are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Where the kernel and dtb should be located.

&lt;ul&gt;
&lt;li&gt;This is determined by the configuration of the SD image you are creating.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Where (address) in RAM should they be loaded?

&lt;ul&gt;
&lt;li&gt;This is determined by the specification of target machine. In case of e-RT3, documented.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Adjust kernel parameters (root=rootfs device, etc.)

&lt;ul&gt;
&lt;li&gt;In case of e-RT3, Ubuntu's kernel image has a uEnv.txt. I could use it with modifications.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This is the first step.&lt;/p&gt;

&lt;p&gt;Once you know the above, you can use uEnv.txt to load the kernel and dtb into the target machine's RAM, pass the kernel parameters, and boot the kernel with uBoot's bootm/z command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next, use Buildroot to create the kernel, dtb, and rootfs.
&lt;/h2&gt;

&lt;p&gt;Once you know how the target boots, you will know that you need to create the kernel, dtb, and rootfs. These can be created by downloading, configuring and compiling the Linux kernel sources, but Nerves use &lt;a href="https://buildroot.org/" rel="noopener noreferrer"&gt;Buildroot&lt;/a&gt; to do this.&lt;/p&gt;

&lt;p&gt;Buildroot is a tool of configuring Linux systems (kernel, dtb, rootfs) for embedded use.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;make menuconfig&lt;/code&gt; used in the Linux kernel sources is also available in Buildroot (wrapped for Buildroot) and can be configured.&lt;/p&gt;

&lt;p&gt;Depending on the configuration, Buildroot will download what it needs and cross-compile it on the host machine.&lt;/p&gt;

&lt;p&gt;What it needs are, for example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The specified version of the Linux kernel sources&lt;/li&gt;
&lt;li&gt;Kernel patches (ex. real-time patches)&lt;/li&gt;
&lt;li&gt;A cross compiler (a compiler that creates binaries for the target machine)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/fwup-home/fwup" rel="noopener noreferrer"&gt;fwup&lt;/a&gt; for image creation&lt;/li&gt;
&lt;li&gt;...etc. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Buildroot allows us to build a linux system with only configuration if the &lt;a href="https://github.com/buildroot/buildroot/tree/master/board" rel="noopener noreferrer"&gt;board&lt;/a&gt; is registered in Buildroot.&lt;/p&gt;

&lt;p&gt;e-RT3 is not registered, so I needed to create the corresponding configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  e-RT3 Linux kernel source investigation and kernel generation
&lt;/h3&gt;

&lt;p&gt;By investigating the provided kernel sources, I found that it would be configured as follows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gz5asfoumsjofz6vgbt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gz5asfoumsjofz6vgbt.png" alt="provided kernel source"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;e-RT3 uses the Xilinx Zynq-7000, and it seemed that Xilinx &lt;a href="https://github.com/Xilinx/linux-xlnx" rel="noopener noreferrer"&gt;linux-xlnx&lt;/a&gt; code is  partially applied.&lt;/p&gt;

&lt;p&gt;The two bottom rows can be downloaded by configuring Buildroot.&lt;br&gt;
So, if I could prepare a patch for the upper two, I could  make a kernel that would cover the functions of e-RT3.&lt;br&gt;
(The &lt;a href="https://github.com/sfjro/aufs4-standalone/tree/aufs4.14.73+" rel="noopener noreferrer"&gt;aufs&lt;/a&gt; patch is not necessary for Nerves, so I ignored it.)&lt;/p&gt;

&lt;p&gt;Therefore, I made a kernel source consisting of the lower three rows shown in the above figure, and made a mixed patch of the upper two rows from the difference between that and the provided kernel source, so that the final kernel source would be shown in the figure below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb20zr3miq64zp0hx4s35.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb20zr3miq64zp0hx4s35.png" alt="kernel source for Nerves"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creating the mixed patch, follow the Buildroot reference to create the necessary configuration files in the board directory, and then generate the kernel. (I'll spare you the details.&lt;/p&gt;

&lt;p&gt;I only read the following two references which can be accessed from &lt;a href="https://buildroot.org/docs.html" rel="noopener noreferrer"&gt;https://buildroot.org/docs.html&lt;/a&gt;. Both are excellent, easy to understand documents.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://buildroot.org/downloads/manual/manual.html" rel="noopener noreferrer"&gt;The Buildroot user manual&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;I read Chapter 9 carefully.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://bootlin.com/doc/training/buildroot/buildroot-slides.pdf" rel="noopener noreferrer"&gt;Bootlin's Buildroot Training slides&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;It's about 350 pages, but you don't have to read all of it; just do a search on the keywords you want to know, you'll find the answer to your question with a high probability.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The br2-external for e-RT3 I created is available here, &lt;a href="https://github.com/pojiro/br2-external-f3rp70" rel="noopener noreferrer"&gt;br2-external-f3rp70&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  booting the created kernel
&lt;/h3&gt;

&lt;p&gt;Burn image (buildroot-dir/output/images/f3rp70.img) to SD,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F369713%2F35e922d2-e44a-2dd4-c870-9c56489727c7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F369713%2F35e922d2-e44a-2dd4-c870-9c56489727c7.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect the serial cable and turn on, and I could confirm  booting the minimal Linux created by Buildroot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F369713%2Fa9a0e3c5-f03f-d9e0-f55a-4dd64237c60a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F369713%2Fa9a0e3c5-f03f-d9e0-f55a-4dd64237c60a.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally, fit it in so that it becomes Nerves
&lt;/h2&gt;

&lt;p&gt;It's difficult to describe what I did, but &lt;a href="https://github.com/pojiro/nerves_system_f3rp70" rel="noopener noreferrer"&gt;nerves_system_f3rp70&lt;/a&gt; that I created is based on &lt;a href="https://github.com/nerves-project/nerves_system_rpi3" rel="noopener noreferrer"&gt;nerves_system_rpi3&lt;/a&gt;. So you can see what I changed by looking at the diff between  them.&lt;/p&gt;

&lt;p&gt;NOTE: In the fitting, I renamed the Buildroot's defconfig to nerves_defconfig for nerves_system_f3rp70.&lt;/p&gt;

&lt;h3&gt;
  
  
  Buildroot minimal Linux becomes Nerves!!
&lt;/h3&gt;

&lt;p&gt;Hi, Nerves!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F369713%2F553b229d-9eae-fb75-93e2-23fd8009af66.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F369713%2F553b229d-9eae-fb75-93e2-23fd8009af66.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Once Nerves is ported
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Elixir's strengths in concurrency and process monitoring/recovering are fully revealed.&lt;/li&gt;
&lt;li&gt;We can use Elixir libraries to their fullest extent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The monitoring/recovering process that was done with systemd or shell scripts in the normal Linux environment can be replaced with Elixir's Supervisor/GenServer.&lt;/li&gt;
&lt;li&gt;Nginx and Apache can be replaced by Elixir's Phoenix.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And there are no configuration files scattered around /etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;These are strengths of Nerves which use Elixir and hide the Linux details.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Addendum: I've published it on hex.pm, yey -&amp;gt; &lt;a href="https://hex.pm/packages/nerves_system_f3rp70" rel="noopener noreferrer"&gt;https://hex.pm/packages/nerves_system_f3rp70&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  e-RT3 specific support
&lt;/h3&gt;

&lt;p&gt;The e-RT3's IO needs to be accessed through libm3.so which is  provided from Yokogawa.&lt;/p&gt;

&lt;p&gt;For example, DIO is not a GPIO, we can't use &lt;a href="https://github.com/elixir-circuits/circuits_gpio" rel="noopener noreferrer"&gt;circuits_gpio&lt;/a&gt;. So, we need to use NIFs as myasu did in &lt;a href="https://qiita.com/myasu/items/0dbf308947b878423112" rel="noopener noreferrer"&gt;Real-time OS controller e-RT3 (controlled from Elixir)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It will be a good idea that creating a library such as circuits_gpio.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Industrial robust hardware, e-RT3 from Yokogawa Electric Corp&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;meets&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nerves, a game changer of embedded Linux development experience&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🎉🎉🎉&lt;/p&gt;

&lt;p&gt;This experience has given me the confidence that I can do simple porting if&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I get the boot specification&lt;/li&gt;
&lt;li&gt;I have working kernel sources (including dts).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Acknowledgements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;myasu&lt;/strong&gt; for the porting idea.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Yokogawa Electric Corp&lt;/strong&gt; for providing info openly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nerves&lt;/strong&gt; on our side&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you!!&lt;/p&gt;

</description>
      <category>nerves</category>
      <category>ert3</category>
      <category>buildroot</category>
      <category>elixir</category>
    </item>
  </channel>
</rss>
