Pages: [1] 2   Go Down
Author Topic: using leonardo to upload sketches to atmega168/328  (Read 2020 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I bought a Leonardo recently. I know how to burn the arduino bootloader on a new atmega8/168/328. what I can't find out is how to then upload my sketches on that processor using the Leonardo. 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?

reason for doing this - i'm building an arduino on a breadboard but I don't want to buy an FT232 breakout board
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You want to use the Leonardo as an ISP programmer, is that it?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes...i wnt to use it to burn the arduino bootloader onto blank atmega chips and then use it to upload sketches to the newly bootloaded chip becuase i don't want to buy an FT232 breakout board
Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 109
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Uno and Leonardo can be used as true ISP programmers/basic debuggers for most target AVRs. You do not have to remove any chip and you do not need to burn any bootloader.
The main idea is to use avrdude:
see: http://arduino.cc/forum/index.php/topic,125248.0.html
(Have enough patience and read the topic to the end.)
When I have time I will make a step by step tutorial.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 109
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In its simplest form it is no more than this:

Code:
// 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.

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!
Logged


Offline Offline
Full Member
***
Karma: 9
Posts: 109
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 9
Posts: 109
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But even without the automatic reset, if I try it manually, it only shows me a bunch of sdk-failed errors.
My guess is that you have serial buffer overruns. Follow the link above and have look at the fixes proposed in "ArduinoISP on the Leonardo"
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PeterVH, your other tutorial on modifying the leonardo helped a lot. i made a breadboard arduino and programmed it via the leonardo but i coduln't do it via the IDE. it kept giving me device signature failures. so i used avrdude from cmd with the -F command at the end to override the signature check and everything worked perfectly

Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 109
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Good to hear, but I find it very suspicious you had to use -F. What signature did avrdude report? Was it 0 (then you still have a problem in your wiring or setup) or just not the signature for your target.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I find it very suspicious you had to use -F
I read the post of riyadhalnur yesterday and did a test using an Uno not a Leonardo because I do not have one. I tried to check if using the -F option will change the error message thrown by avrdude in case a SPI connection is broken.
The reported error changes but it remains an evident error.
If I have good connections between Arduino and the target AVR no error is displayed.

My settings:
Arduino Uno + ArduinoISP as in ISP Programmer
Connections: Arduino Uno (SS, Miso, Mosi, SCK) -> taget on breadboard Atmegae328p (Reset, Miso, Mosi, SCK).

1) Good ISP connections:
Code:
c:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -n
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.17s
avrdude: Device signature = 0x1e950f
avrdude: safemode: Fuses OK
avrdude done.  Thank you.

2) SS->RESET connection broken
Code:
c:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -n
avrdude: stk500_program_enable(): protocol error, expect=0x14, resp=0x50
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51
avrdude done.  Thank you.

3) SS->RESET connection broken but -F added
Code:
c:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -n -F
avrdude: stk500_program_enable(): protocol error, expect=0x14, resp=0x50
avrdude: initialization failed, rc=-1
avrdude: AVR device initialized and ready to accept instructions
avrdude: Device signature = 0x000000
avrdude: Yikes!  [b]Invalid device signature[/b].
avrdude: Expected signature for ATMEGA328P is 1E 95 0F
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51
avrdude done.  Thank you.

Conclusion: -F does not fix anything as long as you have wrong SPI wiring and you do not get the "use -F" error message if you have good connections, excepting the case when the AVR oscillator (external) does not work or the target AVR is not powered!
« Last Edit: November 09, 2012, 05:19:58 pm by simplex » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

avrdude kept reporting the device signature to be 1E--0B when it says it expects the signature to be 1E--06
Logged

Pages: [1] 2   Go Up
Jump to: