Skip to content

Commit 7842675

Browse files
committed
move flc logic into hal
1 parent aac1d4e commit 7842675

File tree

9 files changed

+388
-34
lines changed

9 files changed

+388
-34
lines changed

hal/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ critical-section = "1.2.0"
2929
[features]
3030
rt = ["max78000/rt", "cortex-m-rt"]
3131
low_frequency = []
32-
flc-ram = []
32+
flc-ram = ["rt"]
33+
34+
[build-dependencies]
35+
cc = "1.2.16"

hal/build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::{env, fs::File, io::Write, path::PathBuf};
2+
3+
fn main() {
4+
let out = PathBuf::from(env::var_os("OUT_DIR").unwrap());
5+
6+
let flc_asm_path = out.join("flc_asm.s");
7+
File::create(&flc_asm_path)
8+
.unwrap()
9+
.write_all(include_bytes!("flc_asm.s"))
10+
.unwrap();
11+
12+
cc::Build::new().file(&flc_asm_path).compile("flc_asm");
13+
14+
println!("cargo:rerun-if-changed=build.rs");
15+
println!("cargo:rerun-if-changed=flc_asm.s");
16+
}
File renamed without changes.

hal/src/lib.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
//! A HAL for the Analog Devices MAX78000.
2+
//!
3+
//! # Runtime
4+
//!
5+
//! If the `rt` feature is enabled, this crate uses the runtime of the [`cortex_m_rt`]
6+
//! crate. Note that the HAL uses the [`pre_init`] hook internally, and it is not possible
7+
//! for users of the hal to specify their own `pre_init` routine.
8+
//!
9+
//! # Flash Controller
10+
//!
11+
//! If the `flc-ram` feature is enabled, this crate will expose the [`FlashController`]
12+
//! peripheral. Certain routines for flash operations need to be located in RAM (instead
13+
//! of flash memory), so users of this feature will need to add the following section to
14+
//! their linker script `link.x`:
15+
//!
16+
//! ```ld
17+
//! .analogsucks : ALIGN(4)
18+
//! {
19+
//! . = ALIGN(4);
20+
//! __sanalogsucks = .;
21+
//! *(.analogsucks .analogsucks.*);
22+
//! } > ANALOGSUCKS
23+
//! . = ALIGN(4);
24+
//! __eanalogsucks = .;
25+
//! } > ANALOGSUCKS AT>FLASH
26+
//!
27+
//! __sianalogsucks = LOADADDR(.analogsucks);
28+
//! ```
29+
//!
30+
//! where the `ANALOGSUCKS` is a memory section in RAM defined in `memory.x`.
31+
//!
32+
//! [`pre_init`]: cortex_m_rt::pre_init
33+
//! [`FlashController`]: peripherals::FlashController
234
335
#![warn(missing_docs)]
436
#![no_std]
@@ -9,7 +41,26 @@ pub use max78000;
941
pub use self::max78000::Interrupt as interrupt;
1042

1143
#[cfg(feature = "rt")]
12-
pub use cortex_m_rt::interrupt;
44+
pub use cortex_m_rt::{interrupt, pre_init};
1345

1446
pub mod communication;
1547
pub mod peripherals;
48+
49+
#[cfg(feature = "rt")]
50+
#[pre_init]
51+
unsafe fn pre_init() {
52+
// load the .analogsucks section into memory
53+
#[cfg(feature = "flc-ram")]
54+
core::arch::asm! {
55+
"ldr r0, =__sanalogsucks
56+
ldr r1, =__eanalogsucks
57+
ldr r2, =__sianalogsucks
58+
0:
59+
cmp r1, r0
60+
beq 1f
61+
ldm r2!, {{r3}}
62+
stm r0!, {{r3}}
63+
b 0b
64+
1:"
65+
}
66+
}

hal/src/peripherals/flash_controller.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,20 @@ pub enum FlashErr {
5555

5656
/// Flash Controller peripheral.
5757
pub struct FlashController<'gcr, 'icc> {
58+
#[expect(
59+
unused,
60+
reason = "the unsafe functions we call require us to hold references to these registers"
61+
)]
5862
flc: FLC,
63+
#[expect(
64+
unused,
65+
reason = "the unsafe functions we call require us to hold references to these registers"
66+
)]
5967
icc: &'icc ICC0,
68+
#[expect(
69+
unused,
70+
reason = "the unsafe functions we call require us to hold references to these registers"
71+
)]
6072
gcr: &'gcr GCR,
6173
}
6274

0 commit comments

Comments
 (0)