From ee0743421b2d99c83dc5100077974e5c020bbc86 Mon Sep 17 00:00:00 2001 From: Lup Yuen Lee Date: Sun, 7 Jul 2024 18:27:47 +0800 Subject: [PATCH 1/6] Copy hello_rust from https://github.com/rushabhvg/nuttx-apps/blob/5d07c637c02c89098e9b35d659e5d94b8e859546/examples/hello_rust/hello_rust_main.rs --- examples/hello_rust/hello_rust_main.rs | 150 ++++++++++++++++--------- 1 file changed, 99 insertions(+), 51 deletions(-) diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs index 3a276e94dd6..184696012b6 100644 --- a/examples/hello_rust/hello_rust_main.rs +++ b/examples/hello_rust/hello_rust_main.rs @@ -21,54 +21,102 @@ /**************************************************************************** * Attributes ****************************************************************************/ - -#![no_main] -#![no_std] - -/**************************************************************************** - * Uses - ****************************************************************************/ - -use core::panic::PanicInfo; - -/**************************************************************************** - * Externs - ****************************************************************************/ - -extern "C" { - pub fn printf(format: *const u8, ...) -> i32; -} - -/**************************************************************************** - * Private functions - ****************************************************************************/ - -/**************************************************************************** - * Panic handler (needed for [no_std] compilation) - ****************************************************************************/ - -#[panic_handler] -fn panic(_panic: &PanicInfo<'_>) -> ! { - loop {} -} - -/**************************************************************************** - * Public functions - ****************************************************************************/ - -/**************************************************************************** - * hello_rust_main - ****************************************************************************/ - -#[no_mangle] -pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { - /* "Hello, Rust!!" using printf() from libc */ - - unsafe { - printf(b"Hello, Rust!!\n\0" as *const u8); - } - - /* exit with status 0 */ - - 0 -} + + #![no_main] + #![no_std] + + /**************************************************************************** + * Uses + ****************************************************************************/ + + use core::panic::PanicInfo; + use core::ffi::{ c_void }; + + /**************************************************************************** + * Externs + ****************************************************************************/ + + extern "C" { + pub fn printf(format: *const u8, ...) -> i32; + pub fn puts(s: *const u8) -> i32; + pub fn fgets(buf: *mut u8, n: i32, stream: *mut c_void) -> *mut u8; + pub fn lib_get_stream(fd: i32) -> *mut c_void; + + pub fn open(path: *const u8, oflag: i32, ...) -> i32; + pub fn read(fd: i32, buf: *mut u8, count: u32) -> i32; + pub fn write(fd: i32, buf: *const u8, count: u32) -> i32; + pub fn close(fd: i32) -> i32; + pub fn ioctl(fd: i32, request: i32, ...) -> i32; + pub fn sleep(secs: u32) -> u32; + pub fn usleep(usec: u32) -> u32; + pub fn exit(status: u32) -> !; + } + + /**************************************************************************** + * Rust Constants + ****************************************************************************/ + pub const O_WRONLY: i32 = 1 << 1; + pub const ULEDIOC_SETALL: i32 = 0x1d03; + + /**************************************************************************** + * Private functions + ****************************************************************************/ + + /**************************************************************************** + * Panic handler (needed for [no_std] compilation) + ****************************************************************************/ + + #[panic_handler] + fn panic(_panic: &PanicInfo<'_>) -> ! { + loop {} + } + + /**************************************************************************** + * Public functions + ****************************************************************************/ + + /**************************************************************************** + * hello_rust_main + ****************************************************************************/ + + #[no_mangle] + pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 { + + unsafe { + printf(b"Hello, Rust!!\n\0" as *const u8); + + printf(b"Opening /dev/userleds\n\0" as *const u8); + + let fd = open(b"/dev/userleds\0" as *const u8, O_WRONLY); + + if fd < 0 { + printf(b"Unable to open /dev/userleds, skipping the blinking\n\0" as *const u8); + return 1; + } + + printf(b"Set LED 1 to 1\n\0" as *const u8); + let ret = ioctl(fd, ULEDIOC_SETALL, 1); // Convert ULEDIOC_SETALL (u16) to i32 + + if ret < 0 { + printf(b"ERROR: ioctl(ULEDIOC_SUPPORTED) failed\n\0" as *const u8); + return 1; + } + + // Sleep a while... + printf(b"Waiting...\n\0" as *const u8); + usleep(500 * 1000); + + printf(b"Set LED 1 to 0\n\0" as *const u8); + let ret = ioctl(fd, ULEDIOC_SETALL, 0); + + if ret < 0 { + printf(b"ERROR: ioctl(ULEDIOC_SUPPORTED) failed\n\0" as *const u8); + return 1; + } + close(fd); + } + + /* exit with status 0 */ + 0 + } + \ No newline at end of file From 67efb7010d461a42a3fd8c98f4f49030291d4500 Mon Sep 17 00:00:00 2001 From: Lup Yuen Lee Date: Sun, 7 Jul 2024 18:38:49 +0800 Subject: [PATCH 2/6] Clean up --- examples/hello_rust/hello_rust_main.rs | 33 ++++++++++---------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs index 184696012b6..2ebadc9f1da 100644 --- a/examples/hello_rust/hello_rust_main.rs +++ b/examples/hello_rust/hello_rust_main.rs @@ -30,7 +30,6 @@ ****************************************************************************/ use core::panic::PanicInfo; - use core::ffi::{ c_void }; /**************************************************************************** * Externs @@ -38,16 +37,9 @@ extern "C" { pub fn printf(format: *const u8, ...) -> i32; - pub fn puts(s: *const u8) -> i32; - pub fn fgets(buf: *mut u8, n: i32, stream: *mut c_void) -> *mut u8; - pub fn lib_get_stream(fd: i32) -> *mut c_void; - pub fn open(path: *const u8, oflag: i32, ...) -> i32; - pub fn read(fd: i32, buf: *mut u8, count: u32) -> i32; - pub fn write(fd: i32, buf: *const u8, count: u32) -> i32; pub fn close(fd: i32) -> i32; pub fn ioctl(fd: i32, request: i32, ...) -> i32; - pub fn sleep(secs: u32) -> u32; pub fn usleep(usec: u32) -> u32; pub fn exit(status: u32) -> !; } @@ -80,37 +72,36 @@ ****************************************************************************/ #[no_mangle] - pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 { + pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { unsafe { + /* "Hello, Rust!!" using printf() from libc */ + printf(b"Hello, Rust!!\n\0" as *const u8); - printf(b"Opening /dev/userleds\n\0" as *const u8); - - let fd = open(b"/dev/userleds\0" as *const u8, O_WRONLY); - + /* Blink LED 1 using ioctl() from NuttX */ + + printf(b"Opening /dev/userleds\n\0" as *const u8); + let fd = open(b"/dev/userleds\0" as *const u8, O_WRONLY); if fd < 0 { printf(b"Unable to open /dev/userleds, skipping the blinking\n\0" as *const u8); return 1; } printf(b"Set LED 1 to 1\n\0" as *const u8); - let ret = ioctl(fd, ULEDIOC_SETALL, 1); // Convert ULEDIOC_SETALL (u16) to i32 - + let ret = ioctl(fd, ULEDIOC_SETALL, 1); if ret < 0 { - printf(b"ERROR: ioctl(ULEDIOC_SUPPORTED) failed\n\0" as *const u8); + printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); return 1; } - // Sleep a while... - printf(b"Waiting...\n\0" as *const u8); + printf(b"Sleeping...\n\0" as *const u8); usleep(500 * 1000); printf(b"Set LED 1 to 0\n\0" as *const u8); - let ret = ioctl(fd, ULEDIOC_SETALL, 0); - + let ret = ioctl(fd, ULEDIOC_SETALL, 0); if ret < 0 { - printf(b"ERROR: ioctl(ULEDIOC_SUPPORTED) failed\n\0" as *const u8); + printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); return 1; } close(fd); From f592554544e49e3bcd6193aaf4edea56f201c3e2 Mon Sep 17 00:00:00 2001 From: Lup Yuen Lee Date: Sun, 7 Jul 2024 19:32:10 +0800 Subject: [PATCH 3/6] Clean up --- examples/hello_rust/hello_rust_main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs index 2ebadc9f1da..8d3bad08ec9 100644 --- a/examples/hello_rust/hello_rust_main.rs +++ b/examples/hello_rust/hello_rust_main.rs @@ -41,7 +41,6 @@ pub fn close(fd: i32) -> i32; pub fn ioctl(fd: i32, request: i32, ...) -> i32; pub fn usleep(usec: u32) -> u32; - pub fn exit(status: u32) -> !; } /**************************************************************************** @@ -72,7 +71,8 @@ ****************************************************************************/ #[no_mangle] - pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { +pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { + /* "Hello, Rust!!" using printf() from libc */ unsafe { /* "Hello, Rust!!" using printf() from libc */ @@ -92,22 +92,24 @@ let ret = ioctl(fd, ULEDIOC_SETALL, 1); if ret < 0 { printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); + close(fd); return 1; } printf(b"Sleeping...\n\0" as *const u8); - usleep(500 * 1000); + usleep(500_000); printf(b"Set LED 1 to 0\n\0" as *const u8); let ret = ioctl(fd, ULEDIOC_SETALL, 0); if ret < 0 { printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); + close(fd); return 1; } close(fd); } - /* exit with status 0 */ + /* Exit with status 0 */ + 0 } - \ No newline at end of file From ce4104023b3c3bc9955ca866ef6f4360a76bd250 Mon Sep 17 00:00:00 2001 From: Lup Yuen Lee Date: Sun, 7 Jul 2024 20:02:51 +0800 Subject: [PATCH 4/6] Clean up --- examples/hello_rust/hello_rust_main.rs | 180 +++++++++++++------------ 1 file changed, 91 insertions(+), 89 deletions(-) diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs index 8d3bad08ec9..c6eb620b575 100644 --- a/examples/hello_rust/hello_rust_main.rs +++ b/examples/hello_rust/hello_rust_main.rs @@ -21,95 +21,97 @@ /**************************************************************************** * Attributes ****************************************************************************/ - + #![no_main] #![no_std] - - /**************************************************************************** - * Uses - ****************************************************************************/ - - use core::panic::PanicInfo; - - /**************************************************************************** - * Externs - ****************************************************************************/ - - extern "C" { - pub fn printf(format: *const u8, ...) -> i32; - pub fn open(path: *const u8, oflag: i32, ...) -> i32; - pub fn close(fd: i32) -> i32; - pub fn ioctl(fd: i32, request: i32, ...) -> i32; - pub fn usleep(usec: u32) -> u32; - } - - /**************************************************************************** - * Rust Constants - ****************************************************************************/ - pub const O_WRONLY: i32 = 1 << 1; - pub const ULEDIOC_SETALL: i32 = 0x1d03; - - /**************************************************************************** - * Private functions - ****************************************************************************/ - - /**************************************************************************** - * Panic handler (needed for [no_std] compilation) - ****************************************************************************/ - - #[panic_handler] - fn panic(_panic: &PanicInfo<'_>) -> ! { - loop {} - } - - /**************************************************************************** - * Public functions - ****************************************************************************/ - - /**************************************************************************** - * hello_rust_main - ****************************************************************************/ - - #[no_mangle] + +/**************************************************************************** + * Uses + ****************************************************************************/ + +use core::panic::PanicInfo; + +/**************************************************************************** + * Externs + ****************************************************************************/ + +extern "C" { + pub fn printf(format: *const u8, ...) -> i32; + pub fn open(path: *const u8, oflag: i32, ...) -> i32; + pub fn close(fd: i32) -> i32; + pub fn ioctl(fd: i32, request: i32, ...) -> i32; + pub fn usleep(usec: u32) -> u32; +} + +/**************************************************************************** + * Constants + ****************************************************************************/ + +pub const O_WRONLY: i32 = 1 << 1; +pub const ULEDIOC_SETALL: i32 = 0x1d03; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Panic Handler (needed for [no_std] compilation) + ****************************************************************************/ + +#[panic_handler] +fn panic(_panic: &PanicInfo<'_>) -> ! { + loop {} +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * hello_rust_main + ****************************************************************************/ + +#[no_mangle] pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { - /* "Hello, Rust!!" using printf() from libc */ - - unsafe { - /* "Hello, Rust!!" using printf() from libc */ - - printf(b"Hello, Rust!!\n\0" as *const u8); - - /* Blink LED 1 using ioctl() from NuttX */ - - printf(b"Opening /dev/userleds\n\0" as *const u8); - let fd = open(b"/dev/userleds\0" as *const u8, O_WRONLY); - if fd < 0 { - printf(b"Unable to open /dev/userleds, skipping the blinking\n\0" as *const u8); - return 1; - } - - printf(b"Set LED 1 to 1\n\0" as *const u8); - let ret = ioctl(fd, ULEDIOC_SETALL, 1); - if ret < 0 { - printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); - close(fd); - return 1; - } - - printf(b"Sleeping...\n\0" as *const u8); - usleep(500_000); - - printf(b"Set LED 1 to 0\n\0" as *const u8); - let ret = ioctl(fd, ULEDIOC_SETALL, 0); - if ret < 0 { - printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); - close(fd); - return 1; - } - close(fd); - } - - /* Exit with status 0 */ - - 0 - } + + unsafe { + + /* "Hello, Rust!!" using printf() from libc */ + + printf(b"Hello, Rust!!\n\0" as *const u8); + + /* Blink LED 1 using ioctl() from NuttX */ + + printf(b"Opening /dev/userleds\n\0" as *const u8); + let fd = open(b"/dev/userleds\0" as *const u8, O_WRONLY); + if fd < 0 { + printf(b"Unable to open /dev/userleds, skipping the blinking\n\0" as *const u8); + return 1; + } + + printf(b"Set LED 1 to 1\n\0" as *const u8); + let ret = ioctl(fd, ULEDIOC_SETALL, 1); + if ret < 0 { + printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); + close(fd); + return 1; + } + + printf(b"Sleeping...\n\0" as *const u8); + usleep(500_000); + + printf(b"Set LED 1 to 0\n\0" as *const u8); + let ret = ioctl(fd, ULEDIOC_SETALL, 0); + if ret < 0 { + printf(b"ERROR: ioctl(ULEDIOC_SETALL) failed\n\0" as *const u8); + close(fd); + return 1; + } + + close(fd); + } + + /* Exit with status 0 */ + + 0 +} From df4660a6b802f7f247a4d642b77984f7d799d62c Mon Sep 17 00:00:00 2001 From: Lup Yuen Lee Date: Mon, 8 Jul 2024 16:43:19 +0800 Subject: [PATCH 5/6] Update from https://github.com/apache/nuttx-apps/blob/17de01ddbb37445b2d4b3f0394bc2a91b7a57b88/examples/hello_rust/hello_rust_main.rs --- examples/hello_rust/hello_rust_main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs index c6eb620b575..3f0213b1247 100644 --- a/examples/hello_rust/hello_rust_main.rs +++ b/examples/hello_rust/hello_rust_main.rs @@ -114,4 +114,4 @@ pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { /* Exit with status 0 */ 0 -} +} \ No newline at end of file From c243d8104479d3593d79b900c10cbbce043eda77 Mon Sep 17 00:00:00 2001 From: Lup Yuen Lee Date: Mon, 8 Jul 2024 16:47:38 +0800 Subject: [PATCH 6/6] Check OK: rustfmt --edition 2021 --check /Users/luppy/ox64/apps/examples/hello_rust/hello_rust_main.rs --- examples/hello_rust/hello_rust_main.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs index 3f0213b1247..b23c8696f85 100644 --- a/examples/hello_rust/hello_rust_main.rs +++ b/examples/hello_rust/hello_rust_main.rs @@ -22,8 +22,8 @@ * Attributes ****************************************************************************/ - #![no_main] - #![no_std] +#![no_main] +#![no_std] /**************************************************************************** * Uses @@ -73,9 +73,7 @@ fn panic(_panic: &PanicInfo<'_>) -> ! { #[no_mangle] pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { - unsafe { - /* "Hello, Rust!!" using printf() from libc */ printf(b"Hello, Rust!!\n\0" as *const u8); @@ -114,4 +112,4 @@ pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { /* Exit with status 0 */ 0 -} \ No newline at end of file +}