Upload
yamanekko
View
3.005
Download
2
Tags:
Embed Size (px)
DESCRIPTION
RubyConf2013: "How to control physical devices with mruby" by Yamanekko
Citation preview
How to control How to control physical devicesphysical devices
with mrubywith mrubyRubyConf 2013RubyConf 2013
Team YamanekkoTeam YamanekkoYurie Yamane / Masayoshi TakahashiYurie Yamane / Masayoshi Takahashi
Demo
Blinking 4 LEDson STM32F4 Discovery(using Timer)
DEMO (STM32F4 Discovery)● blinking LED
Green on -> Orange on -> Red on -> Blue on -> Green off -> Orange off -> Red off -> Blue off -> ...
Contents● Demo (STM32F4 Discovery)● Introduction● What's mruby● 7 steps to use mruby● Demo (Raspberry Pi)● Another Demo (STM32F4 Discovery)
Who are we?
●やまね ゆりえ (Yurie Yamane)●高橋征義 (Masayoshi Takahashi)
Who are we?
Team Yamanekko
Independent E-Book Publisher
http://tatsu-zine.com/
Tatsu-zine Publishing Inc.
This session is supported by
http://ruby-no-kai.org/
members of asakusa.rb
Who are we?
●Not professional Embedded programmers– it's our hobby project
●not made e-book reader yet
Our goals
“embedded programming for everyone without hassle”
Our talks
RubyConf2012
Our talks
RubyConf.tw 2012
Our talks
OSDC.tw 2013
Our talks
http://www.flickr.com/photos/igaiga/8564676738/
Ôedo RubyKaigi 03
Embedded is hard??
●small memory●RTOS or no OS●no STDIO●cross compile C/C++/Asm●cross debugger with ICE
Embedded is hard??
●small memory●RTOS or no OS●no STDIO●cross compile C/C++/Asm●cross debugger with ICE
http://mrb.h2so5.net/
What's mruby?
● “yet another implementation of Ruby”–not a new language nor variant
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, ...
mruby's feature
(1) embeddable(2) configurable(3) pluggable
(1) embeddable
*.rb
C/C++ app
CRuby app mruby app
*.rb(bytecode)
/usr/bin/ruby
libmruby.a
CRuby
/usr/bin/ruby
*.rb (your code)
*.rb (stdlib) Gems
mruby
mruby core
*.rb (stdlib)
*.rb (your code)
C/C++libmruby.a
Bytecode(C String)
mrbgems
Bytecode(C String)
(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
#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
(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
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
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!
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!
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
(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
(2) Raspberry Pi● ARM (arm1176jzf-s)● 256/512MB RAM● SD card● 700mA (3.5W)● JTAG
ARM11 & Cortex-M4
Raspberry Pi
STM32F4
http://www.emcu.it/CortexFamily/CortexFamily.html
**NOTICE**
● We do not use OS on Raspberry Pi– no raspbian nor other Linux/Unix
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 :-)
bare metal RasPi Programming
● dwelch67's github repos is incredibly useful– https://github.com/dwelch67/raspberrypi– everything we need is here
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)
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
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!
Our Development Environment
● Cross Compiler: GNU tool chain● IDE: Eclipse CDT + our original plugin● Debugging :
– STLINK (for STM32F4d)– JTAG (for RasPi)
cross compiler for STM32F4GNU Tools for ARM Embedded Processors (https://launchpad.net/gcc-arm-embedded)
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
PC
How to debug with STM32F4d
STM32F4d
EclipseSTLINK stlink
USB cable
stlink: https://github.com/texane/stlink
gdbfor arm
arm-none-eabi-gdb
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
JTAG debugger (hardware)
● connect ARM via USB
https://www.olimex.com/Products/ARM/JTAG/ARM-USB-TINY-H/
ARM-USB-TINY-H
JTAG debugger (hardware)
● connect JTAG cable
and RasPi● Use 6pins only (and GND)
https://www.olimex.com/Products/ARM/_resources/arm-jtag-layout.gif
JTAG debugger (software)
● You can do fash programming and GDB debugging with OpenOCD.
http://openocd.sourceforge.net/
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.”
are you tired?
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!
write application
● write Ruby code● write mrbgems● write C code
Including Ruby code in mruby
● As source code of Ruby– Human readable
● As Byte code– Machine readable
Including Ruby code in mruby
● As source code of Ruby– Human readable
● As Byte code– Machine readable
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;}
hello world in mruby (1)
mrb_state *mrb = mrb_open(); char code[] = "p 'hello world!'"; mrb_load_string(mrb, code); mrb_close(mrb);
Including Ruby code in mruby
● As source code of Ruby– Human readable
● As Byte code– Machine readable
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);
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,};$
mrb_open
Create and initialize new mruby VM
Usage:
mrb_state *mrb = mrb_open();
mrb_close
release mruby VM
Usage:
mrb_close(mrb);
mrb_load_irep
load mruby bytecode and execute.
Usage:
mrb_load_irep(mrb, bcode);
mruby API
In include/mruby.hand include/mruby/*.h
※all API has mrb_state at 1st arg.
ex. mrb_load_string(mrb, code);
mrbgems
● “mruby gems” by bovi● library packaging system like RubyGems
● static linked with compile time
mrbgem.rake
MRuby::Gem::Specification.new(
'mruby-rs-led') do |spec|
spec.license = 'MIT'
spec.authors = 'yamanekko'
end
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
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
build_config.rb
● Ruby (not mruby) script– Execute in host machine at building time
● DSL to build mruby● Load from Rakefile
Default (minimal) build_config.rb
MRuby::Build.new do |conf|
toolchain :gcc
conf.gembox 'default'
end
DEMO (Raspberry Pi)● blinking LED (like STM32F4 Discovery)
Green on -> Orange on -> Red on -> Blue on ->Green off -> Orange off -> Red off -> Blue off
Ruby Code (init)
Ruby Code (blink)
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
Install MrubyTools plugins
Install MrubyTools plugins
create mruby project● cloned from https://github.com/mruby/mruby
CREATE CUSTOM mrbgems● create new mrbgem to control STM32F4-Discovery's LED
from ruby
generated mrbgem filesmrbgem wizard can create necessary files, directory structure and template files
generated mrbgem template
add code for Led class
← class
← constants
↑ methods
build_config.rb for RasPi
libmruby.a generated
CREATE RasPi'S PROJECT● Create new C project● Create new files
– demo.c– syscalls.c– vectors.s– memmap– makefile–
create app project
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> */
before conversion
execute mrbc wrapper
after conversion
OpenOCD uses gdb and telnet
openocd
telnet
gdbEclipse
RasPi
port 3333
port 4444
command Interface
openocd as external tool
call openocd
OpenOCD log in Eclipse
connect OpenOCD via telnet
setting gdb command
Another Demo: blinking LED & MEMS Accelerometer
using STM32F4d
Push Button
Push Button
Power ON
blinkingmode
tilt sensormode
switching 2 modes by pushing button
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
Another Demo: blinking LED & MEMS Accelerometer
using STM32F4d
Push Button
Push Button
Power ON
blinkingmode
tilt sensormode
switching 2 modes by pushing button
future issues● Ruby source level debugger
● support bytecodes generated by mrbc● more mrbgem samples
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
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/
Thanks!Happy mruby Hacking!
special thanks to: @tenderlove and @_zzak