0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LinuxでATmega328Pの開発を行う

Posted at

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

$ sudo apt install gcc-avr avr-libc avrdude

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

疎通確認

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

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

# -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で消去できる。

$ avrdude -c usbasp -p m328p -e

プログラムを書き込む

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

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

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

main.c
#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 を残しておく。

.
├── Makefile
├── bin
├── obj
└── src
    └── main.c

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

Makefile
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でマイコンにバイナリを書き込む。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?