Static access to Serial interface on arduino nano #321
-
Hey there, lately I've been trying to implement my own Messaging protocol for the arduino nano, however I have been having some problems realising this because I cannot find the explicit type of the I need the explicit type definition of the Does anyone have a solution or a workaround on hand for this? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi, you can store your #![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 { }
} |
Beta Was this translation helpful? Give feedback.
-
Ah, yeah so that's the pub type Usart = arduino_hal::hal::usart::Usart0<arduino_hal::DefaultClock>; line from above... I guess we should provide a better alias for this. |
Beta Was this translation helpful? Give feedback.
-
Thanks! Works as expected. |
Beta Was this translation helpful? Give feedback.
Hi,
you can store your
Usart
in a global static and then define a macro-wrapper for accessing it. Roughly like this (untested):