Skip to content

Static access to Serial interface on arduino nano #321

Answered by Rahix
Rechenmaschine asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

you can store your Usart in a global static and then define a macro-wrapper for accessing it. Roughly like this (untested):

#![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)*) => {

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Rahix
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #214 on September 02, 2022 10:47.