The situation is as follows: I committed the sketch to arduino and enjoyed life, but trouble came in the form of a broken hard drive. Now it was necessary to add a couple of sensors, but the sketch did not restore. Is there any way to download it back from the arduino as a code?
No, no hay manera.
Saludos
Actually, there is but you will only have assembly code not C.
Hi,
I learned a long, long time ago that you should back up everything on your hard drive.
But it's not just 1 backup. There are 2 backups, because I also learned that those who have 1 backup do not have any backups.
Glad you only lost your arduino code.
I have friends who lost all their children's photos and movies when they were kids,
Too bad these losses...
I'm interested in knowing how to do that, as I put down my arduino kit for a month or so, and now I can't remember what I uploaded last.
Hi @jamesnslater. Which Arduino board are you using?
Not even that. You will only have a long string of HEX bytes. You can't distinguish whether the bytes are const data or code. You have to examine and reverse-engineer everything. The code created from a compiler is not easy to understand, and you have no labels, function names and so on. You only have data bytes , code bytes and absolute addresses. And no advice what is what - no names, no comments.
Most of the time it is faster and easier to rewrite the sketch from scratch.
It's only usefull to upload to another board of same type without any changes.
In case you are interested in what you can get back easily as far as code goes, the attached file (attachment used because it is longer than the maximum size to post directly) is what I get from running the following command on the Blink sketch binary read from an Uno:
$ avr-objdump --disassemble --section=.sec1 --architecture=avr5 readfile.hex > disassembly.txt
disassembly.txt (655.1 KB)
... and even the disassembler cannot distinguish between data and code. It tries to disassemble everthing - wether its really code or not.
I think that's just meant as a discouraging example .
It surely isn't encouraging to me! Maybe others can make more sense of it. But I think we would all agree that anyone who is hoping to get their sketch code back from a board is going to be disappointed.
When you have the source code you can at least configure the disassembler to interlace the disassembly listing with the source code. Even then I still don't understand much, but at least there is some context.
I'm using an uno R3.
OK, here are the instructions:
Reading the program from the board into a file
A. Generate model command
First, we're going to do a dummy upload in Arduino IDE in order to get it to help us generate the avrdude
command used to read the program from the Arduino board:
- Plug the USB cable of the Arduino board that contains the program you want to read into your computer.
- Select your board from Arduino IDE's Tools > Board menu.
- Select your board's port from the Tools > Port menu.
VERY IMPORTANT
: Disconnect the USB cable of the Arduino board from your computer.
- Select File > Preferences... from the Arduino IDE menus.
The "Preferences" dialog will open. - Check the box next to "Show verbose output during: ☐ upload" in the "Preferences" dialog.
- Click the OK button.
- Select Sketch > Upload from the Arduino IDE menus.
- Wait for the upload to fail.
- Scroll up the black console window at the bottom of the Arduino IDE window until you see the
avrdude
command that was generated for the upload.
It will look something like this:"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v -V -patmega328p -carduino "-PCOM35" -b115200 -D "-Uflash:w:C:\Users\per\AppData\Local\Temp\arduino\sketches\2550236D1AAA5295D4F743C245319248/sketch_aug7a.ino.hex:i"
- Select the full text of the upload command.
- Press Ctrl+C.
This will copy the selected text to the clipboard.
B. Prepare read command
Next, you need to adjust the command you obtained via the procedure above so that it will read the hex file out of your Arduino board instead of writing:
- Start any text editor program.
- In the text editor window, press Ctrl+V.
This will paste the command you copied while following the instructions above into the text editor. - The end of the command will look something like this:
This is the flag that tells AVRDUDE to write."-Uflash:w:C:\Users\per\AppData\Local\Temp\arduino\sketches\2550236D1AAA5295D4F743C245319248/sketch_aug7a.ino.hex:i"
Replace that flag with the flag that tells AVRDUDE to read:
The full command should now look something like this:-Uflash:r:readfile.hex:i
"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v -V -patmega328p -carduino "-PCOM35" -b115200 -D -Uflash:r:readfile.hex:i
- If the paths in the command contain spaces, make sure they are wrapped in quotes.
- Press Ctrl+A to select all the text of the command in the text editor.
- Press Ctrl+C to copy the selected text to the clipboard.
C. Run the command
Now the read command is all prepared. It's time to run it!
- Plug the USB cable of the Arduino board that contains the program you want to read into your computer.
- Open a command line terminal.
I'll provide some links to instructions for doing that on each operating system: - Press Ctrl+V in the terminal window to paste the command you copied while following the instructions above at the command line prompt.
- Press the Enter key.
- Wait for the command to finish successfully.
You should now find a file named "readfile.hex
" in whichever folder you opened the command line terminal in. That is the hex file of the program read from the Arduino board.
Writing the file to a board
You can follow a similar procedure to write the hex file to another Arduino board.
Remember that this file was compiled specifically for the Arduino board you read it from. You can't use it with an Arduino board that has a different configuration. For example, if you read it from an Uno, it is compiled for an ATmega328P running at 16 MHz, and won't work correctly on a board with a different microcontroller or clock speed.
A. Generate model command
First, we're going to do a dummy upload in Arduino IDE in order to get it to help us generate the avrdude
command used to write the program to the Arduino board.
- Plug the USB cable of the Arduino board you want to write to into your computer.
- Select your board from Arduino IDE's Tools > Board menu.
- Select your board's port from the Tools > Port menu.
- Select File > Preferences... from the Arduino IDE menus.
The "Preferences" dialog will open. - Check the box next to "Show verbose output during: ☐ upload" in the "Preferences" dialog.
- Click the OK button.
- Select Sketch > Upload from the Arduino IDE menus.
- Wait for the upload to finish.
- Scroll up the black console window at the bottom of the Arduino IDE window until you see the
avrdude
command that was generated for the upload.
It will look something like this:"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v -V -patmega328p -carduino "-PCOM35" -b115200 -D "-Uflash:w:C:\Users\per\AppData\Local\Temp\arduino\sketches\2550236D1AAA5295D4F743C245319248/sketch_aug7a.ino.hex:i"
- Select the full text of the upload command.
- Press Ctrl+C.
This will copy the upload command to the clipboard.
B. Prepare write command
Next, you need to adjust the command you obtained via the procedure above so that it will write the hex file you read via the "Reading the program from the board into a file" procedure:
- Start any text editor program.
- In the text editor window, press Ctrl+V.
This will paste the command you copied while following the instructions above into the text editor. - The end of the command will look something like this:
This is the flag that tells AVRDUDE to write."-Uflash:w:C:\Users\per\AppData\Local\Temp\arduino\sketches\2550236D1AAA5295D4F743C245319248/sketch_aug7a.ino.hex:i"
Replace the filename in that flag with the name of the file you read:
The full command should now look something like this:-Uflash:w:readfile.hex:i
"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v -V -patmega328p -carduino "-PCOM35" -b115200 -D -Uflash:w:readfile.hex:i
- If the paths in the command contain spaces, make sure they are wrapped in quotes.
- Press Ctrl+A to select all the text of the command in the text editor.
- Press Ctrl+C to copy the selected text to the clipboard.
C. Run the command
Now the write command is all prepared. It's time to run it!
- Plug the USB cable of the Arduino board you want to write to into your computer.
- Open a command line terminal in the folder that contains the hex file, as you did while running the read command.
- Press Ctrl+V in the terminal window to paste the command you copied while following the instructions above at the command line prompt.
- Press the Enter key.
- Wait for the command to finish successfully.
Does the sketch still operate in your Arduino? If so, begin to document(write down on paper) each step you can identify that the code does. From start to finish. Let that be the beginning documentation. Add to the document all the connections to the Arduino and what sensor pin/connection is being made.
If you have a DVM, measure the voltages at each pin while the code is running.
Let this be the guiding document for rewriting the code.
I'm using a very simple program with a graphical interface called GA Flasher
https://gra-afch.com/download/46108/
It is capable of both reading the Arduino's memory and writing to it.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.