JR6HSXのアイコン画像
JR6HSX 2021年11月09日作成 (2022年01月18日更新) © MIT
セットアップや使用方法 セットアップや使用方法 閲覧数 2735
JR6HSX 2021年11月09日作成 (2022年01月18日更新) © MIT セットアップや使用方法 セットアップや使用方法 閲覧数 2735

ATTiny85にUSBaspでプログラムをロードする by Ubuntu

ATTiny85というとても小さなCPUがあります。
ATTiny85-20Uモジュール
これを使ってIoT的な温度をはじめ環境計測を行ったデータを送信する装置を作ろうとしています。
その時に避けられない事は、プログラムをCPUにロードすることです。どんだけプログラムを作るのが達者で速くても実行させることはできません。

Arduino-IDEを使ってATTiny85のプログラムを作ることは他の方も紹介されていますが、私は得意ではありません。arduino-cliを使ってCLIで作っています。

この記事では、CPUとは別に購入することになる USBasp や、それを使うためのソフトウェアの使い方について紹介します。

USBasp プログラムローダー

CPUにプログラムをロードするためには、USBaspを使用しています。
USBasp
ケーブルは、ICSP用の2列6ピンを私のATTiny85に合わせて1列6ピンと電源ケーブルに変換する自作のケーブルです。
自作変換ケーブル
回路的なややこしいことはありません。ピン配列が異なるので、それらを合わせるだけです。
このUSBaspをUbuntuで動いているホストPCに挿し込むと以下のように認識されます。

new low-speed USB device number 9 using ehci-pci
New USB device found, idVendor=16c0, idProduct=05dc, 
bcdDevice= 1.02
New USB device strings: Mfr=1, Product=2, SerialNumber=0
Product: USBasp
Manufacturer: www.fischl.de

Manufacturerに示されるURLにアクセスして、ファームウェアなどをダウンロードしてくる。udevのruleファイルも入っているのでそれをインストールします。
ただし、不具合があるようで以下のように修正を施しています。

# USBasp - USB programmer for Atmel AVR controllers
# Copy this file to /etc/udev/rules.d so

#SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", SYSFS{idVendor}=="16c0", SYSFS{idProduct}=="05dc", MODE="0666"
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="16c0", ATTR{idProduct}=="05dc", MODE="0666"

下の行のように書き改めます。そのあと、

$ sudo systemctl restart udev

を実行します。

avrdude ロードプログラム

このUSBaspを用いてUbuntuでCPUにプログラムをロードするためには、

avrdude

というソフトウェアを使用します。このソフトウェアは、Ubuntuの場合には次の様にインストールします。

$ sudo apt install avrdude

USBaspを用いてプログラムをロードするには、このavrdudeプログラムを使用します。

arduino-cliを使ってコードのコンパイル

詳細は別稿に委ねるとして、概略を記します。

http://drazzy.com/package_drazzy.com_index.json
にアクセスしてインストールの仕方の通り操作して ATTinyCore:avr をcoreとして用意します。
この時に Arduino-IDE を使った方が楽かもしれません。arduino-cliで用意できますが。

blink.ino

void setup(void) { pinMode(4,OUTPUT); } void loop(void) { digitalWrite(4,HIGH); delay(1000); digitalWrite(4,LOW); delay(1000); }

blink.ino としてLEDをブリンクさせる、いわゆるLチカプログラムを次のようにコンパイルします。blink/blink.ino として、blink の存在するディレクトリを cwd として

$ arduino-cli compile -b ATTinyCore:avr:attinyx5 blink

コンパイルの結果、2つのファイルが出来ます。

blink.ATTinyCore.avr.attinyx5.elf
blink.ATTinyCore.avr.attinyx5.hex

下のhexファイルをCPUの書き込みに使います。

avrdude を使ってプログラムをロードする

前述のケーブルを用いてUSBaspとATTiny85基板を接続します。

ATTiny85基板とUSBaspを接続する
USBコネクタをPCに接続して、

$ avrdude -cusbasp -pt85 -Uflash:w:blink/blink.ATTinyCore.avr.attinyx5.hex:i -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 "/home/horimoto/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : usbasp
         AVR Part                      : ATtiny85
         Chip Erase delay              : 4500 us
         PAGEL                         : P00
         BS2                           : P00
         RESET disposition             : possible i/o
         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     6     4    0 no        512    4      0  4000  4500 0xff 0xff
           flash         65     6    32    0 yes      8192   64    128  4500  4500 0xff 0xff
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00

         Programmer Type : usbasp
         Description     : USBasp, http://www.fischl.de/usbasp/

avrdude: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e930b (probably t85)
avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as DD
avrdude: safemode: efuse reads as FE
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: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "blink/blink.ATTinyCore.avr.attinyx5.hex"
avrdude: writing flash (464 bytes):

Writing | ################################################## | 100% 0.36s

avrdude: 464 bytes of flash written
avrdude: verifying flash memory against blink/blink.ATTinyCore.avr.attinyx5.hex:
avrdude: load data flash data from input file blink/blink.ATTinyCore.avr.attinyx5.hex:
avrdude: input file blink/blink.ATTinyCore.avr.attinyx5.hex contains 464 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.28s

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

avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as DD
avrdude: safemode: efuse reads as FE
avrdude: safemode: Fuses OK (E:FE, H:DD, L:E1)

avrdude done.  Thank you.

実行(Lチカ)結果

チカチカします
動画はこちらになります。

https://youtu.be/8GAupwEepww

https://youtu.be/Dez6ceGgaKI

Qiitaにも投稿したのですが、内容的にハードウェア的かな。と思いこちらも投稿しました。

1
ログインしてコメントを投稿する