ATmega Hex Uploader

I asked Nick Gammon this question by IM :

Hi Nick,

I have been reading a little bit of your "ATmega Hex uploader" code

I wanted to reuse it in a design of mine.

In your case you used a bit banged solution because you couldn t share the hardware SPI port, between the SDCARD and the other ATmega. In my case i´m able to multiplex the SPI port, i can use the same port to read from the SDCARD and after Write on the ATmega.

Now the question... Do you think that it would suffice (more or less) to modify your "BB_SPItransfer" function to call the "hardware" SPItransfer for the all sketch to work? Can you give possible problems that i would have, if i would do this?

Thank you

Answer:

Just call SPI.transfer instead. They are supposed to be interchangeable.

If you have any more questions please post them to the forum, and send me a link to the thread. Thanks.

Thanks

Nick can you help?

I have been trying to use yout sketch.

I was having problems with my hardware so to easy things and speed up diagnostics, i mounted your setup: an arduino UNO, SDCARD and arduino MEGA.

Untill the moment i´m able to run the sketch on the UNO and see the files in the SDcard. When i tried to program it, it gave me a lot of verification errors, i think that that might be because of the fuses setup or something like that.

So after that i tried to Read the memory to the card. I was expecting that the 2 files (Blink.exe and BREAD.HEX (file read) ) would be equal, but instead they were very different. I´m posting the files so ppl can compare:

Do you have any ideas for the verification errors and the files comparation?

I´m posting the results i get on the serial monitor when running the program first time:

Atmega hex file uploader. Written by Nick Gammon. Version 1.9 Reading SD card ... step1

HEX files in root directory:

BLINK.HEX : 4542 bytes. Created: 2012-08-03 17:44:42. Modified: 2012-08-03 16:14:42 BREAD.HEX : 622639 bytes. Created: 2012-08-03 17:44:42. Modified: 2000-01-01 01:00:00 step2

--------- Starting ---------

Attempting to enter programming mode ... Entered programming mode OK. Signature = 0x1E 0x98 0x01 Processor = ATmega2560 Flash memory size = 262144 bytes. LFuse = 0xFF HFuse = 0xD8 EFuse = 0xFD Lock byte = 0xFF Clock calibration = 0xA8 Actions: [E] erase flash [F] modify fuses [L] list directory [R] read from flash (save to disk) [V] verify flash (compare to disk) [W] write to flash (read from disk) Enter action:

and the verification errors when writting:

Choose disk file [ ] ... BLINK.HEX Processing file: BLINK.HEX Checking file ...

Lowest address = 0x0 Highest address = 0x647 Bytes to write = 1608 No bootloader. Suggest making high fuse = 0xD9 Attempting to enter programming mode ... Entered programming mode OK. Processing file: BLINK.HEX Erasing chip ... Writing flash ...

Written. Processing file: BLINK.HEX Verifying flash ... Verification error at address 0. Got: 0x00 Expected: 0x0C Verification error at address 1. Got: 0x00 Expected: 0x94 Verification error at address 2. Got: 0x01 Expected: 0x0C Verification error at address 4. Got: 0x02 Expected: 0x0C Verification error at address 5. Got: 0x02 Expected: 0x94 Verification error at address 6. Got: 0x03 Expected: 0x2E Verification error at address 7. Got: 0x03 Expected: 0x01 Verification error at address 8. Got: 0x04 Expected: 0x0C Verification error at address 9. Got: 0x04 Expected: 0x94 Verification error at address A. Got: 0x05 Expected: 0x2E Verification error at address B. Got: 0x05 Expected: 0x01 Verification error at address C. Got: 0x06 Expected: 0x0C Verification error at address D. Got: 0x06 Expected: 0x94 Verification error at address E. Got: 0x07 Expected: 0x2E Verification error at address F. Got: 0x07 Expected: 0x01 Verification error at address 10. Got: 0x08 Expected: 0x0C Verification error at address 11. Got: 0x08 Expected: 0x94 Verification error at address 12. Got: 0x09 Expected: 0x2E Verification error at address 13. Got: 0x09 Expected: 0x01 Verification error at address 14. Got: 0x0A Expected: 0x0C Verification error at address 15. Got: 0x0A Expected: 0x94 Verification error at address 16. Got: 0x0B Expected: 0x2E Verification error at address 17. Got: 0x0B Expected: 0x01 Verification error at address 19. Got: 0x0C Expected: 0x94 Verification error at address 1A. Got: 0x0D Expected: 0x2E Verification error at address 1B. Got: 0x0D Expected: 0x01 Verification error at address 1C. Got: 0x0E Expected: 0x0C Verification error at address 1D. Got: 0x0E Expected: 0x94 Verification error at address 1E. Got: 0x0F Expected: 0x2E Verification error at address 1F. Got: 0x0F Expected: 0x01 Verification error at address 20. Got: 0x10 Expected: 0x0C Verification error at address 21. Got: 0x10 Expected: 0x94 Verification error at address 22. Got: 0x11 Expected: 0x2E Verification error at address 23. Got: 0x11 Expected: 0x01 Verification error at address 24. Got: 0x12 Expected: 0x0C Verification error at address 25. Got: 0x12 Expected: 0x94 Verification error at address 26. Got: 0x13 Expected: 0x2E Verification error at address 27. Got: 0x13 Expected: 0x01 Verification error at address 28. Got: 0x14 Expected: 0x0C Verification error at address 29. Got: 0x14 Expected: 0x94 Verification error at address 2A. Got: 0x15 Expected: 0x2E Verification error at address 2B. Got: 0x15 Expected: 0x01 Verification error at address 2C. Got: 0x16 Expected: 0x0C Verification error at address 2D. Got: 0x16 Expected: 0x94 Verification error at address 2E. Got: 0x17 Expected: 0x2E Verification error at address 2F. Got: 0x17 Expected: 0x01 Verification error at address 30. Got: 0x18 Expected: 0x0C Verification error at address 31. Got: 0x18 Expected: 0x94 Verification error at address 32. Got: 0x19 Expected: 0x2E Verification error at address 33. Got: 0x19 Expected: 0x01 Verification error at address 34. Got: 0x1A Expected: 0x0C Verification error at address 35. Got: 0x1A Expected: 0x94 Verification error at address 36. Got: 0x1B Expected: 0x2E Verification error at address 37. Got: 0x1B Expected: 0x01 Verification error at address 38. Got: 0x1C Expected: 0x0C Verification error at address 39. Got: 0x1C Expected: 0x94 Verification error at address 3A. Got: 0x1D Expected: 0x2E Verification error at address 3B. Got: 0x1D Expected: 0x01 Verification error at address 3C. Got: 0x1E Expected: 0x0C Verification error at address 3D. Got: 0x1E Expected: 0x94 Verification error at address 3E. Got: 0x1F Expected: 0x2E Verification error at address 3F. Got: 0x1F Expected: 0x01 Verification error at address 40. Got: 0x20 Expected: 0x0C Verification error at address 41. Got: 0x20 Expected: 0x94 Verification error at address 42. Got: 0x21 Expected: 0x2E Verification error at address 43. Got: 0x21 Expected: 0x01 Verification error at address 44. Got: 0x22 Expected: 0x0C Verification error at address 45. Got: 0x22 Expected: 0x94 Verification error at address 46. Got: 0x23 Expected: 0x2E Verification error at address 47. Got: 0x23 Expected: 0x01 Verification error at address 48. Got: 0x24 Expected: 0x0C Verification error at address 49. Got: 0x24 Expected: 0x94 Verification error at address 4A. Got: 0x25 Expected: 0x2E Verification error at address 4B. Got: 0x25 Expected: 0x01 Verification error at address 4C. Got: 0x26 Expected: 0x0C Verification error at address 4D. Got: 0x26 Expected: 0x94 Verification error at address 4E. Got: 0x27 Expected: 0x2E Verification error at address 4F. Got: 0x27 Expected: 0x01 Verification error at address 50. Got: 0x28 Expected: 0x0C Verification error at address 51. Got: 0x28 Expected: 0x94 Verification error at address 52. Got: 0x29 Expected: 0x2E Verification error at address 53. Got: 0x29 Expected: 0x01 Verification error at address 54. Got: 0x2A Expected: 0x0C Verification error at address 55. Got: 0x2A Expected: 0x94 Verification error at address 56. Got: 0x2B Expected: 0x2E Verification error at address 57. Got: 0x2B Expected: 0x01 Verification error at address 58. Got: 0x2C Expected: 0x0C Verification error at address 59. Got: 0x2C Expected: 0x94 Verification error at address 5A. Got: 0x2D Expected: 0x2E Verification error at address 5B. Got: 0x2D Expected: 0x01 Verification error at address 5C. Got: 0x2E Expected: 0x0C Verification error at address 5D. Got: 0x2E Expected: 0x94 Verification error at address 5E. Got: 0x2F Expected: 0x4A Verification error at address 5F. Got: 0x2F Expected: 0x01 Verification error at address 60. Got: 0x30 Expected: 0x0C Verification error at address 61. Got: 0x30 Expected: 0x94 Verification error at address 62. Got: 0x31 Expected: 0x2E Verification error at address 63. Got: 0x31 Expected: 0x01 Verification error at address 64. Got: 0x32 Expected: 0x0C Verification error at address 65. Got: 0x32 Expected: 0x94 Verification error at address 66. Got: 0x33 Expected: 0x2E

1604 verification error(s). First 100 shown. No bootloader. Setting high fuse = 0xD9 Done.

I Forgot to add, when i Erase the Flash, it seems to erase it, because after that i have to re-program the bootloader and after the code trough the arduino IDE.

Adding more info...

Tried to writte the ASCIItable example, result:

--------- Starting ---------

Attempting to enter programming mode ... Entered programming mode OK. Signature = 0x1E 0x98 0x01 Processor = ATmega2560 Flash memory size = 262144 bytes. LFuse = 0xFF HFuse = 0xD8 EFuse = 0xFD Lock byte = 0xFF Clock calibration = 0xA8 Actions: [E] erase flash [F] modify fuses [L] list directory [R] read from flash (save to disk) [V] verify flash (compare to disk) [W] write to flash (read from disk) Enter action: W

Choose disk file [ ASCIIT~2.HEX ] ... ASCIIT~2.HEX Processing file: ASCIIT~2.HEX Checking file ...

Lowest address = 0x0 Highest address = 0xBF1 Bytes to write = 3058 No bootloader. Suggest making high fuse = 0xD9 Attempting to enter programming mode ... Entered programming mode OK. Processing file: ASCIIT~2.HEX Erasing chip ... Writing flash ...

Written. Processing file: ASCIIT~2.HEX Verifying flash ...

No errors found. No bootloader. Setting high fuse = 0xD9 Done.

But, when i try to verify/compare it with the ASCIItable.HEX, result:

--------- Starting ---------

Attempting to enter programming mode ... Entered programming mode OK. Signature = 0x1E 0x98 0x01 Processor = ATmega2560 Flash memory size = 262144 bytes. LFuse = 0xFF HFuse = 0xD9 EFuse = 0xFD Lock byte = 0xFF Clock calibration = 0xA8 Actions: [E] erase flash [F] modify fuses [L] list directory [R] read from flash (save to disk) [V] verify flash (compare to disk) [W] write to flash (read from disk) Enter action: V

Choose disk file [ ASCIIT~2.HEX ] ... ASCIIT~2.HEX Processing file: ASCIIT~2.HEX Checking file ...

Lowest address = 0x0 Highest address = 0xBF1 Bytes to write = 3058 No bootloader. Suggest making high fuse = 0xD9 Attempting to enter programming mode ... Entered programming mode OK. Processing file: ASCIIT~2.HEX Verifying flash ... Verification error at address 0. Got: 0xFF Expected: 0x0C Verification error at address 1. Got: 0xFF Expected: 0x94 Verification error at address 2. Got: 0xFF Expected: 0x73 Verification error at address 3. Got: 0xFF Expected: 0x00 Verification error at address 4. Got: 0xFF Expected: 0x0C Verification error at address 5. Got: 0xFF Expected: 0x94 Verification error at address 6. Got: 0xFF Expected: 0xA0 Verification error at address 7. Got: 0xFF Expected: 0x00 Verification error at address 8. Got: 0xFF Expected: 0x0C Verification error at address 9. Got: 0xFF Expected: 0x94 Verification error at address A. Got: 0xFF Expected: 0xA0 Verification error at address B. Got: 0xFF Expected: 0x00 Verification error at address C. Got: 0xFF Expected: 0x0C Verification error at address D. Got: 0xFF Expected: 0x94 Verification error at address E. Got: 0xFF Expected: 0xA0 Verification error at address F. Got: 0xFF Expected: 0x00 .....

I have a slightly later version, although that shouldn't make much difference.

I seem to be able to upload the blink sketch OK:

Reading SD card ...

HEX files in root directory:

BLINKC~1.HEX  :       4616 bytes.  Created: 2012-08-05 09:28:00.  Modified: 2012-08-05 09:28:00

--------- Starting ---------

Attempting to enter programming mode ...
Entered programming mode OK.
Signature = 0x1E 0x98 0x01 
Processor = ATmega2560
Flash memory size = 262144 bytes.
LFuse = 0xFF 
HFuse = 0xD8 
EFuse = 0xFD 
Lock byte = 0xEF 
Clock calibration = 0x5D 
Actions:
 [E] erase flash
 [F] modify fuses
 [L] list directory
 [R] read from flash (save to disk)
 [V] verify flash (compare to disk)
 [W] write to flash (read from disk)
Enter action:
W

Choose disk file [  ] ...
BLINKC~1.HEX
Processing file: BLINKC~1.HEX
Checking file ...

###
Lowest address  = 0x0
Highest address = 0x65F
Bytes to write  = 1632
No bootloader.
Suggest making high fuse = 0xD9 
Attempting to enter programming mode ...
Entered programming mode OK.
Processing file: BLINKC~1.HEX
Erasing chip ...
Writing flash ...

#######
Written.
Processing file: BLINKC~1.HEX
Verifying flash ...

######
No errors found.
No bootloader.
Setting high fuse = 0xD9 
Done.

What is your hardware exactly? I am using the Mega2560 board. Are you using that? If not, what speed are you running the processor at (eg. what crystal)?

Judging by the errors, the data is not uploading correctly.

You could try slowing it down. Find this line in the sketch:

// control speed of programming
const byte BB_DELAY_MICROSECONDS = 4;

Try making the 4 larger (eg. 8) and see if that helps.

Latest version of GitHub:

https://github.com/nickgammon/arduino_sketches

Hi Nick,

I send a photo of the setup i´m using, it seems a little bit sketchy, but i mounted it this way so it could resemble the most with your setup. I´m using the UNO, the Mega as 2560 and the Ethernet daughter for the SDcard. The UNO is using a 16Mhz clock.

Photo: https://dl.dropbox.com/u/7216207/P1010689.JPG

In the last post i made this quote of the sketch result:

--------- Starting ---------

Attempting to enter programming mode ...
Entered programming mode OK.
Signature = 0x1E 0x98 0x01 
Processor = ATmega2560
Flash memory size = 262144 bytes.
LFuse = 0xFF 
HFuse = 0xD8 
EFuse = 0xFD 
Lock byte = 0xFF 
Clock calibration = 0xA8 
Actions:
 [E] erase flash
 [F] modify fuses
 [L] list directory
 [R] read from flash (save to disk)
 [V] verify flash (compare to disk)
 [W] write to flash (read from disk)
Enter action:
W

Choose disk file [ ASCIIT~2.HEX ] ...
ASCIIT~2.HEX
Processing file: ASCIIT~2.HEX
Checking file ...

#####
Lowest address  = 0x0
Highest address = 0xBF1
Bytes to write  = 3058
No bootloader.
Suggest making high fuse = 0xD9 
Attempting to enter programming mode ...
Entered programming mode OK.
Processing file: ASCIIT~2.HEX
Erasing chip ...
Writing flash ...

############
Written.
Processing file: ASCIIT~2.HEX
Verifying flash ...

###########
No errors found.
No bootloader.
Setting high fuse = 0xD9 
Done.

Isn´t this a correct Writting of the Flash? After this writting when i go and try do verify the data written i get errors, could this be because of the LOCKbits and FUSES. Maybe i´m using read protection so the MCU doesn´t allow the sketch to read the written flash correctly?

I´m going to test your sugestions and see what happens.

Hi,

I made a new setup not so sketchy... and this time seems that the sketch is working.

I´am able to write and verify the code.

One thing that i noticed is that when i wirte a sketch to the Mega2560, if i trie to reprogram the board via IDE i´m not able. You are erasing the flash and with the bootloader, is that correct?

When the MCU starts the program, it starts directly from the start of the code without any bootloader?

Can you clarify this to me?

Regards

Yes, the SPI interface only has a command for erasing all the flash (as far as I can see). Thus an upload also removes the bootloader. You would need to put the bootloader back to use the IDE. But if you want to use the IDE, and you can use the IDE, why upload this way?

When the MCU starts the program, it starts directly from the start of the code without any bootloader?

Yes it changes the fuse to do that.

Yeap, i have been reading a little bit more, you pass the high fuse to 0XD9 that enables the BOOTRST bit, and that makes it start from the code and not bootloader.

Well i need this type of writting for my remote firmware upgrade feature, i have 2 ATmega2560 and 1 ATmega328P on the same board, and i want the ATmega328P to reprogram the 2 Atmega2560.

Project: http://img717.imageshack.us/img717/7879/p1010688cs.jpg

Another question, but a little bit different.

Untill now i was using the SD library that comes with the arduino IDE and the system was working without problems. But, to use the ATmega_hex_uploader sketch i have to call the SdFAT library instead of the SD, at least in the ATmega328. I wanted to use the SdFAT library just because i´m lazy :) , but it´s giving me problems with the design and i think i have all the PORTs correctly alocated and changed.

Any ideas to what could be the problem, taking into account that the SD library currently works with the design?

That's pretty non-specific. What problems?

You are right :). I´m going to make some more testing and post sistematic results/problems so you can help me.

So Lazy…

Right now the SdFat library is working on my design.

Had to eliminate the external clockout in your skecth, it used the OC1A which was one of my enable ports for the SPI bus…

At the moment i´m just missing putting the bit banged to work with the shared hardware SPI.
lets see…

Seems to be working on my design right now, using always hardware SPI, both for programming and reading SDcard.

Thank you for the awesome sketch Nick!