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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
吝嗇家のためのAI活用 / AI development for miser - ChatGPT + Issue Driven Development
tooppoo
0
180
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
130
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.7k
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
260
継続モナドとリアクティブプログラミング
yukikurage
3
450
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
480
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
840
はてなアカウント基盤 State of the Union
cockscomb
1
1.3k
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
290
OS アップデート対応の取り組み方がもっと共有されてほしい
andpad
0
110
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
720
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
9
880
From π to Pie charts
rasagy
0
230
Making the Leap to Tech Lead
cromwellryan
135
10k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
870
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
460
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
420
Producing Creativity
orderedlist
PRO
348
40k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
190
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