Moteino Sketch upload from Raspberry Pi (avrdude 6.1, 5.11) [solution]

Started by LittleRock, July 15, 2015, 06:46:44 AM

LittleRock

Hello everybody,

My name is Pierre and I am a student majoring in electronics.

I am working on a project which use Moteino R4 and a RaspberryPi. The RPi uploads the sketch, using avrdude, but it seems that avrdude doesn't detect the Moteino.

/!\ However, at the beginning of my work, sometimes avrdude detected the Moteino and sometimes not. It was just like a random game : I just needed to relaunch avrdude until it was successful.
But now, it doesn't work at all ; that's why I'm asking your help.

Let me explain what I have done.

I first connected the Moteino (FTDI Pin) to the RaspberryPi (GPIO Pin).

QuoteGND to GND (pin 6)
Vin to 5.5V (pin 2)
Rx to TX (pin 8 )
Tx to RX (pin 10)
DTR to GPIO4 (pin 7)

Then I followed some tutoriels such as :
https://github.com/deanmao/avrdude-rpi
https://github.com/mharizanov/avrdude-rpi (I use this one)

In the autoreset file, I let "GPIO.setmode(GPIO.BOARD)" and "pin = 7" (and I even tried all the possible configurations ([BCM, 7], [BOARD, 4],[BCM, 4])).
I have read this topic https://lowpowerlab.com/forum/index.php?topic=547.0 but it didn't resolve my problem since I really want to use the arduino programmer (or maybe I'll just change it in the future).

I didn't forget to install arduino, python and python-rpi.gpio.
I also made all the necessary modifications in /boot/cmdline.txt and /etc/inittab to disable the ttyAMA0 console.

Finally, I just write this command line :
Quoteavrdude -v -c arduino -p atmega328p -P /dev/ttyAMA0 -b 115200 -U flash:w:Hello_World.cpp.hex

And here is what it answered to me :

Quoteavrdude-original: Version 6.1, compiled on Jul  7 2015 at 13:18:47
                  Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
                  Copyright (c) 2007-2014 Joerg Wunsch

                  System wide configuration file is "/etc/avrdude.conf"
                  User configuration file is "/root/.avrduderc"
                  User configuration file does not exist or is not a regular file, skipping

                  Using Port                    : /dev/ttyAMA0
                  Using Programmer              : arduino
                  Overriding Baud Rate          : 115200
avrdude-original: Using autoreset DTR on GPIO Pin 7
pi@raspberrypi ~/sketchbook
(And the LED blinked once.)

Using minicom, I checked the serial port /dev/ttyAMA0 and I didn't see anything.
(The Hello_World program sends in the serial port "Hello World" every three seconds.)

I have verified a lot of time the different files that avrdude uses but I didn't find anything wrong, or maybe I just missed something.
Now I'm just going crazy.

I would really appreciate your help and I apologize if I just made an evident mistake.

/!\ Just in case : the Moteino is running the Wireless_programming_gateway program. Is it possible that this program disturbs the avrdude ? Well, i don't think so, but perhaps it does.

If I haven't told you some information, do not hesitate to ask me.

Thank you.

Felix


LittleRock

Quote from: Felix on July 15, 2015, 10:52:25 AM
Can you try it from a PC running Arduino IDE?

Hi ! Thank you for your quick response !

Well, in this project, I only have to use the RPi to upload sketches.

Since I don't have an FTDI adapter, I can't connect the Moteino to an USB port on a PC.
(When I said "FTDI pins", I meant the pins on the FTDI side.)

Using Arduino IDE on my Raspberry Pi, it didn't detect the serial port ttyAMA0.
I found somewhere that maybe I have to create the virtual serial ports for the Arduino IDE with these lines :
Quotesudo echo 'KERNEL=="ttyAMA0", SYMLINK+="ttyS8",GROUP="dialout",MODE:=0666' >> /etc/udev/rules.d/99-tty.rules
sudo echo 'KERNEL=="ttyACM0", SYMLINK+="ttyS9",GROUP="dialout",MODE:=0666' >> /etc/udev/rules.d/99-tty.rules
Do I really need to do this ? I remember that when it was done successfully, I didn't need to.

I also found that maybe it's not the DTR pin that I have to connect to the GPIO reset pin but the RST pin, next to the GND pin... But it's not the problem because it worked some days ago.

Here is a photograph of the moteino connected to the RPi :
(Red : 5.5V ; Black : GND ; Green : RPi TX ; Orange : RPi RX ; Blue : DTR)

Felix

Oh so you don't have a FTDI/serial adapter. I think that's the missing link. You have to figure out how to enable the GPIO serial port on your Pi. There are guides for that online. I think in recent distributions the serial port is enabled by default but you will have to double check.
Once the serial port is enabled and there is communication with the Moteino you will also have to assert the DTR pin to put the Moteino in the bootloader mode for firmware updating.

Also searching "avrdude" in the forum might reveal some solutions from other people that have tried this.

LittleRock

Quote from: Felix on July 15, 2015, 12:27:12 PM
Oh so you don't have a FTDI/serial adapter. I think that's the missing link. You have to figure out how to enable the GPIO serial port on your Pi. There are guides for that online. I think in recent distributions the serial port is enabled by default but you will have to double check.
Once the serial port is enabled and there is communication with the Moteino you will also have to assert the DTR pin to put the Moteino in the bootloader mode for firmware updating.

Also searching "avrdude" in the forum might reveal some solutions from other people that have tried this.

The serial port is enabled since I can receive something from the Moteino : "Start wireless gateway..." ; which is the sketch that the Moteino is running right now.

A friend just gave me an FTDI/USB adapter. I tried to connect the Moteino to one USB port of the Raspberry Pi. I launched Arduino on the Raspberry and tried to upload a "Hello World" sketch. It didn't work : the Arduino console just showed me the same message than the one in my first post (starting with "avrdude-original: Version 6.1, compiled on...").

I think the logic reason is that I changed the original avrdude by this one : https://github.com/mharizanov/avrdude-rpi . I hope I didn't make any mistake in my understanding about the avrdude.

If I have understood, it's in the autoreset file that I chose the pin which would become the DTR pin. In my example, it's the pin 7 (so the GPIO4).

Here is a part of the autoreset file that I'm using : (sorry, I didn't find a [spoiler] tag)

Quote#!/usr/bin/python

import RPi.GPIO as GPIO
import sys, os, re, time, fcntl
import errno

fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
dtr = re.compile('.+TIOCM_DTR.+')
start = time.time()
pin = 7

def reset():
  GPIO.setup(pin, GPIO.OUT)
  GPIO.output(pin, GPIO.HIGH)
  time.sleep(0.12)
  GPIO.output(pin, GPIO.LOW)

...

Tomorrow, I'll try to use the original avrdude with Arduino IDE.

Anyway, thank you for your advice. I'll check it and give you my results.

EloyP

As Felix mentioned, make sure nothing is using the RPi serial port (by default some distributions run a getty process on it, so that would have to be stopped. How to do that depends on your distribution. Also, the Linux console might be redirected to the serial port).

Then you need to figure out (as Felix also mentioned) how to make the RPi drive the GPIO pin connected to the DTR pin in the FTDI header of the Moteino. That's because the bootloader on the Moteino needs to run so it can flash the new image being sent through the serial port of the RPi. But the boot loader only runs when the Moteino boots, so the programmer (avrdude) toggles the DTR signal, which is connected to the RST pin of the ATmega processor on the Moteino, and that causes the Moteino to reboot and run the boot loader. You may need to write a little shell script (bash, python, perl, whatever) that uses a GPIO library to assert the pin and then execute avrdude. (I don't think avrdude supports the RPi GPIO subsystem natively but I could be wrong.)

An alternative is to manually ground the DTR pin in the FTDI header of the Moteino right before you execute the avrdude command on the RPi. The avrdude command line that I have been using in similar situations is:

avrdude -p atmega328p -P /dev/ttyUSB0 -c arduino    -U flash:w:image.hex

This is on a PC using an USB-to-serial adapter so for the RPi you will have to change /dev/ttyUSB0 to (I think) /dev/ttyAMA0.

Apologies if all this is information you already know but I figured it could be useful to someone.

Cheers!

EloyP

Quote from: EloyP on July 15, 2015, 05:04:48 PM
Apologies if all this is information you already know but I figured it could be useful to someone.

I am very sorry -- I am just realizing that your original post is providing a lot of information that I mentioned in my previous post. For example, I see that your avrdude is driving the DTR signal via GPIO (I didn't know avrdude had been given that knowledge), that you disabled the getty and the Linux console to ttyAMA0, and that you are running avrdude with "-c arduino". So, you are doing everything right, it seems. My apologies for missing all those details.

I am not sure what is happening. That you don't see further output after "avrdude-original: Using autoreset DTR on GPIO Pin 7" is strange. It's like it's crashing right after that. Do you see anything in dmesg's output? These are unofficial avrdude builds that someone added GPIO support to? If so, can you try an official avrdude (like the one that came with your RPi distribution)? To make it work you would have to manually reset the Moteino by grounding the RST or DTR pins. Then you run avrdude.

You can also check that the Moteino is rebooting when you run that avrdude that can handle RPi GPIO -- the Moteino bootloader flashes the onboard LED when it runs.

Cheers!

Felix

Ok I was lazy and didn't read everything, so it sounds like you have serial enabled and DTR pin hooked up correctly.
Does the board reset when you try to upload? (ie does the LED start blinking?)
If not, can you try to reset it manually just before upload? You can do this with a wire - short DTR and GND for a split second, that should start the bootloader and if serial data is coming from the Pi as expected it should be able to upload the sketch.
The bootloader is basically a custom Optiboot, the signature would be the same as on Arduino UNO.

LittleRock

The LED blinks once, yes. So I assume that the board resets when I use avrdude to upload my .hex file.

As you said, I tried to connect the DTR and the GND with a wire, then started avrdude. It doesn't change anything. The sketch isn't uploaded.

I'm pretty sure the Optiboot isn't corrupted/damaged since I can reprogram the Moteino with a FTDI/serial adapter connected to a PC, with Arduino IDE.

I don't know why it doesn't work with the RPi.

The fact is that the RPi just needed a DTR pin, right ? This is the only difference (and also the current voltage also) that I acknowledge.

Quotepi@raspberry ~/sketchbook $ avrdude -v -c arduino -p atmega328p -P /dev/ttyAMA0 -b 115200 -U flash:w:Hello_World.cpp.hex

avrdude-original: Version 6.1, compiled on Jul  7 2015 at 13:18:47
                  Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
                  Copyright (c) 2007-2014 Joerg Wunsch

                  System wide configuration file is "/etc/avrdude.conf"
                  User configuration file is "/root/.avrduderc"
                  User configuration file does not exist or is not a regular file, skipping

                  Using Port                    : /dev/ttyAMA0
                  Using Programmer              : arduino
                  Overriding Baud Rate          : 115200
avrdude-original: Using autoreset DTR on GPIO Pin 7
pi@raspberrypi ~/sketchbook $

Felix

When reset, the LED should blink more than once (ie see how it blinks when you upload on a PC).

LittleRock

Quote from: Felix on July 16, 2015, 09:03:17 AM
When reset, the LED should blink more than once (ie see how it blinks when you upload on a PC).

Sorry, I wasn't precise.

The LED blinks three times just like when I upload on a PC.

Edit : I hope it isn't the Moteino itself which is somehow damaged... I don't think so, since I can reprogram it from a PC.

Felix

Ok so then the reset works. I think your serial lines are connected correctly (Pi TX to Moteino RX and vice versa).
Then there must be some problem with avrduded communicating with the bootloader.
Here's a sample avrduded dump when uploading from Arduino 1.0.6, note the avrdude version on Arduino IDE is 5.11 not 6.1 as I see in your setup:

D:\ARDUINO1.0.6\hardware/tools/avr/bin/avrdude -CD:\ARDUINO1.0.6\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -P\\.\COM255 -b115200 -D -Uflash:w:C:\Users\Felix\AppData\Local\Temp\build2545392494169474396.tmp\Blink.cpp.hex:i 

avrdude: Version 5.11, compiled on Sep  2 2011 at 19:38:36
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "D:\ARDUINO1.0.6\hardware/tools/avr/etc/avrdude.conf"

         Using Port                    : \\.\COM255
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
         AVR Part                      : ATMEGA328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
                                  Block Poll               Page                       Polled

................. A LOT MORE OF THIS STUFF HERE ..................

           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
avrdude: Send: A [41] . [80]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [03] 

................. A LOT MORE OF THIS STUFF HERE ..................

avrdude: verifying ...
avrdude: 3060 bytes of flash verified
avrdude: Send: Q [51]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 

avrdude done.  Thank you.

LittleRock

On my RPi, I have the Arduino 1.0.1 and the avrdude 6.1.

Do you know how to install an older version of the avrdude (ie the 5.11) ? I think that after installing the Arduino IDE on the RPi, I got the latest version of avrdude (6.1).


LittleRock

Quote from: LittleRock on July 16, 2015, 11:59:58 AM
On my RPi, I have the Arduino 1.0.1 and the avrdude 6.1.

Do you know how to install an older version of the avrdude (ie the 5.11) ? I think that after installing the Arduino IDE on the RPi, I got the latest version of avrdude (6.1).

I finally achieved to install an older version of avrdude (5.10) and it works.

So, I have three files : autoreset, avrdude and avrdude-original.

- The first one choses the DTR pin.
- The second one is the "avrdude-autoreset" file, using the autoreset and executing the avrdude-original.
- The last one is the avrdude 5.10 that I installed.

To conclude, I think the problem comes from the version of avrdude (do not upgrade to 6.1, the actual version ?).

Thank you for your help.

alexelite

Hello All,

I spent a few hours today with this issue.

Avrdude worked only once after reboot. After first execution I only got the "avrdude-original: Using autoreset DTR on GPIO Pin 7" and console. I am also using avrdude 6.1.

I found this topic after I got it working by running avrdude twice in a row with pb66's code.

avrdude-autoreset looks like this:
#!/bin/bash

if [[ $@ == */dev/ttyS0* ||  $@ == */dev/ttyAMA0* ]];
then
	sudo strace -o "|autoreset" -eioctl /usr/bin/avrdude-original $@;
	sudo /usr/bin/avrdude-original $@;
fi


I don't get it why it runs ok one time after Pi reboot and why using an older version solves that.

Thank you Felix for all the hard work. Multumesc.