Atmega328 reboots during the reading of the program

Hey there,

I'm using a custom PCB with an Atmega328p, using the internal 8MHz oscillator. For this, I'm using the boards file of the optiboot bootloader, choosing the 32 pins microcontroller, Atmega328p with the 8MHz(int) option. The bootloader was burned using an olimex AVR-ISP-MK2.

I'm using an HC-05 to transfer the firmware over the air, with a Nano on the RX line to look for the 0x30 0x20 bytes that start the firmware transfer, resetting the uC at the right time. It reads the board options just fine, writes the program just fine but sometimes, when reading the program back, the target atmega resets (I notice this because the target PCB has some relays and I hear them opening and closing) and the bluetooth connection is dropped. After that, the IDE just says that the programmer isn't responding and stops programming.

Why would this be happening ? Why would the Atmega be resetting mid reading?

Thanks,

Luís

Here's the output of the error:

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
         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                    : COM4
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xdf
         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 : Arduino
         Description     : Arduino
         Hardware Version: 3
         Firmware Version: 4.4
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\luisf\AppData\Local\Temp\arduino_build_344705/ShortQut_1v94.ino.hex"
avrdude: writing flash (22132 bytes):

Writing | ################################################## | 100% 13.80s

avrdude: 22132 bytes of flash written
avrdude: verifying flash memory against C:\Users\luisf\AppData\Local\Temp\arduino_build_344705/ShortQut_1v94.ino.hex:
avrdude: load data flash data from input file C:\Users\luisf\AppData\Local\Temp\arduino_build_344705/ShortQut_1v94.ino.hex:
avrdude: input file C:\Users\luisf\AppData\Local\Temp\arduino_build_344705/ShortQut_1v94.ino.hex contains 22132 bytes
avrdude: reading on-chip flash data:

Reading | ##############################
avrdude: stk500_paged_load(): (a) protocol error, expect=0x10, resp=0x56
avrdude: stk500_cmd(): programmer is out of sync
avr_read(): error reading address 0x0000
    read operation not supported for memory "flash"
avrdude: failed to read all of flash memory, rc=-2
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x6c

avrdude done.  Thank you.

a porta série 
 selecionada não existe ou a placa não está ligada.

In the instant that it fails, I can hear the relays opening and closing.

I may just waste your time, but I am interested.

Could you give me the general overall hook up information? HC-05, PCB and Nano.

Using Port : COM4
Using Programmer : arduino
Overriding Baud Rate : 57600
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xdf

Do you get this every time you program the PCB?

avrdude: writing flash (22132 bytes):

Writing | ################################################## | 100% 13.80s

Seems like a long time to write.

Sorry about the late response.

Yeah, I do get the avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xdf error everytime, because I need to fail the first sequence of bytes in order to know when the next sequence is comming. So, I check the first two bytes (0x30 and 0x20), wait 9.7 seconds, reset the target atmega and then it receives the new 0x30 0x20 at the correct time.

The write time doesn't bother me at all. What bothers me is how it fails everytime in the read part.

The hookup is simple:

Nano ---- Target Atmega ----- HC-05

D13 ---- Reset
5V -------- 5V ------------------ VCC
GND ----- GND -----------------GND
RX ------- RX ------------------- TX
TX ------------------- RX

Hope this is good enough. Also, there's a much simpler way, removing the Nano altogether, but it still fails in the reading part, which is to add a push-pull configuration to the State pin of the HC-05. That pin is low when there's no connection and high when there's a connection. Using a push-pull with two mosfets, you effectively invert the signal. Adding a 0.1uF cap in series and you can connect this output to the reset pin of the target atmega and it will upload the code.

Still, it fails during the read part. Any ideas?

I notice you are using Pin 13 on the Nano to generate the reset.

This thread might be of interest. Just grasping at straws on my part.

https://forum.arduino.cc/index.php?topic=553621.0

At this moment, I'm not using the Nano anymore. I'm using the State pin in the HC-05 board with the inverter discussed in the previous post in order to generate the reset signal.

But the problem remains. Somewhere during the reading of the flash, the HC-05 stops sending ACKs or whatever it is sending, the atmega gets tired of waiting and resets... But, according to the LED on the HC-05 board, the connection is still open and "functional", since it's blinking twice every 10 seconds or so.

Can you add some -v -v -v to the avrdude command?

Also, a couple of times we have had something grabbing the serial port from another program. I don't know if that would apply to this setup, but it wouldn't hurt to try to pin down.

If I am wasting your time, please just say so and I will quit. I realize this is over my head. :slight_smile:

But, according to the LED on the HC-05 board, the connection is still open and "functional", since it's blinking twice every 10 seconds or so

Any chance the Blinking is generating a reset?

I can cause a failure a lot like yours by hitting the reset button during the Verify/Reading of my sketch.
How about removing your reset lead from the HC05 before the Read/Verify starts.

Regarding your last post, the state pin is not resetting the board during the reading part. I've checked with a scope, and the line stays flat during all the programming stage. With the logic analyzer, I can see that the PC (HC-05) stops acking the blocks of memory the atmega is throwing and that ultimately leads to the watchdog resetting the board.

At this point, the programming part seems fine, even if there's no reading check. But if there comes a time when something in the writing has gone wrong, I would have no way to know...

I'm using the Arduino IDE directly to program and I've turned on the verbose setting, I could try to use the command line to upload the code, but don't think it would help. I guess the connection between the PC and the HC-05 just gets broken, even though the LED on the board signals a connection... I'm saying this because in the end, when the error occurs, it says "The selected COM port doesn't exist or isn't turned on". No other programs are actively opening the COM port (afaik).

You're not wasting my time, sometimes even the smallest things can make us look at something or double check some connection or other parts of code/hardware that could lead us to the solution.

This is an older thread from Stackoverflow . You mentioned using the WDT.

If you want to use the watchdog timer mpflaga has the correct answer although bear in mind depending on your part the watchdog timer may or may not be enabled after the reset, see Atmels page on soft reset here. If your atmega is resetting in the bootloader when you don't expect it to this is likely to be your problem.