Uploading protected Arduino sketch to chip

Is there a way to burn a protected Arduino sketch to the chip, either using the current bootloader or an external programmer?

For example, I write a sketch. I compile it, creating a machine-only readable file (this should be possible? just like compiling software normally). I send that machine-only readable file to a friend over email. He then plugs in his Arduino via USB and uploads that sketch to his Arduino.

Is this doable?

Sure. Google "arduino create hex file" and/or "arduino upload hex file" and you'll find many examples to do what you want.

Good luck!

[quote author=Chris Magagna link=topic=87449.msg656503#msg656503 date=1326784481] Sure. Google "arduino create hex file" and/or "arduino upload hex file" and you'll find many examples to do what you want.

Good luck! [/quote]

Thanks, I've found some good info! Let's say I were to make my own board with an AtMega on it. Could I connect the chip to a computer via USB and use something like AVRDude to upload the hex file to it easily? I'm thinking easy enough for someone who's not programming savvy to do.

I know the arduino boards use an AtMega 8U2 or 16U2 to do this. Those chips contain the bootloader that allows this. Is that correct?

Let’s say I were to make my own board with an AtMega on it.

OK, you make your own board with an AtMega on it.

Sorry…bad joke…

Could I connect the chip to a computer via USB and use something like AVRDude to upload the hex file to it easily?

On the hardware side:

You will need something on your board to interface between the ATMega chip and the computer’s USB ports. Older versions of Arduino (like the Duemilanove) use a FT232RL chip for this; the Uno uses the 8U2 (older revisions) or 16U2 (current R3 version).

I think it’s easier to implement the interface with the FT232RL chip because you basically add a few resistors, capacitors, a USB socket, and you’re done. The downside is it’s a (relatively) expensive chip and you have to install drivers on the computer.

The 8U2/16U2 is a little more work because you’ll need to program it first, but you get more flexibility because you can make it look like any kind of USB interface.

You can find implementations of both types by looking at the “Hardware” section of www.arduino.cc, then the Uno or Duemilanove board’s schematics.

I’m thinking easy enough for someone who’s not programming savvy to do.

On the software side, if you don’t want the user to have to run the Arduino IDE you can send them a batch file or script that runs the AVRDude commands to upload the hex file, or you could use something like

http://smileymicros.com/blog/2010/10/11/ardup-an-arduino-hex-file-uploader/

which does the same thing from a GUI interface.

Good luck!

Thanks for the tips, plenty to dig into. Any pointers on how I'd need to program the 8u2/16u2 chips?

To program it you would have to make a circuit where the 8u2/16u2 is a SPI slave device, just like you would to program another ATMega chip from the Arduino like in http://arduino.cc/en/Tutorial/ArduinoISP (the idea, not the specific pinout).

I think the source code that the Arduino uses for those chips is in the standard 1.0 distribution in hardware \ arduino \ firmwares, either in arduino-usbdfu or arduino-usbserial...off the top of my head I can't remember which, but I do remember there's a "readme.txt" file in there that talks about how to do it.

Good luck!

This page also has a pretty good writeup on what you'd need to do:

http://marcoramilli.blogspot.com/2011/05/how-to-upgrade-atmega8u2-firmware.html

By the time you go to all of that work, you should just build an installer that calls avrdude and punches up the hex to the ATMega via an ICSP header.

cr0sh: By the time you go to all of that work, you should just build an installer that calls avrdude and punches up the hex to the ATMega via an ICSP header.

That may work, but I'm going for ease of use with the USB interface. Your average person has USB. Thanks for the tips guys!

So let's say I give this circuit to my friend. I want it so when he plugs it into his computer it pops up not as "Arduino" but "My Custom Arduino Device" in the drivers. My current understanding based on your help and what I've read is:

  • I have my Atmega chip in the circuit.
  • I have something like an Atmega8U2 also on board.
  • The 8U2 holds a bootloader. I load this on through a serial interface via some pin headers before handing over the device.
  • Once someone else takes possession they can plug it into their computer via USB, it is recognized as "My Custom Arduino Device" (how to do this?)
  • They install drivers if necessary.
  • They then run a custom script (executable) I've given them which uses AVRDude to upload the hex file to the chip.

Does this sound about right? Should I write my own bootloader or are there good open source ones available?

•The 8U2 holds a bootloader. I load this on through a serial interface via some pin headers before handing over the device.

The 8u2 is a processor, just like the ATMega328 (or whatever your main chip is). It's got it's own code (it has 8K of flash) which also has a bootloader to get code on to it. The code it runs is to make it look like a USB serial device to the host computer, and relay those bits via some pins to the ATMega328.

So you're really dealing with two chips with two bootloaders and two user programs. I think this is what you mean, but just want to make sure.

•Once someone else takes possession they can plug it into their computer via USB, it is recognized as "My Custom Arduino Device" (how to do this?)

Are they on Windows? If so all you'll need to do is modify the driver .INF. Check out this page:

http://www.sparkfun.com/products/10277

It describes how SparkFun wired up & configured their version of what you want to do, and has a link to their drive .INF. Open that file up in a text editor and near the bottom you'll see

DESCRIPTION="SparkFun COM Port"

Change that to "monkey123's custom Arduino board" and away you go.

Should I write my own bootloader or are there good open source ones available?

If you mean the bootloader for the main chip (ATMega328) then the one that everybody (even the Arduino team with the Uno) uses these days is Optiboot:

http://code.google.com/p/optiboot/

If you mean the bootloader for the 8u2 then here's the code the Arduino team uses:

https://github.com/arduino/Arduino/tree/master/hardware/arduino/firmwares