【Arduino工作〜発展編】AVR-RustでAtmega328pをLチカ!レベル2
※ 当ページには【広告/PR】を含む場合があります。
2021/10/27
2021/11/04
マイコンの書込構成(おさらい)
avrdudeの準備
avrdude
$ sudo avrdude -c atmelice_isp -P usb -p m328p -v
avrdude: Version 6.3-20171130
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch
System wide configuration file is "/etc/avrdude.conf"
User configuration file is "/root/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : usb
Using Programmer : atmelice_isp
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
AVR Part : ATmega328P
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PC2
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff
flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
Programmer Type : JTAG3_ISP
Description : Atmel-ICE (ARM/AVR) in ISP mode
Vtarget : 5.0 V
SCK period : 8.00 us
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as FF
avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as FF
avrdude: safemode: Fuses OK (E:FF, H:D9, L:62)
avrdude done. Thank you.
Rustでのプログラム実装
$ tree
.
├── Cargo.toml
├── src
│ └── main.rs
└── avr-atmega328p.json
Cargo.toml
[package]
name = "std-blink"
version = "0.1.0"
authors = ["tacoskingdom<contact@tacoskingdom.com>"]
edition = "2018"
[dependencies]
avr-std-stub = "1.0.2"
avrd = "0.3.1"
avr_delay = { git = "https://github.com/avr-rust/delay" }
std-blink
avr-atmega328p.json
{
"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",
"no-compiler-rt": true,
"os": "unknown",
"position-independent-executables": false,
"exe-suffix": ".elf",
"eh-frame-header": false,
"pre-link-args": {
"gcc": ["-mmcu=atmega328p"]
},
"late-link-args": {
"gcc": ["-lgcc", "-lc"]
},
"target-c-int-width": "16",
"target-endian": "little",
"target-pointer-width": "16",
"vendor": "unknown"
}
avr_delayライブラリについて
delay
delay
delay_ms
delay_us
delay_ms
delay_us
avr-config
AVR_CPU_FREQUENCY_HZ
delay_ms
delay_us
delay
pub fn delay(count: u32) {
// Our asm busy-wait takes a 16 bit word as an argument,
// so the max number of loops is 2^16
let outer_count = count / 65536;
let last_count = ((count % 65536)+1) as u16;
for _ in 0..outer_count {
// Each loop through should be 4 cycles.
unsafe {llvm_asm!("1: sbiw $0,1
brne 1b"
:
: "w" (0)
:
:)}
}
unsafe {llvm_asm!("1: sbiw $0,1
brne 1b"
:
: "w" (last_count)
:
:)}
}
count
u16
16MHz
Eq. (1)
Eq. (2)
delay(4000000)
クロック数[Hz] | カウント数/1s |
---|---|
16M | 4000000 |
8M | 2000000 |
4M | 1000000 |
2M | 500000 |
1M | 250000 |
0.5M | 125000 |
main.rsによる実装
src/main.rs
PB1
ビット演算
DDRB(レジスタアドレス: 0x24)
PORTB(レジスタアドレス: 0x25)
#![feature(llvm_asm, lang_items, unwind_attributes)]
#![no_std]
#![no_main]
extern crate avr_delay;
extern crate avr_std_stub;
extern crate avrd;
//👇①
use avrd::atmega328p::{DDRB, PORTB};
use avr_delay::{delay};
//👇Rust組込関数のwrite_volatileからメモリへの書き込みを行う
use core::ptr::write_volatile;
#[no_mangle]
pub extern "C" fn main() {
unsafe {
//👇②
write_volatile(DDRB, 0b00000010);
}
let mut out = 0b00000000;
loop {
unsafe {
//👇③
write_volatile(PORTB, out);
}
//システムクロック周波数1MHzで1秒待つ
delay(250000);
//👇④
out ^= 1 << 1;
}
}
DDRB
PORTB
0b00000010
0x02
write_volatile関数
delay(250000)
ビルドしてみる
$ export AVR_CPU_FREQUENCY_HZ=16000000
$ cargo build -Z build-std=core --target avr-atmega328p.json --release
Updating crates.io index
Updating git repository `https://github.com/avr-rust/delay`
Downloaded proc-macro2 v1.0.30
#...中略
Downloaded 13 crates (1.8 MB) in 0.72s
Compiling std-blink v0.1.0 (/usr/src/app/std-blink)
Finished release [optimized] target(s) in 0.31s
target/[デバイスのタイトル]/release
[パッケージ名].elf
Atmel SAM/AVRのデバッグとプログラミングのための最強開発ツール Atmel-ICE MCU AVR SAM Xmega プログラマー デバッガー
Arduino側へ実行ファイルの書き込み
target/avr-atmega328p/release
std-blink.elf
$ sudo avrdude -p m328p -c atmelice_isp -P usb -U flash:w:target/avr-atmega328p/release/std-blink.elf:e
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "target/avr-atmega328p/release/std-blink.elf"
avrdude: writing flash (2446 bytes):
Writing | ################################################## | 100% 0.72s
avrdude: 2446 bytes of flash written
avrdude: verifying flash memory against target/avr-atmega328p/release/std-blink.elf:
avrdude: load data flash data from input file target/avr-atmega328p/release/std-blink.elf:
avrdude: input file target/avr-atmega328p/release/std-blink.elf contains 2446 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.72s
avrdude: verifying ...
avrdude: 2446 bytes of flash verified
avrdude: safemode: Fuses OK (E:FF, H:D9, L:62)
avrdude done. Thank you.
PB1
delay
まとめ
参考サイト
記事を書いた人
ナンデモ系エンジニア
電子工作を身近に知っていただけるように、材料調達からDIYのハウツーまで気になったところをできるだけ細かく記事にしてブログ配信してます。
カテゴリー