AtomVM ICFP 2021

http://dushin.net/talks/slides/atomvm_icfp_2021/

A tiny Erlang VM for microcontrollers

Motivation

On Fri, Jul 21, 2017 at 5:22 PM, Fred Dushin wrote:
> ... how hard it would [it] be to port a very small subset of
> Erlang to FreeRTOS, so that it could be run on cheap
> micro-controllers like the ... ESP32

You’d need to totally redesign the instruction set … and make a tiny emulator and throw away all the BIFS (in C) - when you’d done this it would no longer be Erlang.

Espressif ESP32

  • 160Mhz dual core Xtensa® 32-bit LX6
  • 520KB RAM (up to 4MB via SPI)
  • 4/8/16MB Flash
  • 34 GPIO pins
  • Built-in networking
    • 802.11 b/g/n WiFi (STA and AP modes)
    • Bluetooth 4.2
  • Power
    • 3.3v
    • 20-25mA (idle)
    • 300mA+ (WiFi)
    • 10uA (deep sleep)

The ESP32 SoC

ESP32 SoC

ESP32 WROOM Module

ESP32 Soc

$2.50 Ali Express

ESP32 devboard

ESP32 Devboard

w/5v micro-USB/UART adapter

AtomVM

  • Ground-up BEAM implementation
  • Written in C (mostly)
  • Limited (but growing) libraries in Erlang
  • Supported on
    • UNIX-like (Linux, MacOS, FreeBSD)
    • STM32
    • ESP32
  • Runs (lightly) modified BEAM files emitted from Erlang/OTP 21, 22, or 23 compiler

Language Features

  • BEAM support
    • (Currently) 110 BEAM OpCodes supported
    • Many standard Bifs and Nifs
  • Data types
    • integers, floats
    • lists, tuples, maps, binaries (refc)
    • funs, pids, references
  • Bit syntax (pack, match)
  • try/catch/after
  • spawn, spawn_link, !, receive

AtomVM-on-ESP32 Features

  • Peripherals
    • GPIO (with interrupts), UART, I2C, SPI, ADC, PWM
  • Networking
    • WiFi (STA + AP mode support)
    • gen_tcp, gen_udp
    • HTTPd, MQTT
  • System
    • Deep sleep, NVS, RTC Memory

Downstream Peripheral Support

  • DHT11/DHT12 temperature sensor
  • Bosch SensorTech BME280 temp/humidity/pressure sensor
  • Rohm Semiconductor BH1750 luminosity sensor
  • NMEA 0183 (UART) GPS receiver
  • Semtech SX127X LoRa transceiver
  • Solomon Systech SSD1306 128x64 OLED display
  • WS2812 (Neopixel)
  • ESP32 Camera

The Biggest Challenge

Memory (i.e., RAM)!

ESP32 Flash Layout

ESP32 Flash layout

mmap tricks

ESP32 BEAM Module

Thank you!

ESP32 SSD1306

(Try scanning it!)

Resources

QR Code

#{
    esp32_free_heap_size => 242188,
    procs => [
        #{  heap_size => 16,
            memory => 472,
            message_queue_len => 0,
            proc => <0.2.0>,
            stack_size => 5},
        #{  heap_size => 21,
            memory => 556,
            message_queue_len => 0,
            proc => <0.1.0>,
            stack_size => 14}
]}

Overflow

AtomVM Layers

AtomVM Layers

What’s NOT there (yet)

  • Arbitrarily large bignums
  • SMP support
  • Code hot swapping
  • disterl
  • Large swaths of OTP

Downstream Tooling

atomvm_rebar3_plugin

%% rebar.config
{plugins, [
    {atomvm_rebar3_plugin, "0.2.0"}
]}.
shell$ rebar3 esp32_flash --port /dev/tty.usbserial-01