Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare to adding DMA support #8

Merged
merged 10 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ jobs:
cd ../hpm-data/build/
git clone https://github.com/hpmicro-rs/hpm-metapac.git
- name: Build Only
run: cargo build --release --examples
run:
for d in $(ls -1 ./examples); do
(cd ./examples/$d && cargo build --release)
done
47 changes: 14 additions & 33 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ license = "MIT/Apache-2.0"
[dependencies]
# path = "../hpm-data/build/hpm-metapac",
# git = "https://github.com/hpmicro-rs/hpm-metapac.git",
hpm-metapac = { version = "0.0.3", path = "../hpm-data/build/hpm-metapac", features = [
"rt",
# temp feature for demo
# "memory-x",

] }
hpm-metapac = { version = "0.0.3", path = "../hpm-data/build/hpm-metapac" }

riscv = { version = "0.11", features = ["critical-section-single-hart"] }
embedded-hal = { version = "1.0.0" }
Expand Down Expand Up @@ -55,12 +50,18 @@ proc-macro2 = "1.0.85"
quote = "1.0.15"

[features]
default = ["hpm5361", "rt", "embassy", "defmt", "time"]
rt = ["dep:riscv-rt"]
default = ["rt", "embassy", "defmt", "time"]

rt = ["dep:riscv-rt", "hpm-metapac/rt"]
defmt = ["dep:defmt"]
time = ["dep:embassy-time"]

embassy = ["dep:embassy-sync", "dep:embassy-futures", "dep:embassy-time-driver", "dep:futures-util"]
embassy = [
"dep:embassy-sync",
"dep:embassy-futures",
"dep:embassy-time-driver",
"dep:futures-util",
]

hpm5301 = ["hpm-metapac/hpm5301"]
hpm5321 = ["hpm-metapac/hpm5321"]
Expand Down Expand Up @@ -90,27 +91,7 @@ hpm6754 = ["hpm-metapac/hpm6754"]
hpm6830 = ["hpm-metapac/hpm6830"]
hpm6850 = ["hpm-metapac/hpm6850"]
hpm6880 = ["hpm-metapac/hpm6880"]
futures-util = ["dep:futures-util"]


[dev-dependencies]
panic-halt = "0.2.0"
riscv-rt = "0.12.2"

embassy-time = { version = "0.3.0", features = ["tick-hz-1_000_000"] }
embassy-executor = { version = "0.5.0", features = [
"nightly",
"integrated-timers",
"arch-riscv32",
"executor-thread",
] }
defmt = "0.3.8"
defmt-rtt = "0.4.1"
embedded-graphics = "0.8.1"


[profile.release]
strip = false # symbols are not flashed to the microcontroller, so don't strip them.
lto = true
opt-level = "z" # Optimize for size.
debug = 2
hpm6e50 = ["hpm-metapac/hpm6e50"]
hpm6e60 = ["hpm-metapac/hpm6e60"]
hpm6e70 = ["hpm-metapac/hpm6e70"]
hpm6e80 = ["hpm-metapac/hpm6e80"]
4 changes: 2 additions & 2 deletions HPMicro.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ flash_algorithms:
- name: algorithm-test
description: A flash algorithm under test
default: true
instructions: EwEB3CMuESIjLIEiIyqRIiMoISO3BQAABUV9Fo1GI4SlSGN51gy3FQD0A6AFgH1WI6DFgAOgBYEjqMWAI6KlkGgAEwYAEIFFlwAAAOeAIBsjLgEQIywBEDcF+fwJBSMmoRAZRSMooRAFZSMqoRA3BQIgAyVF8XRF0cI3BQDzbABwAjcJAPOCliqEKemyRCMAAQQMCAgSEwbAD5cAAADngOApaAxsAlFGlwAAAOeAACk3BQAAIyiVNpMEBTcThUQADBITBgARlwAAAOeAICcjqiQRNwUAAIVFIwS1SCKFgyDBIwMkgSODJEEjAykBIxMBASSCgJcAAADngEANlwAAAOeAwA63BQAAA8aFSAVFAcYBRSOEBUiCgLcFAACDxYVIncG3BQIgg6VF8ZxRmc+3BQAAE4YFNwMnRhG3BgCAqY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NigHmCaiqhhnGNpYjgLYAhQbj7cb+skAiREEBgoBBEQbGIsQACLJAIkRBARcDAABnAIP4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
load_address: 0x20
pc_init: 0x0
pc_uninit: 0x104
Expand All @@ -53,7 +53,7 @@ flash_algorithms:
address_range:
start: 0x80000000
end: 0x80100000
page_size: 0x100
page_size: 0x1000
erased_byte_value: 0xff
program_page_timeout: 1000
erase_sector_timeout: 2000
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This crate is a working-in-progress and not ready for use.
- [x] RTT support (defmt, defmt-rtt)
- [x] UART blocking TX, RX
- [x] I2C blocking
- [x] MBX, blocking and async
- MCUs
- HPM5300 - currently it's the only supported series

Expand Down
66 changes: 65 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::ffi::OsString;
use std::fmt::Write as _;
use std::io::Write as _;
Expand Down Expand Up @@ -166,6 +166,44 @@ fn main() {
}
}

// ========
// Generate dma_trait_impl!
let signals: HashMap<_, _> = [
// (kind, signal) => trait
(("uart", "RX"), quote!(crate::uart::RxDma)),
(("uart", "TX"), quote!(crate::uart::TxDma)),
(("i2c", "GLOBAL"), quote!(crate::i2c::I2cDma)),
]
.into();

for p in METADATA.peripherals {
if let Some(regs) = &p.registers {
let mut dupe = HashSet::new();
for ch in p.dma_channels {
if let Some(tr) = signals.get(&(regs.kind, ch.signal)) {
let peri = format_ident!("{}", p.name);

let key = (ch.signal, ch.request.unwrap().to_string());
if !dupe.insert(key) {
continue;
}

// request number for peripheral DMA
let request = ch.request.expect("DMA request must be specified") as u8;

// let channel = format_ident!("{}", ch.name);

for channel in METADATA.dma_channels {
let channel_ident = format_ident!("{}", channel.name);
g.extend(quote! {
dma_trait_impl!(#tr, #peri, #channel_ident, #request);
});
}
}
}
}
}

// ========
// Write foreach_foo! macrotables

Expand All @@ -178,6 +216,32 @@ fn main() {
pins_table.push(vec![p.name.to_string(), p.index.to_string()]);
}

let mut dmas = TokenStream::new();
for ch in METADATA.dma_channels.iter() {
let name = format_ident!("{}", ch.name);
let idx = ch.channel as u8;

let ch_num = ch.channel as usize;
let mux_num = ch.dmamux_channel as usize;

// HDMA or XDMA
let dma_name = format_ident!("{}", ch.name.split_once('_').expect("DMA channel name format").0);

g.extend(quote!(dma_channel_impl!(#name, #idx);));

dmas.extend(quote! {
crate::dma::ChannelInfo {
dma: crate::dma::DmaInfo::#dma_name(crate::pac::#dma_name),
num: #ch_num,
mux_num: #mux_num,
},
});
}

g.extend(quote! {
pub(crate) const DMA_CHANNELS: &[crate::dma::ChannelInfo] = &[#dmas];
});

for p in METADATA.peripherals {
let Some(regs) = &p.registers else {
continue;
Expand Down
29 changes: 0 additions & 29 deletions examples/blinky.rs

This file was deleted.

34 changes: 34 additions & 0 deletions examples/hpm5300evk/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[build]
target = "riscv32imafc-unknown-none-elf"

[target.riscv32imafc-unknown-none-elf]
# runner = 'riscv64-unknown-elf-gdb -x ../../openocd.gdb'
runner = "probe-rs run --chip HPM5361 --protocol jtag --chip-description-path ../../HPMicro.yaml"
# runner = "probe-rs run --chip HPM5361 --protocol jtag"

rustflags = [
# Target features:
# The default for imacf is is "+m,+a,+c,+f"
"-C",
"target-feature=+d,+zba,+zbb,+zbc,+zbs",
# Linker scripts:
"-C",
"link-arg=-Tmemory.x",
"-C",
"link-arg=-Tdevice.x", # __VECTORED_INTERRUPTS
"-C",
"link-arg=-Tlink-fixed.x",
"-C",
"link-arg=-Tdefmt.x",
"-C",
"link-arg=-nmagic",

# "--emit", "obj",
# "--emit", "asm",
]

[unstable]
build-std = ["core"]

[env]
DEFMT_LOG = "info"
32 changes: 32 additions & 0 deletions examples/hpm5300evk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "hpm5300evk"
version = "0.1.0"
edition = "2021"

[dependencies]
hpm-hal = { path = "../..", features = ["rt", "embassy", "hpm5361"] }

panic-halt = "0.2.0"
riscv-rt = "0.12.2"

embassy-time = { version = "0.3.0", features = ["tick-hz-1_000_000"] }
embassy-executor = { version = "0.5.0", features = [
"nightly",
"integrated-timers",
"arch-riscv32",
"executor-thread",
] }
defmt = "0.3.8"
defmt-rtt = "0.4.1"
embedded-graphics = "0.8.1"
riscv = { version = "0.11.1", features = ["critical-section-single-hart"] }
embedded-hal = "1.0.0"
embedded-io = "0.6.1"
futures-util = { version = "0.3.30", default-features = false }


[profile.release]
strip = false # symbols are not flashed to the microcontroller, so don't strip them.
lto = true
opt-level = "z" # Optimize for size.
debug = 2
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions examples/hpm5300evk/src/bin/blinky.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![no_main]
#![no_std]

use embedded_hal::delay::DelayNs;
use hal::gpio::{Level, Output, Speed};
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, riscv_rt as _};

#[hal::entry]
fn main() -> ! {
let p = hal::init(Default::default());

let mut delay = McycleDelay::new(hal::sysctl::clocks().cpu0.0);

defmt::info!("Board init!");

defmt::info!("CPU0 clock: {}Hz", hal::sysctl::clocks().cpu0.0);

// let mut led = Output::new(p.PA10, Level::Low, Speed::default());
let mut led = Output::new(p.PA23, Level::Low, Speed::default());

loop {
defmt::info!("tick");

led.toggle();
delay.delay_ms(1000);
}
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
defmt::info!("panic");
loop {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ use hpm_hal::uart::UartTx;
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _, riscv_rt as _};

const BANNER: &str = include_str!("./BANNER");

#[hal::entry]
fn main() -> ! {
let p = hal::init(Default::default());

let mut delay = McycleDelay::new(hal::sysctl::clocks().hart0.0);
let mut delay = McycleDelay::new(hal::sysctl::clocks().cpu0.0);

defmt::info!("Board init!");

let mut tx = UartTx::new_blocking(p.UART0, p.PA00, Default::default()).unwrap();

writeln!(tx, "{}", BANNER).unwrap();

tx.blocking_write(b"Hello, board!\r\n").unwrap();

writeln!(tx, "Clocks {:#?}", hal::sysctl::clocks()).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {defmt_rtt as _, hpm_hal as hal, panic_halt as _, riscv_rt as _};
fn main() -> ! {
let p = hal::init(Default::default());

let mut delay = McycleDelay::new(hal::sysctl::clocks().hart0.0);
let mut delay = McycleDelay::new(hal::sysctl::clocks().cpu0.0);

defmt::info!("Board init!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main(spawner: Spawner) -> ! {
println!("==============================");
println!(" {} clock summary", BOARD_NAME);
println!("==============================");
println!("cpu0:\t\t {}Hz", hal::sysctl::clocks().hart0.0);
println!("cpu0:\t\t {}Hz", hal::sysctl::clocks().cpu0.0);
println!("ahb:\t\t {}Hz", hal::sysctl::clocks().ahb.0);
println!("==============================");

Expand Down
Loading
Loading