LoginSignup
3
1

More than 3 years have passed since last update.

Arduino Nano V3 のプログラムサイズを最大化する

Last updated at Posted at 2020-02-26

はじめに

Arduino Nano V3は、Arduino UNO R3と同じくATmega328pを搭載していながらも、サイズが小さく、何かに組みこむ用途で使いやすいボードです。
https://store.arduino.cc/usa/arduino-nano

初期開発は使い慣れたArduino UNOで行い、最終的にArduino Nanoでコンパクトに仕上げる、としたいところですが、実はUNOとNanoでプログラムの最大サイズが異なります。

Arduino UNO: 32256 バイト
Arduino Nano: 30720 バイト

このため、サイズが大きいプログラムについては、UNOで開発していた時には書き込めたのに、Nanoに変えたら容量不足で書き込めなくなった、という問題が起こります。

この記事では、Arduino Nanoのプログラム最大サイズを拡張して、NanoをコンパクトなArduino UNOとして使えるようにする方法を説明します。

2018年あたりの出荷品から、Nanoの初期出荷仕様が変更となり、bootloaderはoptibootになり、フラッシュ書込み速度が115200bpsに上がってはいますが、2020年2月現在もプログラムの最大サイズは変わっておらずです。
実はArduino開発元に、Nanoのプログラム最大サイズを広げるようボード定義の変更をPullRequestしたのですが「製造の都合上Nanoのプログラム最大サイズは変更できない」とのことで、変更差分を取り込んでもらえませんでした。

ハード的にはUNOと同サイズまで使えるところ、設定によって制限がかかっているのはもったいないので、何とか使えるようにするための方法を紹介します。(試す場合は自己責任でお願いします)

準備するもの

ハードウェア

  1. Arduino Nano V3 (ATmega328pを採用しているもの。初期のATmega168を使っているものは対象外です)
  2. ブートローダやFUSEを書き換えるための書込装置。 (この記事ではUSBaspを使う前提で書きます。ArduinoISP等、その他の機器でも問題ありません)
  3. ISP接続ケーブル (Nano上にある6pinのISPピンと書込装置を接続するケーブルを用意します)

ソフトウェア

  1. Arduino IDE (1.8.12)
  2. テキストエディタ

書き換え作業

ボード定義ファイル(boards.txt)の場所を特定する

macOS版の場合は、
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/boards.txt
ただしArduinoIDEをインストールした後にボード定義をバージョンアップしていた場合、
/Users/ユーザ名/Library/Arduino15/packages/arduino/hardware/avr/バージョン/boards.txt

Windows版の場合は、
C:\Program Files (x86)\Arduino\hardware\arduino\avr\boards.txt
ただしArduinoIDEをインストールした後にボード定義をバージョンアップしていた場合、
C:\Users\ユーザ名\AppData\Local\arduino15\packages\arduino\hardware\avr\バージョン\boards.txt

ボード定義ファイル(boards.txt)を書き換える

テキストエディタで boards.txt を書き換えます。

boards.txt(変更前)
(略)
nano.menu.cpu.atmega328.upload.maximum_size=30720
(略)  
nano.menu.cpu.atmega328.bootloader.high_fuses=0xDA
(略)
boards.txt(変更後)
(略)
nano.menu.cpu.atmega328.upload.maximum_size=32256
(略)  
nano.menu.cpu.atmega328.bootloader.high_fuses=0xDE
(略)

参考

ArduinoIDE1.8.12付属のboards.txtを変更した差分例
*** boards.txt.orig 2019-12-02 15:51:16.000000000 +0900
--- boards.txt  2020-02-26 21:20:38.000000000 +0900
***************
*** 142,153 ****
  ## --------------------------
  nano.menu.cpu.atmega328=ATmega328P

! nano.menu.cpu.atmega328.upload.maximum_size=30720
  nano.menu.cpu.atmega328.upload.maximum_data_size=2048
  nano.menu.cpu.atmega328.upload.speed=115200

  nano.menu.cpu.atmega328.bootloader.low_fuses=0xFF
! nano.menu.cpu.atmega328.bootloader.high_fuses=0xDA
  nano.menu.cpu.atmega328.bootloader.extended_fuses=0xFD
  nano.menu.cpu.atmega328.bootloader.file=optiboot/optiboot_atmega328.hex

--- 142,153 ----
  ## --------------------------
  nano.menu.cpu.atmega328=ATmega328P

! nano.menu.cpu.atmega328.upload.maximum_size=32256
  nano.menu.cpu.atmega328.upload.maximum_data_size=2048
  nano.menu.cpu.atmega328.upload.speed=115200

  nano.menu.cpu.atmega328.bootloader.low_fuses=0xFF
! nano.menu.cpu.atmega328.bootloader.high_fuses=0xDE
  nano.menu.cpu.atmega328.bootloader.extended_fuses=0xFD
  nano.menu.cpu.atmega328.bootloader.file=optiboot/optiboot_atmega328.hex

Arduino IDE の設定を変更する

Arduino IDE のツールメニューから、ボードの種類と使用する書込装置を選択します。
スクリーンショット 2020-02-26 21.44.34.png
ボード: Arduino Nano
プロセッサ: ATmega328P (他に二つ選択肢がありますがこれを選んでください)
書込装置: USBasp (使用する書込装置にあったものを選択してください)

書込装置とArduino Nanoを接続する

IMG_3106.JPG
書込装置とArduino Nanoを接続します。
(写真はUSBaspとArduino Nanoを10pin-6pin変換ケーブル経由でISP端子に繋いでいる様子です)

Arduino Nano のISPピンに○印がついていますが、それが1ピンです。

ArduinoNanoISPピンアサイン
│●
│ 1 2
│ 3 4
│ 5 6
│

ブートローダ(optiboot)とfuse値を書き込む

「ツール」メニューから
 ブートローダを書き込む
を選びます。

実行ログ例(USBaspを使ってmacOSから書き換えた場合)
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -cusbasp -Pusb -e -Ulock:w:0x3F:m -Uefuse:w:0xFD:m -Uhfuse:w:0xDE:m -Ulfuse:w:0xFF:m 

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/odaki/.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             : 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       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    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.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip
avrdude: auto set sck period (because given equals null)
avrdude: reading input file "0x3F"
avrdude: writing lock (1 bytes):

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -cusbasp -Pusb -Uflash:w:/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex:i -Ulock:w:0x0F:m 

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/odaki/.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             : 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       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    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)
Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3F:
avrdude: load data lock data from input file 0x3F:
avrdude: input file 0x3F contains 1 bytes
avrdude: reading on-chip lock data:

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

avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude: reading input file "0xFD"
avrdude: writing efuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of efuse written
avrdude: verifying efuse memory against 0xFD:
avrdude: load data efuse data from input file 0xFD:
avrdude: input file 0xFD contains 1 bytes
avrdude: reading on-chip efuse data:

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

avrdude: verifying ...
avrdude: 1 bytes of efuse verified
avrdude: reading input file "0xDE"
avrdude: writing hfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xDE:
avrdude: load data hfuse data from input file 0xDE:
avrdude: input file 0xDE contains 1 bytes
avrdude: reading on-chip hfuse data:

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

avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
avrdude: reading input file "0xFF"
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xFF:
avrdude: load data lfuse data from input file 0xFF:
avrdude: input file 0xFF contains 1 bytes
avrdude: reading on-chip lfuse data:

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

avrdude: verifying ...
avrdude: 1 bytes of lfuse verified

avrdude done.  Thank you.

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e950f (probably m328p)
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: reading input file "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex"
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex:
avrdude: load data flash data from input file /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex:
avrdude: input file /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex contains 32768 bytes
avrdude: reading on-chip flash data:

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

avrdude: verifying ...
avrdude: 32768 bytes of flash verified
avrdude: reading input file "0x0F"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0F:
avrdude: load data lock data from input file 0x0F:
avrdude: input file 0x0F contains 1 bytes
avrdude: reading on-chip lock data:

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

avrdude: verifying ...
avrdude: 1 bytes of lock verified

avrdude done.  Thank you.

Arduino NanoとPCの物理接続を変更する

USBaspとNanoを接続しているケーブルをはずし、NanoとPCをUSBケーブルで接続します。
(シリアルポートとして、Nanoと繋がっているポートを選ぶことを忘れずに)

最大サイズが増えたことを確認する

「ファイル」メニューから
 スケッチブック例
   01.Basics
    Blink
を開いて、
「スケッチ」メニューから
 検証・コンパイル
を実行します。
スクリーンショット 2020-02-26 21.42.27.png
コンパイルが成功し、メッセージが最大30720バイトではなく最大32256バイトと表示されていることを確認します。

シリアルポートが適切に選ばれていれば、プログラムの書込みもできるはずです。

目的の(サイズの大きな)プログラムを開いてコンパイルし、Nanoに書込みましょう。

最後に

楽しいArduinoライフを!

3
1
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
3
1