AVRアセンブラのavraをインストールしたいと思います。
使用環境は、Raspberry Pi3のRaspbian上で実行しています。
インストールは aptコマンドで自動でインストールすることができます。
$sudo apt install avra
$ sudo apt install avra
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
提案パッケージ:
avrprog avrp
以下のパッケージが新たにインストールされます:
avra
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。
114 kB 中 0 B のアーカイブを取得する必要があります。
この操作後に追加で 1,468 kB のディスク容量が消費されます。
以前に未選択のパッケージ avra を選択しています。
(データベースを読み込んでいます ... 現在 130515 個のファイルとディレクトリがインストールされています。)
.../avra_1.3.0-3_armhf.deb を展開する準備をしています ...
avra (1.3.0-3) を展開しています...
avra (1.3.0-3) を設定しています ...
man-db (2.8.5-2) のトリガを処理しています ...
avraのバージョンを確認してみます。
バージョンは、1.3.0のようです。
$ avra --version
AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)
Copyright (C) 1998-2010. Check out README file for more info
AVRA is an open source assembler for Atmel AVR microcontroller family
It can be used as a replacement of 'AVRASM32.EXE' the original assembler
shipped with AVR Studio. We do not guarantee full compatibility for avra.
AVRA comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of avra under the terms
of the GNU General Public License.
For more information about these matters, see the files named COPYING.
次に対応するavrデバイスを確認してみます。
$ avra --devices
AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)
Copyright (C) 1998-2010. Check out README file for more info
AVRA is an open source assembler for Atmel AVR microcontroller family
It can be used as a replacement of 'AVRASM32.EXE' the original assembler
shipped with AVR Studio. We do not guarantee full compatibility for avra.
AVRA comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of avra under the terms
of the GNU General Public License.
For more information about these matters, see the files named COPYING.
Device name | Flash size | RAM start | RAM size | EEPROM size | Supported
| (words) | (bytes) | (bytes) | (bytes) | instructions
------------+------------+-----------+----------+-------------+--------------
(default) | 4194304 | 0x0060 | 8388608 | 65536 | 139
ATtiny4 | 256 | 0x0040 | 32 | 0 | 112
ATtiny5 | 256 | 0x0040 | 32 | 0 | 112
ATtiny9 | 512 | 0x0040 | 32 | 0 | 112
ATtiny10 | 512 | 0x0040 | 32 | 0 | 112
ATtiny11 | 512 | 0x0000 | 0 | 0 | 90
ATtiny12 | 512 | 0x0000 | 0 | 64 | 90
ATtiny13 | 512 | 0x0060 | 64 | 64 | 125
ATtiny13A | 512 | 0x0060 | 64 | 64 | 125
ATtiny15 | 512 | 0x0000 | 0 | 64 | 90
ATtiny20 | 2048 | 0x0040 | 128 | 0 | 115
ATtiny22 | 1024 | 0x0060 | 128 | 128 | 120
ATtiny24 | 1024 | 0x0060 | 128 | 128 | 125
ATtiny24A | 1024 | 0x0060 | 128 | 128 | 125
ATtiny25 | 1024 | 0x0060 | 128 | 128 | 125
ATtiny26 | 1024 | 0x0060 | 128 | 128 | 122
ATtiny28 | 1024 | 0x0000 | 0 | 0 | 90
ATtiny44 | 2048 | 0x0060 | 256 | 256 | 125
ATtiny44A | 2048 | 0x0060 | 256 | 256 | 125
ATtiny45 | 2048 | 0x0060 | 256 | 256 | 125
ATtiny84 | 4096 | 0x0060 | 512 | 512 | 125
ATtiny85 | 4096 | 0x0060 | 512 | 512 | 125
ATtiny2313 | 1024 | 0x0060 | 128 | 128 | 125
ATtiny2313A | 1024 | 0x0060 | 128 | 128 | 125
ATtiny4313 | 2048 | 0x0060 | 256 | 256 | 125
AT90S1200 | 512 | 0x0000 | 0 | 64 | 89
AT90S2313 | 1024 | 0x0060 | 128 | 128 | 120
AT90S2323 | 1024 | 0x0060 | 128 | 128 | 120
AT90S2333 | 1024 | 0x0060 | 128 | 128 | 120
AT90S2343 | 1024 | 0x0060 | 128 | 128 | 120
AT90S4414 | 2048 | 0x0060 | 256 | 256 | 120
AT90S4433 | 2048 | 0x0060 | 128 | 256 | 120
AT90S4434 | 2048 | 0x0060 | 256 | 256 | 120
AT90S8515 | 4096 | 0x0060 | 512 | 512 | 120
AT90C8534 | 4096 | 0x0060 | 256 | 512 | 120
AT90S8535 | 4096 | 0x0060 | 512 | 512 | 120
ATmega8 | 4096 | 0x0060 | 1024 | 512 | 131
ATmega161 | 8192 | 0x0060 | 1024 | 512 | 133
ATmega162 | 8192 | 0x0100 | 1024 | 512 | 133
ATmega163 | 8192 | 0x0060 | 1024 | 512 | 133
ATmega16 | 8192 | 0x0060 | 1024 | 512 | 133
ATmega323 | 16384 | 0x0060 | 2048 | 1024 | 133
ATmega328P | 16384 | 0x0100 | 2048 | 1024 | 133
ATmega32 | 16384 | 0x0060 | 2048 | 1024 | 133
ATmega603 | 32768 | 0x0060 | 4096 | 2048 | 122
ATmega103 | 65536 | 0x0060 | 4096 | 4096 | 123
ATmega104 | 65536 | 0x0060 | 4096 | 4096 | 136
ATmega128 | 65536 | 0x0100 | 4096 | 4096 | 136
ATmega48 | 2048 | 0x0100 | 512 | 256 | 133
ATmega88 | 4096 | 0x0100 | 1024 | 512 | 133
ATmega168 | 8192 | 0x0100 | 1024 | 512 | 133
ATmega8515 | 8192 | 0x0060 | 512 | 512 | 133
AT94K | 8192 | 0x0060 | 16384 | 0 | 131
今回は、フリスクでAVRaspの回路を作成したときに予備で購入したATMEGA88が余っていたのでこれを活用したいと思います。
avraでは、各デバイスで定義されているレジスタなどのインクルードファイルが既に用意されています。
このファイルを使用することで、レジスタなどの名称で、アセンブラで記述することができます。
定義ファイルは、/usr/share/avraに置かれています。
:/usr/share/avra $ ls
1200def.inc m128def.inc m323def.inc m8515def.inc tn15def.inc
2313def.inc m161def.inc m3250def.inc m8535def.inc tn22def.inc
2323def.inc m162def.inc m325def.inc m88def.inc tn2313def.inc
2343def.inc m163def.inc m3290def.inc m8def.inc tn26def.inc
4414def.inc m165def.inc m329def.inc pwm2def.inc tn28def.inc
4433def.inc m168def.inc m32def.inc pwm3def.inc tn45def.inc
4434def.inc m169def.inc m406def.inc tn10def.inc tn4def.inc
8515def.inc m16def.inc m48def.inc tn11def.inc tn5def.inc
8535def.inc m2560def.inc m649def.inc tn12def.inc tn9def.inc
m103def.inc m2561def.inc m64def.inc tn13def.inc
では早速、ATMEGA88を使用して、Lちかのプログラムを作成したいと思います。
回路図が、下記のように、内部発振で、AVRaspにてVccを供給するとともに、デバイスに書き込みを行います
LEDはPB1に接続しています。
プログラムが下記のとおりです。
今後のために、内部発振は 1/8fではなく、1fで供給してるために、L側ヒューズを0xe2に書き換えしています。
;ATMEGA88 LED ON
;
;; ATMEGA88 cpu
;; rom 0000h -- 0fffh
;; ram 0100h -- 04ffh
;; eeprom 0000h -- 0200h
;; internal clock 8MHz
;;
;;
;; assemblers avra
;; file name atmega88_led.asm
;; $ avra -l atmega88_led.lst atmega88_led.asm
;; $ (sudo) avrdude -v -p m88 -c usbasp -P usb -F -e -U flash:w:atmega88_led.hex
;;
;;High Fuse 0xDF
;;$ (sudo) avrdude -v -p m88 -c usbasp -P usb -F -e -U hfuse:w:0xdf:m
;;Low Fuse 0xE2
;;$ (sudo) avrdude -v -p m88 -c usbasp -P usb -F -e -U lfuse:w:0xe2:m
;;
.include "/usr/share/avra/m88def.inc"
start: jmp reset ;Reset
reti ;IRQ0
reti ;IRQ1
reti ;PCINT0
reti ;PCINT1
reti ;PCINT2
reti ;Watchdog Timeout
reti ;Timer2 CompareA
reti ;Timer2 CompareB
reti ;Timer2 Overflow
reti ;Timer1 Capture
reti ;Timer1 CompareA
reti ;Timer1 CompareB
reti ;Timer1 Overflow
reti ;Timer0 CompareA
reti ;Timer0 CompareB
reti ;Timer0 Overflow
reti ;SPI Transfer Complete
reti ;USART RX Complete
reti ;USART UDR Empty
reti ;USART TX Complete
reti ;ADC Conversion Complete
reti ;EEPROM Ready
reti ;Analog Comparator
reti ;2-wire Serial
reti ;SPM Ready
reset:
ldi r16, high(RAMEND) ;Stack pointer setting
out SPH, r16
ldi r16, low(RAMEND)
out SPl, r16
ldi r16, 0b00000010 ;PB1 output setting
out DDRB, r16
ldi r16, 0b00000010 ;PB1 high level outpit
out PORTB, r16
loop:
rjmp loop
アセンブルします。
$ avra -l mega88_led.lst mega88_led.asm (アセンブラリストも作成)
$ avra -l mega88_led.lst mega88_led.asm
AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)
Copyright (C) 1998-2010. Check out README file for more info
AVRA is an open source assembler for Atmel AVR microcontroller family
It can be used as a replacement of 'AVRASM32.EXE' the original assembler
shipped with AVR Studio. We do not guarantee full compatibility for avra.
AVRA comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of avra under the terms
of the GNU General Public License.
For more information about these matters, see the files named COPYING.
Pass 1...
/usr/share/avra/m88def.inc(44) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(48) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(53) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(905) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(906) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(907) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(908) : PRAGMA directives currently ignored
Pass 2...
/usr/share/avra/m88def.inc(44) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(48) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(53) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(905) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(906) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(907) : PRAGMA directives currently ignored
/usr/share/avra/m88def.inc(908) : PRAGMA directives currently ignored
done
Used memory blocks:
Code : Start = 0x0000, End = 0x0023, Length = 0x0024
Assembly complete with no errors.
Segment usage:
Code : 36 words (72 bytes)
Data : 0 bytes
EEPROM : 0 bytes
デバイスに書き込みします。
前述のように、まずはL側ヒューズを書き込みします
avrdude -v -p m88 -c usbasp -P usb -F -e -U lfuse:w:0xe2:m
$ sudo avrdude -v -p m88 -c usbasp -P usb -F -e -U lfuse:w:0xe2:m
[sudo] hogehoge のパスワード:
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 : usbasp
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 : usbasp
Description : USBasp, http://www.fischl.de/usbasp/
avrdude: auto set sck period (because given equals null)
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.02s
avrdude: Device signature = 0x1e930a (probably m88)
avrdude: safemode: lfuse reads as E2
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9
avrdude: erasing chip
avrdude: auto set sck period (because given equals null)
avrdude: reading input file "0xe2"
avrdude: writing lfuse (1 bytes):
Writing | ################################################## | 100% 0.01s
avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xe2:
avrdude: load data lfuse data from input file 0xe2:
avrdude: input file 0xe2 contains 1 bytes
avrdude: reading on-chip lfuse data:
Reading | ################################################## | 100% 0.01s
avrdude: verifying ...
avrdude: 1 bytes of lfuse verified
avrdude: safemode: lfuse reads as E2
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9
avrdude: safemode: Fuses OK (E:F9, H:DF, L:E2)
avrdude done. Thank you.
次にプログラムを書き込みします。
avrdude -v -p m88 -c usbasp -P usb -F -e -U flash:w:mega88_led.hex
$ sudo avrdude -v -p m88 -c usbasp -P usb -F -e -U flash:w:mega88_led.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 "/home/rika/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : usb
Using Programmer : usbasp
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 : usbasp
Description : USBasp, http://www.fischl.de/usbasp/
avrdude: auto set sck period (because given equals null)
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.02s
avrdude: Device signature = 0x1e930a (probably m88)
avrdude: safemode: lfuse reads as E2
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9
avrdude: erasing chip
avrdude: auto set sck period (because given equals null)
avrdude: reading input file "mega88_led.hex"
avrdude: input file mega88_led.hex auto detected as Intel Hex
avrdude: writing flash (72 bytes):
Writing | ################################################## | 100% 0.60s
avrdude: 72 bytes of flash written
avrdude: verifying flash memory against mega88_led.hex:
avrdude: load data flash data from input file mega88_led.hex:
avrdude: input file mega88_led.hex auto detected as Intel Hex
avrdude: input file mega88_led.hex contains 72 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.55s
avrdude: verifying ...
avrdude: 72 bytes of flash verified
avrdude: safemode: lfuse reads as E2
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as F9
avrdude: safemode: Fuses OK (E:F9, H:DF, L:E2)
avrdude done. Thank you.
書き込みが終了すると、PB1に接続したLEDが点灯します。
参考にリストファイルを掲載します。
必要な部分のみ引用しています。
C:000000 940c 001b start: jmp reset ;Reset
C:000002 9518 reti ;IRQ0
C:000003 9518 reti ;IRQ1
C:000004 9518 reti ;PCINT0
C:000005 9518 reti ;PCINT1
C:000006 9518 reti ;PCINT2
C:000007 9518 reti ;Watchdog Timeout
C:000008 9518 reti ;Timer2 CompareA
C:000009 9518 reti ;Timer2 CompareB
C:00000a 9518 reti ;Timer2 Overflow
C:00000b 9518 reti ;Timer1 Capture
C:00000c 9518 reti ;Timer1 CompareA
C:00000d 9518 reti ;Timer1 CompareB
C:00000e 9518 reti ;Timer1 Overflow
C:00000f 9518 reti ;Timer0 CompareA
C:000010 9518 reti ;Timer0 CompareB
C:000011 9518 reti ;Timer0 Overflow
C:000012 9518 reti ;SPI Transfer Complete
C:000013 9518 reti ;USART RX Complete
C:000014 9518 reti ;USART UDR Empty
C:000015 9518 reti ;USART TX Complete
C:000016 9518 reti ;ADC Conversion Complete
C:000017 9518 reti ;EEPROM Ready
C:000018 9518 reti ;Analog Comparator
C:000019 9518 reti ;2-wire Serial
C:00001a 9518 reti ;SPM Ready
reset:
C:00001b e004 ldi r16, high(RAMEND) ;Stack pointer setting
C:00001c bf0e out SPH, r16
C:00001d ef0f ldi r16, low(RAMEND)
C:00001e bf0d out SPl, r16
C:00001f e002 ldi r16, 0b00000010 ;PB1 output setting
C:000020 b904 out DDRB, r16
C:000021 e002 ldi r16, 0b00000010 ;PB1 high level outpit
C:000022 b905 out PORTB, r16
loop:
C:000023 cfff rjmp loop