Go Down

Topic: Downloading/Reading sketches from the Arduino? (Read 11838 times) previous topic - next topic

Sneaker

I have created an RGB LED light program a while ago and I lost the sketch, is there a way to read the ATMega328 on my Arduino and extract the code so I can upload it to another Arduino?

I am downloading AVR Studio from Amtel thinking it will detect the Arduino and allow me to get into the chip, any suggestions?

cr0sh

At best, you can get the hex/binary - but not the source code...
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Grumpy_Mike

Quote
extract the code so I can upload it to another Arduino?


Have you lost the original source code? The answer to this sort of question is normally a slightly qualified no.

robtillaart

#3
Nov 18, 2010, 05:30 pm Last Edit: Nov 18, 2010, 07:58 pm by robtillaart Reason: 1
Quote
so I can upload it to another Arduino?

if you press the [compile-upload] button with the shift pressed you get information how a sketch get uploaded (commands etc). If you are able to extract the hexcode from one Arduino  this verbose output might give some clues how to upload the hexfile. Check http://www.nongnu.org/avrdude/user-manual/avrdude.html#Top


update:
Just read the page myself and found on http://www.nongnu.org/avrdude/user-manual/avrdude_4.html#Option-Descriptions the option -U memtype:op:filename[:format] (options are case sensitive!!)

The op field specifies what operation to perform:
r   read the specified device memory and write to the specified file
w   read the specified file and write it to the specified device memory


So one can read and write from a device ( I expect reading will produce a 32K image, even if the original sketch was less)

and

The format field is optional and contains the format of the file to read or write. Possible values are:
...
r    raw binary; little-endian byte order, in the case of the flash ROM data
...


So it must be possible to download a raw binary from one device and upload it to another one. You must also have the port right and other flags, so read it more carefully than I did. As Cr0sh said you wil not  get the sourcecode but this thread http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241117308 is about disassembling the generated hexcode so you may get a faint echo of the original source

PLease let us know if you succeeded as this is a way to quick multiply a busload of Arduinos with the same sketch. Very interesting.

Rob


update
Avrdude can be found at ~\arduino-0021\hardware\tools\avr\doc\avrdude
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

#4
Nov 18, 2010, 08:09 pm Last Edit: Nov 19, 2010, 03:37 pm by robtillaart Reason: 1
Too interesting, I tried myself the first step: I changed dir to C:\Program Files (x86)\arduino-0021\hardware\tools\avr\bin>
and called:

Code: [Select]
avrdude -C "C:\Program Files (x86)\arduino-0021\hardware\tools\avr\etc\avrdude.conf" -v -v -v -v -p atmega328p -c stk500 -U flash:r:"c:/arduino.bin":r -P\\.\COM5 -b57600

And yes!,  a nice c:\arduino\bin appeared. 32 K in size. So the download works! yippy! Now try to upload the arduino.bin file again.

Code: [Select]
avrdude -C "C:\Program Files (x86)\arduino-0021\hardware\tools\avr\etc\avrdude.conf" -v -v -v -v -p atmega328p -c stk500 -U flash:r:"c:/arduino.bin":w -P\\.\COM5 -b57600

The application still worked - could see that with the serial monitor! However my IDE now gets a failure on avrdude when I try to upload a new sketch. Think I need to restart my PC :( because restaring the IDE did not solve it. I'll be back :)


rebooting didn't help either. :( => will be a reinstall of the IDE.

Sofar no guarantee ...













Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

cr0sh

Quote
The application still worked - could see that with the serial monitor! However my IDE now gets a failure on avrdude when I try to upload a new sketch. Think I need to restart my PC  because restaring the IDE did not solve it. I'll be back


That's because you likely blew away your bootloader uploading the binary - so you'll need to find a bootloader binary for your board, and upload that again so you can upload sketches with the Arduino IDE (if I am understanding you correctly?)...
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

robtillaart

#6
Nov 18, 2010, 08:33 pm Last Edit: Nov 18, 2010, 08:36 pm by robtillaart Reason: 1
(moved to make the thread more readable)
It works again without the reinstall. Did a check on the directory C:\Program Files (x86)\arduino-0021\hardware\tools\avr\bin>
and somehow a file avrdude was made of 0 bytes (besides the avrdude.exe) , somewhere I screwed up I did some avrdude calls that failed. Maybe that created the new file. As the IDE calls avrdude without extension it tried to start the wrong file and failed. After removeing these zero bytes fiel the IDE worked again. Yes!  

@Cr0sh,  wrt the bootloader, think it is in the 32K file(?). as all seems to work again (pffffh) :)

Next step is try to disassemble the 32K file

Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Sneaker

Yeah I lost the original code, it got mixed up in my dir with a bunch of versions of the file... I mean it's not a huge loss because it's simple RGB LED code but I had the colors perfect for what I was doing...




Thanks Rob, I will try extracting and transfering to another Arduino and see how far I get... If I can get it on another Arduino that would be great.

Thanks everyone, I will post my results after the weekend.

Kyle

robtillaart

About disassembly: Tried various formats of the download with avr-objdump but no luck sofar. (end of experiment)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up