Building for microcontrollers #906
Replies: 3 comments 14 replies
-
|
Ah, success! Apparently, building deps first breaks a lot of the linking magic that embedded projects need. By explicitly setting |
Beta Was this translation helpful? Give feedback.
-
|
Ok @dpc, so if I don't set cargoArtifacts to null - I get the error Which has to do with that linker script not being accessible to the linker during the deps build. This makes sense, I suppose, as the deps don't include my source which contains memory.x. However, linking won't work because the partial linker script provided by the cortex-m-rt crate need a memory map. I can hack this a bit by adding preBuild = ''
cp ${src}/memory.x memory.x
'';to the which if I'm understanding this correctly is due to missing the panic handler. This is set up in the application with use panic_probe as _;but of course, that's not in the deps-only build. So, I'm not surprised the deps fail to build when they don't have all the information. Embedded, no_std stuff seems like it needs the whole context. For the rest of the details, here's my .cargo/config.toml for the linker args [target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "probe-rs run --chip RP235x"
rustflags = [
"-C", "linker=flip-link",
"-C", "link-arg=-Tlink.x",
"-C", "link-arg=-Tdefmt.x",
"-Z", "trap-unreachable=no",
"-C", "link-arg=--nmagic",
] |
Beta Was this translation helpful? Give feedback.
-
|
@kiranshila I dug into this and I think I figured out the issue and a path forward:
So here's how to make your project build:
diff --git a/flake.nix b/flake.nix
index a824345b1a..49807c1a46 100644
--- a/flake.nix
+++ b/flake.nix
@@ -69,7 +69,31 @@
asp_link = craneLib.buildPackage (commonArgs
// {
- cargoArtifacts = null;
+ cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {
+ preBuild = ''
+ cp ${./memory.x} ./memory.x
+ cp ${./build.rs} ./build.rs
+ '';
+ dummyBuildrs = "./build.rs";
+ dummyrs = pkgs.writeText "myDummy.rs" ''
+ #![no_std]
+ #![no_main]
+ #![feature(impl_trait_in_assoc_type)]
+
+ use defmt_rtt as _;
+ use panic_probe as _;
+
+ #[embassy_executor::task]
+ async fn feed_watchdog(_wd: embassy_rp::watchdog::Watchdog) {
+ loop {}
+ }
+
+ #[embassy_executor::main]
+ async fn main(_s: embassy_executor::Spawner) -> ! {
+ loop {}
+ }
+ '';
+ });
postInstall = ''
elf2flash convert --board rp2350 $out/bin/asp_link $out/bin/asp_link.uf2
'';Hope this helps! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone,
I use crane a lot, but am running into issues trying to get it to build a microcontroller binary. These certainly are weird because of linker nonsense + no_std.
I spent a few hours trying to get crane to build me a binary, but no such luck.
My repo is here. I'm been reading the likes of
But cannot find a magic combination that makes crane happy.
cargo build --releasein the dev shell works perfectly fine, though.I don't think I want to use nix's cross-comp infra here (correct me if I'm wrong), so I was just trying to build a standard-looking crane package. My understanding is I would need nix's cross comp stuff and the callPackage magic if I were linking in code, which I am obviously not for microcontroller firmware.
I needed to modify the src to include the memory.x linker script that is quite standard for MCU builds.
Here is the flake
Trying to build gives a linker error, but I think that's because the
cargoArtifactsaren't being built for the target I'm setting. So, any help would be appreciated.Beta Was this translation helpful? Give feedback.
All reactions