2019-11-21 07:14:09 +08:00
|
|
|
//! Quad-SPI Flash Controller
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
use core::marker::PhantomData;
|
2020-05-01 07:45:52 +08:00
|
|
|
use log::{error, info, warn};
|
2019-12-18 06:35:58 +08:00
|
|
|
use libregister::{RegisterR, RegisterW, RegisterRW};
|
2020-05-01 07:45:52 +08:00
|
|
|
use crate::{print, println};
|
2019-11-21 07:14:09 +08:00
|
|
|
use super::slcr;
|
2020-01-24 05:44:10 +08:00
|
|
|
use super::clocks::source::{IoPll, ClockSource};
|
2019-11-21 07:14:09 +08:00
|
|
|
|
2019-12-05 06:56:38 +08:00
|
|
|
mod regs;
|
2019-12-05 08:15:14 +08:00
|
|
|
mod bytes;
|
|
|
|
pub use bytes::{BytesTransferExt, BytesTransfer};
|
2019-12-14 08:55:17 +08:00
|
|
|
mod spi_flash_register;
|
|
|
|
use spi_flash_register::*;
|
2019-12-16 02:28:55 +08:00
|
|
|
mod transfer;
|
|
|
|
use transfer::Transfer;
|
2019-11-21 07:14:09 +08:00
|
|
|
|
2019-11-23 08:59:24 +08:00
|
|
|
const FLASH_BAUD_RATE: u32 = 50_000_000;
|
2019-12-14 08:57:51 +08:00
|
|
|
/// 16 MB
|
|
|
|
pub const SINGLE_CAPACITY: u32 = 0x1000000;
|
|
|
|
pub const SECTOR_SIZE: u32 = 0x10000;
|
|
|
|
pub const PAGE_SIZE: u32 = 0x100;
|
2019-11-23 08:59:24 +08:00
|
|
|
|
2019-12-12 07:11:42 +08:00
|
|
|
/// Instruction: Read Identification
|
2019-12-07 09:11:50 +08:00
|
|
|
const INST_RDID: u8 = 0x9F;
|
2020-04-07 04:02:10 +08:00
|
|
|
/// Instruction: Read
|
2019-12-14 08:56:49 +08:00
|
|
|
const INST_READ: u8 = 0x03;
|
2020-04-07 04:02:10 +08:00
|
|
|
/// Instruction: Quad I/O Fast Read
|
|
|
|
const INST_4IO_FAST_READ: u8 = 0xEB;
|
2019-12-14 08:56:49 +08:00
|
|
|
/// Instruction: Write Disable
|
|
|
|
const INST_WRDI: u8 = 0x04;
|
|
|
|
/// Instruction: Write Enable
|
|
|
|
const INST_WREN: u8 = 0x06;
|
2019-12-16 07:48:39 +08:00
|
|
|
/// Instruction: Program page
|
2019-12-22 10:12:53 +08:00
|
|
|
const INST_PP: u8 = 0x02;
|
2019-12-16 07:48:39 +08:00
|
|
|
/// Instruction: Erase 4K Block
|
|
|
|
const INST_BE_4K: u8 = 0x20;
|
2019-12-07 09:11:50 +08:00
|
|
|
|
2019-12-12 07:17:34 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum SpiWord {
|
|
|
|
W8(u8),
|
|
|
|
W16(u16),
|
|
|
|
W24(u32),
|
|
|
|
W32(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u8> for SpiWord {
|
|
|
|
fn from(x: u8) -> Self {
|
|
|
|
SpiWord::W8(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u16> for SpiWord {
|
|
|
|
fn from(x: u16) -> Self {
|
|
|
|
SpiWord::W16(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u32> for SpiWord {
|
|
|
|
fn from(x: u32) -> Self {
|
|
|
|
SpiWord::W32(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 07:13:02 +08:00
|
|
|
/// Memory-mapped mode
|
2019-11-28 10:02:51 +08:00
|
|
|
pub struct LinearAddressing;
|
2019-12-12 07:13:02 +08:00
|
|
|
/// Manual I/O mode
|
2019-11-30 09:48:39 +08:00
|
|
|
pub struct Manual;
|
2019-11-28 10:02:51 +08:00
|
|
|
|
2019-11-21 07:14:09 +08:00
|
|
|
/// Flash Interface Driver
|
2019-11-23 08:59:24 +08:00
|
|
|
///
|
|
|
|
/// For 2x Spansion S25FL128SAGMFIR01
|
2019-11-28 10:02:51 +08:00
|
|
|
pub struct Flash<MODE> {
|
2019-11-21 07:14:09 +08:00
|
|
|
regs: &'static mut regs::RegisterBlock,
|
2019-11-28 10:02:51 +08:00
|
|
|
_mode: PhantomData<MODE>,
|
2019-11-21 07:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-11-30 06:48:08 +08:00
|
|
|
impl<MODE> Flash<MODE> {
|
|
|
|
fn transition<TO>(self) -> Flash<TO> {
|
|
|
|
Flash {
|
|
|
|
regs: self.regs,
|
|
|
|
_mode: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 06:56:38 +08:00
|
|
|
|
|
|
|
fn disable_interrupts(&mut self) {
|
|
|
|
self.regs.intr_dis.write(
|
|
|
|
regs::IntrDis::zeroed()
|
|
|
|
.rx_overflow(true)
|
|
|
|
.tx_fifo_not_full(true)
|
|
|
|
.tx_fifo_full(true)
|
|
|
|
.rx_fifo_not_empty(true)
|
|
|
|
.rx_fifo_full(true)
|
|
|
|
.tx_fifo_underflow(true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_rx_fifo(&self) {
|
|
|
|
while self.regs.intr_status.read().rx_fifo_not_empty() {
|
|
|
|
let _ = self.regs.rx_data.read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_interrupt_status(&mut self) {
|
|
|
|
self.regs.intr_status.write(
|
|
|
|
regs::IntrStatus::zeroed()
|
|
|
|
.rx_overflow(true)
|
|
|
|
.tx_fifo_underflow(true)
|
|
|
|
);
|
|
|
|
}
|
2019-12-12 07:17:34 +08:00
|
|
|
|
|
|
|
fn wait_tx_fifo_flush(&mut self) {
|
2019-12-22 10:13:38 +08:00
|
|
|
self.regs.config.modify(|_, w| w.man_start_com(true));
|
2019-12-12 07:17:34 +08:00
|
|
|
while !self.regs.intr_status.read().tx_fifo_not_full() {}
|
|
|
|
}
|
2019-11-30 06:48:08 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
impl Flash<()> {
|
2019-11-21 07:14:09 +08:00
|
|
|
pub fn new(clock: u32) -> Self {
|
|
|
|
Self::enable_clocks(clock);
|
|
|
|
Self::setup_signals();
|
|
|
|
Self::reset();
|
|
|
|
|
|
|
|
let regs = regs::RegisterBlock::qspi();
|
2019-11-28 10:02:51 +08:00
|
|
|
let mut flash = Flash { regs, _mode: PhantomData };
|
2019-11-23 08:59:24 +08:00
|
|
|
flash.configure((FLASH_BAUD_RATE - 1 + clock) / FLASH_BAUD_RATE);
|
2019-11-21 07:14:09 +08:00
|
|
|
flash
|
|
|
|
}
|
|
|
|
|
2020-01-24 05:44:10 +08:00
|
|
|
/// typical: `200_000_000` Hz
|
2019-11-21 07:14:09 +08:00
|
|
|
fn enable_clocks(clock: u32) {
|
2020-01-24 05:44:10 +08:00
|
|
|
let io_pll = IoPll::freq();
|
2019-11-21 07:14:09 +08:00
|
|
|
let divisor = ((clock - 1 + io_pll) / clock)
|
|
|
|
.max(1).min(63) as u8;
|
|
|
|
|
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
slcr.lqspi_clk_ctrl.write(
|
|
|
|
slcr::LqspiClkCtrl::zeroed()
|
|
|
|
.src_sel(slcr::PllSource::IoPll)
|
|
|
|
.divisor(divisor)
|
|
|
|
.clkact(true)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_signals() {
|
2019-11-23 08:59:24 +08:00
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
// 1. Configure MIO pin 1 for chip select 0 output.
|
|
|
|
slcr.mio_pin_01.write(
|
|
|
|
slcr::MioPin01::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
.pullup(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Configure MIO pins 2 through 5 for I/O.
|
|
|
|
slcr.mio_pin_02.write(
|
|
|
|
slcr::MioPin02::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
slcr.mio_pin_03.write(
|
|
|
|
slcr::MioPin03::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
slcr.mio_pin_04.write(
|
|
|
|
slcr::MioPin04::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
slcr.mio_pin_05.write(
|
|
|
|
slcr::MioPin05::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
|
|
|
|
// 3. Configure MIO pin 6 for serial clock 0 output.
|
|
|
|
slcr.mio_pin_06.write(
|
|
|
|
slcr::MioPin06::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
|
2019-11-28 10:22:26 +08:00
|
|
|
// Option: Add Second Device Chip Select
|
|
|
|
// 4. Configure MIO pin 0 for chip select 1 output.
|
|
|
|
slcr.mio_pin_00.write(
|
|
|
|
slcr::MioPin00::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Option: Add Second Serial Clock
|
|
|
|
// 5. Configure MIO pin 9 for serial clock 1 output.
|
|
|
|
slcr.mio_pin_09.write(
|
|
|
|
slcr::MioPin09::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
2020-04-03 06:17:25 +08:00
|
|
|
.pullup(true)
|
2019-11-28 10:22:26 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Option: Add 4-bit Data
|
|
|
|
// 6. Configure MIO pins 10 through 13 for I/O.
|
|
|
|
slcr.mio_pin_10.write(
|
|
|
|
slcr::MioPin10::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
2020-04-03 06:17:25 +08:00
|
|
|
.pullup(true)
|
2019-11-28 10:22:26 +08:00
|
|
|
);
|
|
|
|
slcr.mio_pin_11.write(
|
|
|
|
slcr::MioPin11::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
2020-04-03 06:17:25 +08:00
|
|
|
.pullup(true)
|
2019-11-28 10:22:26 +08:00
|
|
|
);
|
|
|
|
slcr.mio_pin_12.write(
|
|
|
|
slcr::MioPin12::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
2020-04-03 06:17:25 +08:00
|
|
|
.pullup(true)
|
2019-11-28 10:22:26 +08:00
|
|
|
);
|
|
|
|
slcr.mio_pin_13.write(
|
|
|
|
slcr::MioPin13::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
2020-04-03 06:17:25 +08:00
|
|
|
.pullup(true)
|
2019-11-28 10:22:26 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Option: Add Feedback Output Clock
|
|
|
|
// 7. Configure MIO pin 8 for feedback clock.
|
|
|
|
slcr.mio_pin_08.write(
|
|
|
|
slcr::MioPin08::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
2020-04-03 06:17:25 +08:00
|
|
|
.pullup(true)
|
2019-11-28 10:22:26 +08:00
|
|
|
);
|
2019-11-23 08:59:24 +08:00
|
|
|
});
|
2019-11-21 07:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reset() {
|
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
slcr.lqspi_rst_ctrl.write(
|
|
|
|
slcr::LqspiRstCtrl::zeroed()
|
|
|
|
.ref_rst(true)
|
|
|
|
.cpu1x_rst(true)
|
|
|
|
);
|
|
|
|
slcr.lqspi_rst_ctrl.write(
|
|
|
|
slcr::LqspiRstCtrl::zeroed()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:59:24 +08:00
|
|
|
fn configure(&mut self, divider: u32) {
|
2019-12-05 06:56:38 +08:00
|
|
|
// Disable
|
|
|
|
self.regs.enable.write(
|
|
|
|
regs::Enable::zeroed()
|
|
|
|
);
|
2019-12-03 09:41:49 +08:00
|
|
|
self.disable_interrupts();
|
2019-12-05 06:56:38 +08:00
|
|
|
self.regs.lqspi_cfg.write(
|
|
|
|
regs::LqspiCfg::zeroed()
|
|
|
|
);
|
2019-12-03 09:41:49 +08:00
|
|
|
self.clear_rx_fifo();
|
2019-12-05 06:56:38 +08:00
|
|
|
self.clear_interrupt_status();
|
2019-12-03 09:41:49 +08:00
|
|
|
|
2019-11-23 08:59:24 +08:00
|
|
|
// for a baud_rate_div=1 LPBK_DLY_ADJ would be required
|
|
|
|
let mut baud_rate_div = 2u32;
|
|
|
|
while baud_rate_div < 7 && 2u32.pow(1 + baud_rate_div) < divider {
|
|
|
|
baud_rate_div += 1;
|
|
|
|
}
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
self.regs.config.write(regs::Config::zeroed()
|
2019-11-23 08:59:24 +08:00
|
|
|
.baud_rate_div(baud_rate_div as u8)
|
2019-11-21 07:14:09 +08:00
|
|
|
.mode_sel(true)
|
|
|
|
.leg_flsh(true)
|
2019-12-05 06:56:38 +08:00
|
|
|
.holdb_dr(true)
|
2019-12-03 09:41:49 +08:00
|
|
|
// 32 bits TX FIFO width
|
2019-11-21 07:14:09 +08:00
|
|
|
.fifo_width(0b11)
|
|
|
|
);
|
2019-12-03 09:41:49 +08:00
|
|
|
|
|
|
|
// Initialize RX/TX pipes thresholds
|
|
|
|
unsafe {
|
2019-12-10 09:46:25 +08:00
|
|
|
self.regs.rx_thres.write(1);
|
2019-12-03 09:41:49 +08:00
|
|
|
self.regs.tx_thres.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
pub fn linear_addressing_mode(self) -> Flash<LinearAddressing> {
|
|
|
|
// Set manual start enable to auto mode.
|
|
|
|
// Assert the chip select.
|
|
|
|
self.regs.config.modify(|_, w| w
|
|
|
|
.man_start_en(false)
|
|
|
|
.pcs(false)
|
2019-12-05 06:56:38 +08:00
|
|
|
.manual_cs(false)
|
2019-11-28 10:02:51 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
self.regs.lqspi_cfg.write(regs::LqspiCfg::zeroed()
|
2019-11-30 06:37:54 +08:00
|
|
|
// Quad I/O Fast Read
|
2020-04-07 04:02:10 +08:00
|
|
|
.inst_code(INST_4IO_FAST_READ)
|
|
|
|
.dummy_mask(0x2)
|
|
|
|
.mode_en(false)
|
2019-11-30 06:37:54 +08:00
|
|
|
.mode_bits(0xFF)
|
|
|
|
// 2 devices
|
|
|
|
.two_mem(true)
|
2019-12-05 06:56:38 +08:00
|
|
|
.u_page(false)
|
2020-04-07 04:02:10 +08:00
|
|
|
// Quad SPI mode
|
2019-11-23 08:59:24 +08:00
|
|
|
.lq_mode(true)
|
|
|
|
);
|
2019-11-28 10:02:51 +08:00
|
|
|
|
2019-12-05 06:56:38 +08:00
|
|
|
self.regs.enable.write(
|
|
|
|
regs::Enable::zeroed()
|
|
|
|
.spi_en(true)
|
|
|
|
);
|
2019-11-28 10:02:51 +08:00
|
|
|
|
2019-11-30 06:48:08 +08:00
|
|
|
self.transition()
|
2019-11-23 08:59:24 +08:00
|
|
|
}
|
2019-11-30 09:48:39 +08:00
|
|
|
|
|
|
|
pub fn manual_mode(self, chip_index: usize) -> Flash<Manual> {
|
|
|
|
self.regs.config.modify(|_, w| w
|
|
|
|
.man_start_en(true)
|
|
|
|
.manual_cs(true)
|
2019-12-10 09:45:05 +08:00
|
|
|
.endian(true)
|
2019-11-30 09:48:39 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
self.regs.lqspi_cfg.write(regs::LqspiCfg::zeroed()
|
2020-04-07 04:02:10 +08:00
|
|
|
// Quad I/O Fast Read
|
|
|
|
.inst_code(INST_READ)
|
|
|
|
.dummy_mask(0x2)
|
|
|
|
.mode_en(false)
|
2019-11-30 09:48:39 +08:00
|
|
|
.mode_bits(0xFF)
|
|
|
|
// 2 devices
|
|
|
|
.two_mem(true)
|
2020-04-11 02:35:42 +08:00
|
|
|
.u_page(chip_index != 0)
|
2020-04-07 04:02:10 +08:00
|
|
|
// Quad SPI mode
|
2019-12-05 06:56:38 +08:00
|
|
|
.lq_mode(false)
|
2019-11-30 09:48:39 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
self.transition()
|
|
|
|
}
|
2019-11-28 10:02:51 +08:00
|
|
|
}
|
2019-11-23 08:59:24 +08:00
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
impl Flash<LinearAddressing> {
|
2019-11-30 06:48:08 +08:00
|
|
|
/// Stop linear addressing mode
|
|
|
|
pub fn stop(self) -> Flash<()> {
|
|
|
|
self.regs.enable.modify(|_, w| w.spi_en(false));
|
|
|
|
// De-assert chip select.
|
|
|
|
self.regs.config.modify(|_, w| w.pcs(true));
|
|
|
|
|
|
|
|
self.transition()
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:59:24 +08:00
|
|
|
pub fn ptr<T>(&mut self) -> *mut T {
|
|
|
|
0xFC00_0000 as *mut _
|
|
|
|
}
|
2019-11-30 06:48:08 +08:00
|
|
|
|
|
|
|
pub fn size(&self) -> usize {
|
2019-11-30 09:48:39 +08:00
|
|
|
2 * (SINGLE_CAPACITY as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Flash<Manual> {
|
|
|
|
pub fn stop(self) -> Flash<()> {
|
|
|
|
self.transition()
|
|
|
|
}
|
|
|
|
|
2019-12-14 08:55:17 +08:00
|
|
|
pub fn read_reg<R: SpiFlashRegister>(&mut self) -> R {
|
|
|
|
let args = Some(R::inst_code());
|
2019-12-16 06:52:47 +08:00
|
|
|
let transfer = self.transfer(args.into_iter(), 2)
|
|
|
|
.bytes_transfer();
|
|
|
|
R::new(transfer.skip(1).next().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_reg_until<R, F, A>(&mut self, f: F) -> A
|
|
|
|
where
|
|
|
|
R: SpiFlashRegister,
|
|
|
|
F: Fn(R) -> Option<A>,
|
|
|
|
{
|
|
|
|
let mut result = None;
|
|
|
|
while result.is_none() {
|
|
|
|
let args = Some(R::inst_code());
|
|
|
|
for b in self.transfer(args.into_iter(), 32)
|
|
|
|
.bytes_transfer().skip(1) {
|
|
|
|
result = f(R::new(b));
|
|
|
|
|
|
|
|
if result.is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Status Register-1 remains `0x00` immediately after invoking a command.
|
|
|
|
fn wait_while_sr1_zeroed(&mut self) -> SR1 {
|
|
|
|
self.read_reg_until::<SR1, _, SR1>(|sr1|
|
|
|
|
if sr1.is_zeroed() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(sr1)
|
|
|
|
}
|
|
|
|
)
|
2019-12-12 08:02:09 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 08:55:17 +08:00
|
|
|
/// Read Identification
|
2019-12-12 07:17:34 +08:00
|
|
|
pub fn rdid(&mut self) -> core::iter::Skip<BytesTransfer<Transfer<core::option::IntoIter<u32>, u32>>> {
|
2019-12-10 09:50:44 +08:00
|
|
|
let args = Some((INST_RDID as u32) << 24);
|
|
|
|
self.transfer(args.into_iter(), 0x44)
|
2019-12-05 08:15:14 +08:00
|
|
|
.bytes_transfer().skip(1)
|
2019-12-05 06:56:38 +08:00
|
|
|
}
|
|
|
|
|
2019-12-10 09:50:44 +08:00
|
|
|
/// Read flash data
|
2019-12-12 07:17:34 +08:00
|
|
|
pub fn read(&mut self, offset: u32, len: usize
|
|
|
|
) -> core::iter::Take<core::iter::Skip<BytesTransfer<Transfer<core::option::IntoIter<u32>, u32>>>>
|
|
|
|
{
|
|
|
|
let args = Some(((INST_READ as u32) << 24) | (offset as u32));
|
2019-12-10 09:50:44 +08:00
|
|
|
self.transfer(args.into_iter(), len + 6)
|
|
|
|
.bytes_transfer().skip(6).take(len)
|
|
|
|
}
|
|
|
|
|
2019-12-16 07:48:39 +08:00
|
|
|
pub fn erase(&mut self, offset: u32) {
|
|
|
|
let args = Some(((INST_BE_4K as u32) << 24) | (offset as u32));
|
|
|
|
self.transfer(args.into_iter(), 4);
|
|
|
|
|
|
|
|
let sr1 = self.wait_while_sr1_zeroed();
|
|
|
|
|
|
|
|
if sr1.e_err() {
|
2020-05-01 07:45:52 +08:00
|
|
|
error!("E_ERR");
|
2019-12-16 07:48:39 +08:00
|
|
|
} else if sr1.p_err() {
|
2020-05-01 07:45:52 +08:00
|
|
|
error!("P_ERR");
|
2019-12-16 07:48:39 +08:00
|
|
|
} else if sr1.wip() {
|
2020-05-01 07:45:52 +08:00
|
|
|
info!("Erase in progress");
|
2019-12-16 07:48:39 +08:00
|
|
|
while self.read_reg::<SR1>().wip() {
|
|
|
|
print!(".");
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
} else {
|
2020-05-01 07:45:52 +08:00
|
|
|
warn!("erased? sr1={:02X}", sr1.inner);
|
2019-12-16 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn program<I: Iterator<Item=u32>>(&mut self, offset: u32, data: I) {
|
|
|
|
{
|
|
|
|
let len = 4 + 4 * data.size_hint().0;
|
2019-12-22 10:14:18 +08:00
|
|
|
let args = Some(SpiWord::W32(((INST_PP as u32) << 24) | (offset as u32))).into_iter()
|
2019-12-16 07:48:39 +08:00
|
|
|
.chain(data.map(SpiWord::W32));
|
|
|
|
self.transfer(args, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// let sr1 = self.wait_while_sr1_zeroed();
|
|
|
|
let sr1 = self.read_reg::<SR1>();
|
|
|
|
|
|
|
|
if sr1.e_err() {
|
2020-05-01 07:45:52 +08:00
|
|
|
error!("E_ERR");
|
2019-12-16 07:48:39 +08:00
|
|
|
} else if sr1.p_err() {
|
2020-05-01 07:45:52 +08:00
|
|
|
error!("P_ERR");
|
2019-12-16 07:48:39 +08:00
|
|
|
} else if sr1.wip() {
|
2020-05-01 07:45:52 +08:00
|
|
|
info!("Program in progress");
|
2019-12-16 07:48:39 +08:00
|
|
|
while self.read_reg::<SR1>().wip() {
|
|
|
|
print!(".");
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
} else {
|
2020-05-01 07:45:52 +08:00
|
|
|
warn!("programmed? sr1={:02X}", sr1.inner);
|
2019-12-16 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 08:56:49 +08:00
|
|
|
pub fn write_enabled<F: Fn(&mut Self) -> R, R>(&mut self, f: F) -> R {
|
|
|
|
// Write Enable
|
|
|
|
let args = Some(INST_WREN);
|
|
|
|
self.transfer(args.into_iter(), 1);
|
|
|
|
self.regs.gpio.modify(|_, w| w.wp_n(true));
|
2019-12-16 06:52:47 +08:00
|
|
|
let sr1 = self.wait_while_sr1_zeroed();
|
|
|
|
if !sr1.wel() {
|
|
|
|
panic!("Cannot write-enable flash");
|
|
|
|
}
|
2019-12-14 08:56:49 +08:00
|
|
|
|
|
|
|
let result = f(self);
|
|
|
|
|
|
|
|
// Write Disable
|
|
|
|
let args = Some(INST_WRDI);
|
|
|
|
self.transfer(args.into_iter(), 1);
|
|
|
|
self.regs.gpio.modify(|_, w| w.wp_n(false));
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-12-12 07:17:34 +08:00
|
|
|
pub fn transfer<'s: 't, 't, Args, W>(&'s mut self, args: Args, len: usize) -> Transfer<'t, Args, W>
|
2019-12-05 08:15:14 +08:00
|
|
|
where
|
2019-12-12 07:17:34 +08:00
|
|
|
Args: Iterator<Item = W>,
|
|
|
|
W: Into<SpiWord>,
|
2019-12-05 08:15:14 +08:00
|
|
|
{
|
2019-12-10 09:50:44 +08:00
|
|
|
Transfer::new(self, args, len)
|
2019-12-05 06:56:38 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 08:07:46 +08:00
|
|
|
pub fn dump(&mut self, label: &'_ str, inst_code: u8) {
|
|
|
|
print!("{}:", label);
|
|
|
|
|
|
|
|
let args = Some(u32::from(inst_code) << 24);
|
|
|
|
for b in self.transfer(args.into_iter(), 32).bytes_transfer() {
|
|
|
|
print!(" {:02X}", b);
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|
|
|
|
}
|