Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gpio7 #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions examples/hello_rust/hello_rust_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -53,7 +64,7 @@ fn panic(_panic: &PanicInfo<'_>) -> ! {
}

/****************************************************************************
* Public functions
* Public Functions
****************************************************************************/

/****************************************************************************
Expand All @@ -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
}
Loading