Trying to program a factory ATmega16u2

Hello all,

I recently purchased a factory ATmega16u2 and am trying to program it to run as the USB to Serial converter for the Mega2560 chip. I have gone through this forum post and tried Nick Gammon’s ATmega_Board_Programmer file. The program works, as seen in the below serial output. However the device manager still sees it as an “Unknown Device” I have tried to enter the chip into DFU mode by shorting reset and ground, but the device still appears as an “Unknown device”, and I cannot install the driver from the Atmel 3.4.7 install location, which I would then use FLIP to program(which I have done before easily). Any suggestions?

Atmega chip programmer.
Written by Nick Gammon.
Version 1.38
Compiled on Jul 31 2018 at 06:51:27 with Arduino IDE 10805.
Attempting to enter ICSP programming mode ...
Entered programming mode OK.
Signature = 0x1E 0x94 0x89 
Processor = ATmega16U2
Flash memory size = 16384 bytes.
LFuse = 0xEF 
HFuse = 0xD9 
EFuse = 0xF4 
Lock byte = 0xCF 
Clock calibration = 0x8B 
Bootloader address = 0x3000
Bootloader length = 3380 bytes.
Type 'Q' to quit, 'V' to verify, or 'G' to program the chip with the bootloader ...
Erasing chip ...
Writing bootloader ...
Committing page starting at 0x3000
Committing page starting at 0x3080
Committing page starting at 0x3100
Committing page starting at 0x3180
Committing page starting at 0x3200
Committing page starting at 0x3280
Committing page starting at 0x3300
Committing page starting at 0x3380
Committing page starting at 0x3400
Committing page starting at 0x3480
Committing page starting at 0x3500
Committing page starting at 0x3580
Committing page starting at 0x3600
Committing page starting at 0x3680
Committing page starting at 0x3700
Committing page starting at 0x3780
Committing page starting at 0x3800
Committing page starting at 0x3880
Committing page starting at 0x3900
Committing page starting at 0x3980
Committing page starting at 0x3A00
Committing page starting at 0x3A80
Committing page starting at 0x3B00
Committing page starting at 0x3B80
Committing page starting at 0x3C00
Committing page starting at 0x3C80
Committing page starting at 0x3D00
Written.
Verifying ...
No errors found.
Writing fuses ...
LFuse = 0xEF 
HFuse = 0xD9 
EFuse = 0xF4 
Lock byte = 0xCF 
Clock calibration = 0x8B 
Done.
Programming mode off.
Type 'C' when ready to continue with another chip ...

I have heard that the lock byte has to be 0x0F in order to install drivers, but I cannot seem to make the program set the lock byte to that value.

Edit: I am able to upload normally now, thank you everyone for your help!

C:\Program Files (x86)\Arduino\hardware\arduino\firmwares\atmegaxxu2

Check README.txt

Modify the avrdude command for your programmer and add Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex

Notice I changed the -p atmega16u2 , the -c usbasp and the Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex
If you are using arduino as isp you would use -cstk500v1 -P/dev/ttyACM0 -b19200

One command does everything. I don’t use -F and specify the actual chip being programmed.

avrdude -p atmega16u2 -c usbasp -U flash:w:Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m -U efuse:w:0xF4:m -U lock:w:0x0F:m

Bus 003 Device 004: ID 2341:0042 Arduino SA Mega 2560 R3 (CDC ACM)

Bus 003 Device 005: ID 03eb:2fef Atmel Corp. ******DFU MODE

Thank you for your reply. I have tried the following commands with the following results. I am new to avrdude so please contain any giggles at my stupidity.

Attempt1
Attempt1.PNG

Attempt2
Attempt2.PNG

Attempt3
Attempt3.PNG

Attempt4
Attempt4.PNG

What programmer do you have, and how do you have it hooked up?

Change -pusb162 to -m16u2 for atmega16u2.

I am using an Uno running the ArduinoISP sketch, connected to the ICSP header for the other board (see picture below). I also made sure to use the 10uF cap between reset and ground on the programmer board.

-pm16u2 is said to not be not found by avrdude. It gives me a list and one of the options is usb162 for at90usb162, which is the 16u2

The picture sucks, so here is what I have connected:
Uno 16u2 ICSP
5V 5V
11 MOSI
Gnd Gnd
12 MISO
13 SCK
10 Reset

board hookup.jpg

Here is a better picture on the pads for the 16u2. I made my own package so that I could use a QFP or a QFN (I hand-soldered the QFN one time and I hated it). The ICSP header is also rotated 180 degrees, but is still in the same layout.

16u2.jpg

I just used this command to program the atmega16u2 using "Arduino as ISP".

~/ken/Downloads/arduino-1.8.5/hardware/arduino/avr/firmwares/atmegaxxu2 $ avrdude -p m16u2 -v  -cstk500v1 -P/dev/ttyUSB0 -b19200 -U flash:w:Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m -U efuse:w:0xF4:m -U lock:w:0x0F:m

I am using Linux Mint and avrdude version 6.3. m16u2 = ATmega16U2

Your Windows may be using something different from my -cstk500v1 -P~~/dev/ttyUSB0~~ -b19200 I am using a clone Uno.

Maybe check a command from the Arduino IDE.

The fact you could do the bootloader using Gammon's programmer tells me this should work.

I tried the following command and still no luck. The only difference is the path where I issue the command.

Attempt5.PNG

I still do not see any m16u2 option. I am running the latest avrdude (downloaded winavrfrom scourgefourge yesterday)

I am on a windows 7 laptop. I also tried the Nick Gammon sketch on my Windows 10 laptop last night and that worked as well. The avrdude commands all returned that the device ID was 0x000000, but I tried the -F command and still got the “not in sync” error.

For the programmer name, I referred to the forum post I have linked in the first post of this thread. I also tried this instructables post to no avail.

What I am going to try to do is turn on verbose output during the Nick Gammon upload and see if I can use those in a command window.

Okay I have made some headway. I had the Uno programmed with Nick Gammons sketch and not ArduinoISP (omg im dumb). I uploaded the ISP sketch and re-ran the command I last posted and got this (two pictures)

Success_1_1.PNG

Success_1_2.PNG

When I use FLIP, for some reason I cannot use the COMBINED Mega hex file, but I can use the one with the shorter title meant for the 8u2, so I tried that instead, and got this (two pictures again)

Success_2_1.PNG

Success_2_2.PNG

We’re so close!! I’m still not sure what to do though, and I haven’t yet looked up Nick Gammon’s commands yet.

According to the verbose output, the device is in safemode, but the command needs immediate mode. Does this mean I have to do the reset to ground short?

I did some googling and found this article that gives some info on avrdude commands. Specifically that the safemode means that the fuses are not to be changed, and if they are, change them back to the originals. Supposedly, calling “-u” enables safemode, so I dont know how to disable it.

Just remove the :M from the -U flash:w:Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex:M

I did, and the bootloader did upload and verify (I think), as well as the fuses. However, when I go to device manager, I still cannot update the driver. I uploaded Nick Gammons sketch again and I got the following output (I did not program anything with the sketch, just checking).

Atmega chip programmer.
Written by Nick Gammon.
Version 1.38
Compiled on Jul 31 2018 at 14:32:56 with Arduino IDE 10805.
Attempting to enter ICSP programming mode ...
Entered programming mode OK.
Signature = 0x1E 0x94 0x89 
Processor = ATmega16U2
Flash memory size = 16384 bytes.
LFuse = 0xFF 
HFuse = 0xD9 
EFuse = 0xF4 
Lock byte = 0xCF 
Clock calibration = 0x8B 
Bootloader address = 0x3000
Bootloader length = 3380 bytes.
Type 'Q' to quit, 'V' to verify, or 'G' to program the chip with the bootloader ...

As you can see from the original post, the LFuse has changed from 0xEF to 0xFF, which is good, but the lock fuse is still at 0xCF.

Update: I checked a good mega board that I have and used Nick Gammons sketch to check it. The lock byte was 0xFF. I then used avrdude to program that as the lock byte, but supposedly the write failed. I then checked with Nick's sketch again and the lock byte is now 0xFF, and yet I still cannot install the driver.

Last update for now: I set the fuses to 0xFF, 0xD9, 0xF4, and 0xFF for low, high, extended, and lock. I'm going to bring the board home and see if my laptop can install the driver. If not, I'll try to replace the qfp with a qfn and see how that works.

Ok so somehow the atmega now shows up in the device manager as an actual COM port and as "Arduino Mega 2560" or something, so that part is good. How on earth it didn't work before is beyond me, but thank you so much for your help with that.

With that said, when I try to upload the blink sketch to the main 2560 chip, I am getting the error

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

I also noticed that the SPI lines on the 2560 are not at 5V, but rather are floating around 0V. This chip is not a factory unit, but is a chip that I desoldered from an Elegoo Mega2560 that I got from Amazon. I desoldered by heating the PCB up from underneath with a heat gun and letting that heat up the pins. I then removed the chip with tweezers (carefully) and soldered it onto the new PCB. All the 5V lines are about 4.85V and all the ground pins are 0V. Could I have destroyed the chip?

This has happened to me once before when I desoldered from the same method. However, I previously desoldered this first chip heating it up from the top and resoldered it in the same PCB and it worked fine.

Try burning the bootloader on the 2560 chip.

I have tried the Nick Gammon sketch, and the regular Arduino as ISP method. Both cannot connect to the 2560. The regular bootloader method says the device ID is 0x000000. I will try to use plain avrdude to get it to work. Where is the bootloader for the 2560?

I am trying to do the main avrdude commands, but the device signature is still 0x000000. I have used the -F command to ignore this.

The fuses being read are all 0. What should I put them as?

I found a hex file in arduino/avr/bootloaders/stk500v2

Using the -F is a waste of time in almost every case. If you cannot talk to the chip, you cannot change the fuses.The device signature is 0x000000 is usually an indication you are not communicating with the target chip. I can get the same thing with nothing connected to the target chip. Check wiring and crystal.

Your ISP leads are on pins 50,51 and 52 on the 2560 chip,right?

Yes here are the following connections:

Uno(ISP) Mega2560 5V 5V GND GND 10 Reset 11 51 12 50 13 52

I get 0.71V on XTAL1, 0.83V on XTAL2, and neither are shorted to the ground pin (its a 3-pin resonator). I also have a 1M resistor between the two XTAL channels.

Would desoldering and resoldering the chip remove or corrupt the bootloader?