Pages: [1]   Go Down
Author Topic: Downloading/Reading sketches from the Arduino?  (Read 6469 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

At best, you can get the hex/binary - but not the source code...
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31543
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 18, 2010, 01:58:33 pm by robtillaart » Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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 smiley-sad because restaring the IDE did not solve it. I'll be back smiley

rebooting didn't help either. smiley-sad => will be a reinstall of the IDE.

Sofar no guarantee ...












« Last Edit: November 19, 2010, 09:37:47 am by robtillaart » Logged

Rob Tillaart

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

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(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) smiley

Next step is try to disassemble the 32K file

Rob
« Last Edit: November 18, 2010, 02:36:12 pm by robtillaart » Logged

Rob Tillaart

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: