101
How to control How to control physical devices physical devices with mruby with mruby RubyConf 2013 RubyConf 2013 Team Yamanekko Team Yamanekko Yurie Yamane / Masayoshi Takahashi Yurie Yamane / Masayoshi Takahashi

How to control physical devices with mruby

Embed Size (px)

DESCRIPTION

RubyConf2013: "How to control physical devices with mruby" by Yamanekko

Citation preview

Page 1: How to control physical devices with mruby

How to control How to control physical devicesphysical devices

with mrubywith mrubyRubyConf 2013RubyConf 2013

Team YamanekkoTeam YamanekkoYurie Yamane / Masayoshi TakahashiYurie Yamane / Masayoshi Takahashi

Page 2: How to control physical devices with mruby

Demo

Blinking 4 LEDson STM32F4 Discovery(using Timer)

Page 3: How to control physical devices with mruby

DEMO (STM32F4 Discovery)● blinking LED

Green on -> Orange on -> Red on -> Blue on -> Green off -> Orange off -> Red off -> Blue off -> ...

Page 4: How to control physical devices with mruby

Contents● Demo (STM32F4 Discovery)● Introduction● What's mruby● 7 steps to use mruby● Demo (Raspberry Pi)● Another Demo (STM32F4 Discovery)

Page 5: How to control physical devices with mruby

Who are we?

●やまね ゆりえ (Yurie Yamane)●高橋征義 (Masayoshi Takahashi)

Page 6: How to control physical devices with mruby

Who are we?

Team Yamanekko

Page 7: How to control physical devices with mruby

Independent E-Book Publisher

http://tatsu-zine.com/

Tatsu-zine Publishing Inc.

Page 8: How to control physical devices with mruby

This session is supported by

http://ruby-no-kai.org/

Page 9: How to control physical devices with mruby

members of asakusa.rb

Page 10: How to control physical devices with mruby

Who are we?

●Not professional Embedded programmers– it's our hobby project

●not made e-book reader yet

Page 11: How to control physical devices with mruby

Our goals

“embedded programming for everyone without hassle”

Page 12: How to control physical devices with mruby

Our talks

RubyConf2012

Page 13: How to control physical devices with mruby

Our talks

RubyConf.tw 2012

Page 14: How to control physical devices with mruby

Our talks

OSDC.tw 2013

Page 15: How to control physical devices with mruby

Our talks

http://www.flickr.com/photos/igaiga/8564676738/

Ôedo RubyKaigi 03

Page 16: How to control physical devices with mruby

Embedded is hard??

●small memory●RTOS or no OS●no STDIO●cross compile C/C++/Asm●cross debugger with ICE

Page 17: How to control physical devices with mruby

Embedded is hard??

●small memory●RTOS or no OS●no STDIO●cross compile C/C++/Asm●cross debugger with ICE

Page 18: How to control physical devices with mruby

http://mrb.h2so5.net/

Page 19: How to control physical devices with mruby

What's mruby?

● “yet another implementation of Ruby”–not a new language nor variant

Page 20: How to control physical devices with mruby

CRuby vs mruby

●mruby is not “an application”●mruby is “a part”

–embedded into other application●webserver, editor, game, ...

–embedded into other systems●robot, router, vending machine, ...

Page 21: How to control physical devices with mruby

mruby's feature

(1) embeddable(2) configurable(3) pluggable

Page 22: How to control physical devices with mruby

(1) embeddable

*.rb

C/C++ app

CRuby app mruby app

*.rb(bytecode)

/usr/bin/ruby

libmruby.a

Page 23: How to control physical devices with mruby

CRuby

/usr/bin/ruby

*.rb (your code)

*.rb (stdlib) Gems

Page 24: How to control physical devices with mruby

mruby

mruby core

*.rb (stdlib)

*.rb (your code)

C/C++libmruby.a

Bytecode(C String)

mrbgems

Bytecode(C String)

Page 25: How to control physical devices with mruby

(2) configurable● mruby has many #define macros to configure

– float or double as Float object– using NaN boxing or not– default size of heap, pool, khash, ...

● You can change them at compile time

Page 26: How to control physical devices with mruby

#define in mrbconf.h● MRB_USE_FLOAT● MRB_INT64● MRB_NAN_BOXING● MRB_ENDIAN_BIG● MRB_FUNCALL_ARGC_MAX● MRB_HEAP_PAGE_SIZE● MRB_USE_IV_SEGLIST● MRB_IVHASH_INIT_SIZE

● MRB_IREP_ARRAY_INIT_SIZE● KHASH_DEFAULT_SIZE● POOL_ALIGNMENT● POOL_PAGE_SIZE● MRB_STR_BUF_MIN_SIZE● MRB_PARSER_BUF_SIZE● ENABLE_DEBUG

Page 27: How to control physical devices with mruby

(3) pluggable● mruby has mrbgems like RubyGems● mrbgems in C are linked statically● some functions in CRuby are provided as gem– you can choose which is added / removed

Page 28: How to control physical devices with mruby

standard mrbgems● mrbgems/mruby-array-ext● mrbgems/mruby-enum-ext● mrbgems/mruby-eval● mrbgems/mruby-exit● mrbgems/mruby-fiber● mrbgems/mruby-hash-ext● mrbgems/mruby-math● mrbgems/mruby-numeric-ext● mrbgems/mruby-object-ext● mrbgems/mruby-objectspace

● mrbgems/mruby-print● mrbgems/mruby-proc-ext● mrbgems/mruby-random● mrbgems/mruby-range-ext● mrbgems/mruby-sprintf● mrbgems/mruby-string-ext● mrbgems/mruby-struct● mrbgems/mruby-symbol-ext● mrbgems/mruby-time● mrbgems/mruby-toplevel-ext

you can remove them all

Page 29: How to control physical devices with mruby

7 steps to use mruby ● choose your target board● setup development environments● write application● cross build● transfer binary to the board● cross debugging● it works!

Page 30: How to control physical devices with mruby

7 steps to use mruby ● choose your target board● setup development environments● write application● cross build● transfer binary to the board● cross debugging● it works!

Page 31: How to control physical devices with mruby

Our targets crieteria● easy to buy around the world

– some are hard outside Japan :(● don't use apps depending on OSs or environments

– FLOSS tools are better● a bit large memory that mruby can run on

– > 64KB is MUST, > 1MB is BETTER

Page 32: How to control physical devices with mruby

(1) STM32F4 Discovery● ARM (Cortex M4)● 1MB Flash, 192KB RAM● 8LEDs, 2Buttons, MEMS Accelerometer etc.

● on board STLINK

http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF252419

Page 33: How to control physical devices with mruby

(2) Raspberry Pi● ARM (arm1176jzf-s)● 256/512MB RAM● SD card● 700mA (3.5W)● JTAG

Page 34: How to control physical devices with mruby

ARM11 & Cortex-M4

Raspberry Pi

STM32F4

http://www.emcu.it/CortexFamily/CortexFamily.html

Page 35: How to control physical devices with mruby

**NOTICE**

● We do not use OS on Raspberry Pi– no raspbian nor other Linux/Unix

Page 36: How to control physical devices with mruby

Why no OS (bare metal)?● same as STM32F4 Discovery

–STM32F4 Discovery is used without OS● We want to use Raspberry Pi like MCU, not PC

– just as MCU with HUGE memory–If using Linux, even CRuby can work on Raspberry Pi :-/

● Bare metal programming is fun :-)

Page 37: How to control physical devices with mruby

bare metal RasPi Programming

● dwelch67's github repos is incredibly useful– https://github.com/dwelch67/raspberrypi– everything we need is here

Page 38: How to control physical devices with mruby

STM32F4d architecture

CortexMCU

Systicktimer

NVICinterrupt controller

otherperipheralsM

CU

Peripheral Register & Interrupt Vector Definitions

Core PeripheralFunctions

Device PeripheralFunctions

mruby core (in c)

mrbgems for STM32F4 Discovery

Ruby code (*.rb → bytecode)

CMSI

Sap

ps

libc (newlib), syscalls

libmruby.amain(c)

Page 39: How to control physical devices with mruby

Raspberry Pi architecture

ARM11MCU

Timer Other BCM2385peripheralsSo

C

mruby core

mrbgems for Raspberry Pi

Ruby code (*.rb → bytecode)

apps

libc (newlib), syscalls

vectors.s

libmruby.a

Page 40: How to control physical devices with mruby

7 steps to use mruby ● choose your target board● setup development environments● write application● cross build● transfer binary to the board● cross debugging● it works!

Page 41: How to control physical devices with mruby

Our Development Environment

● Cross Compiler: GNU tool chain● IDE: Eclipse CDT + our original plugin● Debugging :

– STLINK (for STM32F4d)– JTAG (for RasPi)

Page 42: How to control physical devices with mruby

cross compiler for STM32F4GNU Tools for ARM Embedded Processors (https://launchpad.net/gcc-arm-embedded)

Page 43: How to control physical devices with mruby

cross compiler for RasPi● 'GNU Tools for ARM Embedded Processors' is...

– “1. ARM11 is not our primary targets, so those tools are not tested on such targets.”

– “Thus I don't recommend this tool chain for Raspberry Pi.”https://answers.launchpad.net/gcc-arm-embedded/+question/227876

● So we use GNU Tools built by our own.https://github.com/dwelch67/build_gcc/blob/master/build_arm

Page 44: How to control physical devices with mruby

PC

How to debug with STM32F4d

STM32F4d

EclipseSTLINK stlink

USB cable

stlink: https://github.com/texane/stlink

gdbfor arm

arm-none-eabi-gdb

Page 45: How to control physical devices with mruby

PC

How to debug with RasPi

RasPi

USB and JTAG cable

OpenOCD: http://openocd.sf.net/armjtag: https://github.com/dwelch67/raspberrypi/tree/master/armjtag

Eclipsearmjtag OpenOCD gdbfor arm

arm-none-eabi-gdb

Page 46: How to control physical devices with mruby

JTAG debugger (hardware)

● connect ARM via USB

https://www.olimex.com/Products/ARM/JTAG/ARM-USB-TINY-H/

ARM-USB-TINY-H

Page 47: How to control physical devices with mruby

JTAG debugger (hardware)

● connect JTAG cable

and RasPi● Use 6pins only (and GND)

https://www.olimex.com/Products/ARM/_resources/arm-jtag-layout.gif

Page 48: How to control physical devices with mruby

JTAG debugger (software)

● You can do fash programming and GDB debugging with OpenOCD.

http://openocd.sourceforge.net/

Page 49: How to control physical devices with mruby

armjtag

https://github.com/dwelch67/raspberrypi/tree/master/armjtag

“an alternate bootloader”“The ARM11 on the Raspberry Pi has a jtag based debug port for software development.”

Page 50: How to control physical devices with mruby

are you tired?

Page 51: How to control physical devices with mruby

7 steps to use mruby ● choose your target board● setup development environments● write application● cross build● transfer binary to the board● cross debugging● it works!

Page 52: How to control physical devices with mruby

write application

● write Ruby code● write mrbgems● write C code

Page 53: How to control physical devices with mruby

Including Ruby code in mruby

● As source code of Ruby– Human readable

● As Byte code– Machine readable

Page 54: How to control physical devices with mruby

Including Ruby code in mruby

● As source code of Ruby– Human readable

● As Byte code– Machine readable

Page 55: How to control physical devices with mruby

hello world in mruby (1)#include <stdlib.h>#include <stdio.h>#include <mruby.h>#include <mruby/compile.h>

int main(void){ mrb_state *mrb = mrb_open(); char code[] = "p 'hello world!'"; mrb_load_string(mrb, code); mrb_close(mrb); return 0;}

Page 56: How to control physical devices with mruby

hello world in mruby (1)

mrb_state *mrb = mrb_open(); char code[] = "p 'hello world!'"; mrb_load_string(mrb, code); mrb_close(mrb);

Page 57: How to control physical devices with mruby

Including Ruby code in mruby

● As source code of Ruby– Human readable

● As Byte code– Machine readable

Page 58: How to control physical devices with mruby

hello world in mruby (2) mrb_state *mrb = mrb_open();

const uint8_t bcode[] = { 0x52,0x49,0x54,0x45,0x30,0x30,0x30, (… byte byte byte …) 0x00,0x00,0x00,0x00,0x08, };

mrb_load_irep(mrb, bcode); mrb_close(mrb);

Page 59: How to control physical devices with mruby

hello world in mruby (2)$ cat hello.rbp 'hello world!'$ ./bin/mrbc -Bbcode hello.rb$ cat hello.c#include <stdint.h>const uint8_t foo[] = {0x52,0x49,0x54,0x45,0x30,0x30,0x30,0x31,0xb9,0x71,0x00,0x00,0x00,0x65,0x4d,0x41,(…)0x00,0x00,0x00,0x00,0x08,};$

Page 60: How to control physical devices with mruby

mrb_open

Create and initialize new mruby VM

Usage:

mrb_state *mrb = mrb_open();

Page 61: How to control physical devices with mruby

mrb_close

release mruby VM

Usage:

mrb_close(mrb);

Page 62: How to control physical devices with mruby

mrb_load_irep

load mruby bytecode and execute.

Usage:

mrb_load_irep(mrb, bcode);

Page 63: How to control physical devices with mruby

mruby API

In include/mruby.hand include/mruby/*.h

※all API has mrb_state at 1st arg.

ex. mrb_load_string(mrb, code);

Page 64: How to control physical devices with mruby

mrbgems

● “mruby gems” by bovi● library packaging system like RubyGems

● static linked with compile time

Page 65: How to control physical devices with mruby

mrbgem.rake

MRuby::Gem::Specification.new(

'mruby-rs-led') do |spec|

spec.license = 'MIT'

spec.authors = 'yamanekko'

end

Page 66: How to control physical devices with mruby

minirake

mruby has minirake full source– minimal rake clone by masuidrive– you do not have to install rake even if using Ruby 1.8

● mrbgems and mruby itself are built by minirake

Page 67: How to control physical devices with mruby

Why (mini)rake● old mruby used CMake● but mruby build system became complex

– build mrbgems (Ruby and C)– get source code from github– support gem bundle (gembox)

● we want to write them in Ruby

Page 68: How to control physical devices with mruby

build_config.rb

● Ruby (not mruby) script– Execute in host machine at building time

● DSL to build mruby● Load from Rakefile

Page 69: How to control physical devices with mruby

Default (minimal) build_config.rb

MRuby::Build.new do |conf|

toolchain :gcc

conf.gembox 'default'

end

Page 70: How to control physical devices with mruby

DEMO (Raspberry Pi)● blinking LED (like STM32F4 Discovery)

Green on -> Orange on -> Red on -> Blue on ->Green off -> Orange off -> Red off -> Blue off

Page 71: How to control physical devices with mruby

Ruby Code (init)

Page 72: How to control physical devices with mruby

Ruby Code (blink)

Page 73: How to control physical devices with mruby

7 steps to use mruby ● choose your target board● setup development environments● write application● cross build● transfer binary to the board● cross debugging● it works!

demousing Eclipse

Page 74: How to control physical devices with mruby

Install MrubyTools plugins

Page 75: How to control physical devices with mruby

Install MrubyTools plugins

Page 76: How to control physical devices with mruby

create mruby project● cloned from https://github.com/mruby/mruby

Page 77: How to control physical devices with mruby

CREATE CUSTOM mrbgems● create new mrbgem to control STM32F4-Discovery's LED

from ruby

Page 78: How to control physical devices with mruby

generated mrbgem filesmrbgem wizard can create necessary files, directory structure and template files

Page 79: How to control physical devices with mruby

generated mrbgem template

Page 80: How to control physical devices with mruby

add code for Led class

← class

← constants

↑ methods

Page 81: How to control physical devices with mruby

build_config.rb for RasPi

Page 82: How to control physical devices with mruby

libmruby.a generated

Page 83: How to control physical devices with mruby

CREATE RasPi'S PROJECT● Create new C project● Create new files

– demo.c– syscalls.c– vectors.s– memmap– makefile–

Page 84: How to control physical devices with mruby

create app project

Page 85: How to control physical devices with mruby

Write ruby code● Insert “beginning tag” and “ending tag”● write ruby code between beginning tag and ending tag● Two ways to embed ruby code1.Use menu bar

2.Use tool bar

Insert ruby code in .c file

Insert .rb file name in .c file

/* <ruby->

... <-ruby> */

/* <rubyfile->

aaa.rb <-rubyfile> */

Page 86: How to control physical devices with mruby

before conversion

Page 87: How to control physical devices with mruby

execute mrbc wrapper

Page 88: How to control physical devices with mruby

after conversion

Page 89: How to control physical devices with mruby

OpenOCD uses gdb and telnet

openocd

telnet

gdbEclipse

RasPi

port 3333

port 4444

command Interface

Page 90: How to control physical devices with mruby

openocd as external tool

Page 91: How to control physical devices with mruby

call openocd

Page 92: How to control physical devices with mruby

OpenOCD log in Eclipse

Page 93: How to control physical devices with mruby

connect OpenOCD via telnet

Page 94: How to control physical devices with mruby

setting gdb command

Page 95: How to control physical devices with mruby

Another Demo: blinking LED & MEMS Accelerometer

using STM32F4d

Push Button

Push Button

Power ON

blinkingmode

tilt sensormode

switching 2 modes by pushing button

Page 96: How to control physical devices with mruby

Tilt sensor in STM32F4d

● using 3-axis MEMS accelerometer in STM32F4 Discovery– only use 2 axis (X and Y)– LED show which side is downward

Page 97: How to control physical devices with mruby

Another Demo: blinking LED & MEMS Accelerometer

using STM32F4d

Push Button

Push Button

Power ON

blinkingmode

tilt sensormode

switching 2 modes by pushing button

Page 98: How to control physical devices with mruby

future issues● Ruby source level debugger

● support bytecodes generated by mrbc● more mrbgem samples

Page 99: How to control physical devices with mruby

Our Links● source code

● http://github.com/yamanekko/mruby/tree/rubyconf● Eclipse Plugin download site (for Eclipse)

– http://yamanekko.github.io/mruby-tools-EclipsePlugin● This slide

● http://slideshare.net/yamanekko/rubyconf2013

Page 100: How to control physical devices with mruby

Useful mruby linksmruby repos: https://github.com/mruby/mrubymruby.sh: http://blog.mruby.sh/Introduction to Lightweight Ruby "mruby" (by Toshiba): http://www.tjsys.co.jp/page.jsp?id=3059

mruby code search: http://mruby-code-search.ongaeshi.me/ngx_mruby: http://matsumoto-r.github.io/ngx_mruby/webruby: https://github.com/xxuejie/webrubymobiruby: http://mobiruby.org/

Page 101: How to control physical devices with mruby

Thanks!Happy mruby Hacking!

special thanks to: @tenderlove and @_zzak