diff --git a/examples/hello_rust/hello_rust_main.rs b/examples/hello_rust/hello_rust_main.rs index 3a276e94dd..b23c8696f8 100644 --- a/examples/hello_rust/hello_rust_main.rs +++ b/examples/hello_rust/hello_rust_main.rs @@ -37,14 +37,25 @@ use core::panic::PanicInfo; 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; } /**************************************************************************** - * Private functions + * 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 (needed for [no_std] compilation) ****************************************************************************/ #[panic_handler] @@ -53,7 +64,7 @@ fn panic(_panic: &PanicInfo<'_>) -> ! { } /**************************************************************************** - * Public functions + * Public Functions ****************************************************************************/ /**************************************************************************** @@ -62,13 +73,43 @@ fn panic(_panic: &PanicInfo<'_>) -> ! { #[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 */ + /* Exit with status 0 */ 0 }