Arduino Pt.4

Arduino Pt.4

Doubling the Arduino Flash Memory once again

Times really flys when you’re having fun – it has been more two years since I posted this journal entry about how to double the Arduino board’s Flash memory by replacing the ATmega8 with an ATmega168 microcontroller. Lots has changed since then, e.g. the Arduino project now includes several board designs and layouts, most of which feature the ATmega168.

For the last couple of months now, the even better AVR 8-Bit RISC ATmega328P micro-controller was hard get. However, it’s now more broadly available and four of these 8-Bit Micros have arrived today, shipped form one of my favorite Online-Electronics stores: SparkFun. The ATmega328P features 32 KBytes of Flash, 2 KBytes of RAM, and 1 KByte of EEPROM a nice improvement over Arduino’s original configuration with an ATmega8: (8 KBytes of flash, 1 KByte of RAM, and 512 Bytes of EEPROM).
Time to revisit the Arduino Project, an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software, intended for anyone interested in creating interactive objects or environments.

Burn Baby Burn .. Again

The ATmega328 comes in the same 28-lead PDIP packaging and replacing the chip was straight forward and took only a couple of seconds; burning the boot loader on the other hand – just like last time – took a little longer. The Arduino board has the AVR typical 6 header ICSP, an in-system programming port, which allows burning a boot loader without removing the microcontroller chip from the board. At least for Mac users, I recommend AVR’s AVRISP mkII (ATAVRIPS2-ND), available for $34 at DigiKey.
The AVRISP can program newer 8-bit RISC microcontrollers (with ICSP Interface) through the USB port. The target board still needs to be powered and its regular ports should be disconnected from other devices.

Since switching form the ATmege8 to the 16, the Arduino’s Development Environment has also switched to avrdude, the software that loads programs from your machine onto the chips – manipulates the ROM and EEPROM contents of AVR microcontrollers. To use avrdude successfully with the new ATmega3258P, you may need to update its configuration file somewhere like here: /Applications/arduino-0013/hardware/tools/avr/etc, (look for the configuration file in theResources areas below).

Preparation …

At this point, the Arduino’s microcontroller had been replaced with the 32 KByte ATmega328P and everything but the ICSP port had been disconnected. The Arduino board itself was powered by a 7.5V 500mA power supply and the AVRISP-mkII connected the board with the Mac’s USB port.
On the software side, avrdude 5.5 was installed, the avrdude.conf file had been updated, and the ATmega328 boot-loader was made available here/AT328.hex, (look for the configuration file in the Resources areas below).

Burning, Fuses, and Flashing the Bootloader

Burning a new boot loader is a three-step process: unlocking the boot loader segment, uploading the new boot loader, and looking boot loader segment again. For an AVR microcontroller, the closest thing to a configuration file is its set of fuses. Fuses are used to configure important system parameters and a few important but obscure options can only be set through the ICSP. For instance, the ATmega328 defaults to the use of a 1 MHz internal clock and would ignoring the 16 MHz crystal on the Arduino board, if not told otherwise, through a fuse setting.

  1. Unlocking the boot loader segment, erase the chip, and set fuses (set boot loader size to 2K words [=2KByte]: 0×3800-0xFFFF, set clock speed to external 16 MHz)
    avrdude -p m328p -b 115200 -P usb -c avrispmkII -V -e -U lock:w:0x3F:m -U hfuse:w:0xD8:m -U lfuse:w:0xFF:m -U efuse:w:0x03:m
  2. Uploading the boot loader
    avrdude -p m328p -b 115200 -P usb -c avrispmkII -V -D -U flash:w:/AT328.hex:i
  3. Re-locking the boot loader segment
    avrdude -p m328p -b 115200 -P usb -c avrispmkII -V -U lock:w:0x0F:m

Check out the Atmel AVR Fuse Calculator for more details.

Burn Log

wpbook:~ wolf$ avrdude -p m328p -b 115200 -P usb -c avrispmkII -V -e -U lock:w:0x3F:m -U hfuse:w:0xD8:m -U lfuse:w:0xFF:m -U efuse:w:0×03:m
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.10s
avrdude: Device signature = 0x1e950f
avrdude: erasing chip
avrdude: reading input file “0x3F”
avrdude: writing lock (1 bytes):
Writing | ################################################## | 100% 0.03s
avrdude: 1 bytes of lock written
avrdude: reading input file “0xD8″
avrdude: writing hfuse (1 bytes):
Writing | ################################################## | 100% 0.10s
avrdude: 1 bytes of hfuse written
avrdude: reading input file “0xFF”
avrdude: writing lfuse (1 bytes):
Writing | ################################################## | 100% 0.03s
avrdude: 1 bytes of lfuse written
avrdude: reading input file “0×03″
avrdude: writing efuse (1 bytes):
Writing | ################################################## | 100% 0.10s
avrdude: 1 bytes of efuse written
avrdude: safemode: Fuses OK
avrdude done.  Thank you.
wpbook:~ wolf$ avrdude -p m328p -b 115200 -P usb -c avrispmkII -V -D -U flash:w:/AT328.hex:i
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.10s
avrdude: Device signature = 0x1e950f
avrdude: reading input file “/AT328.hex”
avrdude: writing flash (32670 bytes):
Writing | ################################################## | 100% 63.68s
avrdude: 32670 bytes of flash written
avrdude: safemode: Fuses OK
avrdude done.  Thank you.
wpbook:~ wolf$ avrdude -p m328p -b 115200 -P usb -c avrispmkII -V -U lock:w:0x0F:m
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.10s
avrdude: Device signature = 0x1e950f
avrdude: reading input file “0x0F”
avrdude: writing lock (1 bytes):
Writing | ################################################## | 100% 0.10s
avrdude: 1 bytes of lock written
avrdude: safemode: Fuses OK
avrdude done.  Thank you.
wpbook:~ wolf$

 

Arduino IDE

After removing the AVRISP mkII In-System Programmer and changing the power-selector on the Arduino board, it was time to fire-up the Arduino IDE, but not before replacing the board definition file, required to make the IDE recognize the new micorcontroller, (look for the configuration file in the Resources areas below). Now the ATmega328 can be set by selecting Arduino w/ ATmege328 from the Tools/Board menu.
The Fibonacci program was quickly uploaded and running – the IDE reported Binary sketch size: 2020 bytes (of a 30720 byte maximum), which makes sense, considering that we set the efuse to preserve 2K words for the boot loader.

 

Fibonacci Program Output for the 1st 39 fibs
fib(0)= 0
fib(1)= 1
fib(2)= 1
fib(3)= 2
fib(4)= 3
fib(5)= 5
fib(6)= 8
fib(7)= 13
fib(8)= 21
fib(9)= 34
fib(10)= 55
fib(11)= 89
fib(12)= 144
fib(13)= 233
fib(14)= 377
fib(15)= 610
fib(16)= 987
fib(17)= 1597
fib(18)= 2584
fib(19)= 4181
fib(20)= 6765
fib(21)= 10946
fib(22)= 17711
fib(23)= 28657
fib(24)= 46368
fib(25)= 75025
fib(26)= 121393
fib(27)= 196418
fib(28)= 317811
fib(29)= 514229
fib(30)= 832040
fib(31)= 1346269
fib(32)= 2178309
fib(33)= 3524578
fib(34)= 5702887
fib(35)= 9227465
fib(36)= 14930352
fib(37)= 24157817
fib(38)= 39088169
fib(39)= 63245986

Resources