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