Make an example that compiles, add pinout

This commit is contained in:
Sofia 2025-09-04 00:21:56 +03:00
parent 6196d41c93
commit fad56304e2
6 changed files with 291 additions and 89 deletions

11
.cargo/config.toml Normal file
View File

@ -0,0 +1,11 @@
[build]
target = "avr-none"
rustflags = ["-C", "target-cpu=atmega328p"]
[target.'cfg(target_arch = "avr")']
runner = "ravedude"
# To run in simulator, replace the line above with this:
# runner = "simavr -m atmega328p"
[unstable]
build-std = ["core"]

206
Cargo.lock generated
View File

@ -3,10 +3,117 @@
version = 4 version = 4
[[package]] [[package]]
name = "libc" name = "atmega-hal"
version = "0.2.175" version = "0.1.0"
source = "git+https://github.com/Rahix/avr-hal?tab=readme-ov-file?rev=cd3edea#cd3edea5529c46fa7a5650fd0e768600ab2356a4"
dependencies = [
"avr-device",
"avr-hal-generic",
]
[[package]]
name = "avr-device"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" checksum = "520f2031240156132bd83639d86aeb5b1907e6a228d9a4b44c3e9699827e6dae"
dependencies = [
"avr-device-macros",
"bare-metal",
"cfg-if",
"vcell",
]
[[package]]
name = "avr-device-macros"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47c26fd925156183eb10e821b2ef7e06f8163f5a64a0bbe52fc896be2c6cbd3f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "avr-hal-generic"
version = "0.1.0"
source = "git+https://github.com/Rahix/avr-hal?tab=readme-ov-file?rev=cd3edea#cd3edea5529c46fa7a5650fd0e768600ab2356a4"
dependencies = [
"avr-device",
"embedded-hal 0.2.7",
"embedded-hal 1.0.0",
"embedded-hal-bus",
"embedded-storage",
"nb 1.1.0",
"paste",
"ufmt",
"unwrap-infallible",
]
[[package]]
name = "bare-metal"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603"
[[package]]
name = "cfg-if"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "critical-section"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
[[package]]
name = "embedded-hal"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff"
dependencies = [
"nb 0.1.3",
"void",
]
[[package]]
name = "embedded-hal"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89"
[[package]]
name = "embedded-hal-bus"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57b4e6ede84339ebdb418cd986e6320a34b017cdf99b5cc3efceec6450b06886"
dependencies = [
"critical-section",
"embedded-hal 1.0.0",
]
[[package]]
name = "embedded-storage"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "723dce4e9f25b6e6c5f35628e144794e5b459216ed7da97b7c4b66cdb3fa82ca"
[[package]]
name = "nb"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f"
dependencies = [
"nb 1.1.0",
]
[[package]]
name = "nb"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
[[package]] [[package]]
name = "panic-halt" name = "panic-halt"
@ -15,9 +122,98 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11" checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11"
[[package]] [[package]]
name = "udp-tests" name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "proc-macro2"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tests"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"libc", "atmega-hal",
"avr-device",
"embedded-hal 1.0.0",
"panic-halt", "panic-halt",
"ufmt",
] ]
[[package]]
name = "ufmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a64846ec02b57e9108d6469d98d1648782ad6bb150a95a9baac26900bbeab9d"
dependencies = [
"ufmt-macros",
"ufmt-write",
]
[[package]]
name = "ufmt-macros"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d337d3be617449165cb4633c8dece429afd83f84051024079f97ad32a9663716"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "ufmt-write"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69"
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "unwrap-infallible"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "151ac09978d3c2862c4e39b557f4eceee2cc72150bc4cb4f16abf061b6e381fb"
[[package]]
name = "vcell"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002"
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"

View File

@ -1,14 +1,27 @@
[package] [package]
name = "udp-tests" name = "tests"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
libc = { version = "0.2.175", default-features = false }
panic-halt = "1.0.0" panic-halt = "1.0.0"
avr-device = { version = "0.7", features = ["rt"] }
embedded-hal = "1.0"
atmega-hal = { git = "https://github.com/Rahix/avr-hal?tab=readme-ov-file", rev="cd3edea", version = "0.1.0", features=["atmega328p"] }
ufmt = "0.2.0"
# nb = "1.1.0"
# pwm-pca9685 = "1.0.0"
# infrared = "0.14.1"
# embedded-storage = "0.2"
# arduino-hal = { git = "https://github.com/Rahix/avr-hal?tab=readme-ov-file", version = "0.1.0", features=["arduino-uno"] }
[profile.dev] [profile.dev]
panic = "abort" panic = "abort"
[profile.release]
panic = "abort"
[build] [build]
rustflags = ["-C", "link-args=-lc"] rustflags = ["-C", "link-args=-lc"]

7
Ravedude.toml Normal file
View File

@ -0,0 +1,7 @@
[general]
board = "uno"
serial-baudrate = 57600
open-console = true
# For documentation about this file, check here:
# https://github.com/Rahix/avr-hal/blob/main/ravedude/README.md#ravedudetoml-format

11
rust-toolchain.toml Normal file
View File

@ -0,0 +1,11 @@
[toolchain]
channel = "nightly-2025-04-27"
components = [
"rustc",
"cargo",
"clippy",
"rustfmt",
"rust-src",
"rust-analyzer",
]
profile = "minimal"

View File

@ -1,89 +1,53 @@
/*!
* Blink the builtin LED - the "Hello World" of embedded programming.
*/
#![no_std] #![no_std]
#![no_main] #![no_main]
use core::{ use atmega_hal::{Usart, usart::Baudrate};
alloc::{GlobalAlloc, Layout}, use embedded_hal::delay::DelayNs;
ffi::CStr,
ptr::null_mut,
};
extern crate alloc;
use alloc::vec::Vec;
use panic_halt as _; use panic_halt as _;
fn handle_error(code: i32, err: &'static core::ffi::CStr) { type CoreClock = atmega_hal::clock::MHz8;
unsafe {
if code == -1 { #[avr_device::entry]
libc::perror(err.as_ptr()); fn main() -> ! {
libc::exit(libc::EXIT_FAILURE); let dp = atmega_hal::Peripherals::take().unwrap();
} let pins = atmega_hal::pins!(dp);
let a0 = pins.pc0;
let a1 = pins.pc1;
let a2 = pins.pc2;
let a3 = pins.pc3;
let mosi = pins.pb3;
let miso = pins.pb4;
let sck = pins.pb5;
let rx = pins.pd0;
let tx = pins.pd1;
let d2 = pins.pd2;
let d3 = pins.pd3;
let d4 = pins.pd4;
let d5 = pins.pd5;
let d6 = pins.pd6;
let d7 = pins.pd7;
let d9 = pins.pb1;
let d10 = pins.pb2;
let mut led = pins.pb0.into_output();
led.set_high();
let mut delay = atmega_hal::delay::Delay::<CoreClock>::new();
let mut serial = Usart::new(
dp.USART0,
rx,
tx.into_output(),
Baudrate::<CoreClock>::new(57600),
);
loop {
led.toggle();
delay.delay_ms(1000);
ufmt::uwriteln!(&mut serial, "Hello from ATmega!\r").unwrap();
} }
} }
struct MyAllocator;
unsafe impl GlobalAlloc for MyAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe { libc::malloc(layout.size()) as *mut u8 }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { libc::free(ptr as *mut libc::c_void) }
}
}
#[global_allocator]
static GLOBAL: MyAllocator = MyAllocator;
#[unsafe(no_mangle)]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
// Since we are passing a C string the final null character is mandatory
unsafe {
libc::printf(c"Hello world!\n".as_ptr());
let socket = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_UDP);
let address = libc::sockaddr_in {
sin_family: libc::AF_INET as u16,
sin_port: libc::htons(8080),
sin_addr: libc::in_addr {
s_addr: libc::INADDR_ANY,
},
sin_zero: [0, 0, 0, 0, 0, 0, 0, 0],
};
handle_error(
libc::bind(
socket,
&raw const address as *const libc::sockaddr,
core::mem::size_of::<libc::sockaddr_in>() as u32,
),
c"bind",
);
let mut otus = Vec::with_capacity(10);
loop {
let buf = libc::malloc(16) as *mut u8;
let mut other = libc::malloc(core::mem::size_of::<libc::sockaddr_in>());
let mut other_size = 0;
libc::recvfrom(
socket,
buf as *mut libc::c_void,
1,
0,
&raw mut other as *mut libc::sockaddr,
&mut other_size,
);
otus.push(*buf);
libc::printf(c"Data: %s\n".as_ptr(), buf);
libc::free(buf as *mut libc::c_void);
}
}
0
}