Open kuon opened 3 years ago
The PSEL register contains the pin and port (on 52840), so you can use Pin::from_psel_bits
which is unsafe, but as the TWIM instance owns the pins this should be sound.
I've added a function like
pub fn free_with_pins(self) -> (T, Pins) {
// get pin and port from psel
let scl_pin = (self.0.psel.scl.read().bits() & 0b111111) as _;
let sda_pin = (self.0.psel.sda.read().bits() & 0b111111) as _;
// disconnecting the pin resets the register -> reading PSEL before disconnecting, no reset needed afterwards
self.0.psel.scl.write(|w| w.connect().disconnected());
self.0.psel.sda.write(|w| w.connect().disconnected());
let sda = unsafe { Pin::from_psel_bits(sda_pin) };
let scl = unsafe { Pin::from_psel_bits(scl_pin) };
(self.0, Pins { sda, scl })
}
for this purpose. I guess it could be added to nrf-hal, but I think the port part would need to be feature flagged, as the reset value for all the bits is 1 and not all devices have a port1
I'd like to use TWIM0 with multiple sets of pins in sequence, something like:
I realize that there is the
Twim::free()
function which can give me theTWIM0
device back but then I cannot reuse it, so the pins are "lost".I have 4 i2c devices with same addresses. I just used 8 pins on the nrf52840 thinking I would just enable them in sequence, but I realize it might have been better to use a hardware multiplexer.