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
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
840
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
120
The NotImplementedError Problem in Ruby
koic
1
840
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.4k
エージェンティックRAGにAWSで入門しよう!
har1101
8
1.7k
New "Type" system on PicoRuby
pocke
1
970
Oxlintのカスタムルールの現況
syumai
6
1.1k
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.3k
Webフレームワークの ベンチマークについて
yusukebe
0
170
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
6.8k
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
290
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
360
Featured
See All Featured
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
210
A Modern Web Designer's Workflow
chriscoyier
698
190k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
230
Odyssey Design
rkendrick25
PRO
2
700
Skip the Path - Find Your Career Trail
mkilby
1
150
Paper Plane (Part 1)
katiecoart
PRO
0
9.1k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
400
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
240
YesSQL, Process and Tooling at Scale
rocio
174
15k
A Tale of Four Properties
chriscoyier
163
24k
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