我正试图在nrf52-dk板上工作,并试图闪烁一盏灯,同时让SPI工作。
我可以让其中一个同时工作,但不能同时让两个都工作。
我非常确定nrf52810_hal::pac::Peripherals::take()不应该被多次调用,因为它的数据会改变所有的引用,但当我指定pins时,它会被移动。
我不确定如何才能在不传递变量的情况下让它工作,而不会用完变量。
在下面的示例中,总是写出“外围设备为空”,并且代码会因为后面的语句而死机。
我确实需要让外设成为“静态静音”,因为我需要在另一个“线程”中使用它们,因为它是一个中断调用的函数,我无法向其传递数据。
#![no_main]
#![no_std]
#[allow(unused_extern_crates)]
use panic_halt as _;
use asm_delay::AsmDelay;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprint;
use hal::gpio::Level;
use hal::pac::interrupt;
use nrf52810_hal as hal;
use nrf52810_hal::prelude::*;
use nrf52810_pac as nrf;
use nrf52810_pac::{Interrupt, NVIC};
static mut PERIPHERALS: Option<nrf::Peripherals> = None;
#[entry]
unsafe fn main() -> ! {
let p = hal::pac::Peripherals::take().unwrap();
let port0 = hal::gpio::p0::Parts::new(p.P0);
let spiclk = port0.p0_25.into_push_pull_output(Level::Low).degrade();
let spimosi = port0.p0_24.into_push_pull_output(Level::Low).degrade();
let spimiso = port0.p0_23.into_floating_input().degrade();
let pins = hal::spim::Pins {
sck: spiclk,
miso: Some(spimiso),
mosi: Some(spimosi),
};
let spi = hal::Spim::new(
p.SPIM0,
pins,
hal::spim::Frequency::K500,
hal::spim::MODE_0,
0,
);
let reference_data = "Hello World!".as_bytes();
let mut eh_spi = embedded_hal_spy::new(spi, |_| {});
use embedded_hal::blocking::spi::Write;
match eh_spi.write(reference_data) {
Ok(_) => {}
Err(_) => {}
}
PERIPHERALS = nrf::Peripherals::take();
if PERIPHERALS.is_none() {
hprint!("PERIPHERALS are null!").unwrap();
}
NVIC::unmask(Interrupt::SWI0_EGU0);
let mut d = AsmDelay::new(asm_delay::bitrate::U32BitrateExt::mhz(74));
PERIPHERALS
.as_ref()
.unwrap()
.P0
.dir
.write(|w| w.pin20().output());
PERIPHERALS
.as_ref()
.unwrap()
.P0
.out
.write(|w| w.pin20().low());
loop {
NVIC::pend(Interrupt::SWI0_EGU0);
d.delay_ms(100u32);
}
}
#[interrupt]
fn SWI0_EGU0() {
static mut LED_STATE: bool = false;
flip_led(LED_STATE);
*LED_STATE = !*LED_STATE;
}
fn flip_led(led_state: &mut bool) {
match led_state {
true => unsafe {
PERIPHERALS
.as_ref()
.unwrap()
.P0
.out
.write(|w| w.pin20().low());
},
false => unsafe {
PERIPHERALS
.as_ref()
.unwrap()
.P0
.out
.write(|w| w.pin20().high());
},
}
}发布于 2020-07-24 14:53:05
你真的需要从你的中断上下文访问所有Peripherals吗?可能不需要,您可能只需要访问那里的特定外围设备。您可以将它们移出Peripherals结构并放入static中。然后,您将在main中将所需的外围设备作为本地变量放在那里,其他所有内容都放在static中。
但在我看来,还有一个更好的解决方案:使用RTIC。它就是为处理这种用例而设计的。它允许您准确地指定在哪个上下文中需要哪些资源,并使这些资源在那里可用。您甚至可以在不同的上下文之间安全地共享资源。根据需要,它将自动使用互斥保护它们。
我怎么推荐RTIC都不为过。对我来说,不使用它的唯一理由是,如果我的程序没有任何中断处理程序。
https://stackoverflow.com/questions/63067919
复制相似问题