Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RustのHAL(avr-hal)でLeafonyを動かした件

84931a5617d844ecfda982721de3a9e9?s=47 k-abe
October 24, 2021

 RustのHAL(avr-hal)でLeafonyを動かした件

2021/10/24 TOPPERS開発者会議 LT発表資料

84931a5617d844ecfda982721de3a9e9?s=128

k-abe

October 24, 2021
Tweet

More Decks by k-abe

Other Decks in Programming

Transcript

  1. 2021.10.24 ύʔιϧςΫϊϩδʔελοϑגࣜձࣾ Ѩ෦ߞೋ koji.abe@persol.co.jp RustͷHAL(avr-hal)ͰLeafonyΛಈ͔ͨ݅͠ TOPPERS։ൃऀձٞ2021

  2. • ࣗݾ঺հ • ͓࿩͢Δ͜ͱ • ༻ޠઆ໌ • ։ൃ؀ڥ • ۩ମత࡞ۀ

    • ݁Ռ • ՝୊ ໨࣍
  3. • ໊લɿѨ෦ɹߞೋ(͋΂ɹ͜͏͡) • ύʔιϧςΫϊϩδʔελοϑגࣜձࣾɹϝʔϧɿkoji.abe@persol.co.jp • ҩྍػث։ൃɻ • ૊ࠐΈιϑτ΢ΣΞ։ൃɻCݴޠɺOSͳ͠(ϕΞϝλϧ)ͷ։ൃۀ຿ܦݧ͕΄ͱΜͲɻ • twitter:

    @juraruming • ϒϩά: https://k-abe.hatenablog.com/ ࣗݾ঺հ
  4. • Leafonyͱ͍͏খܕج൘ͷCιʔεαϯϓϧΛRustʹҠ২ͨ͠ɻ ɹϋʔυ΢ΣΞґଘ෦ͷҠ২ɻHAL(avr-hal)ͰରԠ͍ͯ͠ͳ͍ϋʔ υ΢ΣΞʹରԠͤͨ͞ɻ • ಈ࡞֬ೝͰ͖֤ͨछϖϦϑΣϥϧ ɹgpioͰLνΧɺuartɺi2cɺdelay ͓࿩͢Δ͜ͱ

  5. 1.Leafony 2.avr-hal ༻ޠઆ໌

  6. 1.Leafony ɾখܕͷج൘ɻৄ͘͠͸ͪ͜ΒΛࢀরɻ https://docs.leafony.com/docs/overview/ ɾArduino UNOͱಉ͡AVRϚΠίϯ(Atmega328P)͕౥ࡌ͞Ε͍ͯΔ ɾArduino UNOͱͷҧ͍͸ΫϩοΫ(UNO =16MHz, Leafony =

    8MHz) ɾࠓճ͸ʲBasic Kitʳ͕ର৅ɻ https://docs.leafony.com/docs/products/basic/ ༻ޠઆ໌
  7. 2, avr-hal ɾArduino޲͚ͷHALɻ https://github.com/Rahix/avr-hal ɾHALͷબ୒ࢶͱͯ͠avr-rustͱavr-hal͕͋ͬͨ ͕avr-halΛબ୒(avr-rust͸؀ڥߏஙͰ͖ͣஅ೦)ɻ ɾavr-hal͸ӈਤͷ֤छArduinoγϦʔζΛαϙʔ τɻ ༻ޠઆ໌

  8. ։ൃ؀ڥ No ߲໨ όʔδϣϯ උߟ 1 ϗετPC MacBook macOS Big

    Sur όʔδϣϯ11.6 2 λʔήοτϋʔυ΢ΣΞ Leafony Basic Kit 3 Rust ίϯύΠϥ ɾnightly-2021-01-07 ɾrustc 1.51.0-nightly avr-halͰόʔδϣϯࢦఆͷͨΊ 4 avr-hal 2021/10/15ݱࡏ࠷৽ͷ ίϛοτ: 8ad1be ίϛοτ೔࣌: 2021/9/17
  9. 1. ։ൃ؀ڥߏஙํ๏ 2. C⇛RustҠ২ 3. ίϯύΠϧ 4. ॻ͖ࠐΈ ※ࠓճͷιʔείʔυ͸ˣ https://github.com/grace2riku/avr-hal/tree/add_leafony_211015

    ۩ମత࡞ۀ
  10. 1. ։ൃ؀ڥߏஙํ๏ • avr-halͷखॱͷ௨Γ https://github.com/Rahix/avr-hal README.md -> Quickstart ※ίϯύΠϥ͸stableͰ͸ͳ͘ɺnightlyΛ࢖͏ͱ͜Ζ͕஫ҙ఺ɻ ۩ମత࡞ۀ

  11. 2, C⇛RustҠ২ • Ҡ২͢ΔCαϯϓϧιʔε͸͜Εɻ όοςϦిѹಡΈऔΓ https://docs.leafony.com/docs/examples/beginner/1_p/basic/read_battery/ ɹ⇛i2cͰόοςϦిѹಡΈग़͠⇛uartૹ৴⇛LEDτάϧ⇛1ඵdelayͷ܁Γฦ͠ • avr-halͷArduino UNOαϯϓϧίʔυΛϕʔεʹLeafony޲͚ʹվ଄͍ͯ͘͠ɻ

    ɹexamples/arduino-uno/src/bin σΟϨΫτϦҎԼʹαϯϓϧίʔυ͕͋Δɻ • ΫϩοΫઃఆมߋɹ˞࣮૷ʹ՝୊͋Γɻ಺༰͸ޙड़ɻ ɹArduino UNO͸16MHz, Leafony͸8MHzɻ ɹarduino-hal/src/clock.rsͷpub type DefaultClock = avr_hal_generic::clock::MHz16;Λpub type DefaultClock = avr_hal_generic::clock::MHz8;ʹมߋɻ ۩ମత࡞ۀ
  12. 3, ίϯύΠϧ avr-halͷखॱͷ௨Γ https://github.com/Rahix/avr-hal README.md -> Quickstart cd examples/arduino-uno #

    Build and run it on a connected board cargo run --bin uno-blink ɹ⇛͜ͷ1ίϚϯυͰίϯύΠϧɺॻ͖ࠐΈ·Ͱ࣮ߦͯ͘͠ΕΔɻʲuno-blinkʳʹ͸ίϯύΠϧ͍ͨ͠ ιʔείʔυΛࢦఆ͢Δɻ ۩ମత࡞ۀ
  13. 3, ίϯύΠϧ Leafonyͷ৔߹ɺʲcargo run --bin uno-blinkʳͰॻ͖ࠐΈ͕ࣦഊ͢ ΔɻίϯύΠϧɾॻ͖ࠐΈΛͦΕͧΕͷίϚϯυͰ࣮ࢪͨ͠ɻ ʲίϯύΠϧʳ cargo build

    --bin battery_voltage ※battery_voltage͕ࠓճ௥Ճͨ͠ιʔείʔυͷ໊শ ۩ମత࡞ۀ
  14. 4, ॻ͖ࠐΈ ॻ͖ࠐΈ༻ͷπʔϧʲavrdudeʳΛࣄલʹΠϯετʔϧ͠ɺelfͱಉ ͡֊૚Ͱ࣍ͷίϚϯυ࣮ߦɻɹ avrdude -patmega328p -carduino -P/dev/cu.usbserial-AH07FVYA -b 57600

    -D -U flash:w:battery_voltage.elf:e ۩ମత࡞ۀ
  15. • ίϯύΠϧɺॻ͖ࠐΈ͕ऴΘͬͨΒλʔϛφϧͰ࣍ͷίϚϯυͰ઀ଓ͢Δɻࠓճ͸λʔϛφϧʹminicomΛ࢖༻ɻ minicom -D /dev/cu.usbserial-AH07FVYA -b 57600 • ઀ଓͰ͖Δͱ࣍ͷϝοηʔδ͕λʔϛφϧʹදࣔ͞ΕΔɻ [0]=0,

    [1]=0, [2]=255, [3]=255 Batt[V] = 0 [0]=0, [1]=0, [2]=255, [3]=255 Batt[V] = 0 [0]=0, [1]=0, [2]=255, [3]=255 Batt[V] = 0 [0]=7, [1]=64, [2]=255, [3]=255 // ӈਤͷϦʔϑ(ج൘)ͷεΠονΛಈ͔͢ͱίΠϯి஑ͷిѹΛऔಘͰ͖Δɻ Batt[V] = 2990 // ϦʔϑͷεΠονΛมԽͤ͞ΔͱόοςϦిѹ͕ಡΈग़͍ͤͯΔɻ [0]=7, [1]=80, [2]=255, [3]=255 Batt[V] = 3016 [0]=7, [1]=64, [2]=255, [3]=255 ݁Ռ
  16. • Leafony޲͚ʹΫϩοΫͷมߋ(16MHz⇛8MHz)Λߦͬͨɻ ɹݱঢ়ͷରԠͩͱLeafonyҎ֎ͷArduinoͷΫϩοΫઃఆ͕1/2ʹ ͳͬͯ͠·͏ͨΊظ଴௨Γʹಈ͔ͳ͍ɻLeafonyͷϏϧυߏ੒Λ௥ Ճ͢Ε͹ଞͷArduinoͱڞଘͰ͖Δɻ • cargo run --bin uno-blinkͷ1ίϚϯυͰॻ͖ࠐΈ·Ͱ΍Γ͍ͨɻ

    • warning͕Ͱ͍ͯΔ͕ແࢹ͍ͯ͠ΔͷͰରԠɻ ՝୊
  17. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ ऴΘΓ