Hello and my apologies for the bad summary. I didn't find a good way to summarize my problem, which is:
As a rust rookie, I'm trying to write some code for an attiny85, mainly using attiny_hal. I want to write some code for communication via RS485. For this, I need (at least) access to a timer, the serial interface and one pin that changes the direction of the communication (half duplex).
I get access to the peripherals like this:
let peripherals = attiny_hal::Peripherals::take().unwrap();
let timer0 = peripherals.TC0;
let usi = peripherals.USI;
let portb = peripherals.PORTB; // that's bad :(
let mut uart = usiuart::new(timer0, usi, portb);
uart.tx_init();
Then I initialise my struct and give it ownership of the peripherals. After all, timer0 and usi should only be handled by my communication code, but no one else:
pub struct UsiUart {
timer0: TC0, // Access to the timer 0 device
usi: USI, // Access to the Universal Serial Interface
portb: PORTB, // TODO: I'm passing the whole PORTB while I only need ddrb because I don't know how to do only the latter.
// more internal stuff
}
pub fn new(timer0: TC0, usi: USI, portb: PORTB) -> UsiUart {
UsiUart {
timer0,
usi,
portb,
// more internal stuff
}
}
So I'm currently giving the whole port B to UsiUart
which is of course complete nonsense: I only need control over a few pins (PB0, PB1 and PB3) and I need the other pins to access other hardware. Does it make sense to borrow PORTB
/ store a reference to it in UsiUart
? How else can I handle it?
There are much more problems for me to solve, but this is the most imminent. I think, for example, that UsiUart
should be a singleton itself since it doesn't make sense to have two of them (at least on the attiny85). But that's a different problem.
Thanks a lot for your help!