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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
関数型プログラミングのメリットって何だろう?
wanko_it
0
160
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
170
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
580
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
2.3k
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.1k
Creating Composable Callables in Contemporary C++
rollbear
0
200
act1-costs.pdf
sumedhbala
0
210
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
260
OSINT for SRE: 学術論文とポストモーテムから探る システム障害の共通パターン / SRE NEXT 2026
tomoyk
1
3.5k
スマートグラスで並列バイブコーディング
hyshu
0
290
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
210
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
4 Signs Your Business is Dying
shpigford
187
22k
Balancing Empowerment & Direction
lara
6
1.2k
WCS-LA-2024
lcolladotor
0
680
How to Talk to Developers About Accessibility
jct
2
340
The Language of Interfaces
destraynor
162
27k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
550
30 Presentation Tips
portentint
PRO
1
350
Amusing Abliteration
ianozsvald
1
220
Visualization
eitanlees
152
17k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
460
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