无法在init中使用lateinit变量



我正试图用Rust和RTIC制作一个嵌入式应用程序,它可以通过SPI进行通信并使LED闪烁,但我一直在尝试通过initinterrupt功能共享GPIO库。

init中,我必须使用GPIO组来设置SPI,但如果不使用组,我就无法设置引脚。

我的代码:


#![no_main]
#![no_std]
#[allow(unused_extern_crates)]
use panic_halt as _;
use asm_delay::AsmDelay;
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};
#[rtic::app(device = nrf52810_pac, peripherals = true)]
const APP: () = {
struct Resources {
LED: nrf52810_pac::p0::out::PIN20_W<'static>,
}

#[init]
fn init(_: init::Context) -> init::LateResources {
let p = hal::pac::Peripherals::take().unwrap();
let LED = None;
p.P0.out.write(|w| {LED = Some(*w.pin20()); w.pin20().low()});
let li = init::LateResources{
LED: LED.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(_) => {}
}
let mut d = AsmDelay::new(asm_delay::bitrate::U32BitrateExt::mhz(74));
rtic::pend(Interrupt::SWI0_EGU0);
li
}
#[task(binds = SWI0_EGU0, resources = [LED])]
fn led_switch(_: led_switch::Context) {
static mut LED_STATE: bool = false;
// flip_led(LED_STATE);
*LED_STATE = !*LED_STATE;
}
};

编辑

我已经设法将pin存储在可以在函数之间共享的静态变量中,但我不确定如何切换它的状态,因为它再次需要作为值而不是引用。

struct Resources {
LED: nrf52810_hal::gpio::p0::P0_20<Output<PushPull>>,
}

#[init]
fn init(_: init::Context) -> init::LateResources {
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 LED = port0.p0_20.into_push_pull_output(Level::High);
let li = init::LateResources{
LED,
};
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(_) => {}
}
rtic::pend(Interrupt::SWI0_EGU0);
li
}
#[task(binds = SWI0_EGU0, resources = [LED])]
fn led_switch(c: led_switch::Context) {
static mut LED_STATE: bool = false;
// flip_led(LED_STATE);
let LED: &mut nrf52810_hal::gpio::p0::P0_20<Output<PushPull>> = c.resources.LED;
if *LED_STATE {
LED.into_push_pull_output(Level::Low);
}else{
LED.into_push_pull_output(Level::High);
}
*LED_STATE = !*LED_STATE;
}

我通过调用可以引用以更改led_switch中状态的方法修复了问题中的代码。

fn led_switch(cx: led_switch::Context) {
static mut LED_STATE: bool = true;
let led = cx.resources.led;
if *LED_STATE {
led.set_low().unwrap();
}else{
led.set_high().unwrap();
}
*LED_STATE = !*LED_STATE;
}

相关内容

  • 没有找到相关文章

最新更新