Skip to content

Commit 019ee60

Browse files
committed
Implement flash configuration
1 parent f052443 commit 019ee60

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

assemble.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
# remove existing blobs because otherwise this will append object files to the old blobs
6+
rm -f bin/*.a
7+
8+
riscv64-unknown-elf-gcc -ggdb3 -fdebug-prefix-map=$(pwd)=/hifive1 -c -mabi=ilp32 -march=rv32imac flash.S -o bin/flash.o
9+
riscv64-unknown-elf-ar crs bin/flash.a bin/flash.o
10+
11+
rm bin/flash.o

bin/flash.a

2.17 KB
Binary file not shown.

build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ fn main() {
4242
}
4343

4444
fs::copy("hifive1-link.x", out_dir.join("hifive1-link.x")).unwrap();
45+
46+
// Copy library with flash setup code
47+
let name = env::var("CARGO_PKG_NAME").unwrap();
48+
fs::copy("bin/flash.a", out_dir.join(format!("lib{}.a", name))).unwrap();
49+
println!("cargo:rustc-link-lib=static={}", name);
50+
println!("cargo:rerun-if-changed=bin/flash.a");
4551
}

flash.S

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.cfi_sections .debug_frame
2+
3+
.section .data._setup_is25lp
4+
.global _setup_is25lp
5+
.cfi_startproc
6+
_setup_is25lp:
7+
li a1, 0x10014000 // QSPI0 base address
8+
9+
// Disable mapped region
10+
sw zero,96(a1) // fctrl.en = 0
11+
12+
// Construct ffmt value for 4 dummy cycles
13+
li a2, 0x00BB1447
14+
15+
beqz a0, 2f
16+
17+
// We need to set 8 dummy cycles instead of 4.
18+
// Issue a "Set Read Parameters" command.
19+
20+
li a0,2
21+
sw a0,24(a1) // csmode = HOLD
22+
li a0,0xC0
23+
sw a0,72(a1) // txdata = 0xC0
24+
li a0,0xF0
25+
sw a0,72(a1) // txdata = 0xF0
26+
sw zero,24(a1) // csmode = AUTO
27+
28+
// Discard two response bytes
29+
1: lw a0,76(a1)
30+
bltz a0,1b
31+
1: lw a0,76(a1)
32+
bltz a0,1b
33+
34+
addi a2,a2,0x40 // ffmt: 4 -> 8 dummy cycles
35+
2:
36+
sw a2,100(a1) // Write ffmt
37+
38+
// Enable mapped region
39+
li a0, 1
40+
sw a0,96(a1) // fctrl.en = 1
41+
ret
42+
43+
44+
.cfi_endproc
45+
.size _setup_is25lp, . - _setup_is25lp

src/flash.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! On-board SPI Flash
2+
3+
use e310x_hal::e310x::QSPI0;
4+
use e310x_hal::clock::Clocks;
5+
6+
/// Configure SPI Flash interface to maximum supported speed
7+
#[inline(always)]
8+
pub fn configure_spi_flash(qspi: &QSPI0, clocks: &Clocks) {
9+
unsafe {
10+
extern "C" {
11+
fn _setup_is25lp(dummy8: bool);
12+
}
13+
14+
if clocks.coreclk().0 <= 208_000_000 {
15+
_setup_is25lp(false)
16+
} else {
17+
_setup_is25lp(true)
18+
}
19+
}
20+
qspi.sckdiv.modify(|_, w| unsafe { w.div().bits(0) });
21+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub use e310x_hal as hal;
88
pub mod clock;
99
pub use clock::configure as configure_clocks;
1010

11+
pub mod flash;
12+
1113
#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))]
1214
pub mod led;
1315
#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))]

0 commit comments

Comments
 (0)