Skip to content

Commit

Permalink
SPI working
Browse files Browse the repository at this point in the history
  • Loading branch information
romancardenas committed Dec 1, 2024
1 parent b16cfc1 commit df544aa
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 25 deletions.
8 changes: 4 additions & 4 deletions e310x-hal/src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ pub struct Channel<'a, PWM, PIN> {
pin: PIN,
}

impl<'a, PWM, PIN> Channel<'a, PWM, PIN> {
impl<PWM, PIN> Channel<'_, PWM, PIN> {
/// Frees the PWM channel
pub fn free(self) -> PIN {
self.pin
}
}

impl<'a, PWM: PwmX, PIN: Pin<PWM>> Channel<'a, PWM, PIN> {
impl<PWM: PwmX, PIN: Pin<PWM>> Channel<'_, PWM, PIN> {
/// Returns the period of the PWM
pub fn get_period(&self) -> PWM::CmpWidth {
self.pwm.get_period()
Expand Down Expand Up @@ -237,11 +237,11 @@ impl<'a, PWM: PwmX, PIN: Pin<PWM>> Channel<'a, PWM, PIN> {
}
}

impl<'a, PWM: PwmX, PIN: Pin<PWM>> ErrorType for Channel<'a, PWM, PIN> {
impl<PWM: PwmX, PIN: Pin<PWM>> ErrorType for Channel<'_, PWM, PIN> {
type Error = ErrorKind;
}

impl<'a, PWM: PwmX, PIN: Pin<PWM>> SetDutyCycle for Channel<'a, PWM, PIN> {
impl<PWM: PwmX, PIN: Pin<PWM>> SetDutyCycle for Channel<'_, PWM, PIN> {
fn max_duty_cycle(&self) -> u16 {
PWM::bits_from_cmp_width(self.max_duty()) as _
}
Expand Down
6 changes: 3 additions & 3 deletions e310x-hal/src/spi/shared_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct SpiSharedDevice<'bus, SPI, PINS, CS> {
config: SpiConfig,
}

impl<'bus, SPI, PINS, CS> SpiSharedDevice<'bus, SPI, PINS, CS> {
impl<SPI, PINS, CS> SpiSharedDevice<'_, SPI, PINS, CS> {
/// Releases the CS pin back
pub fn release(self) -> CS {
self.cs
Expand All @@ -34,7 +34,7 @@ where
}
}

impl<'bus, SPI, PINS, CS> ErrorType for SpiSharedDevice<'bus, SPI, PINS, CS>
impl<SPI, PINS, CS> ErrorType for SpiSharedDevice<'_, SPI, PINS, CS>
where
SPI: SpiX,
PINS: PinsNoCS<SPI>,
Expand All @@ -43,7 +43,7 @@ where
type Error = ErrorKind;
}

impl<'bus, SPI, PINS, CS> SpiDevice for SpiSharedDevice<'bus, SPI, PINS, CS>
impl<SPI, PINS, CS> SpiDevice for SpiSharedDevice<'_, SPI, PINS, CS>
where
SPI: SpiX,
PINS: PinsNoCS<SPI> + PinsFull<SPI>,
Expand Down
2 changes: 1 addition & 1 deletion e310x-hal/src/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nb::block;
/// implementations.
pub struct Stdout<'p, T: 'p>(pub &'p mut T);

impl<'p, T: serial::Write> Write for Stdout<'p, T> {
impl<T: serial::Write> Write for Stdout<'_, T> {
fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
for byte in s.as_bytes() {
if *byte == b'\n' {
Expand Down
3 changes: 2 additions & 1 deletion hifive1-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ riscv-rt = { version = "0.13.0", features = ["single-hart"] }
panic-halt = "1.0.0"
semihosting = { version = "0.1", features = ["stdio", "panic-handler"] }
# max3010x = "0.2.0" # TODO uncomment when the driver is published
max3010x = { git = "https://github.com/romancardenas/max3010x-rs.git" }
max3010x = { git = "https://github.com/eldruin/max3010x-rs.git" }
mfrc522 = "0.8.0"

[features]
v-trap = ["hifive1/v-trap"]
4 changes: 2 additions & 2 deletions hifive1-examples/examples/i2c_max3010x.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Basic blinking LEDs example using mtime/mtimecmp registers for "sleep" in a loop.
//! Blinks each led once and goes to the next one.
//! Sample example for the MAX3010x pulse oximeter and heart rate sensor
//! using the I2C interface.
#![no_std]
#![no_main]
Expand Down
64 changes: 64 additions & 0 deletions hifive1-examples/examples/spi_rc522.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Basic example of the RC522 RFID reader using the SPI interface.
#![no_std]
#![no_main]

use hifive1::{
clock,
hal::{
e310x::CLINT,
prelude::*,
spi::{SpiBus, SpiConfig, SpiExclusiveDevice, MODE_0},
DeviceResources,
},
pin, sprintln,
};
use mfrc522::{comm::blocking::spi::SpiInterface, Mfrc522};
extern crate panic_halt;

#[riscv_rt::entry]
fn main() -> ! {
let dr = DeviceResources::take().unwrap();
let p = dr.peripherals;
let pins = dr.pins;

// Configure clocks
let clocks = clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());

// Configure UART for stdout
hifive1::stdout::configure(
p.UART0,
pin!(pins, uart0_tx),
pin!(pins, uart0_rx),
115_200.bps(),
clocks,
);

let sck = pin!(pins, spi1_sck).into_iof0();
let miso = pin!(pins, spi1_miso).into_iof0();
let mosi = pin!(pins, spi1_mosi).into_iof0();
let cs = pin!(pins, spi1_ss0).into_iof0();

let spi_bus = SpiBus::new(p.QSPI1, (mosi, miso, sck, cs));
let spi_cfg = SpiConfig::new(MODE_0, 1_000_000.hz(), &clocks);
let spi_device = SpiExclusiveDevice::new(spi_bus, &spi_cfg);
let spi_itf = SpiInterface::new(spi_device);

let mut mfrc522 = match Mfrc522::new(spi_itf).init() {
Ok(mfrc522) => mfrc522,
Err(e) => {
sprintln!("Error initializing sensor: {:?}", e);
loop {}
}
};
// The reported version is expected to be 0x91 or 0x92
let version = mfrc522.version().unwrap();
sprintln!("Version: {:x}", version);

// Get the sleep struct from CLINT
let mut sleep = CLINT::delay();
const STEP: u32 = 1000; // 1s
loop {
sleep.delay_ms(STEP);
}
}
32 changes: 18 additions & 14 deletions hifive1/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ macro_rules! pin {
()
};
// spi
($gpio:ident, spi0_sck) => {
($gpio:ident, spi1_sck) => {
$gpio.pin5
};
($gpio:ident, spi0_mosi) => {
($gpio:ident, spi1_mosi) => {
$gpio.pin3
};
($gpio:ident, spi0_miso) => {
($gpio:ident, spi1_miso) => {
$gpio.pin4
};
($gpio:ident, spi0_ss0) => {
($gpio:ident, spi1_ss0) => {
$gpio.pin2
};
// spi_ss1 is not documented
($gpio:ident, spi0_ss2) => {
($gpio:ident, spi1_ss1) => {
$gpio.pin8
};
($gpio:ident, spi1_ss2) => {
$gpio.pin9
};
($gpio:ident, spi0_ss3) => {
($gpio:ident, spi1_ss3) => {
$gpio.pin10
};
// i2c
Expand Down Expand Up @@ -184,23 +186,25 @@ macro_rules! pin {
()
};
// spi
($gpio:ident, spi0_sck) => {
($gpio:ident, spi1_sck) => {
$gpio.pin5
};
($gpio:ident, spi0_mosi) => {
($gpio:ident, spi1_mosi) => {
$gpio.pin3
};
($gpio:ident, spi0_miso) => {
($gpio:ident, spi1_miso) => {
$gpio.pin4
};
($gpio:ident, spi0_ss0) => {
($gpio:ident, spi1_ss0) => {
$gpio.pin2
};
// spi_ss1 is not documented
($gpio:ident, spi0_ss2) => {
($gpio:ident, spi1_ss1) => {
$gpio.pin8
};
($gpio:ident, spi1_ss2) => {
$gpio.pin9
};
($gpio:ident, spi0_ss3) => {
($gpio:ident, spi1_ss3) => {
$gpio.pin10
};
// i2c
Expand Down

0 comments on commit df544aa

Please sign in to comment.