|
| 1 | +/**************************************************************************** |
| 2 | + * apps/examples/leds_rust/leds_rust_main.rs |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Attributes |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +/* Comment out these lines for testing with Rust Standard Library */ |
| 26 | + |
| 27 | +#![no_main] |
| 28 | +#![no_std] |
| 29 | + |
| 30 | +/**************************************************************************** |
| 31 | + * Uses |
| 32 | + ****************************************************************************/ |
| 33 | + |
| 34 | +#[cfg(target_os = "none")] |
| 35 | +use core::{ |
| 36 | + panic::PanicInfo, |
| 37 | + result::Result::{self, Err, Ok}, |
| 38 | +}; |
| 39 | + |
| 40 | +/**************************************************************************** |
| 41 | + * Modules |
| 42 | + ****************************************************************************/ |
| 43 | + |
| 44 | +mod nuttx; |
| 45 | + |
| 46 | +/**************************************************************************** |
| 47 | + * Private Functions |
| 48 | + ****************************************************************************/ |
| 49 | + |
| 50 | +/**************************************************************************** |
| 51 | + * Panic Handler (needed for [no_std] compilation) |
| 52 | + ****************************************************************************/ |
| 53 | + |
| 54 | +#[cfg(target_os = "none")] /* For NuttX */ |
| 55 | +#[panic_handler] |
| 56 | +fn panic(_panic: &PanicInfo<'_>) -> ! { |
| 57 | + loop {} |
| 58 | +} |
| 59 | + |
| 60 | +/**************************************************************************** |
| 61 | + * rust_main |
| 62 | + ****************************************************************************/ |
| 63 | + |
| 64 | +fn rust_main(_argc: i32, _argv: *const *const u8) -> Result<i32, i32> { |
| 65 | + /* "Hello, Rust!!" using puts() from libc */ |
| 66 | + |
| 67 | + nuttx::safe_puts("Hello, Rust!!"); |
| 68 | + |
| 69 | + /* Blink LED 1 using ioctl() from NuttX */ |
| 70 | + |
| 71 | + nuttx::safe_puts("Opening /dev/userleds"); |
| 72 | + let fd = nuttx::safe_open("/dev/userleds", nuttx::O_WRONLY)?; |
| 73 | + nuttx::safe_puts("Set LED 1 to 1"); |
| 74 | + |
| 75 | + nuttx::safe_ioctl(fd, nuttx::ULEDIOC_SETALL, 1)?; |
| 76 | + nuttx::safe_puts("Sleeping..."); |
| 77 | + unsafe { |
| 78 | + nuttx::usleep(500_000); |
| 79 | + } |
| 80 | + |
| 81 | + nuttx::safe_puts("Set LED 1 to 0"); |
| 82 | + nuttx::safe_ioctl(fd, nuttx::ULEDIOC_SETALL, 0)?; |
| 83 | + unsafe { |
| 84 | + nuttx::close(fd); |
| 85 | + } |
| 86 | + |
| 87 | + /* Exit with status 0 */ |
| 88 | + |
| 89 | + Ok(0) |
| 90 | +} |
| 91 | + |
| 92 | +/**************************************************************************** |
| 93 | + * Public Functions |
| 94 | + ****************************************************************************/ |
| 95 | + |
| 96 | +/**************************************************************************** |
| 97 | + * leds_rust_main |
| 98 | + ****************************************************************************/ |
| 99 | + |
| 100 | +#[no_mangle] |
| 101 | +pub extern "C" fn leds_rust_main(_argc: i32, _argv: *const *const u8) -> i32 { |
| 102 | + /* Call the program logic in Rust Main */ |
| 103 | + |
| 104 | + let res = rust_main(0, core::ptr::null()); |
| 105 | + |
| 106 | + /* If Rust Main returns an error, print it */ |
| 107 | + |
| 108 | + if let Err(e) = res { |
| 109 | + unsafe { |
| 110 | + nuttx::printf( |
| 111 | + b"ERROR: rust_main() failed with error %d\n\0" as *const u8, |
| 112 | + e, |
| 113 | + ); |
| 114 | + } |
| 115 | + e |
| 116 | + } else { |
| 117 | + 0 |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/**************************************************************************** |
| 122 | + * main |
| 123 | + ****************************************************************************/ |
| 124 | + |
| 125 | +#[cfg(not(target_os = "none"))] /* For Testing Locally */ |
| 126 | +fn main() { |
| 127 | + leds_rust_main(0, core::ptr::null()); |
| 128 | +} |
0 commit comments