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?
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
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.
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.
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.
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.
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.