Page 1 of 1
RPi GPIO programming Arduino issues
Posted: Wed Apr 29, 2015 6:27 am
by bkenobi
I've been using a RPi B v2 for around a year and a half as my home automation system. I'm adding an Arduino Pro Mini 3v3 (328P@16MHz) to my setup to collect sensor data, process it, and dump the formatted data to the RPi over GPIO serial (/dev/ttyAMA0). I have followed a number of guides including the following:
http://www.deanmao.com/2012/08/06/ardui ... pberry-pi/
As a result, I have Arduino-mk and avrdude 5.11 set up, but I'm still having some issues uploading. The guide shows how to install avrdude-rpi such that the reset pin will be triggered at the right time. But, I can still only compile and fail on upload.
Code: Select all
pi@raspberrypi ~/arduino $ sudo make upload
for STTYF in 'stty -F' 'stty --file' 'stty -f' 'stty <' ; \
do $STTYF /dev/tty >/dev/null 2>&1 && break ; \
done ; \
$STTYF hupcl ; \
(sleep 0.1 2>/dev/null || sleep 1) ; \
$STTYF -hupcl
stty: hupcl: No such file or directory
stty: -hupcl: No such file or directory
Makefile:503: recipe for target 'reset' failed
make: *** [reset] Error 1
I have tried deleting things and starting from scratch with a different guide (same basic steps though) and ended up in the same place. Any help would be very much appreciated.
Re: RPi GPIO programming Arduino issues
Posted: Thu Apr 30, 2015 5:59 am
by bkenobi
I think I am closer, but still have an error. I shouldn't have run as root to upload (sudo make upload). I removed the statement and got a different error:
Code: Select all
pi@raspberrypi ~/arduino $ make upload
for STTYF in 'stty -F' 'stty --file' 'stty -f' 'stty <' ; \
do $STTYF /dev/tty >/dev/null 2>&1 && break ; \
done ; \
$STTYF /dev/ttyAMA0 hupcl ; \
(sleep 0.1 2>/dev/null || sleep 1) ; \
$STTYF /dev/ttyAMA0 -hupcl
/usr/bin/avrdude -q -V -p atmega328p -C /etc/avrdude.conf -c arduino -b 57600 -P /dev/ttyAMA0 \
-U flash:w:build-cli/arduino.hex:i
sh: 1: autoreset: Permission denied
I found that I couldn't even run the autoreset script because I didn't have the right permissions. I added execute to the file and now I get a little further...I think.
Code: Select all
pi@raspberrypi ~/arduino $ make upload
for STTYF in 'stty -F' 'stty --file' 'stty -f' 'stty <' ; \
do $STTYF /dev/tty >/dev/null 2>&1 && break ; \
done ; \
$STTYF /dev/ttyAMA0 hupcl ; \
(sleep 0.1 2>/dev/null || sleep 1) ; \
$STTYF /dev/ttyAMA0 -hupcl
/usr/bin/avrdude -q -V -p atmega328p -C /etc/avrdude.conf -c arduino -b 57600 -P /dev/ttyAMA0 \
-U flash:w:build-cli/arduino.hex:i
avrdude-original: stk500_recv(): programmer is not responding
avrdude-original done. Thank you.
Makefile:496: recipe for target 'raw_upload' failed
make: *** [raw_upload] Error 1
Any suggestions now? It looks like the autoreset doesn't actually make the Arduino reset successfully.
Re: RPi GPIO programming Arduino issues
Posted: Thu Apr 30, 2015 6:11 am
by bkenobi
And, to confirm my suspicions, I was able to write a sketch to the Arduino if I manually hold the reset button and release just as the avrdude command is issued.
Code: Select all
pi@raspberrypi ~/arduino $ make upload
/usr/bin/avr-g++ -MM -mmcu=atmega328p -DF_CPU=8000000L -DARDUINO=100 -I. -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard -g -Os -w -Wall -ffunction-sections -fdata-sections -fno-exceptions build-cli/blink.cpp -MF build-cli/blink.d -MT build-cli/blink.o
cat build-cli/blink.d > build-cli/depends.mk
for STTYF in 'stty -F' 'stty --file' 'stty -f' 'stty <' ; \
do $STTYF /dev/tty >/dev/null 2>&1 && break ; \
done ; \
$STTYF /dev/ttyAMA0 hupcl ; \
(sleep 0.1 2>/dev/null || sleep 1) ; \
$STTYF /dev/ttyAMA0 -hupcl
/usr/bin/avr-g++ -c -mmcu=atmega328p -DF_CPU=8000000L -DARDUINO=100 -I. -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard -g -Os -w -Wall -ffunction-sections -fdata-sections -fno-exceptions build-cli/blink.cpp -o build-cli/blink.o
/usr/bin/avr-gcc -mmcu=atmega328p -Wl,--gc-sections -Os -o build-cli/arduino.elf build-cli/blink.o build-cli/libcore.a -lc -lm
/usr/bin/avr-objcopy -O ihex -R .eeprom build-cli/arduino.elf build-cli/arduino.hex
/usr/bin/avrdude -q -V -p atmega328p -C /etc/avrdude.conf -c arduino -b 57600 -P /dev/ttyAMA0 \
-U flash:w:build-cli/arduino.hex:i
avrdude-original: AVR device initialized and ready to accept instructions
avrdude-original: Device signature = 0x1e950f
avrdude-original: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude-original: erasing chip
avrdude-original: reading input file "build-cli/arduino.hex"
avrdude-original: writing flash (1078 bytes):
avrdude-original: 1078 bytes of flash written
avrdude-original: safemode: Fuses OK
avrdude-original done. Thank you.
This means I need to figure out why the autoreset code doesn't actually work. Still open to thoughts...
autoreset code:
https://github.com/deanmao/avrdude-rpi
Code: Select all
#!/usr/bin/python
import RPi.GPIO as GPIO
import sys, os, re, time, fcntl
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
GPIO.setmode(GPIO.BOARD)
dtr = re.compile('.+TIOCM_DTR.+')
start = time.time()
def reset():
pin = 27
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.12)
GPIO.output(pin, GPIO.LOW)
def process():
while True:
try:
duration = time.time() - start
input = sys.stdin.readline().strip()
if dtr.match(input):
reset()
return
elif duration > 5000:
return
except:
continue
process()
print "done with autoreset"
Re: RPi GPIO programming Arduino issues
Posted: Thu Apr 30, 2015 8:27 am
by DougieLawson
The Arduberry uses the Arduino IDE and a serial connection (GPIO 14/15) to do the programming. You could install their modifications to the arduino/avrdude stuff and it should "just work".
https://www.dexterindustries.com/Ardube ... g-started/
Re: RPi GPIO programming Arduino issues
Posted: Thu Apr 30, 2015 1:16 pm
by bkenobi
Thanks for the suggestion. I'm only running on the command line for setup and will only connect to the RPi via web when complete, so I've been avoiding using the full IDE installation. I've read that the IDE takes a lot of resources. If there's a way to use the command line only that would be much better, but I'll take a look at this guide.
Re: RPi GPIO programming Arduino issues
Posted: Thu Apr 30, 2015 2:08 pm
by DougieLawson
You may be able to use
http://inotool.org/ which is a command line compiler for Arduinos. I don't know how you update that to use the serial interface.
Re: RPi GPIO programming Arduino issues
Posted: Thu Apr 30, 2015 2:38 pm
by bkenobi
I hadn't seen ino before, so that's interesting. I wonder how it compares to arduino-mk + avrdude. Of course, if it doesn't upload via serial, then I'm little better off than now since I was able to upload with a manual button press. Thanks!
Re: RPi GPIO programming Arduino issues
Posted: Thu Apr 30, 2015 4:07 pm
by bkenobi
I think I figured out the issue. I am no Python programmer beyond the 2 day class I took years ago. It looks like the code Dean Mao provided in the avrdude-reset script is referencing the GPIO header pin number rather than the GPIO number. I have things connected to GPIO 27 (pin 13) and entered "pin=27". But, that would actually point to an invalid location (RPi B v2 only has 26 pins).
http://www.raspberrypi-spy.co.uk/2012/0 ... -and-pins/
So, I believe I need to either change the code to "pin=13" or change line 10 from:
to
https://github.com/deanmao/avrdude-rpi/ ... oreset#L10
Re: RPi GPIO programming Arduino issues
Posted: Fri May 01, 2015 6:47 am
by bkenobi
I figured out the problems. The issue was 2 separate things.
1) I was addressing the header pin in the python script but using the GPIO number. That is corrected.
2) If I run "make upload", I cannot upload because the python script (autoreset) does not have proper permissions to access the GPIO. If I run "sudo make upload", I have to add the settings to the make file. When I do this, the code compiles and uploads to the board correctly.
So, what would I need to do to get python to run correctly without changing to root via sudo? I've used Linux for years as an end user and have never had the opportunity to learn all of these things with permissions and whatnot (off limits at work).
Re: RPi GPIO programming Arduino issues
Posted: Fri May 01, 2015 4:40 pm
by bkenobi
From what I can determine, /dev/mem is protected from the normal user to prevent mischief (or accidents). GPIO access is part of /dev/mem which means that in order to access them via python, the script would need elevated privileges. I found a thread that suggests this might work:
Code: Select all
chown root:root /usr/bin/autoreset
chmod 4775 /usr/bin/autoreset
If so, then I should be all set.
Re: RPi GPIO programming Arduino issues
Posted: Fri May 01, 2015 8:20 pm
by DougieLawson
That chmod command is setting the suid bit for the program. So a non-root user gets root privileges when they run that one. That's a security exposure unless you're 100% sure you know what the suid program is doing.
Re: RPi GPIO programming Arduino issues
Posted: Sat May 02, 2015 8:53 pm
by bkenobi
Turns out Raspian doesn't like setting that permission for scripts as it made no difference. I may just compile it to a binary so the permissions work.
But, now that I can program the Arduino, I find a different issue. I can write code to the MCU just fine using the stock LED blink code, but if I try to communicate with the board over serial via minicom at the correct baud rate I only see garbage. I wrotw a sketch that simply writes the ASCII code for any character it receives. I connected and sent a character but got giberish as though I was connected at the wrong baud.
The only explanation I can think of is that I tried to be fancy and bought an Arduino Pro Mini 328p 16MHz 3.3v/5v selectable board. The documentation is terrible so I'm not sure if the input voltage is 3.3v or 5v. I know the pins output 3.3v, but I also don't know for sure if the serial is 3.3v or 5v. If I can't make sense of this, I'll have to get a different 3.3v board to talk to the RPi at some point. Right now I have a CD4050 that should help the situation.
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 1:00 am
by bkenobi
Well I can't explain this, but I'll post it anyway. I uploaded this code to the Arduino:
Code: Select all
byte number = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available()) {
number = Serial.read();
Serial.print("character recieved: ");
Serial.println(number, DEC);
}
}
I then connected with minicom with the following line:
Code: Select all
minicom -o -D /dev/ttyAMA0 -b 19200
And for some reason I get a valid connection:
Code: Select all
Welcome to minicom 2.6.1
OPTIONS: I18n
Compiled on Apr 28 2012, 19:24:31.
Port /dev/ttyAMA0
Press CTRL-A Z for help on special keys
character recieved: 255
character recieved: 97
character recieved: 115
character recieved: 100
character recieved: 103
character recieved: 100
It makes absolutely no sense since I should have been connecting at 9600 not 19200. Anyone have a thought on this?
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 3:19 am
by Douglas6
And you've compiled the Arduino code with the proper board setting (16MHz, not 8MHz)?
[Edit: looking deeper into this, I'm confused. You stated you have an "Arduino Pro Mini 3v3 (328P@16MHz)", but I'm not sure I can find such a configuration. Not sure if the AT328p can be clocked reliably at 3v3 and 16MHz. The datasheet suggests it can't. Are you sure it's running at 16MHz?]
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 7:35 am
by bkenobi
Correct, the board is confusing. But your comment is something that I'll have to verify. The board is an Arduino Pro Mini ATmega328p @16MHz that is 3.3v/5v selectable. The documentation is basically nonexixtent, so I'm not clear if that makes the board 3.3v input or just applies some kind of voltage divider on the output. I have "pro328" set for avrdude which corresponds to the 3.3v 8MHz pro mini. Perhaps that's the issue. I'll try "pro5v328" and "uno" tomorrow which are supposed to work with the 16MHz 328 chip.
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 9:34 am
by DougieLawson
Running a low voltage Arduino WILL change the processor clock frequency from 16MHz to 8MHz.
http://www.arduino.cc/en/Main/ArduinoBoardProMini
BTW, "receive" is one of the very few English words that adheres to the surprisingly incorrect "i before e, except after c" rule.
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 12:09 pm
by danjperron
Could you just try this sketch and tell us what is the speed it reports?
Code: Select all
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(F_CPU);
delay(1000);
}
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 4:02 pm
by bkenobi
The sketch returns 8MHz. I changed the avrdude settings to "pro5v328" which I figured should work, but I get the same result (8MHz output to display).
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 4:10 pm
by bkenobi
I've read that data page before but didn't see anything about underclocking via dropping voltage. This is not an official board obviously, so I wouldn't expect the official documentation to describe how this chip functions when the feature is not stock. The only thing I've read (on the Arduino forum, not official documentation) suggests that if you want to run at a lower frequency you must replace the crystal which is not simple on a surface mount component.
Re: RPi GPIO programming Arduino issues
Posted: Sun May 03, 2015 6:02 pm
by bkenobi
I got it! Thanks to some help on the Arduino forums, I found that I had a few issues.
1) "BOARD=pro328" -> "BOARD_TAG=pro5v328" The board was running at 8MHz in code but was running at 16MHz physically. Changing this was critical. Also, it appears that BOARD was not the correct parameter to set. BOARD_TAG is what's checked for in the make file, so I assume it was defaulting back to something else. Not sure if that change is required, but I'll keep it anyway.
2) "sudo rm -rfv build-cli" I thought the make process would update needed components if a board change required it. That is not the case. I had to delete the complied files and run the make process again for it to update the board to the correct settings.
Thanks for the help. Everything currently appears to be working!