-
Notifications
You must be signed in to change notification settings - Fork 218
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
Add Serial print and println functionality #115
Comments
I ended up on this page from a google search. I was trying to use the code here with
The first thing I noticed is that |
I eventually figured out a way to do it: https://stackoverflow.com/questions/70883797/what-is-the-rust-equivalent-of-serial-println-from-the-arduino-c-api/70883798#70883798 |
@mutantbob, what you've linked is code to use a UART (for this, also check examples/arduino-uno/src/bin/uno-usart.rs). This is enough for simple scenarios where you just want to print from your main function. What this issue was originally about is having a standalone I've found a piece of code which does this for newer versions of avr-hal but I have not verified that it actually works... Here it is: #![no_std]
#![no_main]
use arduino_hal::prelude::*;
use panic_halt as _;
pub mod serial {
use avr_device::interrupt::Mutex;
use core::cell::RefCell;
pub type Usart = arduino_hal::hal::usart::Usart0<arduino_hal::DefaultClock>;
pub static GLOBAL_SERIAL: Mutex<RefCell<Option<Usart>>> = Mutex::new(RefCell::new(None));
pub fn init(serial: Usart) {
avr_device::interrupt::free(|cs| {
GLOBAL_SERIAL.borrow(&cs).replace(Some(serial));
})
}
#[macro_export]
macro_rules! serial_println {
($($arg:tt)*) => {
::avr_device::interrupt::free(|cs| {
if let Some(serial) = &mut *crate::serial::GLOBAL_SERIAL.borrow(&cs).borrow_mut() {
::ufmt::uwriteln!(serial, $($arg)*)
} else {
Ok(())
}
})
}
}
}
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let serial = arduino_hal::default_serial!(dp, pins, 57600);
serial::init(serial);
serial_println!("Hello from Arduino!\r").void_unwrap();
loop {
}
} |
In the intervening month I have complicated your example into the following:
It is a little more complicated, because I use |
@mutantbob, the code you shared has a race-condition. You can't have both "global The problem is that your println!() implementation is now not re-entrancy safe. Consider what happens when the function is currently sending out data when an interrupt hits which again tries to If you cannot afford the long critical section, I would advise to instead pass the UART handle to each function that wants to send information instead of creating such a "global" |
Serial print and println functionality similar to that implemented by Arduino.
I understand this is already in the works so I'm just placing this here to track progress.
In the mean time for any passers by trying to get this functionality to work (example tailored to Arduino Uno for simplicity):
The text was updated successfully, but these errors were encountered: