Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
hello-world-on-RTOS
Search
tomoyuki-nakabayashi
March 25, 2019
Programming
920
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
hello-world-on-RTOS
tomoyuki-nakabayashi
March 25, 2019
More Decks by tomoyuki-nakabayashi
See All by tomoyuki-nakabayashi
Nature Remo SDKアップデートの軌跡
tomoyuki
1
1.3k
How to learn Embedded Rust Edition 2021
tomoyuki
0
1.6k
Embedded-Rust
tomoyuki
1
3.2k
Baremetal Rust for RISC-V
tomoyuki
5
1.2k
Other Decks in Programming
See All in Programming
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
470
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.8k
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
440
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
170
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.5k
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
960
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
250
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
140
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
1
270
yield再入門 #phpcon
o0h
PRO
0
930
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
330
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
360
Featured
See All Featured
How to build a perfect <img>
jonoalderson
1
5.8k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
480
Facilitating Awesome Meetings
lara
57
7.1k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
510
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
Information Architects: The Missing Link in Design Systems
soysaucechin
0
1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Color Theory Basics | Prateek | Gurzu
gurzu
0
410
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.9k
Transcript
3/25/2019 reveal.js http://localhost:3000/?print-pdf 1/18 Hello World Hello World だけ だけ
だけ だけ で来ました で来ました で来ました で来ました Edition Edition RTOS RTOS 中林智之(connectFree 株式会社) 2019/3/25 Rust LT #3 @LDScell
3/25/2019 reveal.js http://localhost:3000/?print-pdf 2/18 今回の 今回の 今回の 今回の Hello World
Hello World RTOS (Real Time Operating System) 上でRust から Hello World する ターゲット環境: Linux Foundation 傘下のプロジェクト device tree やKconfig など、Linux から⼊りやすい ROM: 10KB, RAM: 4KB から動作する C ⾔語 Zephyr
3/25/2019 reveal.js http://localhost:3000/?print-pdf 3/18 Rust Rust から⾒た から⾒たRTOS から⾒た から⾒たRTOS
no_std だけど、若⼲ホストされていて だけど、若⼲ホストされていて中途半端 POSIX 互換のRTOS もある POSIX subset のRTOS もある(Zephyr も) libc が提供されることもある (多くの場合)configuration でAPI が増減する
3/25/2019 reveal.js http://localhost:3000/?print-pdf 4/18 Rust Rust のホストとして⾒る のホストとして⾒る のホストとして⾒る のホストとして⾒る
Zephyr Zephyr なんとかなりそうな感じを醸し出してくる minimal libc malloc/free, etc printf/puts/fwrite, etc strcmp/memcopy, etc posix open/close/sleep/sem, etc socket/pthread/signal, etc
3/25/2019 reveal.js http://localhost:3000/?print-pdf 5/18 早速 早速 早速 早速 bindings bindings
を作って を作って を作って を作って printf printf #![no_std] use bindings as zephyr; use cty; #[no_mangle] pub extern "C" fn rust_main() { unsafe { zephyr::printf( b"Hello %s\0".as_ptr() as *const cty::c_char, b"Rust\0".as_ptr()); } }
3/25/2019 reveal.js http://localhost:3000/?print-pdf 6/18 RTOS 上で 上で RTOS 上で 上で
Rust Rust から から から から Hello World Hello World でき でき でき でき た! た! た! た! 完 完 完 完
3/25/2019 reveal.js http://localhost:3000/?print-pdf 7/18 という訳にはいかない! という訳にはいかない! という訳にはいかない! という訳にはいかない! ダサすぎ!安全性皆無! unsafe
{ zephyr::printf( b"Hello %s\0".as_ptr() as *const cty::c_char, b"Rust\0".as_ptr()); }
3/25/2019 reveal.js http://localhost:3000/?print-pdf 8/18 print! print!/ /println! println! マクロを実 マクロを実
マクロを実 マクロを実 装する 装する 装する 装する println! は、(基本)print! に \n をくっつけるだけ。 マクロへの引数を全て渡して format_args! を 呼び出す format_args の結果を引数として、 _print を呼び 出す #[macro_export] macro_rules! print { ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))); }
3/25/2019 reveal.js http://localhost:3000/?print-pdf 9/18 format_args! format_args! コンパイラ組込みの⼿続きマクロ。 フォーマット⽂字列と引数群から、良い感じ fmt::Arguments を構築(するコードを⽣成)
良い感じに、で納得できない⽅は、下のブログが ⾮常に詳しいです。 ubnt-intrepid's blog:Rust の⽂字列フォーマット回り (改訂版)
3/25/2019 reveal.js http://localhost:3000/?print-pdf 10/18 _print _print fmt::Write トレイトを実装したオブジェクトの write_fmt に
fmt::Arguments を渡して呼び出 す。 pub fn _print(args: fmt::Arguments) { use core::fmt::Write; let mut writer = DebugWriter {}; writer.write_fmt(args).unwrap(); // ★ }
3/25/2019 reveal.js http://localhost:3000/?print-pdf 11/18 write_fmt write_fmt fmt::Write トレイトのメソッド。 デフォルト実装が あり、fmt::Arguments
を分解し、str に変換して、 write_str を呼び出す Hello {} で1 回、 From {} で1 回、それぞれ write_str が呼ばれる print!("Hello {} From {}", "World", "Rust");
3/25/2019 reveal.js http://localhost:3000/?print-pdf 12/18 write_str write_str pub struct DebugWriter {}
impl fmt::Write for DebugWriter { fn write_str(&mut self, s: &str) -> fmt::Result { // Zephyr のAPI を使って`s` を表⽰する Ok(()) } }
3/25/2019 reveal.js http://localhost:3000/?print-pdf 13/18 ボツ実装 ボツ実装1 (擬似コード) (擬似コード) ボツ実装 ボツ実装1
(擬似コード) (擬似コード) C ⾔語の⽂字列はナル⽂字で終端されてないとダ メ! バッファオーバーランして死亡! fn write_str(&mut self, s: &str) -> fmt::Result { unsafe { printf("%s\0", s.as_ptr()); } Ok(()) }
3/25/2019 reveal.js http://localhost:3000/?print-pdf 14/18 ボツ実装 ボツ実装2 (擬似コード) (擬似コード) ボツ実装 ボツ実装2
(擬似コード) (擬似コード) 64MHz のプロセッサで無駄なことやるなし! 組込 み屋さんとしての敗北! fn write_str(&mut self, s: &str) -> fmt::Result { let iter = s.bytes().chain("\0".bytes()); for (i, byte) in iter.enumerate() { buf[i] = byte; // 別途⽤意したバッファにコピー } unsafe { printf(b"%s\0".as_ptr() as *const cty::c_char, &b Ok(()) }
3/25/2019 reveal.js http://localhost:3000/?print-pdf 15/18 printf printf では埒があかぬ! では埒があかぬ! では埒があかぬ! では埒があかぬ!
所詮printf は、stdout にフォーマットした⽂字列を 出⼒するだけ Rust がフォーマットしてくれているのだから、 stdout にバイナリデータをぶち込めれば良い fwrite() を使うことに
3/25/2019 reveal.js http://localhost:3000/?print-pdf 16/18 完成品 完成品 完成品 完成品 println! マクロがZephyr
上で使える! fn write_str(&mut self, s: &str) -> fmt::Result { unsafe { zephyr::fwrite(s.as_ptr() as *const cty::c_void, s.len(), 1, stdout_as_ptr_mut()); } Ok(()) } println!("Hello from {}", "Rust");
3/25/2019 reveal.js http://localhost:3000/?print-pdf 17/18 まとめ まとめ まとめ まとめ RTOS(Zephyr) 上でRust
のprintln! マクロを実装し て、Hello World した! Hello World だけでも意外とおもしろい発表ネ タ! Hello World だけでも堂々と発表しよう!
3/25/2019 reveal.js http://localhost:3000/?print-pdf 18/18 宣伝〜和訳やってます〜 宣伝〜和訳やってます〜 宣伝〜和訳やってます〜 宣伝〜和訳やってます〜 今朝⼀通り和訳完了! print!
マクロの作り⽅解説もあるよ! The Embedded Rust Book Discovery