From 7fb80a8b34473f81392303fc167819849a3899ff Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Tue, 25 Jun 2024 03:46:19 +0200 Subject: Initial commit! --- .cargo/config.toml | 9 +++++++ .gitignore | 1 + Cargo.toml | 15 ++++++++++++ Embed.toml | 12 ++++++++++ LICENSE | 21 +++++++++++++++++ README.org | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ build.rs | 15 ++++++++++++ dev-shell.sh | 11 +++++++++ manifest.scm | 17 ++++++++++++++ src/add_one.c | 1 + src/main.rs | 37 +++++++++++++++++++++++++++++ 11 files changed, 208 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 Embed.toml create mode 100644 LICENSE create mode 100644 README.org create mode 100644 build.rs create mode 100755 dev-shell.sh create mode 100644 manifest.scm create mode 100644 src/add_one.c create mode 100644 src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..7634ede --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,9 @@ +[target.thumbv6m-none-eabi] +runner = "arm-none-eabi-gdb" +rustflags = [ + "-C", "link-arg=-Tlink.x", + # "-C", "linker=arm-none-eabi-ld", +] + +[build] +target = "thumbv6m-none-eabi" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7829f31 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +authors = ["{{authors}}"] +edition = "2018" +readme = "README.md" +name = "{{project-name}}" +version = "0.1.0" + +[dependencies] +panic-halt = "0.2" +microbit="0.12.0" +cortex-m-rt="0.7.0" +rtt-target = { version = "0.3.1", features = ["cortex-m"] } + +[build-dependencies] +cc = "1.0" diff --git a/Embed.toml b/Embed.toml new file mode 100644 index 0000000..b299033 --- /dev/null +++ b/Embed.toml @@ -0,0 +1,12 @@ +[default.general] +# chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2 +chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1 + +[default.reset] +halt_afterwards = false + +[default.rtt] +enabled = false + +[default.gdb] +enabled = false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1e691a7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 jdlugosz963 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.org b/README.org new file mode 100644 index 0000000..2d6027a --- /dev/null +++ b/README.org @@ -0,0 +1,69 @@ +#+TITLE: Rust microbit guix template + +* What's that? +- If you are a hipster like me, and you want to hack on your microbit + with Rust using a Guix machine, then you couldn't find better + place!! I prepared this repo especially for that case, so you can + hop straight to concrete! +- If you ever use this, let me know. It took some time before I + figured out how to do this XD. + +* How to run? +- To pull this repo, you can use =cargo generete=, like this: + #+begin_src sh + cargo generate --git {{this repo url}} + #+end_src + +- You need to have a rustup guix channel pulled: + #+begin_src scheme + ;; ~/.config/guix/channels.scm + (cons (channel + (name 'rustup) + (url "https://github.com/declantsien/guix-rustup") + (introduction + (make-channel-introduction + "325d3e2859d482c16da21eb07f2c6ff9c6c72a80" + (openpgp-fingerprint + "F695 F39E C625 E081 33B5 759F 0FC6 8703 75EF E2F5")))) + %default-channels) + #+end_src + + #+begin_src sh + guix pull + #+end_src + +- Then simply run =./dev-shell.sh= and whole dev environment should be + working! + +- To compile and write program on microbit, you can run this command: =cargo embed= + +* Next +- You can learn more here: [[https://docs.rust-embedded.org/discovery/microbit/index.html][Rust embedded discovery book]] + +* Extra +- At the time I wrote this and everything was working fine, I was using these channels: + #+begin_src scheme + ;; ~/.config/guix/channels.scm + (list (channel + (name 'rustup) + (url "https://github.com/declantsien/guix-rustup") + (branch "master") + (commit + "190a3e434eea18135a9bb44a7bc61ca023d72dbe") + (introduction + (make-channel-introduction + "325d3e2859d482c16da21eb07f2c6ff9c6c72a80" + (openpgp-fingerprint + "F695 F39E C625 E081 33B5 759F 0FC6 8703 75EF E2F5")))) + (channel + (name 'guix) + (url "https://git.savannah.gnu.org/git/guix.git") + (branch "master") + (commit + "027a35dff89cbb35512afee4f4a78ae103a2f484") + (introduction + (make-channel-introduction + "9edb3f66fd807b096b48283debdcddccfea34bad" + (openpgp-fingerprint + "BBB0 2DDF 2CEA F6A8 0D1D E643 A2A0 6DF2 A33A 54FA"))))) + #+end_src diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..595c920 --- /dev/null +++ b/build.rs @@ -0,0 +1,15 @@ +use std::env; +use cc; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + + cc::Build::new() + .file("src/add_one.c") + .compile("add_one"); + + + println!("cargo::rustc-link-search=native={}", out_dir); + println!("cargo::rustc-link-lib=static=add_one"); + println!("cargo::rerun-if-changed=src/add_one.c"); +} diff --git a/dev-shell.sh b/dev-shell.sh new file mode 100755 index 0000000..d6cb164 --- /dev/null +++ b/dev-shell.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env -S guix shell -m manifest.scm -- bash + +export PATH="$PATH:$(realpath ~/)/.cargo/bin" + +if ! which probe-rs; then + guix shell cmake make pkg-config eudev -- cargo install probe-rs-tools; +fi + +nohup emacs & +bash + diff --git a/manifest.scm b/manifest.scm new file mode 100644 index 0000000..c32eb44 --- /dev/null +++ b/manifest.scm @@ -0,0 +1,17 @@ +(use-modules (gnu packages embedded) + (rustup build toolchain)) + +(concatenate-manifests + (list (packages->manifest + (list (rustup #:targets (list "thumbv6m-none-eabi")) + (make-arm-none-eabi-toolchain-7-2018-q2-update) + (make-gdb-arm-none-eabi))) + (specifications->manifest + (list "gcc-toolchain" + "minicom" + "rust-analyzer" + "emacs" + "emacs-rust-mode" + "emacs-dap-mode" + "emacs-flycheck-rust")))) + diff --git a/src/add_one.c b/src/add_one.c new file mode 100644 index 0000000..74853d0 --- /dev/null +++ b/src/add_one.c @@ -0,0 +1 @@ +int c_add_one(int x) { return x + 1; } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3781a12 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,37 @@ +#![no_main] +#![no_std] + +use core::{fmt::Write, ffi::c_int}; +use cortex_m_rt::entry; +use microbit::{ + hal::uart::{Uart, Baudrate, Parity}, + Board, +}; + +extern crate panic_halt; +extern crate microbit; +extern "C" { + fn c_add_one(x: c_int) -> c_int; +} + +pub fn add_one(x: i32) -> i32 { + unsafe { + c_add_one(x) + } +} + +#[entry] +fn main() -> ! { + let board = Board::take().unwrap(); + let mut serial = Uart::new( + board.UART0, + board.uart.into(), + Parity::EXCLUDED, + Baudrate::BAUD115200, + ); + + let ten_plus_one = add_one(10); + writeln!(serial, "10 + 1 = {}\r", ten_plus_one).unwrap(); + + loop {} +} -- cgit v1.2.3