Raspberry Pi AVRを始めよう(環境を構築する)
Raspberry PiではAVR StudioのようなGUIのソフトウェアはありませんがコマンドラインベースのツールとライタがあればAVRのソフト開発は問題なく行えると思います。
インストール
下記を実行すると依存するパッケージもインストールされます。
# apt-get install gcc-avr # apt-get install avr-libc
書き込みソフトのインストール
# apt-get install avrdude
以上でインストールは完了です。
ライタ(USBasp)
ATMELの純正AVRISP mkIIを使っている方が多いかと思いますがUSBaspはArduino IDEからも利用できる、また安い、自作も可能という点で現在はUSBaspを常用しています。
初期の状態ではUSBaspはroot権限でしかアクセスできないためuserでアクセスできるように新たに設定ファイルを作成します。
# cd /etc/udev/rules.d # vi 99-USBasp.rules ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05dc", GROUP="users", MODE="0666"
ハードウェア
普通はブレッドボード+ライタというパターンが定番と思いますが、当方の環境は秋月のAE-ATmega基板+USBaspライタという構成をとっています。
このボードにはUSBシリアル変換が乗ってないため単にAVRボードとして動作しています。
IC部にはピンヘッダを立ててブレイクアウトボード風に
IC部のオスピンは7.7mmのロープロファイルピンヘッダを使用、抜き差ししやすいように
USBシリアル変換部はUSBaspの固定とUSBからの電源(5V)だけをボード側に供給しています。
Arduino用ユニバーサル基板は25mmの連結ピンヘッダを立ててICのでっぱりを逃しています。
この上に小さいブレッドボードを置いて簡単な実験はすべてボード上ですませられるので非常にお手軽です。
ISP端子も常時接続してるのでArduino感覚で扱えます。(Arduinoは持ってないのですが : )
さて本題に
ターミナルから動作を確認してみます。
% avrdude -c usbasp -p m168p avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e940b avrdude: safemode: Fuses OK avrdude done. Thank you.
次はやっぱりLチカですね
Arduino互換ボードですのでピン19 PB5(SCK)にLEDがぶら下がっています。
以下のファイル(test.c)を用意します。
Arduinoからスケッチの例、Blink風に書いてみました。
#define F_CPU 16000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRB = 0x20; PORTB = 0x00; while(1) { PORTB = 0x20; _delay_ms(1000); PORTB = 0x00; _delay_ms(1000); } return 0; }
コンパイルします。
$ avr-gcc -mmcu=atmega168p -Wall -Os test.c $ avr-objcopy -O ihex -R .eeprom a.out test.hex
mcuに書き込みます。
$ avrdude -c usbasp -p m168p -U flash:w:test.hex:a avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.01s avrdude: Device signature = 0x1e940b 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 "test.hex" avrdude: input file test.hex auto detected as Intel Hex avrdude: writing flash (180 bytes): Writing | ################################################## | 100% 0.05s avrdude: 180 bytes of flash written avrdude: verifying flash memory against test.hex: avrdude: load data flash data from input file test.hex: avrdude: input file test.hex auto detected as Intel Hex avrdude: input file test.hex contains 180 bytes avrdude: reading on-chip flash data: Reading | ################################################## | 100% 0.04s avrdude: verifying ... avrdude: 180 bytes of flash verified avrdude: safemode: Fuses OK avrdude done. Thank you.
LEDが点滅を始めます。
Raspberry Piでも特に問題ありませんね。
avr-gccの -mmcu 指定
$ avr-gcc --target-help | less
avrdudeの -p 指定
$ avrdude -v
で調べます。
Raspbian(debian)ではattiny10のサポート(avr-gcc)が無いようです。