【Arduino工作〜発展編】AVR-RustでAtmega328pをLチカ!レベル2
※ 当ページには【広告/PR】を含む場合があります。
2021/10/27
2021/11/04

今回の記事では、このruduinoの内部の実装を参考にしながら、もう少しだけ応用範囲を拡げられるように、AVR-Rustライブラリを使った1レベル上のLチカを実装する手順を紹介します。
マイコンの書込構成(おさらい)
サラのAtmega328pにプログラムを書き込む際に利用するピンと、書込装置を正しく接続することが重要になってきます。

AVRマイコンをSPI(ISP)モードで書き込む場合には、専用ライターが必要になります。
どのようなAVRマイコンでも安定して書き込めるように、また何かあったらヒューズビットも書き込みできるように、例えば『
著者の場合はAVRだけでなくSAMも使うので正規品の
正規品をセールスで出ているのを狙って買うか、以下のようなサードパーティ製の互換品を購入することもできます。
ちなみに付属品のJTAGケーブルのアクセサリーは失くすと、色んなプログラミングモードでピンの番号の対応を確認しなくてはならないという地味に辛い作業をしなくてはならないので、大切に保管しましょう。
以下は、Atmel-ICEとAVRマイコンの接続例を模した図になります。

avrdudeの準備
AVRマイコンへの実行プログラム書き込みを行うコマンドは
avrdude
書込装置とマイコンへの接続をテストするためにAtmega328pのメモリ情報を出力します。
$ 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.
エラーもなくAtmega328pのメモリにアクセスできているようなので、これで書き込み準備はOKです。
続いてRustでのプログラムの実装をやっていきましょう。
Rustでのプログラム実装
まず今回のプロジェクトの構造は以下のようにしています。
$ tree
.
├── Cargo.toml
├── src
│ └── main.rs
└── avr-atmega328p.json
これはほとんど
まずはruduinoを参考にしながら
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
次にAtmega328p用にツールチェーンやビルド設定ファイル・
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"
}
このファイルについては
Atmega328p以外のAVRマイコンを使う場合には、そちらを参考にしてください。
avr_delayライブラリについて