Skip to content

Instantly share code, notes, and snippets.

@pandax381
Last active June 25, 2018 01:33
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pandax381/f024e03b9763a07bc404a376c60e0a77 to your computer and use it in GitHub Desktop.
arduino-esp32 に mruby を組み込む手順

mruby本体の準備

$ export ARDUINO_ESP32_ROOT=$HOME/Documents/Arduino/hardware/espressif/esp32
$ cd $ARDUINO_ESP32_ROOT
$ mkdir mruby
$ cd mruby
$ git clone https://github.com/mruby/mruby.git
$ cd mruby 
$ git checkout 1.4.1

mrubyのビルドコンフィグを作成

$ vi $ARDUINO_ESP32_ROOT/mruby/build_config.rb
MRuby::Build.new do |conf|
  toolchain :gcc

  conf.cc do |cc|
    cc.command = 'gcc'
    cc.flags = [%w()]
  end

  conf.cxx do |cxx|
    cxx.command = 'g++'
    cxx.flags = [%w()]
  end

  conf.archiver do |archiver|
    archiver.command = "ar"
  end

  conf.gembox 'default'
end

MRuby::CrossBuild.new('esp32') do |conf|
  toolchain :gcc

  [conf.cc, conf.objc, conf.asm].each do |cc|
    cc.command = 'xtensa-esp32-elf-gcc'
    cc.defines << %w(MRB_HEAP_PAGE_SIZE=64)
    cc.defines << %w(MRB_USE_IV_SEGLIST)
    cc.defines << %w(KHASH_DEFAULT_SIZE=8)
    cc.defines << %w(MRB_STR_BUF_MIN_SIZE=20)
    cc.defines << %w(MRB_GC_STRESS)
    cc.defines << %w(ESP_PLATFORM)
  end

  [conf.cxx].each do |cxx|
    cxx.command = 'xtensa-esp32-elf-g++'
    cxx.defines = conf.cc.defines.dup
  end

  conf.archiver do |archiver|
    archiver.command = "xtensa-esp32-elf-ar"
  end

  conf.bins = []
  conf.build_mrbtest_lib_only
  conf.disable_cxx_exception

  conf.gem :core => "mruby-print"
  conf.gem :core => "mruby-compiler"
  conf.gem :github => "mruby-esp32/mruby-esp32-system"
end

Makefileを作成

$ vi $ARDUINO_ESP32_ROOT/mruby/Makefile
ARDUINO_ESP32_ROOT=$(HOME)/Documents/Arduino/hardware/espressif/esp32

SDK_PATH := $(ARDUINO_ESP32_ROOT)/tools/sdk

INCLUDE_DIRS := $(foreach dir,$(shell ls $(SDK_PATH)/include),$(shell echo "-I$(SDK_PATH)/include/$(dir)"))

CPPFLAGS := -DESP_PLATFORM -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h" -DHAVE_CONFIG_H $(INCLUDE_DIRS)

CFLAGS := -std=gnu99 -Og -ggdb -Wno-maybe-uninitialized -fstack-protector -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -Wpointer-arith -w -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -Wno-old-style-declaration -MMD -c $(CPPFLAGS)

CXXFLAGS := -std=gnu++11 -fno-exceptions -Og -ggdb -Wno-maybe-uninitialized -Wpointer-arith -fexceptions -fstack-protector -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -w -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -fno-rtti -MMD -c $(CPPFLAGS)

ARFLAGS := cru

export CFLAGS CXXFLAGS ARFLAGS

PATH := $(ARDUINO_ESP32_ROOT)/tools/xtensa-esp32-elf/bin:$(PATH)
export PATH

all:
	cd mruby && MRUBY_CONFIG=../build_config.rb $(MAKE)

clean:
	cd mruby && MRUBY_CONFIG=../build_config.rb $(MAKE) clean

mrubyをビルド

$ cd $ARDUINO_ESP32_ROOT/mruby
$ make

libmruby.aとヘッダファイルへをarduino-esp32から参照できるようにシンボリックリンクを作成

$ ln -s $ARDUINO_ESP32_ROOT/mruby/mruby/build/esp32/lib/libmruby.a $ARDUINO_ESP32_ROOT/tools/sdk/lib
$ ln -s $ARDUINO_ESP32_ROOT/mruby/mruby/include $ARDUINO_ESP32_ROOT/tools/sdk/include/mruby

arduino-esp32のビルドコンフィグを修正

$ vi $ARDUINO_ESP32_ROOT/platform.txt
  • compiler.cpreprocessor.flags= の末尾に "-I{compiler.sdk.path}/include/mruby" を追加
  • compiler.c.elf.flags= の末尾に -lmruby を追加

サンプルのスケッチ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment