init new shared periph crate
This commit is contained in:
@ -15,10 +15,11 @@ cortex-m = { version = "0.7", features = ["critical-section-single-core"] }
|
||||
cortex-m-rt = "0.7"
|
||||
embedded-hal = "1"
|
||||
nb = "1"
|
||||
bitfield = ">=0.17, <=0.19"
|
||||
bitbybit = "1.3"
|
||||
arbitrary-int = "1.3"
|
||||
max116xx-10bit = "0.3"
|
||||
|
||||
va108xx-hal = { version = ">=0.10, <=0.11", features = ["rt"] }
|
||||
va108xx-hal = { version = ">=0.10, <=0.11", path = "../va108xx-hal", features = ["rt"] }
|
||||
|
||||
[features]
|
||||
rt = ["va108xx-hal/rt"]
|
||||
|
@ -4,17 +4,17 @@ use cortex_m_rt::entry;
|
||||
use embedded_hal::delay::DelayNs;
|
||||
use panic_rtt_target as _;
|
||||
use rtt_target::{rprintln, rtt_init_print};
|
||||
use va108xx_hal::{pac, prelude::*, timer::set_up_ms_delay_provider};
|
||||
use va108xx_hal::{pac, prelude::*, timer::CountdownTimer};
|
||||
use vorago_reb1::temp_sensor::Adt75TempSensor;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
rtt_init_print!();
|
||||
rprintln!("-- Vorago Temperature Sensor and I2C Example --");
|
||||
let mut dp = pac::Peripherals::take().unwrap();
|
||||
let mut delay = set_up_ms_delay_provider(&mut dp.sysconfig, 50.MHz(), dp.tim0);
|
||||
let mut temp_sensor = Adt75TempSensor::new(&mut dp.sysconfig, 50.MHz(), dp.i2ca)
|
||||
.expect("Creating temperature sensor struct failed");
|
||||
let dp = pac::Peripherals::take().unwrap();
|
||||
let mut delay = CountdownTimer::new(dp.tim0, 50.MHz());
|
||||
let mut temp_sensor =
|
||||
Adt75TempSensor::new(50.MHz(), dp.i2ca).expect("Creating temperature sensor struct failed");
|
||||
loop {
|
||||
let temp = temp_sensor
|
||||
.read_temperature()
|
||||
|
@ -9,13 +9,14 @@ use embedded_hal::delay::DelayNs;
|
||||
use embedded_hal::spi::{SpiBus, MODE_3};
|
||||
use panic_rtt_target as _;
|
||||
use rtt_target::{rprintln, rtt_init_print};
|
||||
use va108xx_hal::spi::SpiClkConfig;
|
||||
use va108xx_hal::gpio::{Output, PinState};
|
||||
use va108xx_hal::pins::PinsA;
|
||||
use va108xx_hal::spi::{configure_pin_as_hw_cs_pin, SpiClkConfig};
|
||||
use va108xx_hal::timer::CountdownTimer;
|
||||
use va108xx_hal::{
|
||||
gpio::PinsA,
|
||||
pac,
|
||||
prelude::*,
|
||||
spi::{Spi, SpiConfig},
|
||||
timer::set_up_ms_delay_provider,
|
||||
};
|
||||
|
||||
const READ_MASK: u8 = 1 << 7;
|
||||
@ -29,19 +30,15 @@ const PWR_MEASUREMENT_MODE_MASK: u8 = 1 << 3;
|
||||
fn main() -> ! {
|
||||
rtt_init_print!();
|
||||
rprintln!("-- Vorago Accelerometer Example --");
|
||||
let mut dp = pac::Peripherals::take().unwrap();
|
||||
let mut delay = set_up_ms_delay_provider(&mut dp.sysconfig, 50.MHz(), dp.tim0);
|
||||
let pinsa = PinsA::new(&mut dp.sysconfig, dp.porta);
|
||||
let (sck, mosi, miso) = (
|
||||
pinsa.pa20.into_funsel_2(),
|
||||
pinsa.pa19.into_funsel_2(),
|
||||
pinsa.pa18.into_funsel_2(),
|
||||
);
|
||||
let cs_pin = pinsa.pa16.into_funsel_2();
|
||||
let dp = pac::Peripherals::take().unwrap();
|
||||
let pinsa = PinsA::new(dp.porta);
|
||||
let mut delay = CountdownTimer::new(dp.tim0, 50.MHz());
|
||||
let (sck, mosi, miso) = (pinsa.pa20, pinsa.pa19, pinsa.pa18);
|
||||
let cs_pin = pinsa.pa16;
|
||||
let hw_cs_id = configure_pin_as_hw_cs_pin(cs_pin);
|
||||
|
||||
// Need to set the ADC chip select low
|
||||
let mut adc_cs = pinsa.pa17.into_push_pull_output();
|
||||
adc_cs.set_high();
|
||||
Output::new(pinsa.pa17, PinState::Low);
|
||||
|
||||
let spi_cfg = SpiConfig::default()
|
||||
.clk_cfg(
|
||||
@ -49,14 +46,8 @@ fn main() -> ! {
|
||||
)
|
||||
.mode(MODE_3)
|
||||
.slave_output_disable(true);
|
||||
let mut spi = Spi::new(
|
||||
&mut dp.sysconfig,
|
||||
50.MHz(),
|
||||
dp.spib,
|
||||
(sck, miso, mosi),
|
||||
spi_cfg,
|
||||
);
|
||||
spi.cfg_hw_cs_with_pin(&cs_pin);
|
||||
let mut spi = Spi::new(dp.spib, (sck, miso, mosi), spi_cfg).unwrap();
|
||||
spi.cfg_hw_cs(hw_cs_id);
|
||||
|
||||
let mut tx_rx_buf: [u8; 3] = [0; 3];
|
||||
tx_rx_buf[0] = READ_MASK | DEVID_REG;
|
||||
|
@ -10,10 +10,10 @@ use panic_rtt_target as _;
|
||||
use rtt_target::{rprintln, rtt_init_print};
|
||||
use va108xx_hal::{
|
||||
clock::{set_clk_div_register, FilterClkSel},
|
||||
gpio::{FilterType, InterruptEdge, PinsA},
|
||||
gpio::{FilterType, InterruptEdge},
|
||||
pac::{self, interrupt},
|
||||
prelude::*,
|
||||
timer::{default_ms_irq_handler, set_up_ms_tick, InterruptConfig},
|
||||
pins::PinsA,
|
||||
timer::InterruptConfig,
|
||||
};
|
||||
use vorago_reb1::button::Button;
|
||||
use vorago_reb1::leds::Leds;
|
||||
@ -35,18 +35,18 @@ fn main() -> ! {
|
||||
rtt_init_print!();
|
||||
rprintln!("-- Vorago Button IRQ Example --");
|
||||
let mut dp = pac::Peripherals::take().unwrap();
|
||||
let pinsa = PinsA::new(&mut dp.sysconfig, dp.porta);
|
||||
let pinsa = PinsA::new(dp.porta);
|
||||
let edge_irq = match PRESS_MODE {
|
||||
PressMode::Toggle => InterruptEdge::HighToLow,
|
||||
PressMode::Keep => InterruptEdge::BothEdges,
|
||||
};
|
||||
|
||||
// Configure an edge interrupt on the button and route it to interrupt vector 15
|
||||
let mut button = Button::new(pinsa.pa11.into_floating_input());
|
||||
let mut button = Button::new(pinsa.pa11);
|
||||
|
||||
if PRESS_MODE == PressMode::Toggle {
|
||||
// This filter debounces the switch for edge based interrupts
|
||||
button.configure_filter_type(FilterType::FilterFourClockCycles, FilterClkSel::Clk1);
|
||||
button.configure_filter_type(FilterType::FilterFourCycles, FilterClkSel::Clk1);
|
||||
set_clk_div_register(&mut dp.sysconfig, FilterClkSel::Clk1, 50_000);
|
||||
}
|
||||
button.configure_and_enable_edge_interrupt(
|
||||
@ -54,18 +54,7 @@ fn main() -> ! {
|
||||
InterruptConfig::new(pac::interrupt::OC15, true, true),
|
||||
);
|
||||
|
||||
set_up_ms_tick(
|
||||
InterruptConfig::new(pac::Interrupt::OC0, true, true),
|
||||
&mut dp.sysconfig,
|
||||
Some(&mut dp.irqsel),
|
||||
50.MHz(),
|
||||
dp.tim0,
|
||||
);
|
||||
let mut leds = Leds::new(
|
||||
pinsa.pa10.into_push_pull_output(),
|
||||
pinsa.pa7.into_push_pull_output(),
|
||||
pinsa.pa6.into_push_pull_output(),
|
||||
);
|
||||
let mut leds = Leds::new(pinsa.pa10, pinsa.pa7, pinsa.pa6);
|
||||
for led in leds.iter_mut() {
|
||||
led.off();
|
||||
}
|
||||
@ -79,11 +68,6 @@ fn main() -> ! {
|
||||
}
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
fn OC0() {
|
||||
default_ms_irq_handler();
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
fn OC15() {
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
|
@ -9,7 +9,13 @@
|
||||
use cortex_m_rt::entry;
|
||||
use embedded_hal::delay::DelayNs;
|
||||
use panic_halt as _;
|
||||
use va108xx_hal::{gpio::PinsA, pac, prelude::*, timer::set_up_ms_delay_provider};
|
||||
use va108xx_hal::{
|
||||
gpio::{Output, PinState},
|
||||
pac,
|
||||
pins::PinsA,
|
||||
prelude::*,
|
||||
timer::CountdownTimer,
|
||||
};
|
||||
use vorago_reb1::leds::Leds;
|
||||
|
||||
// REB LED pin definitions. All on port A
|
||||
@ -26,7 +32,7 @@ enum LibType {
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let mut dp = pac::Peripherals::take().unwrap();
|
||||
let dp = pac::Peripherals::take().unwrap();
|
||||
|
||||
let lib_type = LibType::Bsp;
|
||||
|
||||
@ -60,11 +66,11 @@ fn main() -> ! {
|
||||
}
|
||||
}
|
||||
LibType::Hal => {
|
||||
let pins = PinsA::new(&mut dp.sysconfig, dp.porta);
|
||||
let mut led1 = pins.pa10.into_readable_push_pull_output();
|
||||
let mut led2 = pins.pa7.into_readable_push_pull_output();
|
||||
let mut led3 = pins.pa6.into_readable_push_pull_output();
|
||||
let mut delay = set_up_ms_delay_provider(&mut dp.sysconfig, 50.MHz(), dp.tim0);
|
||||
let pins = PinsA::new(dp.porta);
|
||||
let mut led1 = Output::new(pins.pa10, PinState::Low);
|
||||
let mut led2 = Output::new(pins.pa7, PinState::Low);
|
||||
let mut led3 = Output::new(pins.pa6, PinState::Low);
|
||||
let mut delay = CountdownTimer::new(dp.tim0, 50.MHz());
|
||||
for _ in 0..10 {
|
||||
led1.set_low();
|
||||
led2.set_low();
|
||||
@ -83,13 +89,9 @@ fn main() -> ! {
|
||||
}
|
||||
}
|
||||
LibType::Bsp => {
|
||||
let pinsa = PinsA::new(&mut dp.sysconfig, dp.porta);
|
||||
let mut leds = Leds::new(
|
||||
pinsa.pa10.into_push_pull_output(),
|
||||
pinsa.pa7.into_push_pull_output(),
|
||||
pinsa.pa6.into_push_pull_output(),
|
||||
);
|
||||
let mut delay = set_up_ms_delay_provider(&mut dp.sysconfig, 50.MHz(), dp.tim0);
|
||||
let pinsa = PinsA::new(dp.porta);
|
||||
let mut leds = Leds::new(pinsa.pa10, pinsa.pa7, pinsa.pa6);
|
||||
let mut delay = CountdownTimer::new(dp.tim0, 50.MHz());
|
||||
for _ in 0..10 {
|
||||
// Blink all LEDs quickly
|
||||
for led in leds.iter_mut() {
|
||||
|
@ -14,20 +14,19 @@ use max116xx_10bit::VoltageRefMode;
|
||||
use max116xx_10bit::{AveragingConversions, AveragingResults};
|
||||
use panic_rtt_target as _;
|
||||
use rtt_target::{rprintln, rtt_init_print};
|
||||
use va108xx_hal::gpio::Port;
|
||||
use va108xx_hal::spi::{OptionalHwCs, SpiClkConfig};
|
||||
use va108xx_hal::gpio::{Input, Output, PinState, Port};
|
||||
use va108xx_hal::pins::PinsA;
|
||||
use va108xx_hal::spi::{configure_pin_as_hw_cs_pin, SpiClkConfig};
|
||||
use va108xx_hal::timer::CountdownTimer;
|
||||
use va108xx_hal::{
|
||||
gpio::PinsA,
|
||||
pac::{self, interrupt},
|
||||
pac,
|
||||
prelude::*,
|
||||
spi::{Spi, SpiBase, SpiConfig},
|
||||
timer::{default_ms_irq_handler, set_up_ms_tick, DelayMs, InterruptConfig},
|
||||
spi::{HwChipSelectId, Spi, SpiConfig},
|
||||
};
|
||||
use va108xx_hal::{port_function_select, FunSel};
|
||||
use vorago_reb1::max11619::{
|
||||
max11619_externally_clocked_no_wakeup, max11619_externally_clocked_with_wakeup,
|
||||
max11619_internally_clocked, EocPin, AN2_CHANNEL, POTENTIOMETER_CHANNEL,
|
||||
max11619_internally_clocked, AN2_CHANNEL, POTENTIOMETER_CHANNEL,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||
@ -57,34 +56,34 @@ const MUX_MODE: MuxMode = MuxMode::None;
|
||||
|
||||
// This is probably more or less a re-implementation of https://docs.rs/embedded-hal-bus/latest/embedded_hal_bus/spi/struct.ExclusiveDevice.html.
|
||||
// Users should look at the embedded-hal-bus crate for sharing the bus.
|
||||
pub struct SpiWithHwCs<Delay, HwCs> {
|
||||
inner: SpiBase<pac::Spib, u8>,
|
||||
pub struct SpiWithHwCs<Delay> {
|
||||
inner: Spi<u8>,
|
||||
delay_provider: Delay,
|
||||
hw_cs: HwCs,
|
||||
hw_cs_id: HwChipSelectId,
|
||||
}
|
||||
|
||||
impl<Delay: DelayNs, HwCs: OptionalHwCs<pac::Spib>> SpiWithHwCs<Delay, HwCs> {
|
||||
pub fn new(spi: SpiBase<pac::Spib, u8>, hw_cs: HwCs, delay_provider: Delay) -> Self {
|
||||
impl<Delay: DelayNs> SpiWithHwCs<Delay> {
|
||||
pub fn new(spi: Spi<u8>, hw_cs_id: HwChipSelectId, delay_provider: Delay) -> Self {
|
||||
Self {
|
||||
inner: spi,
|
||||
hw_cs,
|
||||
hw_cs_id,
|
||||
delay_provider,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Delay, HwCs> embedded_hal::spi::ErrorType for SpiWithHwCs<Delay, HwCs> {
|
||||
impl<Delay> embedded_hal::spi::ErrorType for SpiWithHwCs<Delay> {
|
||||
type Error = Infallible;
|
||||
}
|
||||
|
||||
impl<Delay: DelayNs, HwCs: OptionalHwCs<pac::Spib>> SpiDevice for SpiWithHwCs<Delay, HwCs> {
|
||||
impl<Delay: DelayNs> SpiDevice for SpiWithHwCs<Delay> {
|
||||
fn transaction(
|
||||
&mut self,
|
||||
operations: &mut [spi::Operation<'_, u8>],
|
||||
) -> Result<(), Self::Error> {
|
||||
// Only the HW CS is configured here. This is not really necessary, but showcases
|
||||
// that we could scale this multiple SPI devices.
|
||||
self.inner.cfg_hw_cs_with_pin(&self.hw_cs);
|
||||
self.inner.cfg_hw_cs(self.hw_cs_id);
|
||||
for operation in operations {
|
||||
match operation {
|
||||
spi::Operation::Read(buf) => self.inner.read(buf).ok().unwrap(),
|
||||
@ -111,28 +110,17 @@ fn main() -> ! {
|
||||
rprintln!("-- Vorago ADC Example --");
|
||||
|
||||
let mut dp = pac::Peripherals::take().unwrap();
|
||||
let tim0 = set_up_ms_tick(
|
||||
InterruptConfig::new(pac::Interrupt::OC0, true, true),
|
||||
&mut dp.sysconfig,
|
||||
Some(&mut dp.irqsel),
|
||||
SYS_CLK,
|
||||
dp.tim0,
|
||||
);
|
||||
let delay = DelayMs::new(tim0).unwrap();
|
||||
let mut delay = CountdownTimer::new(dp.tim0, SYS_CLK);
|
||||
unsafe {
|
||||
cortex_m::peripheral::NVIC::unmask(pac::Interrupt::OC0);
|
||||
}
|
||||
|
||||
let pinsa = PinsA::new(&mut dp.sysconfig, dp.porta);
|
||||
let pinsa = PinsA::new(dp.porta);
|
||||
let spi_cfg = SpiConfig::default()
|
||||
.clk_cfg(SpiClkConfig::from_clk(SYS_CLK, 3.MHz()).unwrap())
|
||||
.mode(MODE_0)
|
||||
.blockmode(true);
|
||||
let (sck, mosi, miso) = (
|
||||
pinsa.pa20.into_funsel_2(),
|
||||
pinsa.pa19.into_funsel_2(),
|
||||
pinsa.pa18.into_funsel_2(),
|
||||
);
|
||||
let (sck, mosi, miso) = (pinsa.pa20, pinsa.pa19, pinsa.pa18);
|
||||
|
||||
if MUX_MODE == MuxMode::PortB19to17 {
|
||||
port_function_select(&mut dp.ioconfig, Port::B, 19, FunSel::Sel1).ok();
|
||||
@ -141,39 +129,30 @@ fn main() -> ! {
|
||||
port_function_select(&mut dp.ioconfig, Port::B, 16, FunSel::Sel1).ok();
|
||||
}
|
||||
// Set the accelerometer chip select low in case the board slot is populated
|
||||
let mut accel_cs = pinsa.pa16.into_push_pull_output();
|
||||
accel_cs.set_high();
|
||||
Output::new(pinsa.pa16, PinState::Low);
|
||||
|
||||
let spi = Spi::new(
|
||||
&mut dp.sysconfig,
|
||||
50.MHz(),
|
||||
dp.spib,
|
||||
(sck, miso, mosi),
|
||||
spi_cfg,
|
||||
)
|
||||
.downgrade();
|
||||
let delay_provider = CountdownTimer::new(&mut dp.sysconfig, 50.MHz(), dp.tim1);
|
||||
let spi_with_hwcs = SpiWithHwCs::new(spi, pinsa.pa17.into_funsel_2(), delay_provider);
|
||||
let hw_cs_id = configure_pin_as_hw_cs_pin(pinsa.pa17);
|
||||
let spi = Spi::new(dp.spib, (sck, miso, mosi), spi_cfg).unwrap();
|
||||
|
||||
let delay_spi = CountdownTimer::new(dp.tim1, SYS_CLK);
|
||||
let spi_with_hwcs = SpiWithHwCs::new(spi, hw_cs_id, delay_spi);
|
||||
match EXAMPLE_MODE {
|
||||
ExampleMode::NotUsingEoc => spi_example_externally_clocked(spi_with_hwcs, delay),
|
||||
ExampleMode::NotUsingEoc => spi_example_externally_clocked(spi_with_hwcs, &mut delay),
|
||||
ExampleMode::UsingEoc => {
|
||||
spi_example_internally_clocked(spi_with_hwcs, delay, pinsa.pa14.into_floating_input());
|
||||
spi_example_internally_clocked(
|
||||
spi_with_hwcs,
|
||||
&mut delay,
|
||||
Input::new_floating(pinsa.pa14),
|
||||
);
|
||||
}
|
||||
ExampleMode::NotUsingEocWithDelay => {
|
||||
let delay_us = CountdownTimer::new(&mut dp.sysconfig, 50.MHz(), dp.tim2);
|
||||
spi_example_externally_clocked_with_delay(spi_with_hwcs, delay, delay_us);
|
||||
spi_example_externally_clocked_with_delay(spi_with_hwcs, &mut delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
#[allow(non_snake_case)]
|
||||
fn OC0() {
|
||||
default_ms_irq_handler();
|
||||
}
|
||||
|
||||
/// Use the SPI clock as the conversion clock
|
||||
fn spi_example_externally_clocked(spi: impl SpiDevice, mut delay: DelayMs) -> ! {
|
||||
fn spi_example_externally_clocked(spi: impl SpiDevice, delay: &mut impl DelayNs) -> ! {
|
||||
let mut adc = max11619_externally_clocked_no_wakeup(spi)
|
||||
.expect("Creating externally clocked MAX11619 device failed");
|
||||
if READ_MODE == ReadMode::AverageN {
|
||||
@ -228,11 +207,7 @@ fn spi_example_externally_clocked(spi: impl SpiDevice, mut delay: DelayMs) -> !
|
||||
}
|
||||
}
|
||||
|
||||
fn spi_example_externally_clocked_with_delay(
|
||||
spi: impl SpiDevice,
|
||||
mut delay: DelayMs,
|
||||
mut delay_us: impl DelayNs,
|
||||
) -> ! {
|
||||
fn spi_example_externally_clocked_with_delay(spi: impl SpiDevice, delay: &mut impl DelayNs) -> ! {
|
||||
let mut adc =
|
||||
max11619_externally_clocked_with_wakeup(spi).expect("Creating MAX116xx device failed");
|
||||
let mut cmd_buf: [u8; 32] = [0; 32];
|
||||
@ -244,7 +219,7 @@ fn spi_example_externally_clocked_with_delay(
|
||||
ReadMode::Single => {
|
||||
rprintln!("Reading single potentiometer channel");
|
||||
let pot_val = adc
|
||||
.read_single_channel(&mut cmd_buf, POTENTIOMETER_CHANNEL, &mut delay_us)
|
||||
.read_single_channel(&mut cmd_buf, POTENTIOMETER_CHANNEL, delay)
|
||||
.expect("Creating externally clocked MAX11619 ADC failed");
|
||||
rprintln!("Single channel read:");
|
||||
rprintln!("\tPotentiometer value: {}", pot_val);
|
||||
@ -255,7 +230,7 @@ fn spi_example_externally_clocked_with_delay(
|
||||
&mut cmd_buf,
|
||||
&mut res_buf.iter_mut(),
|
||||
POTENTIOMETER_CHANNEL,
|
||||
&mut delay_us,
|
||||
delay,
|
||||
)
|
||||
.expect("Multi-Channel read failed");
|
||||
print_res_buf(&res_buf);
|
||||
@ -266,7 +241,7 @@ fn spi_example_externally_clocked_with_delay(
|
||||
&mut cmd_buf,
|
||||
&mut res_buf.iter_mut(),
|
||||
AN2_CHANNEL,
|
||||
&mut delay_us,
|
||||
delay,
|
||||
)
|
||||
.expect("Multi-Channel read failed");
|
||||
rprintln!("Multi channel read from 2 to 3:");
|
||||
@ -283,7 +258,11 @@ fn spi_example_externally_clocked_with_delay(
|
||||
}
|
||||
|
||||
/// This function uses the EOC pin to determine whether the conversion finished
|
||||
fn spi_example_internally_clocked(spi: impl SpiDevice, mut delay: DelayMs, eoc_pin: EocPin) -> ! {
|
||||
fn spi_example_internally_clocked(
|
||||
spi: impl SpiDevice,
|
||||
delay: &mut impl DelayNs,
|
||||
eoc_pin: Input,
|
||||
) -> ! {
|
||||
let mut adc = max11619_internally_clocked(
|
||||
spi,
|
||||
eoc_pin,
|
||||
|
@ -5,7 +5,7 @@ use cortex_m_rt::entry;
|
||||
use embedded_hal::delay::DelayNs;
|
||||
use panic_rtt_target as _;
|
||||
use rtt_target::{rprintln, rtt_init_print};
|
||||
use va108xx_hal::{pac, time::Hertz, timer::CountdownTimer};
|
||||
use va108xx_hal::{pac, spi::SpiClkConfig, time::Hertz, timer::CountdownTimer};
|
||||
use vorago_reb1::m95m01::{M95M01, PAGE_SIZE};
|
||||
|
||||
const CLOCK_FREQ: Hertz = Hertz::from_raw(50_000_000);
|
||||
@ -15,12 +15,13 @@ fn main() -> ! {
|
||||
rtt_init_print!();
|
||||
rprintln!("-- VA108XX REB1 NVM example --");
|
||||
|
||||
let mut dp = pac::Peripherals::take().unwrap();
|
||||
let dp = pac::Peripherals::take().unwrap();
|
||||
|
||||
let mut timer = CountdownTimer::new(&mut dp.sysconfig, CLOCK_FREQ, dp.tim0);
|
||||
let mut nvm = M95M01::new(&mut dp.sysconfig, CLOCK_FREQ, dp.spic);
|
||||
let mut delay = CountdownTimer::new(dp.tim0, CLOCK_FREQ);
|
||||
let clk_config = SpiClkConfig::new(2, 4);
|
||||
let mut nvm = M95M01::new(dp.spic, clk_config);
|
||||
let status_reg = nvm.read_status_reg().expect("reading status reg failed");
|
||||
if status_reg.zero_segment() == 0b111 {
|
||||
if status_reg.zero_segment().value() == 0b111 {
|
||||
panic!("status register unexpected values");
|
||||
}
|
||||
|
||||
@ -51,6 +52,6 @@ fn main() -> ! {
|
||||
|
||||
nvm.write(0, &orig_content).unwrap();
|
||||
loop {
|
||||
timer.delay_ms(500);
|
||||
delay.delay_ms(500);
|
||||
}
|
||||
}
|
||||
|
@ -5,16 +5,18 @@
|
||||
//! - [Button Blinky with IRQs](https://egit.irs.uni-stuttgart.de/rust/va108xx-rs/src/branch/main/vorago-reb1/examples/blinky-button-irq.rs)
|
||||
//! - [Button Blinky with IRQs and RTIC](https://egit.irs.uni-stuttgart.de/rust/va108xx-rs/src/branch/main/vorago-reb1/examples/blinky-button-rtic.rs)
|
||||
use va108xx_hal::{
|
||||
gpio::{FilterClkSel, FilterType, InputFloating, InterruptEdge, InterruptLevel, Pin, PA11},
|
||||
clock::FilterClkSel,
|
||||
gpio::{FilterType, Input, InterruptEdge, InterruptLevel, Pin},
|
||||
pins::Pa11,
|
||||
InterruptConfig,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Button(pub Pin<PA11, InputFloating>);
|
||||
pub struct Button(pub Input);
|
||||
|
||||
impl Button {
|
||||
pub fn new(pin: Pin<PA11, InputFloating>) -> Button {
|
||||
Button(pin)
|
||||
pub fn new(pin: Pin<Pa11>) -> Button {
|
||||
Button(Input::new_floating(pin))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -6,20 +6,20 @@
|
||||
//! - [Button Blinky using IRQs](https://egit.irs.uni-stuttgart.de/rust/va108xx-rs/src/branch/main/vorago-reb1/examples/blinky-button-irq.rs)
|
||||
//! - [Button Blinky using IRQs and RTIC](https://egit.irs.uni-stuttgart.de/rust/va108xx-rs/src/branch/main/vorago-reb1/examples/blinky-button-rtic.rs)
|
||||
use va108xx_hal::{
|
||||
gpio::dynpin::DynPin,
|
||||
gpio::pin::{Pin, PushPullOutput, PA10, PA6, PA7},
|
||||
gpio::{Output, PinState},
|
||||
pins::{Pa10, Pa6, Pa7, Pin},
|
||||
};
|
||||
|
||||
pub type LD2 = Pin<PA10, PushPullOutput>;
|
||||
pub type LD3 = Pin<PA7, PushPullOutput>;
|
||||
pub type LD4 = Pin<PA6, PushPullOutput>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Leds(pub [Led; 3]);
|
||||
|
||||
impl Leds {
|
||||
pub fn new(led_pin1: LD2, led_pin2: LD3, led_pin3: LD4) -> Leds {
|
||||
Leds([led_pin1.into(), led_pin2.into(), led_pin3.into()])
|
||||
pub fn new(led_pin1: Pin<Pa10>, led_pin2: Pin<Pa7>, led_pin3: Pin<Pa6>) -> Leds {
|
||||
Leds([
|
||||
Led(Output::new(led_pin1, PinState::Low)),
|
||||
Led(Output::new(led_pin2, PinState::Low)),
|
||||
Led(Output::new(led_pin3, PinState::Low)),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,38 +52,24 @@ impl core::ops::IndexMut<usize> for Leds {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Led(pub DynPin);
|
||||
|
||||
macro_rules! ctor {
|
||||
($($ldx:ident),+) => {
|
||||
$(
|
||||
impl From<$ldx> for Led {
|
||||
fn from(led: $ldx) -> Self {
|
||||
Led(led.into())
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
ctor!(LD2, LD3, LD4);
|
||||
pub struct Led(Output);
|
||||
|
||||
impl Led {
|
||||
/// Turns the LED off. Setting the pin high actually turns the LED off
|
||||
#[inline]
|
||||
pub fn off(&mut self) {
|
||||
self.0.set_high().ok();
|
||||
self.0.set_high();
|
||||
}
|
||||
|
||||
/// Turns the LED on. Setting the pin low actually turns the LED on
|
||||
#[inline]
|
||||
pub fn on(&mut self) {
|
||||
self.0.set_low().ok();
|
||||
self.0.set_low();
|
||||
}
|
||||
|
||||
/// Toggles the LED
|
||||
#[inline]
|
||||
pub fn toggle(&mut self) {
|
||||
self.0.toggle().ok();
|
||||
self.0.toggle();
|
||||
}
|
||||
}
|
||||
|
@ -7,20 +7,25 @@
|
||||
//! # Example
|
||||
//!
|
||||
//! - [REB1 EEPROM example](https://egit.irs.uni-stuttgart.de/rust/va108xx-rs/src/branch/main/vorago-reb1/examples/nvm.rs)
|
||||
use arbitrary_int::{u2, u3};
|
||||
use core::convert::Infallible;
|
||||
use embedded_hal::spi::SpiBus;
|
||||
|
||||
pub const PAGE_SIZE: usize = 256;
|
||||
|
||||
bitfield::bitfield! {
|
||||
pub struct StatusReg(u8);
|
||||
impl Debug;
|
||||
u8;
|
||||
pub status_register_write_protect, _: 7;
|
||||
pub zero_segment, _: 6, 4;
|
||||
pub block_protection_bits, set_block_protection_bits: 3, 2;
|
||||
pub write_enable_latch, _: 1;
|
||||
pub write_in_progress, _: 0;
|
||||
#[bitbybit::bitfield(u8)]
|
||||
#[derive(Debug)]
|
||||
pub struct StatusReg {
|
||||
#[bit(7, r)]
|
||||
status_register_write_protect: bool,
|
||||
#[bits(4..=6, r)]
|
||||
zero_segment: u3,
|
||||
#[bits(2..=3, rw)]
|
||||
block_protection_bits: u2,
|
||||
#[bit(1, r)]
|
||||
write_enable_latch: bool,
|
||||
#[bit(0, r)]
|
||||
write_in_progress: bool,
|
||||
}
|
||||
|
||||
// Registers.
|
||||
@ -42,11 +47,10 @@ pub mod regs {
|
||||
use regs::*;
|
||||
use va108xx_hal::{
|
||||
pac,
|
||||
prelude::*,
|
||||
spi::{RomMiso, RomMosi, RomSck, Spi, SpiClkConfig, SpiConfig, BMSTART_BMSTOP_MASK},
|
||||
spi::{Spi, SpiClkConfig, SpiConfig, SpiLowLevel, BMSTART_BMSTOP_MASK},
|
||||
};
|
||||
|
||||
pub type RomSpi = Spi<pac::Spic, (RomSck, RomMiso, RomMosi), u8>;
|
||||
pub type RomSpi = Spi<u8>;
|
||||
|
||||
/// Driver for the ST device M95M01 EEPROM memory.
|
||||
///
|
||||
@ -59,14 +63,8 @@ pub struct M95M01 {
|
||||
pub struct PageBoundaryExceededError;
|
||||
|
||||
impl M95M01 {
|
||||
pub fn new(syscfg: &mut pac::Sysconfig, sys_clk: impl Into<Hertz>, spi: pac::Spic) -> Self {
|
||||
let spi = RomSpi::new(
|
||||
syscfg,
|
||||
sys_clk,
|
||||
spi,
|
||||
(RomSck, RomMiso, RomMosi),
|
||||
SpiConfig::default().clk_cfg(SpiClkConfig::new(2, 4)),
|
||||
);
|
||||
pub fn new(spi: pac::Spic, clk_config: SpiClkConfig) -> Self {
|
||||
let spi = RomSpi::new_for_rom(spi, SpiConfig::default().clk_cfg(clk_config)).unwrap();
|
||||
let mut spi_dev = Self { spi };
|
||||
spi_dev.clear_block_protection().unwrap();
|
||||
spi_dev
|
||||
@ -74,7 +72,7 @@ impl M95M01 {
|
||||
|
||||
pub fn release(mut self) -> pac::Spic {
|
||||
self.set_block_protection().unwrap();
|
||||
self.spi.release().0
|
||||
unsafe { pac::Spic::steal() }
|
||||
}
|
||||
|
||||
// Wait until the write-in-progress state is cleared. This exposes a [nb] API, so this function
|
||||
@ -90,7 +88,7 @@ impl M95M01 {
|
||||
pub fn read_status_reg(&mut self) -> Result<StatusReg, Infallible> {
|
||||
let mut write_read: [u8; 2] = [regs::RDSR, 0x00];
|
||||
self.spi.transfer_in_place(&mut write_read)?;
|
||||
Ok(StatusReg(write_read[1]))
|
||||
Ok(StatusReg::new_with_raw_value(write_read[1]))
|
||||
}
|
||||
|
||||
pub fn write_enable(&mut self) -> Result<(), Infallible> {
|
||||
@ -104,10 +102,10 @@ impl M95M01 {
|
||||
}
|
||||
|
||||
pub fn set_block_protection(&mut self) -> Result<(), Infallible> {
|
||||
let mut reg = StatusReg(0);
|
||||
reg.set_block_protection_bits(0b11);
|
||||
let mut reg = StatusReg::new_with_raw_value(0);
|
||||
reg.set_block_protection_bits(u2::new(0b11));
|
||||
self.write_enable()?;
|
||||
self.spi.write(&[WRSR, reg.0])
|
||||
self.spi.write(&[WRSR, reg.raw_value()])
|
||||
}
|
||||
|
||||
fn common_init_write_and_read(&mut self, address: usize, reg: u8) -> Result<(), Infallible> {
|
||||
|
@ -9,7 +9,7 @@ use max116xx_10bit::{
|
||||
Error, ExternallyClocked, InternallyClockedInternallyTimedSerialInterface, Max116xx10Bit,
|
||||
Max116xx10BitEocExt, VoltageRefMode, WithWakeupDelay, WithoutWakeupDelay,
|
||||
};
|
||||
use va108xx_hal::gpio::{Floating, Input, Pin, PA14};
|
||||
use va108xx_hal::gpio::Input;
|
||||
|
||||
pub type Max11619ExternallyClockedNoWakeup<Spi> =
|
||||
Max116xx10Bit<Spi, ExternallyClocked, WithoutWakeupDelay>;
|
||||
@ -17,7 +17,6 @@ pub type Max11619ExternallyClockedWithWakeup<Spi> =
|
||||
Max116xx10Bit<Spi, ExternallyClocked, WithWakeupDelay>;
|
||||
pub type Max11619InternallyClocked<Spi, Eoc> =
|
||||
Max116xx10BitEocExt<Spi, Eoc, InternallyClockedInternallyTimedSerialInterface>;
|
||||
pub type EocPin = Pin<PA14, Input<Floating>>;
|
||||
|
||||
pub const AN0_CHANNEL: u8 = 0;
|
||||
pub const AN1_CHANNEL: u8 = 1;
|
||||
@ -44,9 +43,9 @@ pub fn max11619_externally_clocked_with_wakeup<Spi: SpiDevice>(
|
||||
|
||||
pub fn max11619_internally_clocked<Spi: SpiDevice>(
|
||||
spi: Spi,
|
||||
eoc: EocPin,
|
||||
eoc: Input,
|
||||
v_ref: VoltageRefMode,
|
||||
) -> Result<Max11619InternallyClocked<Spi, EocPin>, Error<Spi::Error, Infallible>> {
|
||||
) -> Result<Max11619InternallyClocked<Spi, Input>, Error<Spi::Error, Infallible>> {
|
||||
let mut adc = Max116xx10Bit::max11619(spi)?
|
||||
.into_int_clkd_int_timed_through_ser_if_without_wakeup(v_ref, eoc)?;
|
||||
adc.reset(false)?;
|
||||
|
@ -15,7 +15,7 @@ use va108xx_hal::{
|
||||
const ADT75_I2C_ADDR: u8 = 0b1001000;
|
||||
|
||||
pub struct Adt75TempSensor {
|
||||
sensor_if: I2cMaster<pac::I2ca, SevenBitAddress>,
|
||||
sensor_if: I2cMaster<SevenBitAddress>,
|
||||
cmd_buf: [u8; 1],
|
||||
current_reg: RegAddresses,
|
||||
}
|
||||
@ -48,17 +48,12 @@ impl From<Error> for AdtInitError {
|
||||
}
|
||||
|
||||
impl Adt75TempSensor {
|
||||
pub fn new(
|
||||
sys_cfg: &mut pac::Sysconfig,
|
||||
sys_clk: impl Into<Hertz> + Copy,
|
||||
i2ca: pac::I2ca,
|
||||
) -> Result<Self, Error> {
|
||||
pub fn new(sys_clk: Hertz, i2ca: pac::I2ca) -> Result<Self, Error> {
|
||||
let mut sensor = Adt75TempSensor {
|
||||
// The master construction can not fail for regular I2C speed.
|
||||
sensor_if: I2cMaster::new(
|
||||
sys_cfg,
|
||||
sys_clk,
|
||||
i2ca,
|
||||
sys_clk,
|
||||
MasterConfig::default(),
|
||||
I2cSpeed::Regular100khz,
|
||||
)
|
||||
|
Reference in New Issue
Block a user