From 6167bdf428110e4d8070847ef0768207ad878359 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 29 Oct 2023 11:55:35 -0400 Subject: [PATCH] build at attiny85-blink project --- Cargo.lock | 44 +++++++++++++++++++++ Cargo.toml | 1 + attiny85-blink/.cargo/config.toml | 5 +++ attiny85-blink/Cargo.toml | 10 +++++ attiny85-blink/rust-toolchain | 3 ++ attiny85-blink/src/main.rs | 65 +++++++++++++++++++++++++++++++ avr-atmega328p.json | 31 +++++++++++++++ avr-atmega32u4.json | 31 +++++++++++++++ avr-attiny85.json | 25 ++++++++++++ flake.lock | 16 ++++++++ flake.nix | 8 +++- 11 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 attiny85-blink/.cargo/config.toml create mode 100644 attiny85-blink/Cargo.toml create mode 100644 attiny85-blink/rust-toolchain create mode 100644 attiny85-blink/src/main.rs create mode 100644 avr-atmega328p.json create mode 100644 avr-atmega32u4.json create mode 100644 avr-attiny85.json diff --git a/Cargo.lock b/Cargo.lock index 4d963ce..979d6d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,6 +128,13 @@ dependencies = [ "num-traits", ] +[[package]] +name = "attiny85-blink" +version = "0.1.0" +dependencies = [ + "avr_delay", +] + [[package]] name = "autocfg" version = "0.1.8" @@ -143,6 +150,23 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "avr-config" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb2276df19ea0f25741658d218861b8a24d78eaf926127eb2384f0927f9ab9f" +dependencies = [ + "const_env--value", +] + +[[package]] +name = "avr_delay" +version = "0.4.2" +source = "git+https://github.com/avr-rust/delay/?rev=0.4.2#849918a8dfb2f6211787bd8ff69c4083d685cf54" +dependencies = [ + "avr-config", +] + [[package]] name = "backtrace" version = "0.3.69" @@ -436,6 +460,26 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f" +[[package]] +name = "const_env--value" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ade503a5095050a5bf3feb8ae71842fb49dfc4af01dd613beb4f00bb7144645" +dependencies = [ + "const_env_impl--value", +] + +[[package]] +name = "const_env_impl--value" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a046e06596453266c54d3eb790813fd047494fc1d04d5077fba628bf84e1b83a" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "cookie" version = "0.17.0" diff --git a/Cargo.toml b/Cargo.toml index 80b71cc..2acc686 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "attiny85-blink", "changeset", "config", "config-derive", diff --git a/attiny85-blink/.cargo/config.toml b/attiny85-blink/.cargo/config.toml new file mode 100644 index 0000000..58ca205 --- /dev/null +++ b/attiny85-blink/.cargo/config.toml @@ -0,0 +1,5 @@ +[build] +target = "../avr-attiny85.json" + +[unstable] +build-std = ["core"] diff --git a/attiny85-blink/Cargo.toml b/attiny85-blink/Cargo.toml new file mode 100644 index 0000000..ce652f6 --- /dev/null +++ b/attiny85-blink/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "attiny85-blink" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +avr_delay = { git = "https://github.com/avr-rust/delay/", rev = "0.4.2" } + diff --git a/attiny85-blink/rust-toolchain b/attiny85-blink/rust-toolchain new file mode 100644 index 0000000..67d9a53 --- /dev/null +++ b/attiny85-blink/rust-toolchain @@ -0,0 +1,3 @@ +[toolchain] +channel = "nightly" +profile = "minimal" diff --git a/attiny85-blink/src/main.rs b/attiny85-blink/src/main.rs new file mode 100644 index 0000000..899713c --- /dev/null +++ b/attiny85-blink/src/main.rs @@ -0,0 +1,65 @@ +#![no_std] +#![no_main] + +use avr_delay::delay_ms; + +// PORTB address is 0x38 (documentation says 0x18) +// DDRB address is 0x37 (documentation says 0x17) +// PINB address is 0x36 (documentation says 0x16) +// +// Whenever accessing a pin/port via the LD and ST instructions, add 0x20 to each address. This is +// hidden in the attiny85 datasheet in section 5.4. Section 5.2 shows a memory map in which the I/O +// registers start at address 0x20. Before that are the normal general purpose registers. +// +// Writing to a bit to a register means setting up a write mask, then using +// `core::ptr::write_volatile`. Watch for the `ops::Bit...` traits to help outwith bit operations. + +trait Register { + type T; + const ADDRESS: *mut Self::T; + + fn write(value: Self::T) { + unsafe { + core::ptr::write_volatile(Self::ADDRESS, value); + } + } + + fn read() -> Self::T { + unsafe { core::ptr::read_volatile(Self::ADDRESS) } + } +} + +#[allow(non_camel_case_types)] +pub struct PINB; + +impl Register for PINB { + type T = u8; + const ADDRESS: *mut u8 = 0x36 as *mut u8; +} + +#[allow(non_camel_case_types)] +pub struct DDRB; + +impl Register for DDRB { + type T = u8; + const ADDRESS: *mut u8 = 0x37 as *mut u8; +} + +#[allow(non_camel_case_types)] +pub struct PORTB; + +impl Register for PORTB { + type T = u8; + const ADDRESS: *mut u8 = 0x38 as *mut u8; +} + +#[no_mangle] +pub extern "C" fn main() { + DDRB::write(0x01); + loop { + PORTB::write(0x01); + delay_ms(500); + PORTB::write(0x00); + delay_ms(500); + } +} diff --git a/avr-atmega328p.json b/avr-atmega328p.json new file mode 100644 index 0000000..279cd0a --- /dev/null +++ b/avr-atmega328p.json @@ -0,0 +1,31 @@ +{ + "arch": "avr", + "cpu": "atmega328p", + "data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8", + "env": "", + "executables": true, + "linker": "avr-gcc", + "linker-flavor": "gcc", + "linker-is-gnu": true, + "llvm-target": "avr-unknown-unknown", + "os": "unknown", + "position-independent-executables": false, + "exe-suffix": ".elf", + "eh-frame-header": false, + "pre-link-args": { + "gcc": [ + "-Os", + "-mmcu=atmega328p" + ] + }, + "late-link-args": { + "gcc": [ + "-lc", + "-lgcc" + ] + }, + "target-c-int-width": "16", + "target-endian": "little", + "target-pointer-width": "16", + "vendor": "unknown" +} diff --git a/avr-atmega32u4.json b/avr-atmega32u4.json new file mode 100644 index 0000000..a4d84e9 --- /dev/null +++ b/avr-atmega32u4.json @@ -0,0 +1,31 @@ +{ + "arch": "avr", + "cpu": "atmega32u4", + "data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8", + "env": "", + "executables": true, + "linker": "avr-gcc", + "linker-flavor": "gcc", + "linker-is-gnu": true, + "llvm-target": "avr-unknown-unknown", + "os": "unknown", + "position-independent-executables": false, + "exe-suffix": ".elf", + "eh-frame-header": false, + "pre-link-args": { + "gcc": [ + "-Os", + "-mmcu=atmega32u4" + ] + }, + "late-link-args": { + "gcc": [ + "-lc", + "-lgcc" + ] + }, + "target-c-int-width": "16", + "target-endian": "little", + "target-pointer-width": "16", + "vendor": "unknown" +} diff --git a/avr-attiny85.json b/avr-attiny85.json new file mode 100644 index 0000000..5d092a1 --- /dev/null +++ b/avr-attiny85.json @@ -0,0 +1,25 @@ +{ + "arch": "avr", + "atomic-cas": false, + "cpu": "attiny85", + "data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8", + "eh-frame-header": false, + "exe-suffix": ".elf", + "executables": true, + "late-link-args": { + "gcc": [ + "-lgcc" + ] + }, + "linker": "avr-gcc", + "llvm-target": "avr-unknown-unknown", + "max-atomic-width": 8, + "no-default-libraries": false, + "pre-link-args": { + "gcc": [ + "-mmcu=attiny85" + ] + }, + "target-c-int-width": "16", + "target-pointer-width": "16" +} diff --git a/flake.lock b/flake.lock index ca25ad3..542f89c 100644 --- a/flake.lock +++ b/flake.lock @@ -64,6 +64,21 @@ "type": "indirect" } }, + "nixpkgs-22-11": { + "locked": { + "lastModified": 1688392541, + "narHash": "sha256-lHrKvEkCPTUO+7tPfjIcb7Trk6k31rz18vkyqmkeJfY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ea4c80b39be4c09702b0cb3b42eab59e2ba4f24b", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-22.11", + "type": "indirect" + } + }, "nixpkgs_2": { "locked": { "lastModified": 1654275867, @@ -119,6 +134,7 @@ "root": { "inputs": { "nixpkgs": "nixpkgs", + "nixpkgs-22-11": "nixpkgs-22-11", "pkgs-cargo2nix": "pkgs-cargo2nix", "typeshare": "typeshare", "unstable": "unstable" diff --git a/flake.nix b/flake.nix index 1eab404..b892285 100644 --- a/flake.nix +++ b/flake.nix @@ -3,12 +3,13 @@ inputs = { nixpkgs.url = "nixpkgs/nixos-23.05"; + nixpkgs-22-11.url = "nixpkgs/nixos-22.11"; unstable.url = "nixpkgs/nixos-unstable"; pkgs-cargo2nix.url = "github:cargo2nix/cargo2nix"; typeshare.url = "github:1Password/typeshare"; }; - outputs = { self, nixpkgs, unstable, pkgs-cargo2nix, typeshare, ... }: + outputs = { self, nixpkgs, nixpkgs-22-11, unstable, pkgs-cargo2nix, typeshare, ... }: let version = builtins.string 0 8 self.lastModifiedDate; supportedSystems = [ "x86_64-linux" ]; @@ -17,12 +18,17 @@ devShell."x86_64-linux" = let pkgs = import nixpkgs { system = "x86_64-linux"; }; + pkgs-22-11 = import nixpkgs-22-11 { system = "x86_64-linux"; }; pkgs-unstable = import unstable { system = "x86_64-linux"; }; cargo2nix = pkgs-cargo2nix.packages."x86_64-linux"; + avr = pkgs-22-11.pkgsCross.avr; in pkgs.mkShell { name = "ld-tools-devshell"; buildInputs = [ + avr.buildPackages.gcc + avr.buildPackages.avrdude + avr.avrlibc pkgs.libadwaita pkgs.clang pkgs.entr