How to control physical devices with mruby

4,371 views

Published on

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

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,371
On SlideShare
0
From Embeds
0
Number of Embeds
286
Actions
Shares
0
Downloads
32
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

How to control physical devices with mruby

  1. 1. How to control physical devices with mruby RubyConf 2013 Team Yamanekko Yurie Yamane / Masayoshi Takahashi
  2. 2. Demo Blinking 4 LEDs on STM32F4 Discovery (using Timer)
  3. 3. DEMO (STM32F4 Discovery) ● blinking LED Green on -> Orange on -> Red on -> Blue on -> Green off -> Orange off -> Red off -> Blue off -> ...
  4. 4. Contents ● Demo (STM32F4 Discovery) ● Introduction ● What's mruby ● 7 steps to use mruby ● Demo (Raspberry Pi) ● Another Demo (STM32F4 Discovery)
  5. 5. Who are we? ● やまね ゆりえ ● 高橋征義 (Yurie Yamane) (Masayoshi Takahashi)
  6. 6. Who are we? Team Yamanekko
  7. 7. Independent E-Book Publisher Tatsu-zine Publishing Inc. http:/ /tatsu-zine.com/
  8. 8. This session is supported by http:/ /ruby-no-kai.org/
  9. 9. members of asakusa.rb
  10. 10. Who are we? ● Not professional Embedded programmers – it's our hobby project ● not made e-book reader yet
  11. 11. Our goals “embedded programming for everyone without hassle”
  12. 12. Our talks RubyConf2012
  13. 13. Our talks RubyConf.tw 2012
  14. 14. Our talks OSDC.tw 2013
  15. 15. Our talks http://www.flickr.com/photos/igaiga/8564676738/ Ôedo RubyKaigi 03
  16. 16. Embedded is hard?? ● small memory ● RTOS or no OS ● no STDIO ● cross compile C/C++/Asm ● cross debugger with ICE
  17. 17. Embedded is hard?? ● small memory ● RTOS or no OS ● no STDIO ● cross compile C/C++/Asm ● cross debugger with ICE
  18. 18. http://mrb.h2so5.net/
  19. 19. What's mruby? ● “yet another implementation of Ruby” – not a new language nor variant
  20. 20. 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, ...
  21. 21. mruby's feature (1) embeddable (2) configurable (3) pluggable
  22. 22. (1) embeddable CRuby app mruby app *.rb *.rb (bytecode) libmruby.a /usr/bin/ruby C/C++ app
  23. 23. CRuby *.rb (your code) *.rb (stdlib) Gems /usr/bin/ruby
  24. 24. Bytecode (C String) Bytecode (C String) mruby *.rb (your code) *.rb (stdlib) mrbgems libmruby.a mruby core C/C++
  25. 25. (2) configurable ● mruby has many #define macros to configure – – using NaN boxing or not – ● float or double as Float object default size of heap, pool, khash, ... You can change them at compile time
  26. 26. #define in mrbconf.h ● MRB_USE_FLOAT ● MRB_IREP_ARRAY_INIT_SIZE ● MRB_INT64 ● KHASH_DEFAULT_SIZE ● MRB_NAN_BOXING ● POOL_ALIGNMENT ● MRB_ENDIAN_BIG ● POOL_PAGE_SIZE ● MRB_FUNCALL_ARGC_MAX ● MRB_STR_BUF_MIN_SIZE ● MRB_HEAP_PAGE_SIZE ● MRB_PARSER_BUF_SIZE ● MRB_USE_IV_SEGLIST ● ENABLE_DEBUG ● MRB_IVHASH_INIT_SIZE
  27. 27. (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
  28. 28. 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
  29. 29. 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!
  30. 30. 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!
  31. 31. Our targets crieteria ● easy to buy around the world – ● don't use apps depending on OSs or environments – ● some are hard outside Japan :( FLOSS tools are better a bit large memory that mruby can run on – > 64KB is MUST, > 1MB is BETTER
  32. 32. (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
  33. 33. (2) Raspberry Pi ● ARM (arm1176jzf-s) ● 256/512MB RAM ● SD card ● 700mA (3.5W) ● JTAG
  34. 34. ARM11 & Cortex-M4 Raspberry Pi STM32F4 http://www.emcu.it/CortexFamily/CortexFamily.html
  35. 35. **NOTICE** ● We do not use OS on Raspberry Pi – no raspbian nor other Linux/Unix
  36. 36. Why no OS (bare metal)? ● same as STM32F4 Discovery – ● We want to use Raspberry Pi like MCU, not PC – – ● STM32F4 Discovery is used without OS just as MCU with HUGE memory If using Linux, even CRuby can work on Raspberry Pi :-/ Bare metal programming is fun :-)
  37. 37. bare metal RasPi Programming ● dwelch67's github repos is incredibly useful – – https:/ /github.com/dwelch67/raspberrypi everything we need is here
  38. 38. apps STM32F4d architecture main (c) Ruby code (*.rb → bytecode) mruby core (in c) mrbgems for STM32F4 Discovery libmruby.a CMSIS Core Peripheral Device Peripheral Functions Functions Peripheral Register & Interrupt Vector Definitions MCU libc (newlib), syscalls Cortex MCU Systick timer NVIC interrupt controller other peripherals
  39. 39. Raspberry Pi architecture Ruby code (*.rb → bytecode) apps mruby core mrbgems for Raspberry Pi libmruby.a libc (newlib), syscalls SoC vectors.s ARM11 MCU Timer Other BCM2385 peripherals
  40. 40. 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!
  41. 41. Our Development Environment ● Cross Compiler: GNU tool chain ● IDE: Eclipse CDT + our original plugin ● Debugging : – STLINK (for STM32F4d) – JTAG (for RasPi)
  42. 42. cross compiler for STM32F4 GNU Tools for ARM Embedded Processors (https:/ /launchpad.net/gcc-arm-embedded)
  43. 43. 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
  44. 44. How to debug with STM32F4d PC STM32F4d stlink STLINK USB cable gdb for arm Eclipse arm-none-eabi-gdb stlink: https:/ /github.com/texane/stlink
  45. 45. How to debug with RasPi PC RasPi armjtag OpenOCD USB and JTAG cable gdb for arm Eclipse arm-none-eabi-gdb OpenOCD: http:/ /openocd.sf.net/ armjtag: https:/ /github.com/dwelch67/raspberrypi/tree/master/armjtag
  46. 46. JTAG debugger (hardware) ARM-USB-TINY-H ● connect ARM via USB https://www.olimex.com/Products/ARM/JTAG/ARM-USB-TINY-H/
  47. 47. JTAG debugger (hardware) ● connect JTAG cable and RasPi ● Use 6pins only (and GND) https:/ /www.olimex.com/Products/ARM/_resources/arm-jtag-layout.gif
  48. 48. JTAG debugger (software) ● http://openocd.sourceforge.net/ You can do fash programming and GDB debugging with OpenOCD.
  49. 49. 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.”
  50. 50. are you tired?
  51. 51. 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!
  52. 52. write application ● write Ruby code ● write mrbgems ● write C code
  53. 53. Including Ruby code in mruby ● As source code of Ruby Human readable As Byte code – ● – Machine readable
  54. 54. Including Ruby code in mruby ● As source code of Ruby Human readable As Byte code – ● – Machine readable
  55. 55. hello world in mruby (1) #include #include #include #include <stdlib.h> <stdio.h> <mruby.h> <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; }
  56. 56. hello world in mruby (1) mrb_state *mrb = mrb_open(); char code[] = "p 'hello world!'"; mrb_load_string(mrb, code); mrb_close(mrb);
  57. 57. Including Ruby code in mruby ● As source code of Ruby Human readable As Byte code – ● – Machine readable
  58. 58. 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);
  59. 59. hello world in mruby (2) $ cat hello.rb p '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,0x b9,0x71,0x00,0x00,0x00,0x65,0x4d,0x41, (…) 0x00,0x00,0x00,0x00,0x08, }; $
  60. 60. mrb_open Create and initialize new mruby VM Usage: mrb_state *mrb = mrb_open();
  61. 61. mrb_close release mruby VM Usage: mrb_close(mrb);
  62. 62. mrb_load_irep load mruby bytecode and execute. Usage: mrb_load_irep(mrb, bcode);
  63. 63. mruby API In include/mruby.h and include/mruby/*.h ※all API has mrb_state at 1st arg. ex. mrb_load_string(mrb, code);
  64. 64. mrbgems ● ● ● “mruby gems” by bovi library packaging system like RubyGems static linked with compile time
  65. 65. mrbgem.rake MRuby::Gem::Specification.new( 'mruby-rs-led') do |spec| spec.license = 'MIT' spec.authors = 'yamanekko' end
  66. 66. 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 – ●
  67. 67. Why (mini)rake ● old mruby used CMake ● but mruby build system became complex – – get source code from github – ● build mrbgems (Ruby and C) support gem bundle (gembox) we want to write them in Ruby
  68. 68. build_config.rb ● Ruby (not mruby) script Execute in host machine at building time DSL to build mruby – ● ● Load from Rakefile
  69. 69. Default (minimal) build_config.rb MRuby::Build.new do |conf| toolchain :gcc conf.gembox 'default' end
  70. 70. DEMO (Raspberry Pi) ● blinking LED (like STM32F4 Discovery) Green on -> Orange on -> Red on -> Blue on -> Green off -> Orange off -> Red off -> Blue off
  71. 71. Ruby Code (init)
  72. 72. Ruby Code (blink)
  73. 73. 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! demo using Eclipse
  74. 74. Install MrubyTools plugins
  75. 75. Install MrubyTools plugins
  76. 76. create mruby project ● cloned from https:/ /github.com/mruby/mruby
  77. 77. CREATE CUSTOM mrbgems ● create new mrbgem to control STM32F4-Discovery's LED from ruby
  78. 78. generated mrbgem files mrbgem wizard can create necessary files, directory structure and template files
  79. 79. generated mrbgem template
  80. 80. add code for Led class ← class ← constants ↑ methods
  81. 81. build_config.rb for RasPi
  82. 82. libmruby.a generated
  83. 83. CREATE RasPi'S PROJECT ● Create new C project ● Create new files – demo.c – syscalls.c – vectors.s – memmap – makefile –
  84. 84. create app project
  85. 85. Write ruby code ● Insert “beginning tag” and “ending tag” ● write ruby code between beginning tag and ending tag ● Two ways to embed ruby code 1. se U menu bar Insert ruby code in .c file 2. se U tool bar /* <ruby-> ... <-ruby> */ Insert .rb file name in .c file /* <rubyfile-> aaa.rb <-rubyfile> */
  86. 86. before conversion
  87. 87. execute mrbc wrapper
  88. 88. after conversion
  89. 89. OpenOCD uses gdb and telnet Eclipse gdb command Interface telnet port 3333 openocd RasPi port 4444
  90. 90. openocd as external tool
  91. 91. call openocd
  92. 92. OpenOCD log in Eclipse
  93. 93. connect OpenOCD via telnet
  94. 94. setting gdb command
  95. 95. Another Demo: blinking LED & MEMS Accelerometer using STM32F4d Power ON blinking mode Push Button Push Button tilt sensor mode switching 2 modes by pushing button
  96. 96. 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 –
  97. 97. Another Demo: blinking LED & MEMS Accelerometer using STM32F4d Power ON blinking mode Push Button Push Button tilt sensor mode switching 2 modes by pushing button
  98. 98. future issues ● Ruby source level debugger ● ● support bytecodes generated by mrbc more mrbgem samples
  99. 99. Our Links ● source code ● ● Eclipse Plugin download site (for Eclipse) – ● http:/ /github.com/yamanekko/mruby/tree/rubyconf http:/ /yamanekko.github.io/mruby-tools-EclipsePlugin This slide ● http:/ /slideshare.net/yamanekko/rubyconf2013
  100. 100. Useful mruby links mruby repos: https:/ /github.com/mruby/mruby mruby.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/webruby mobiruby: http:/ /mobiruby.org/
  101. 101. Thanks! Happy mruby Hacking! special thanks to: @tenderlove and @_zzak

×