Go Down

Topic: using leonardo to upload sketches to atmega168/328 (Read 2 times) previous topic - next topic

riyadhalnur


That question came up before, two options were given over here: http://arduino.cc/forum/index.php/topic,122973.msg924852.html#msg924852

If your goal is just to run some code on the targets, you are better off without bootloader: just burn your compiled sketches with ArduinoISP (I assume you used that to burn the bootloader into the targets?)

If you really want to use the target as an arduino on a breadbord, and program it from the IDE etc... You'd need to run a sketch on the leo that copies everything received from Serial (usb) to Serial1 (the regular uart) and vice versa. You'd have to connect  respectively  tx/rx on the leo to rx/tx on your target.

You can even combine the two functionalities into one sketch and use a jumper to select between isp mode and usb2serial mode (I saw the idea here: http://arduino.cc/forum/index.php/topic,101690.0.html, though the situation with the leonardo is a bit simpler: augmenting a sketch (ArduinoISP) with USB to serial conversion instead of adapting a bootloader)


i saw the post about using the leonardo but the way to dit is not specified there...it woud be nice to get the sketch which will copy everything from Serial to Serial1

PeterVH

In its simplest form it is no more than this:

Code: [Select]

// for now, the baud rate is hard coded
// it should match what the target's bootloader uses
static long baud = 57600;

void setup() {
  Serial.begin(baud);
  Serial1.begin(baud);
}

void loop() {
  if (Serial.available()) {
    char c = (char)Serial.read();
    Serial1.write(c);
  }
  if (Serial1.available()) {
    char c = (char)Serial1.read();
    Serial.write(c);
  }
}



I tested it uploading a blink sketch to an atmega328p with some duemilanove bootloader.
It requires some practice though: keep the target in reset. Hit the download icon. Release the reset button right after the compilation phase is done...

What we really need is autoreset support. I'll cook something...

What would also be nice is that the leo automatically sets Serial1's baud rate, when the pc sets the "baudrate" of the virtual com port.


simplex


the tutorial on the arduino site uses uno which has a removable microcontroller but the Leonardo doesn't have that option. how do I go about doing that?

There is a tutorial about Arduino Uno as an ISP programmer that teaches you how to program a Target AVR without removing the microcontroller from Arduino board.

see: http://pdp11.byethost12.com/AVR/ArduinoAsProgrammer.htm

I believe the method will also work for Leonardo.

kopfkopfkopfaffe

I'm really sorry for hijacking this thread, but if I had to define a topic for my problem, this would be exactly it.

So I've got this setup with an Atmega32U4 and an Atmega168 on a custom board. The Atmega32U4 is equipped with the Leonardo Bootloader (caterina) and works like a charm. The Atmega168 is supposed to run one of the other Arduino bootloaders. For my test setup, I use an old Duemilanove with an Atmega328.
So what I'm trying to do right now is to use the /dev/ttyACMn interface of the Leonardo to program the Duemilanove which is connected to the secondary (serial1) UART.
Or simply:

[PC]===[Leonardo]===[Due]

The Code on the Leonardo looks like this:

Code: [Select]
int led = 7;         // incoming serial byte
int reset = PORTD4;
int stillReset = 0;
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(reset, OUTPUT);
  digitalWrite(reset, LOW);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

}

void loop()
{
  if (Serial.available() > 0) {
    if (stillReset > 0){
      digitalWrite(reset,HIGH);
    }
    if (stillReset == 0){
      digitalWrite(reset,LOW);
      stillReset ++;
    }

    digitalWrite(led, HIGH);
    Serial1.write(Serial.read());               
  }
    if (Serial1.available() > 0) {
      digitalWrite(led, LOW);
    Serial.write(Serial1.read());               
  }
}


As you can see, the Due is reset when the first byte is received from the PC. My analysis seemed to show that the PC is sending the character '0' three times and waiting for the Arduino to reply. But even without the automatic reset, if I try it manually, it only shows me a bunch of sdk-failed errors.
Any idea why this is not working? Is the /dev/ttyACMn really equal to the /dev/ttyUSBn or /dev/ttySn?

Thx for your help!

PeterVH

@kopfkopfkopfaffe: I am glad you ask. I just finished a write up of an experiment that exactly does that.

It is an improved version of sketch posted earlier in this thread.
It now handles autoreset and sets the baud rate of the uart.
It can be used to download sketches via the ide, but also to communicate with the sketch itself.
A minor hack in the core is needed though.

Here it is:
http://petervanhoyweghen.wordpress.com/2012/11/08/using-the-leonardo-as-usb-to-serial-converter

Go Up