Unable to program ATMEGA328P bootloader with Nano as ISP

Hi everyone:

I am having some difficulty programming a new ATMEGA328P-PU IC with a custom bootloader. The bootloader is a recompilation of optiboot so that the microcontroller will use an external 12Mhz crystal (configuration copied below). For a programmer, I am using a Nano-clone, programmed using the ArduinoISP sketch. The only oddity of this Nano board is that it uses the "old bootloader", although I don't think that matters. Also, I'm confident that the 328P is bare as it came straight from the LCSC electronics distributor though I don't know of a way to confirm that.

Each time I attempt to program the bare 328P IC, I receive a series of "stk500_getsync() attempt 1 of 10: not in sync: resp=0x15" messages, followed by "avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x14". The common suggestion for this issue is to install a 10uF capacitor between the reset and ground pins, but doing so then yields "avrdude: Device signature = 0x000000" messages. Verbose logs are copied below for reference.

The breadboard circuit for the 328P is the bare-minimum circuit:

  • 100nF decoupling capacitor across pins 7,8 and 21,22
  • Pins 20,21 connected (voltage reference pins)
  • 12Mhz crystal connected to pins 9,10
  • 22pF capacitors connected to each side of the crystal and ground
  • From the Nano SPI interface:
    • Reset to pin 1
    • MOSI to pin 17
    • MISO to pin 18
    • SCK to pin 19
  • Power is received from the Nano SPI interface too, which measures around 4.7-4.8V

I'm a bit lost and not sure what to check next as this seems like a very standard setup for programming. I found this post that suggests that the lock bits in my configuration may be incorrect, but I don't want to blindly change those from 0F to CF without understanding what this will do.

Is there something wrong with my configuration or methodology?


Optiboot configuration (makefile)

atmega328_12mhz: TARGET = atmega328
atmega328_12mhz: MCU_TARGET = atmega328p
atmega328_12mhz: CFLAGS += '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'
atmega328_12mhz: AVR_FREQ = 12000000L
atmega328_12mhz: LDSECTIONS  = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe
atmega328_12mhz: $(PROGRAM)_atmega328_12mhz.hex
atmega328_12mhz: $(PROGRAM)_atmega328_12mhz.lst

atmega328_12mhz_isp: atmega328
atmega328_12mhz_isp: TARGET = atmega328
atmega328_12mhz_isp: MCU_TARGET = atmega328p
# 512 byte boot, SPIEN
atmega328_12mhz_isp: HFUSE = DE
# Low power xtal (8-16MHz) 16KCK/14CK+65ms
atmega328_12mhz_isp: LFUSE = FF
# 2.7V brownout
atmega328_12mhz_isp: EFUSE = 05
atmega328_12mhz_isp: isp

Boards.txt configuration for 12Mhz programming

##############################################################

atmega328bb12.name=ATmega328 12MHz crystal

# atmega328bb12.vid.0=0x2341
# atmega328bb12.pid.0=0x0043
# atmega328bb12.vid.1=0x2341
# atmega328bb12.pid.1=0x0001
# atmega328bb12.vid.2=0x2A03
# atmega328bb12.pid.2=0x0043
# atmega328bb12.vid.3=0x2341
# atmega328bb12.pid.3=0x0243

atmega328bb12.upload.tool=avrdude
atmega328bb12.upload.protocol=arduino
atmega328bb12.upload.maximum_size=32256
atmega328bb12.upload.maximum_data_size=2048
atmega328bb12.upload.speed=115200

atmega328bb12.bootloader.tool=avrdude
atmega328bb12.bootloader.low_fuses=0xFF
atmega328bb12.bootloader.high_fuses=0xDE
atmega328bb12.bootloader.extended_fuses=0xFD
atmega328bb12.bootloader.unlock_bits=0x3F
atmega328bb12.bootloader.lock_bits=0x0F
atmega328bb12.bootloader.file=optiboot/optiboot_atmega328_12.hex

atmega328bb12.build.mcu=atmega328p
atmega328bb12.build.f_cpu=12000000L
# uno.build.board=AVR_UNO
atmega328bb12.build.core=arduino
atmega328bb12.build.variant=standard

Verbose log without 10uF capacitor (reset->ground)

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM5 -b19200 -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 "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM5
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x15
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x15
         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 : STK500
         Description     : Atmel STK500 Version 1.x firmware

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x14

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x02

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x10
         Hardware Version: 4744608
         Firmware Version: 0.4611299
         Topcard         : STK502
         Vtarget         : 1.8 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

Error while burning bootloader.
avrdude: stk500_initialize(): (b) protocol error, expect=0x10, resp=0x01
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x10

avrdude done.  Thank you.

Verbose log with 10uF capacitor (reset->ground)

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM5 -b19200 -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 "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM5
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         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 : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000 (retrying)

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000 (retrying)

Error while burning bootloader.
Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000
avrdude: Yikes!  Invalid device signature.
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you.

shouldnt the programmer be set to 'Arduino as ISP'
I may be wrong as I dont own a nano.

How to use an arduno Nano as an ISP (In System Programmer) (best-microcontroller-projects.com)

Yes it should, and I have set the programmer as such in the IDE. Note that the example sketch is named differently though: "ArduinoISP".

If you have any doubt about your home made boot loader, just try an 8Mhz one (say LILLYPAD) and temporarily remove the crystal.

I'm a bit confused about exactly how you have the wiring, but you do not connect the nano RESET to the chip being programmed. If I recall correctly, the Arduino as ISP sketch uses pin 10 to reset the processor being programmed.

You're right, thank you! I was looking at the wrong column on this SparkFun page, which has a table showing the programming connections by defined wire color and target processor. (About 1/2 way through article)

That said, I recompiled the ArduinoISP sketch with and without the USE_OLD_STYLE_WIRING define (so that the explicit digital pins are used to the define the target processor SPI interface). Unfortunately, reprogramming now produces the same error as I initially received (stk500 protocol error), copied in my first message. I receive this with and without a 10uF capacitor between the reset pin and ground, as well as with and without a 10K resistor between the reset pin and +5V.

As for my connections, the pins I listed earlier are for the target processor's physical pins. They correspond to:

  • From the Nano SPI interface:
    • Reset to D10 (physical pin 16)
    • MOSI to D11 (physical pin 17)
    • MISO to D12 (physical pin 18)
    • SCK to D13 (physical pin 19)

Just to make sure we have the full picture, please provide the exact connections.

I find this website to be a very useful tool for better understanding AVR configuration fuses:

There's nothing there that is not also in the datasheet, but it's presented in a different format.

I can tell you that the bootloader is not a factor because the process is not even getting to the second step where the bootloader file is flashed to the board. That doesn't tell you what is wrong, but at least you can focus your attention on the relevant factors.

I will recommend that you triple check all your wiring. Do a continuity check on everything. Jumper wires can have internal breaks. Some breadboards also have breaks in unexpected places. Look carefully at your crystal circuit. Even though the chips come from the factory running on the internal oscillator, it's possible that one of your intiial attempts did successfully change the fuses to the external oscillator. If the chip is relying on an external crystal that is not working, you will get this 0x000000 signature (though there are other causes too).

Thank you for your advice, @ino! I triple-checked my connections, then tore everything down and rebuilt the circuit so that everything was on a single breadboard (the Nano was held up in the air due to the tension in the jumper wires I used). I checked the connections again, and verified continuity between the pins on the Nano, pins on the 328P, and the crystal and capacitors. No problems.

It was only after the rebuild and continuity-check did the programming work. I swear, I didn't do anything significantly different in terms of connections -- only the voltage source for the 328P changed from the SPI header to the proper 5V pin (on the Nano). Regardless, there must have been a faulty connection as you suspected!

For reference, here is a document I used for programming at 12Mhz and configuring the Nano as a programmer.

Compiling Optiboot - ATmega328P at custom frequency (tttapa.github.io)

You might want to have a look at MCUdude's MiniCore, it has optiboot pre-compiled for a large variety of options, so you don't have to go through the whole process yourself, or modify the boards.txt file. It would be a good idea to make a backup of your modified files, otherwise an update to the boards package could overwrite boards.txt.

2 Likes

I'm glad to hear it's working now!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.