USBaspのファームウェアを書き込みするときに、鶏は先か卵が先かの悩ましい問題が発生します。
もう一台のUSBaspがあれば、問題はすぐ解決するけど、書き込み器がない場合どうしようもありません。

ですが、RaspberryPiがあれば、 ファームウェア を書き込みすることができます。

その方法について実際にやってみたので、メモしたいと思います。

USBaspのホームページには、回路図と ファームウェア をダウンロードできます。
今回は、usbasp.2011-05-28.tar.gz (519 kB) TPI support, supports programmers with ATMega88 and ATMega8.をダウンロードしました。

ATMega88 を使用して、 ファームウェア を書き込みしたいと思います。

まずは、 RaspberryPi で ファームウェア を書き込む方法として、avrdudeを使用します。

avrdude をインストールします。

$ sudo apt-get install avrdude

RaspberryPi のGPIOを使用して、 avrdude から書き込みできる「linuxgpio」を設定していきます。
AVRの書き込みに必要な端子は、RESET、SCK、MOSI、MISO、および電源3.3V、GNDです。

linuxgpio では RESET、SCK、MOSI、MISO は任意の端子に設定できますが今回は下記のように振り分けました。

RESET  GPIO4(7pin)
GND (9pin)
SCK   GPIO17(11pin)
MOSI   GPIO27(13pin)
MISO GPIO22(15pin)
3.3V (17pin)

コネクターは、7pinから1列になるように設定しました。

回路図は下記のとおりです。
水晶は、内部発振から外部発振にした場合には必要になります。

次に、 avrdude が「 linuxgpio 」を認識するように設定します。

avrdude のコンフィグ設定のうち linuxgpio の項目をコメントを外したのち、上記回路図で設定した RESET、SCK、MOSI、MISO のGPIO端子を設定します。

$ sudo vim /etc/avrdude.conf

1265行付近の linuxgpio の項目 を設定します。

#
#To check if your avrdude build has support for the linuxgpio programmer compiled in,
#use -c?type on the command line and look for linuxgpio in the list. If it's not available
#you need pass the --enable-linuxgpio=yes option to configure and recompile avrdude. 
# 
programmer 
  id    = "linuxgpio"; 
  desc  = "Use the Linux sysfs interface to bitbang GPIO lines"; 
  type  = "linuxgpio"; 
  reset = 4; 
  sck   = 17; 
  mosi  = 27; 
  miso  = 22; 
;

linuxgpio がちゃんと設定されているか確認します。

$ avrdude -c?

中略
jtagmkII = Atmel JTAG ICE mkII
jtagmkII_avr32 = Atmel JTAG ICE mkII im AVR32 mode
linuxgpio = Use the Linux sysfs interface to bitbang GPIO lines
linuxspi = Use Linux SPI device in /dev/spidev*
lm3s811 = Luminary Micro LM3S811 Eval Board (Rev. A)
mib510 = Crossbow MIB510 programming board
mkbutterfly = Mikrokopter.de Butterfly
中略

ATMega88 を接続し接続状況を確認します。

$ sudo avrdude -v -p m88 -c linuxgpio -P usb -F

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              : linuxgpio
         AVR Part                      : ATmega88
         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        512    4      0  3600  3600 0xff 0xff
           flash         65     6    64    0 yes      8192   64    128  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 : linuxgpio
         Description     : Use the Linux sysfs interface to bitbang GPIO lines
         Pin assignment  : /sys/class/gpio/gpio{n}
           RESET   =  4
           SCK     =  17
           MOSI    =  27
           MISO    =  22

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e930a (probably m88)
avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9

avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9
avrdude: safemode: Fuses OK (E:F9, H:DF, L:62)

avrdude done.  Thank you.

usbasp.2011-05-28.tar.gzを回答し、hexファイルのある場所に移動します。

$ tar -zxvf usbasp.2011-05-28.tar.gz
$ cd usbasp.2011-05-28/bin/firmware
$ ls
Makefile    Makefile88                      usbasp.atmega8.2011-05-28.hex
Makefile48  usbasp.atmega48.2009-02-28.hex  usbasp.atmega88.2011-05-28.hex

usbasp.atmega88.2011-05-28.hex を書き込みします。

$ sudo avrdude -v -p m88 -c linuxgpio -P usb -F -e -U flash:w:usbasp.atmega88.2011-05-28.hex

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              : linuxgpio
         AVR Part                      : ATmega88
         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        512    4      0  3600  3600 0xff 0xff
           flash         65     6    64    0 yes      8192   64    128  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 : linuxgpio
         Description     : Use the Linux sysfs interface to bitbang GPIO lines
         Pin assignment  : /sys/class/gpio/gpio{n}
           RESET   =  4
           SCK     =  17
           MOSI    =  27
           MISO    =  22

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e930a (probably m88)
avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9
avrdude: erasing chip
avrdude: reading input file "usbasp.atmega88.2011-05-28.hex"
avrdude: input file usbasp.atmega88.2011-05-28.hex auto detected as Intel Hex
avrdude: writing flash (4716 bytes):

Writing | ################################################## | 100% 1.44s

avrdude: 4716 bytes of flash written
avrdude: verifying flash memory against usbasp.atmega88.2011-05-28.hex:
avrdude: load data flash data from input file usbasp.atmega88.2011-05-28.hex:
avrdude: input file usbasp.atmega88.2011-05-28.hex auto detected as Intel Hex
avrdude: input file usbasp.atmega88.2011-05-28.hex contains 4716 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 1.08s

avrdude: verifying ...
avrdude: 4716 bytes of flash verified

avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9
avrdude: safemode: Fuses OK (E:F9, H:DF, L:62)

avrdude done.  Thank you.

ヒューズを書き込みします。
TARGET=atmega88 HFUSE=0xdd LFUSE=0xffとなっていますので
H側=0xdd L側=0xffとなります。
なおL側のヒューズを書き込みすると、これ以降、内部発振から外部発振に切り替わるので、水晶が必要になります。
まずはH側から書き込みします

$ sudo avrdude -v -p m88 -c linuxgpio -P usb -F -u -U hfuse:w:0xdd:m

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              : linuxgpio
         AVR Part                      : ATmega88
         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        512    4      0  3600  3600 0xff 0xff
           flash         65     6    64    0 yes      8192   64    128  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 : linuxgpio
         Description     : Use the Linux sysfs interface to bitbang GPIO lines
         Pin assignment  : /sys/class/gpio/gpio{n}
           RESET   =  4
           SCK     =  17
           MOSI    =  27
           MISO    =  22

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e930a (probably m88)
avrdude: reading input file "0xdd"
avrdude: writing hfuse (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xdd:
avrdude: load data hfuse data from input file 0xdd:
avrdude: input file 0xdd contains 1 bytes
avrdude: reading on-chip hfuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of hfuse verified

avrdude done.  Thank you.

続いて、L側を書き込みます。

$ sudo avrdude -v -p m88 -c linuxgpio -P usb -F -u -U lfuse:w:0xff:m

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              : linuxgpio
         AVR Part                      : ATmega88
         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        512    4      0  3600  3600 0xff 0xff
           flash         65     6    64    0 yes      8192   64    128  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 : linuxgpio
         Description     : Use the Linux sysfs interface to bitbang GPIO lines
         Pin assignment  : /sys/class/gpio/gpio{n}
           RESET   =  4
           SCK     =  17
           MOSI    =  27
           MISO    =  22

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e930a (probably m88)
avrdude: reading input file "0xff"
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xff:
avrdude: load data lfuse data from input file 0xff:
avrdude: input file 0xff contains 1 bytes
avrdude: reading on-chip lfuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lfuse verified

avrdude done.  Thank you.

これでファームウェアを書き込みすることができました。

おすすめの記事