コンテンツにスキップ

04. Linux で 開発する

AVR-GCC と AVRDUDE をインストールする

1
$ sudo apt install gcc-avr avr-libc avrdude

avr-libcavr/io.hとかが入ったライブラリ。

疎通確認

Linux では USBasp のドライバーをインストールするとかそういう作業は不要らしい。

USBasp を Linux マシンに接続し、疎通確認を行う。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -c usbasp: プログラマのタイプを指定する(USBasp)
# -p m328p: 対象のマイコンの型番を指定する(ATmega328P)
# -v: 詳細な出力を表示する
$ sudo avrdude -c usbasp -p m328p -v

avrdude: Version 7.1
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         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                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         Serial program mode           : yes
         Parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                           Block Poll               Page                       Polled
           Memory Type Alias    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    1      0  4500  4500 0x00 0x00
           hfuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           efuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           lock                    0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
           calibration             0     0     0    0 no          1    1      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 usbasp_spi_set_sck_period() error: cannot set sck period; please check for usbasp firmware update
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)

avrdude done.  Thank you.

マイコンのフラッシュメモリをクリアする

-eで消去できる。

1
$ avrdude -c usbasp -p m328p -e

プログラムを書き込む

16MHz の水晶振動子を使う場合は以下を実行しておく。

1
$ avrdude -c usbasp -p m328p -U lfuse:w:0xe7:m

適当に L チカするプログラムを書く。(以下はPB5に LED がつながっている想定)

main.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
  DDRB  = 0b11111111;   // PBxのピンのうち、全てを出力に設定する
  PORTB = 0b00000000;   // PBxのピン全ての出力を0にする

  while (1) {
    PORTB ^= 0b00100000;    // PB5をビット反転させる
    _delay_ms(1000);
  }

  return 0;
}

Makefile

以下の構成のときの簡易的な Makefile を残しておく。

1
2
3
4
5
6
.
├── Makefile
├── bin
├── obj
└── src
    └── main.c

インデントはタブに変換すること。でないとエラーが出る。

Makefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
bin/main.hex: obj/main.elf
    avr-objcopy -O ihex obj/main.elf bin/main.hex

obj/main.elf: src/main.c
    avr-gcc -Os -mmcu=atmega328p -o obj/main.elf src/main.c

build: bin/main.hex

write:
    avrdude -c usbasp -p m328p -P usb -U flash:w:"bin/main.hex":i

clear:
    avrdude -c usbasp -p m328p -e

clean:
    rm obj/*.elf
    rm bin/*.hex

make buildでバイナリを生成し、make writeでマイコンにバイナリを書き込む。