[Atmel-ICE on Linux] Debianからavrdudeを使ってブレッドボード上でAtmega328pのプログラムを書き込む
※ 当ページには【広告/PR】を含む場合があります。
2021/03/09
準備 ~ ハード編
Atmega328p(AVRマイコン)のプログラム書き込みに必要なもの
+ ATMEGA328Pマイコン(※未使用もしくは初期化された状態のもの)
+ ブレッドボード
+ 5V供給用のUSBアダプター
+ 抵抗 10kΩ
書き込み前のピン配線
Atmel-ICEとマイコンとの配線接続
準備 ~ ソフト編
Linux(Debian)にavrdudeをインストール
$ sudo apt-get install binutils-avr gcc-avr avr-libc avrdude
USBドライバをインストール
libusb-dev
libusb-1.0-0-dev
$ sudo apt-get install libusb-dev
#👇ドライバ・ライブラリが追加されていることを確認
$ dpkg -L libusb-dev | grep /usr/include
/usr/include
/usr/include/usb.h
$ sudo apt-get install libusb-1.0-0-dev
#👇ドライバ・ライブラリが追加されていることを確認
$ dpkg -L libusb-1.0-0-dev | grep /usr/include
/usr/include
/usr/include/libusb-1.0
/usr/include/libusb-1.0/libusb.h
Windows:
Atmel-ICEをUSB接続すると自動でUSBドライバがロードされる
Mac:
インストールは不要
Linux:
99-platformio-udev.rulesというudevルールを設定する必要あり。
もし過去に入れている場合には、最新のドライバにするためにアップデート推薦
$ curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules \
| sudo tee /etc/udev/rules.d/99-platformio-udev.rules
$ ls /etc/udev/rules.d/
#👇ルールが追加されている
... 99-platformio-udev.rules ...
$ sudo service udev restart
$ curl -L -O https://raw.githubusercontent.com/artynet/arduino-linux-setup/master/arduino-linux-setup-12.sh
$ chmod +x arduino-linux-setup-12.sh
$ ./arduino-linux-setup-12.sh <your_username>
ターゲットデバイス名&書き込み装置名の確認
$ avrdude -p ?
Valid parts are:
uc3a0512 = AT32UC3A0512
c128 = AT90CAN128
c32 = AT90CAN32
#...
m328p = ATmega328P
#...
x8e5 = ATxmega8E5
ucr2 = deprecated, use 'uc3a0512'
$ avrdude -c ?
Valid programmers are:
2232HIO = FT2232H based generic programmer
#...
atmelice = Atmel-ICE (ARM/AVR) in JTAG mode
atmelice_dw = Atmel-ICE (ARM/AVR) in debugWIRE mode
atmelice_isp = Atmel-ICE (ARM/AVR) in ISP mode
atmelice_pdi = Atmel-ICE (ARM/AVR) in PDI mode
atmelice_updi = Atmel-ICE (ARM/AVR) in UPDI mode
#...
xplainedpro_updi = Atmel AVR XplainedPro in UPDI mode
atmelice_isp
SPI(ISP)設定のピン配置
AVR用 10ピンヘッダ(Atmel-ICE側) | マイコンへの接続先 |
---|---|
Pin 1 (TCK) | SCK |
Pin 2 (GND) | GND |
Pin 3 (TDO) | MISO |
Pin 4 (VTG) | VTG |
Pin 6 (nSRST) | /RESET |
Pin 9 (TDI) | MOSI |
マイコンへの接続テスト
$ 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.
$ 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: usbhid_open(): No response from device
avrdude: usbdev_open(): Found Atmel-ICE CMSIS-DAP, serno: J41800091527
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
avrdude: jtag3_edbg_prepare(): failed to read from serial port (-1)
avrdude: failed to sync with the JTAGICE3 in ISP mode
avrdude done. Thank you.
元Arduino Uno
簡単なプログラムの書き込み
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0xFF; //Makes PORTB an Output.
while(1) {
PORTB = 0xFF; //Turns ON All LEDs
_delay_ms(1000); //1 second delay
PORTB= 0x00; //Turns OFF All LEDs
_delay_ms(1000); //1 second delay
}
}
sample_program.c
#👇コンパイル
$ avr-gcc -Wall -g -Os -mmcu=atmega328p ./sample_program.c -o ./sample_program.out
$ ls
sample_program.c sample_program.out
#👇実行ファイルのサイズをチェック
$ avr-size -C ./sample_program.out
AVR Memory Usage
----------------
Device: Unknown
Program: 178 bytes
(.text + .data + .bootloader)
Data: 0 bytes
(.data + .bss + .noinit)
#👇hexファイルに書き込み形式を変更
$ avr-objcopy -j .text -j .data -O ihex ./sample_program.out ./sample_program.hex
$ ls
sample_program.c sample_program.hex sample_program.out
hex
$ sudo avrdude -c atmelice_isp -P usb -p m328p -U flash:w:sample_program.hex:i
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 "sample_program.hex"
avrdude: writing flash (178 bytes):
Writing | ################################################## | 100% 0.05s
avrdude: 178 bytes of flash written
avrdude: verifying flash memory against sample_program.hex:
avrdude: load data flash data from input file sample_program.hex:
avrdude: input file sample_program.hex contains 178 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.07s
avrdude: verifying ...
avrdude: 178 bytes of flash verified
avrdude: safemode: Fuses OK (E:FF, H:D9, L:62)
avrdude done. Thank you.
余談 ~ avrdudeのフューズの書き換え方
Ext:0xFF
High:0xD9
Low:0x62
0xD9
0x99
$ sudo avrdude -c atmelice_dw -P usb -p m328p -U hfuse:w:0x99:m -v
$ sudo avrdude -c atmelice_isp -P usb -p m328p -U hfuse:w:0xd9:m -U lfuse:w:0x62:m -U efuse:w:0xff:m -v
Lチカで動作確認
まとめ
Atmel-ICE
USBドライバを正常にインストール
参考サイト
記事を書いた人
ナンデモ系エンジニア
電子工作を身近に知っていただけるように、材料調達からDIYのハウツーまで気になったところをできるだけ細かく記事にしてブログ配信してます。
カテゴリー