Skip to content

Commit ca929cc

Browse files
committed
Adapt examples to RISC-V rework
1 parent e61f99f commit ca929cc

File tree

14 files changed

+68
-67
lines changed

14 files changed

+68
-67
lines changed

e310x-hal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ embedded-io = "0.6.1"
1717
e310x = { path = "../e310x", version = "0.12.0", features = ["rt", "critical-section"] }
1818
nb = "1.0.0"
1919
portable-atomic = { version = "1.9", default-features = false}
20-
riscv = { version = "0.12.1", features = ["critical-section-single-hart"] }
20+
riscv = { version = "0.14.0", features = ["critical-section-single-hart"] }
2121

2222
[features]
2323
g002 = ["e310x/g002"]

e310x/Cargo.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ edition = "2021"
1212

1313
[dependencies]
1414
critical-section = { version = "1.2.0", optional = true }
15-
riscv = { git = "https://github.com/rust-embedded/riscv.git", branch = "riscv-peripheral-rework" }
16-
riscv-peripheral = { git = "https://github.com/rust-embedded/riscv.git", branch = "riscv-peripheral-rework" }
17-
riscv-rt = { git = "https://github.com/rust-embedded/riscv.git", branch = "riscv-peripheral-rework", features = ["no-interrupts"], optional = true }
18-
# riscv = "0.13.0"
19-
# riscv-peripheral = "0.2.0"
20-
# riscv-rt = { version = "0.14.0", features = ["no-interrupts"], optional = true }
15+
riscv = "0.14.0"
16+
riscv-peripheral = "0.3.0"
17+
riscv-rt = { version = "0.15.0", features = ["no-interrupts"], optional = true }
2118
vcell = "0.1.3"
2219

2320
[features]

hifive1-examples/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ rust-version = "1.72"
1515
[dependencies]
1616
critical-section = { version = "1.2.0" }
1717
hifive1 = { path = "../hifive1", version = "0.13.0", features = ["board-hifive1-revb"] } # Change to your board
18-
riscv = { version = "0.13.0" }
19-
riscv-rt = { version = "0.14.0", features = ["single-hart"] }
18+
riscv = { version = "0.14.0" }
19+
riscv-rt = { version = "0.15.0", features = ["single-hart"] }
2020
panic-halt = "1.0.0"
2121
semihosting = { version = "0.1", features = ["stdio", "panic-handler"] }
22-
# max3010x = "0.2.0" # TODO uncomment when the driver is published
23-
max3010x = { git = "https://github.com/eldruin/max3010x-rs.git" }
22+
max3010x = "0.2.0"
2423
mfrc522 = "0.8.0"
2524

2625
[features]

hifive1-examples/examples/button_poll.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Example of polling a button and turning on a LED when the button is pressed.
1+
//! Example of polling a button and turning on an LED when the button is pressed.
22
//!
33
//! # Hardware
44
//!
@@ -10,22 +10,23 @@
1010

1111
use hifive1::{
1212
clock,
13-
hal::{e310x::CLINT, prelude::*, DeviceResources},
14-
pin, sprintln, Led,
13+
hal::{prelude::*, DeviceResources},
14+
pin, sprintln, stdout, Led,
1515
};
1616
extern crate panic_halt;
1717

1818
#[riscv_rt::entry]
1919
fn main() -> ! {
2020
let dr = DeviceResources::take().unwrap();
21+
let cp = dr.core_peripherals;
2122
let p = dr.peripherals;
2223
let pins = dr.pins;
2324

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

2728
// Configure UART for stdout
28-
hifive1::stdout::configure(
29+
stdout::configure(
2930
p.UART0,
3031
pin!(pins, uart0_tx),
3132
pin!(pins, uart0_rx),
@@ -40,8 +41,8 @@ fn main() -> ! {
4041
let pin = pin!(pins, led_blue);
4142
let mut led = pin.into_inverted_output();
4243

43-
// Get the sleep struct from CLINT
44-
let mut sleep = CLINT::delay();
44+
// Get the MTIMER peripheral from CLINT
45+
let mut mtimer = cp.clint.mtimer();
4546

4647
const STEP: u32 = 1000; // 1s
4748
loop {
@@ -53,6 +54,6 @@ fn main() -> ! {
5354
led.off();
5455
}
5556
sprintln!("LED is on: {}", led.is_on());
56-
sleep.delay_ms(STEP);
57+
mtimer.delay_ms(STEP);
5758
}
5859
}

hifive1-examples/examples/hello_world.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
//! Prints "hello world!" to the host console.
2-
//!
3-
//! If "semihosting" feature is enabled, the message is printed using semihosting.
4-
//! Otherwise, the message is printed using the UART0 peripheral.
1+
//! Prints "hello world!" to the host console using the UART0 peripheral.
52
63
#![no_std]
74
#![no_main]
85

96
extern crate panic_halt;
107
use hifive1::{
8+
clock,
119
hal::{prelude::*, DeviceResources},
12-
pin, sprintln as println,
10+
pin, sprintln, stdout,
1311
};
1412

1513
#[riscv_rt::entry]
@@ -19,18 +17,18 @@ fn main() -> ! {
1917
let pins = dr.pins;
2018

2119
// Configure clocks
22-
let clocks = hifive1::clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());
20+
let clocks = clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());
2321

2422
// Configure UART for stdout
25-
hifive1::stdout::configure(
23+
stdout::configure(
2624
p.UART0,
2725
pin!(pins, uart0_tx),
2826
pin!(pins, uart0_rx),
2927
115_200.bps(),
3028
clocks,
3129
);
3230

33-
println!("Hello, world!");
31+
sprintln!("Hello, world!");
3432
loop {
3533
riscv::asm::wfi();
3634
}

hifive1-examples/examples/i2c_max3010x.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use hifive1::{
88
clock,
99
hal::{
10-
e310x::CLINT,
1110
i2c::{I2c, Speed},
1211
prelude::*,
1312
DeviceResources,
@@ -20,6 +19,7 @@ extern crate panic_halt;
2019
#[riscv_rt::entry]
2120
fn main() -> ! {
2221
let dr = DeviceResources::take().unwrap();
22+
let cp = dr.core_peripherals;
2323
let p = dr.peripherals;
2424
let pins = dr.pins;
2525

@@ -50,12 +50,12 @@ fn main() -> ! {
5050

5151
let mut data = [0; 3];
5252

53-
// Get the sleep struct from CLINT
54-
let mut sleep = CLINT::delay();
53+
// Get the MTIMER peripheral from CLINT
54+
let mut mtimer = cp.clint.mtimer();
5555
const STEP: u32 = 1000; // 1s
5656
loop {
5757
let samples_read = sensor.read_fifo(&mut data).unwrap();
5858
sprintln!("Samples read: {}", samples_read);
59-
sleep.delay_ms(STEP);
59+
mtimer.delay_ms(STEP);
6060
}
6161
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
//! Basic blinking LEDs example using mtime/mtimecmp registers for "sleep" in a loop.
2-
//! Blinks each led once and goes to the next one.
1+
//! Basic blinking LED example using mtime/mtimecmp registers for "sleep" in a loop.
2+
//! Blinks the blue LED of RED-V board.
33
44
#![no_std]
55
#![no_main]
66

77
use hifive1::{
88
clock,
9-
hal::{e310x::CLINT, prelude::*, DeviceResources},
10-
pin, sprintln, Led,
9+
hal::{prelude::*, DeviceResources},
10+
pin, sprintln, stdout, Led,
1111
};
1212
extern crate panic_halt;
1313

1414
#[riscv_rt::entry]
1515
fn main() -> ! {
1616
let dr = DeviceResources::take().unwrap();
17+
let cp = dr.core_peripherals;
1718
let p = dr.peripherals;
1819
let pins = dr.pins;
1920

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

2324
// Configure UART for stdout
24-
hifive1::stdout::configure(
25+
stdout::configure(
2526
p.UART0,
2627
pin!(pins, uart0_tx),
2728
pin!(pins, uart0_rx),
@@ -33,13 +34,13 @@ fn main() -> ! {
3334
let pin = pin!(pins, led_blue);
3435
let mut led = pin.into_inverted_output();
3536

36-
// Get the sleep struct from CLINT
37-
let mut sleep = CLINT::delay();
37+
// Get the MTIMER peripheral from CLINT
38+
let mut mtimer = cp.clint.mtimer();
3839

3940
const STEP: u32 = 1000; // 1s
4041
loop {
4142
Led::toggle(&mut led);
4243
sprintln!("LED toggled. New state: {}", led.is_on());
43-
sleep.delay_ms(STEP);
44+
mtimer.delay_ms(STEP);
4445
}
4546
}

hifive1-examples/examples/led_pwm.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99

1010
use hifive1::{
1111
clock,
12-
hal::{e310x::CLINT, prelude::*, DeviceResources},
13-
pin, sprintln,
12+
hal::{prelude::*, DeviceResources},
13+
pin, sprintln, stdout,
1414
};
1515
extern crate panic_halt;
1616

1717
#[riscv_rt::entry]
1818
fn main() -> ! {
1919
let dr = DeviceResources::take().unwrap();
20+
let cp = dr.core_peripherals;
2021
let p = dr.peripherals;
2122
let pins = dr.pins;
2223

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

2627
// Configure UART for stdout
27-
hifive1::stdout::configure(
28+
stdout::configure(
2829
p.UART0,
2930
pin!(pins, uart0_tx),
3031
pin!(pins, uart0_rx),
@@ -40,8 +41,8 @@ fn main() -> ! {
4041

4142
let mut channel = pwm0.channel(pin);
4243

43-
// Get the sleep struct from CLINT
44-
let mut sleep = CLINT::delay();
44+
// Get the MTIMER peripheral from CLINT
45+
let mut mtimer = cp.clint.mtimer();
4546

4647
const STEP: u32 = 1000; // 1s
4748
const DUTY_DELTA: u8 = 32;
@@ -52,6 +53,6 @@ fn main() -> ! {
5253
channel.set_duty_cycle(duty as u16).unwrap();
5354
duty = duty.wrapping_add(DUTY_DELTA);
5455

55-
sleep.delay_ms(STEP);
56+
mtimer.delay_ms(STEP);
5657
}
5758
}

hifive1-examples/examples/rgb_blink.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@
66

77
use hifive1::{
88
clock,
9-
hal::{e310x::CLINT, prelude::*, DeviceResources},
10-
pin, pins, sprintln, Led,
9+
hal::{prelude::*, DeviceResources},
10+
pin, pins, sprintln, stdout, Led,
1111
};
1212
extern crate panic_halt;
1313

1414
#[riscv_rt::entry]
1515
fn main() -> ! {
1616
let dr = DeviceResources::take().unwrap();
17+
let cp = dr.core_peripherals;
1718
let p = dr.peripherals;
1819
let pins = dr.pins;
1920

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

2324
// Configure UART for stdout
24-
hifive1::stdout::configure(
25+
stdout::configure(
2526
p.UART0,
2627
pin!(pins, uart0_tx),
2728
pin!(pins, uart0_rx),
@@ -35,15 +36,15 @@ fn main() -> ! {
3536
// get leds as the Led trait in an array so we can index them
3637
let mut ileds: [&mut dyn Led; 3] = [&mut tleds.0, &mut tleds.1, &mut tleds.2];
3738

38-
// Get the sleep struct from CLINT
39-
let mut sleep = CLINT::delay();
39+
// Get the MTIMER peripheral from CLINT
40+
let mut mtimer = cp.clint.mtimer();
4041

4142
const STEP: u32 = 1000; // 1s
4243
loop {
4344
for (i, led) in ileds.iter_mut().enumerate() {
4445
led.toggle().unwrap();
4546
sprintln!("LED {} toggled. New state: {}", i, led.is_on());
46-
sleep.delay_ms(STEP);
47+
mtimer.delay_ms(STEP);
4748
}
4849
}
4950
}

hifive1-examples/examples/sh_led_blink.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66

77
use hifive1::{
88
clock,
9-
hal::{e310x::CLINT, prelude::*, DeviceResources},
9+
hal::{prelude::*, DeviceResources},
1010
pin, Led,
1111
};
1212
use semihosting::{println, process::exit};
1313

1414
#[riscv_rt::entry]
1515
fn main() -> ! {
1616
let dr = DeviceResources::take().unwrap();
17+
let cp = dr.core_peripherals;
1718
let p = dr.peripherals;
1819
let pins = dr.pins;
1920

2021
// Configure clocks
21-
let clocks = clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());
22+
clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());
2223

2324
// get all 3 led pins in a tuple (each pin is it's own type here)
2425
let pin = pin!(pins, led_blue);
2526
let mut led = pin.into_inverted_output();
2627

27-
// Get the sleep struct from CLINT
28-
let mut sleep = CLINT::delay();
28+
// Get the MTIMER peripheral from CLINT
29+
let mut mtimer = cp.clint.mtimer();
2930

3031
const N_TOGGLE: usize = 4;
3132
const STEP: u32 = 500; // 500 ms
@@ -34,7 +35,7 @@ fn main() -> ! {
3435
for _ in 0..N_TOGGLE {
3536
Led::toggle(&mut led);
3637
println!("LED toggled. New state: {}", led.is_on());
37-
sleep.delay_ms(STEP);
38+
mtimer.delay_ms(STEP);
3839
}
3940
println!("Done toggling LED");
4041
exit(0);

0 commit comments

Comments
 (0)