<?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: Matan Mates</title>
    <description>The latest articles on DEV Community by Matan Mates (@mtnmts).</description>
    <link>https://dev.to/mtnmts</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%2F314789%2F0161dcf0-34d0-48e4-8da3-30d7ec00727e.jpeg</url>
      <title>DEV Community: Matan Mates</title>
      <link>https://dev.to/mtnmts</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mtnmts"/>
    <language>en</language>
    <item>
      <title>Containerized builds for Rust on the ESP32</title>
      <dc:creator>Matan Mates</dc:creator>
      <pubDate>Mon, 13 Jan 2020 15:47:17 +0000</pubDate>
      <link>https://dev.to/mtnmts/containerized-builds-for-rust-on-the-esp32-e8m</link>
      <guid>https://dev.to/mtnmts/containerized-builds-for-rust-on-the-esp32-e8m</guid>
      <description>&lt;h2&gt;
  
  
  What is the ESP32?
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/ESP32" rel="noopener noreferrer"&gt;ESP32&lt;/a&gt; "is a series of low-cost, low-power system on a chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth", I've bought a few of these recently for personal projects, the very low power draw and low cost, paired with Wi-Fi and dual-mode Bluetooth capabilites make it extremely attractive for a wide range of projects.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmx9erz41di8y5h4u6zwr.jpeg" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmx9erz41di8y5h4u6zwr.jpeg" alt="ESP32 Chip"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've previously worked with the ESP32's predecessor, the &lt;a href="https://en.wikipedia.org/wiki/ESP8266" rel="noopener noreferrer"&gt;ESP8266&lt;/a&gt;, both devices have a RISC CPU that runs an ISA called Xtensa.&lt;/p&gt;

&lt;p&gt;This was a few years back and I was programming most of my embedded projects in C, Since then I fell in love with Rust and have been using it for a few projects, especially ones which are relatively close to hardware, such as programming an ESP32, so last week when I started looking programming the device I checked to see if I could target it with rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiling Rust for the Xtensa ISA
&lt;/h2&gt;

&lt;p&gt;For this to work, LLVM would need to support an Xtensa backend, in LLVM a Frontend takes some higher level language such as C,Fortran, Ada or Rust and emits LLVM-IR AST, LLVM then has optimisers that work on LLVM-IR, and finally a Backend accepts LLVM-IR and emits architecture specific &lt;em&gt;(x86_64/ARM/PowerPC/etc)&lt;/em&gt; instructions.&lt;/p&gt;

&lt;p&gt;Unfortunately, LLVM upstream does not currently support an Xtensa ISA backend, so the rust compiler does not support Xtensa out of the box, fortunately &lt;a href="https://github.com/espressif/llvm-xtensa" rel="noopener noreferrer"&gt;there is a LLVM fork&lt;/a&gt; by Espressif implementing support for Xtensa, and thanks to the awesome work of &lt;a href="https://mabez.dev/" rel="noopener noreferrer"&gt;Scott Mabin (MabezDev)&lt;/a&gt; there is a &lt;a href="https://github.com/MabezDev/rust-xtensa" rel="noopener noreferrer"&gt;fork of the Rust compiler toolchain&lt;/a&gt; supporting Xtensa. With these two components already prepared all you need to do is build LLVM and the compiler and do a bit of environment setup and you're ready to go!&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/http%3A%2F%2F4.bp.blogspot.com%2F-WDcl2mtJ8Do%2FUWnL-S-FgiI%2FAAAAAAAAAn0%2F3LTWx4a32XY%2Fs1600%2Fclang.gif" 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/http%3A%2F%2F4.bp.blogspot.com%2F-WDcl2mtJ8Do%2FUWnL-S-FgiI%2FAAAAAAAAAn0%2F3LTWx4a32XY%2Fs1600%2Fclang.gif" alt="Clang Frontend/Backend design"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a great &lt;a href="http://quickhack.net/nom/blog/2019-05-14-build-rust-environment-for-esp32.html" rel="noopener noreferrer"&gt;blog post&lt;/a&gt; by Yoshinari Nomura explaining exactly what you need to do, while the process is relatively straightforward there are quite a few dependencies and a few pitfalls that made me spend a few hours getting it to work properly for me.&lt;/p&gt;

&lt;p&gt;I work from a few different computers, and considering the time it takes to build LLVM and the compiler I decided to create a Docker image with the build environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building for the ESP32 with Docker
&lt;/h2&gt;

&lt;p&gt;I've built a Dockerfile that builds the environment (LLVM and the compiler) for out-of-the-box compilation.&lt;/p&gt;

&lt;p&gt;a quick disclaimer about the image is that it is a local build I pushed to Docker Hub and it was not built by Docker Hub due to the build taking too long and timing out, you do not need to trust the image, and you can build the Dockerfile yourself (and possibly push it to Docker Hub yourself). be aware that pushed images that were not built by Docker Hub from the Dockerfile can essentially be anything.&lt;/p&gt;

&lt;p&gt;You can now build your Rust ESP32 project with a single command if you have Docker installed, If you're looking for a baseline for your project you can use &lt;a href="https://github.com/MabezDev/xtensa-rust-quickstart" rel="noopener noreferrer"&gt;xtensa-rust-quickstart&lt;/a&gt; also by MabezDev, note that you do not need to use &lt;em&gt;setenv&lt;/em&gt; inside the project as everything is already set up properly in the machine.&lt;/p&gt;

&lt;p&gt;all you need to is &lt;code&gt;docker run -v $PWD:/code mtnmts/rust-esp32&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The image expects the project to be mounted at &lt;code&gt;/code&lt;/code&gt; (your Cargo.toml should be at &lt;code&gt;/code/Cargo.toml&lt;/code&gt;) inside the container.&lt;/p&gt;

&lt;p&gt;The image is quite heavy, if you would like to create a slimmed down version you can fork it and remove some of the intermediate build artifacts (such as LLVM).&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdvajg7qfwz0gq0brlp3m.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdvajg7qfwz0gq0brlp3m.png" alt="Containerized Build Demo"&gt;&lt;/a&gt;&lt;br&gt;
And that's it, &lt;a href="https://github.com/mtnmts/rust-esp32-container" rel="noopener noreferrer"&gt;Dockerfile is here&lt;/a&gt; if you're interested.&lt;/p&gt;

&lt;h1&gt;
  
  
  Serial Forwarding &amp;amp; Working with WSL2
&lt;/h1&gt;

&lt;p&gt;My main desktop is running Windows, but i do most of my programming work in Linux under WSL2, I wanted to be able to program the ESP32 from within WSL2, to do this I forwarded the serial port to the WSL2 VM.&lt;/p&gt;

&lt;p&gt;First, you will need &lt;a href="http://www.dest-unreach.org/socat/" rel="noopener noreferrer"&gt;socat for Windows&lt;/a&gt;, there is an unofficial build of the sources over at &lt;a href="https://sourceforge.net/projects/unix-utils/files/socat/1.7.3.2/" rel="noopener noreferrer"&gt;SourceForge&lt;/a&gt; if you don't want to build it yourself.&lt;/p&gt;

&lt;p&gt;If you don't already know it, Find the serial port assigned to your device &lt;em&gt;COM[1-9]&lt;/em&gt;&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F3pylb02uphj0pi31v2bx.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F3pylb02uphj0pi31v2bx.png" alt="Device Manager Image, Finding the correct COM Port"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking in "Device Manager" should be indicative&lt;/p&gt;

&lt;p&gt;Next, run &lt;em&gt;socat&lt;/em&gt; and set it to tunnel the COM port to TCP, do note that &lt;code&gt;/dev/ttyS3&lt;/code&gt; is not a mistake, even though there is no such device (or file system hierarchy) on Windows, this is the syntax that &lt;em&gt;socat&lt;/em&gt; expects on Windows, the number (3 in my case) is the COM port number.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; socat -d TCP4-LISTEN:1337,reuseaddr,fork /dev/ttyS3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;From there you can forward the port in many different ways to your WSL2/Linux instance, I use Tunnelier's S2C port forwarding for this, Putty can probably do this too.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;esptool&lt;/em&gt; supports flashing over TCP, the syntax is &lt;code&gt;--port socket://&amp;lt;host&amp;gt;:&amp;lt;port&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;more information on remote serial ports programming for &lt;em&gt;esptool&lt;/em&gt; can be found &lt;a href="https://github.com/espressif/esptool/wiki/Remote-Serial-Ports" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Everyone who worked on getting Rust on the ESP32 going, especially MabezDev, and to Yoshinari Nomura for his blog post explaining how to set up the environment&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Espressif for creating the LLVM fork supporting your architecture!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy Hacking!&lt;/p&gt;

&lt;h3&gt;
  
  
  Anecdotes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First time I programmed the device I stupidly guessed I should flash to 0x0, that didn't work so I guessed &lt;em&gt;again&lt;/em&gt; and flashed &lt;em&gt;0x1000&lt;/em&gt; ,That wrote over the bootloader (&lt;em&gt;yikes&lt;/em&gt;). Luckily I had the same board twice so I read the flash off the second one and re-flashed the first one, I highly recommend you backup your original flash!&lt;/li&gt;
&lt;li&gt;(The correct flash offset ended up being &lt;em&gt;0x10000&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>esp32</category>
      <category>rust</category>
      <category>llvm</category>
      <category>embedded</category>
    </item>
  </channel>
</rss>
