Skip to content

Commit 6e48bf8

Browse files
committed
somethings wrong
1 parent c34fbb0 commit 6e48bf8

File tree

4 files changed

+119
-26
lines changed

4 files changed

+119
-26
lines changed

va108xx-hal/src/spi.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
PeripheralSelect,
1818
};
1919
use core::{convert::Infallible, fmt::Debug, marker::PhantomData, ops::Deref};
20-
use embedded_hal::spi::{Mode, MODE_0, MODE_1, MODE_2, MODE_3};
20+
use embedded_hal::spi::Mode;
2121

2222
//==================================================================================================
2323
// Defintions
@@ -226,13 +226,15 @@ hw_cs_pins!(
226226
pub struct RomSck;
227227
pub struct RomMosi;
228228
pub struct RomMiso;
229+
pub struct RomCs;
229230

230231
impl Sealed for RomSck {}
231232
impl PinSck<pac::Spic> for RomSck {}
232233
impl Sealed for RomMosi {}
233234
impl PinMosi<pac::Spic> for RomMosi {}
234235
impl Sealed for RomMiso {}
235236
impl PinMiso<pac::Spic> for RomMiso {}
237+
impl Sealed for RomCs {}
236238

237239
hw_cs_pins!(
238240
pac::Spic, SpiPort::Portc:
@@ -250,6 +252,12 @@ hw_cs_pins!(
250252
(PA20, AltFunc3, HwChipSelectId::Id4, HwCs4SpiCPortA),
251253
);
252254

255+
impl HwCsProvider for RomCs {
256+
const CS_ID: HwChipSelectId = HwChipSelectId::Id0;
257+
const SPI_PORT: SpiPort = SpiPort::Portc;
258+
}
259+
impl OptionalHwCs<pac::Spic> for RomCs {}
260+
253261
//==================================================================================================
254262
// Config
255263
//==================================================================================================
@@ -674,6 +682,18 @@ where
674682
#[inline]
675683
pub fn fill_word(&self) -> Word;
676684

685+
#[inline]
686+
pub fn spi(&self) -> &SpiI;
687+
688+
#[inline]
689+
pub fn cfg_hw_cs(&mut self, hw_cs: HwChipSelectId);
690+
691+
#[inline]
692+
pub fn cfg_hw_cs_with_pin<HwCs: OptionalHwCs<SpiI>>(&mut self, _hwcs: &HwCs);
693+
694+
#[inline]
695+
pub fn cfg_hw_cs_disable(&mut self);
696+
677697
pub fn cfg_transfer<HwCs: OptionalHwCs<SpiI>>(
678698
&mut self, transfer_cfg: &TransferConfigWithHwcs<HwCs>
679699
);
@@ -698,6 +718,11 @@ impl<SpiInstance: Instance, Word: WordProvider> SpiBase<SpiInstance, Word>
698718
where
699719
<Word as TryFrom<u32>>::Error: core::fmt::Debug,
700720
{
721+
#[inline]
722+
pub fn spi(&self) -> &SpiInstance {
723+
&self.spi
724+
}
725+
701726
#[inline]
702727
pub fn cfg_clock(&mut self, cfg: SpiClkConfig) {
703728
self.spi
@@ -717,12 +742,7 @@ where
717742

718743
#[inline]
719744
pub fn cfg_mode(&mut self, mode: Mode) {
720-
let (cpo_bit, cph_bit) = match mode {
721-
MODE_0 => (false, false),
722-
MODE_1 => (false, true),
723-
MODE_2 => (true, false),
724-
MODE_3 => (true, true),
725-
};
745+
let (cpo_bit, cph_bit) = mode_to_cpo_cph_bit(mode);
726746
self.spi.ctrl0().modify(|_, w| {
727747
w.spo().bit(cpo_bit);
728748
w.sph().bit(cph_bit)

vorago-reb1/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ panic-halt = "0.2"
3333
nb = "1"
3434
rtt-target = "0.5"
3535
panic-rtt-target = "0.1"
36+
embedded-hal-bus = "0.2"
37+
dummy-pin = "1"
3638

3739
[package.metadata.docs.rs]
3840
all-features = true

vorago-reb1/examples/nvm.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use cortex_m_rt::entry;
5+
use embedded_hal::spi::{SpiBus, MODE_0};
6+
use embedded_hal_bus::spi::ExclusiveDevice;
7+
use panic_rtt_target as _;
8+
use rtt_target::{rprintln, rtt_init_print};
9+
use va108xx_hal::{
10+
pac,
11+
spi::{RomCs, RomMiso, RomMosi, RomSck, Spi, SpiClkConfig, SpiConfig, TransferConfigWithHwcs},
12+
time::Hertz,
13+
};
14+
use vorago_reb1::m95m01::{regs::RDSR, M95M01};
15+
16+
const CLOCK_FREQ: Hertz = Hertz::from_raw(50_000_000);
17+
18+
#[entry]
19+
fn main() -> ! {
20+
rtt_init_print!();
21+
rprintln!("-- VA108XX REB1 NVM example --");
22+
23+
let mut dp = pac::Peripherals::take().unwrap();
24+
let cp = cortex_m::Peripherals::take().unwrap();
25+
26+
let mut spi = Spi::new(
27+
&mut dp.sysconfig,
28+
CLOCK_FREQ,
29+
dp.spic,
30+
(RomSck, RomMiso, RomMosi),
31+
// These values are taken from the vorago bootloader app, don't want to experiment here..
32+
SpiConfig::default().clk_cfg(SpiClkConfig::new(2, 4)),
33+
None,
34+
);
35+
spi.cfg_mode(MODE_0);
36+
spi.cfg_hw_cs(va108xx_hal::spi::HwChipSelectId::Id0);
37+
spi.spi().ctrl1().modify(|_, w| w.blockmode().set_bit());
38+
let mut read_buf: [u8; 2] = [0; 2];
39+
spi.transfer(&mut read_buf, &[RDSR, 0]);
40+
rprintln!("read buf {:?}", read_buf);
41+
/*
42+
let mut nvm =
43+
M95M01::new(ExclusiveDevice::new_no_delay(spi, dummy_pin::DummyPin::new_low()).unwrap())
44+
.expect("creating NVM structure failed");
45+
let status_reg = nvm.read_status_reg().expect("reading status reg failed");
46+
rprintln!("status reg: {:?}", status_reg);
47+
if status_reg.zero_segment() == 0b111 {
48+
panic!("status register unexpected values");
49+
}
50+
51+
let mut read_buf: [u8; 16] = [0; 16];
52+
nvm.read(0x4000, &mut read_buf[0..4])
53+
.expect("reading NVM failed");
54+
rprintln!("NVM address 0x4000: {:x?}", &read_buf[0..4]);
55+
let write_buf: [u8; 4] = [1, 2, 3, 4];
56+
nvm.write(0x4000, &write_buf).unwrap();
57+
nvm.read(0x4000, &mut read_buf[0..4]).unwrap();
58+
assert_eq!(&read_buf[0..4], write_buf);
59+
*/
60+
loop {}
61+
}

vorago-reb1/src/m95m01.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
use core::fmt::Debug;
12
use embedded_hal::spi::SpiDevice;
23

34
bitfield::bitfield! {
45
pub struct StatusReg(u8);
56
impl Debug;
67
u8;
7-
status_register_write_protect, _: 7, 0;
8-
block_protection_bits, set_block_protection_bits: 3, 2;
9-
write_enable_latch, _: 1;
10-
write_in_progress, _: 0;
8+
pub status_register_write_protect, _: 7;
9+
pub zero_segment, _: 6, 4;
10+
pub block_protection_bits, set_block_protection_bits: 3, 2;
11+
pub write_enable_latch, _: 1;
12+
pub write_in_progress, _: 0;
1113
}
1214

1315
// Registers.
@@ -33,18 +35,22 @@ pub struct M95M01<Spi: SpiDevice> {
3335
spi: Spi,
3436
}
3537

36-
pub enum Error<SpiError> {
38+
#[derive(Debug)]
39+
pub enum Error<SpiError: Debug> {
3740
Spi(SpiError),
3841
BufTooShort,
3942
}
4043

41-
impl<SpiError> From<SpiError> for Error<SpiError> {
44+
impl<SpiError: Debug> From<SpiError> for Error<SpiError> {
4245
fn from(value: SpiError) -> Self {
4346
Self::Spi(value)
4447
}
4548
}
4649

47-
impl<Spi: SpiDevice> M95M01<Spi> {
50+
impl<Spi: SpiDevice> M95M01<Spi>
51+
where
52+
Spi::Error: Debug,
53+
{
4854
pub fn new(spi: Spi) -> Result<Self, Spi::Error> {
4955
let mut spi_dev = Self { spi };
5056
spi_dev.clear_block_protection()?;
@@ -59,21 +65,27 @@ impl<Spi: SpiDevice> M95M01<Spi> {
5965
// Wait until the write-in-progress state is cleared. This exposes a [nb] API, so this function
6066
// will return [nb::Error::WouldBlock] if the EEPROM is still busy.
6167
pub fn writes_are_done(&mut self) -> nb::Result<(), Spi::Error> {
62-
let mut read: [u8; 2] = [0; 2];
63-
self.spi.transfer(&mut read, &[regs::RDSR, 0x00])?;
64-
let rdsr = StatusReg(read[1]);
68+
let rdsr = self.read_status_reg()?;
6569
if rdsr.write_in_progress() {
6670
return Err(nb::Error::WouldBlock);
6771
}
6872
Ok(())
6973
}
7074

75+
pub fn read_status_reg(&mut self) -> Result<StatusReg, Spi::Error> {
76+
let mut write_read: [u8; 2] = [regs::RDSR, 0x00];
77+
self.spi.transfer_in_place(&mut write_read)?;
78+
Ok(StatusReg(write_read[1]))
79+
}
80+
7181
pub fn write_enable(&mut self) -> Result<(), Spi::Error> {
7282
self.spi.write(&[regs::WREN])
7383
}
7484

7585
pub fn clear_block_protection(&mut self) -> Result<(), Spi::Error> {
76-
self.spi.write(&[WREN, WRSR, 0x00])
86+
// Has to be written separately.
87+
self.spi.write(&[WREN])?;
88+
self.spi.write(&[WRSR, 0x00])
7789
}
7890

7991
pub fn set_block_protection(&mut self) -> Result<(), Spi::Error> {
@@ -83,6 +95,7 @@ impl<Spi: SpiDevice> M95M01<Spi> {
8395
}
8496

8597
pub fn write(&mut self, address: u32, data: &[u8]) -> Result<(), Spi::Error> {
98+
nb::block!(self.writes_are_done())?;
8699
self.write_enable()?;
87100
self.spi.write(&[
88101
WRITE,
@@ -94,26 +107,23 @@ impl<Spi: SpiDevice> M95M01<Spi> {
94107
Ok(())
95108
}
96109

97-
pub fn read(
98-
&mut self,
99-
address: u32,
100-
size: usize,
101-
buf: &mut [u8],
102-
) -> Result<(), Error<Spi::Error>> {
103-
if buf.len() < size {
110+
pub fn read(&mut self, address: u32, buf: &mut [u8]) -> Result<(), Error<Spi::Error>> {
111+
if buf.len() < buf.len() {
104112
return Err(Error::BufTooShort);
105113
}
114+
nb::block!(self.writes_are_done())?;
106115
self.spi.write(&[
107116
READ,
108117
((address >> 16) & 0xff) as u8,
109118
((address >> 8) & 0xff) as u8,
110119
(address & 0xff) as u8,
111120
])?;
112-
self.spi.read(&mut buf[0..size])?;
121+
self.spi.read(buf)?;
113122
Ok(())
114123
}
115124

116125
pub fn verify(&mut self, address: u32, data: &[u8]) -> Result<bool, Spi::Error> {
126+
nb::block!(self.writes_are_done())?;
117127
// Write the read command and address
118128
self.spi.write(&[
119129
READ,

0 commit comments

Comments
 (0)