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
Vite+ Unified Toolchain for the Web
naokihaba
0
740
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
610
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
230
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
220
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
170
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
1
160
スマートグラスで並列バイブコーディング
hyshu
0
290
霧の中の代数的エフェクト
funnyycat
1
340
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
200
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
160
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
190
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
360
Featured
See All Featured
New Earth Scene 8
popppiees
3
2.4k
Building Adaptive Systems
keathley
44
3.1k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.3k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
420
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Scaling GitHub
holman
464
140k
Side Projects
sachag
455
43k
Navigating Weather and Climate Data
rabernat
0
310
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
330
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