User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Help with serial comms between the Pi and GertDuino [SOLVED]

Mon Dec 02, 2013 2:34 am

I am having trouble with serial communications between the Raspberry Pi and the GertDuino (Atmega328).
My setup is as follows:-
  • Raspberry Pi Model A
  • Raspbian Wheezy
  • Generic RT5370 wireless adaptor
  • GertDuino
I have tried a number of different power supplies (that have all worked for me over the last year or so, since I received my first Raspberry Pi).

I have the m48 running with the RTC code from Gordon.

I am trying to get serial communications between the Raspberry Pi and the AtMega 328 on the GertDuino. The required changes to /boot/cmdline.txt and /etc/inittab have been made so that /dev/ttyAMA0 is no longer expecting a terminal connection. I have jumper wires in place on J13 as shown on page 5 of the GertDuino manual (Pi to Atmega-328). I also have the jumpers set on J13 so that I can program the Atmega-328 using gpio (as show on page 6 of the user manual).

On the Atmega-328 I have tried a number of different sketches that send characters using Serial (including the Serial_startup sketch in Gert's archive from Element14). I have tried baud rates of 115200 and 9600.
On the Raspberry Pi I have tried my own code as well as minicom.

I am receiving characters on the Raspberry Pi, but they are not the characters sent by the GertDuino. For example if i send

Code: Select all

abcd
I would expect to get (in hex)

Code: Select all

61 62 63 64
But instead I get

Code: Select all

18 00 00 51 FC
What am I missing?
Last edited by AndyD on Mon Dec 02, 2013 11:35 pm, edited 1 time in total.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 5:11 am

My guess is a baudrate mismatch. The sender is sending slower than the receiver is expecting.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 8:15 am

ame wrote:My guess is a baudrate mismatch. The sender is sending slower than the receiver is expecting.
Thanks ame. I have very carefully matched the baud rate and parity etc on both the Atmega-328 sketch and the software on the Raspberry Pi. I have been using 8N1, as this appears to be the default on both systems. I have only tried baud rates of 115200 and 9600.

I have now tried again with the jumpers used to program the Atmega-328 removed, but this made no difference.

My question is am I missing something obvious or something specific to the Raspberry Pi/Atmega-328 that is not obvious? Alternatively, is it possible that there is something wrong with the RS232 section of my GertDuino?

I have been in software engineering for a very long time and I am very conscious that software engineers sometimes blame the hardware too readily.

I have two other Raspberry Pi's (An original 256MB Model B and a 512MB Model B) I will try these soon to rule out any issue with the my new Model A. The only reason I haven't tried another Raspberry Pi as yet is that I don't like removing the GertDuino from the Raspberry Pi as it is a snug fit on the GPIO pins and I don't want to damage anything.

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 3:56 pm

I've done a lot of comms between the Atmega on the Gertduino (& Gertboard) and the Pi. It works very well - 115200 baud is the fastest I've used. (There is, maybe was? a kernel or hardware bug that sends a spurious character down the line from the Pi when you first open the serial port though)

What I'd suggest is an incremental approach - remove the Gertduino, bridge the Tx & Rx pins on the Pi, run minicom and it ought to echo what you type.

them plug the Gertduino on and load up a simple blink sketch - verify that it blinks at the rate in your sketch! This is to doubly-make sure that the ATmega's clock speed is set to 16MHz - if it's not blinking at the speed you expect, then fix the fuses on the ATmega with my avrsetup script. (If it's a brand new ATmega that's not had the fuse bits set, then it will be running at 1MHz off the internal oscillator and that will directly affect the baud rate generator)

Then, assuming that's OK - write a simple sketch for the ATmega that does nothing more than print "Hello world\n" once a second and run that on the ATmega and run minicom on the Pi...

Hopefully if you can get that far you can then get comms going the other way too...

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 5:03 pm

Then, assuming that's OK - write a simple sketch for the ATmega that does nothing more than print "Hello world\n" once a second and run that on the ATmega and run minicom on the Pi...

There is, maybe was? a kernel or hardware bug that sends a spurious character down the line from the Pi when you first open the serial port though)
I have provided a tested and verfied sketch for that becasue I KNOW how difficult it can be to get your
simple serial link working, even after 40 years in computing.
See gertduino.zip:gertduino->arduino_sketchbook->Serial_startup->Serial_startup.ino

It requires all the things Gordon said about the Arduino speed etc. And it runs at 9600 baud.

Yes there was a bug in the SW ( I know because at some point it ended up on my desk as "Why does the hardware send a spurious character".
After which I had to prove with a chip simulation that was NOT in the hardware) But I think it is gone now.

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 6:10 pm

Did you set " Flow Control: None" in minicom as i had a problem with that some time ago.
Batteries not included, Some assembly required.

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 6:19 pm

DexOS wrote:Did you set " Flow Control: None" in minicom as i had a problem with that some time ago.
I used a different terminal program.
Can' remember and I have no immediate access to the relevant PI.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 9:59 pm

Thanks Gordon, sound advice. So far I have bridged the Tx/Rx pins on the Raspberry Pi and confirmed at least that the Pi is echoing back the characters typed in minicom. The clock speed is also something I hadn't thought of, I will now look at that.

Thanks Gert,
Yes I am using your Serial_startup sketch as well as some others. At the moment I am simply using a sketch that sends 'abcd' every second.

Thanks for your help. I will let you know how I go.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Help with serial comms between the Pi and GertDuino 328

Mon Dec 02, 2013 11:32 pm

I have found the problem!

The timing on the Atmega-328 was off. I was running the following sketch on the Atmega-328

Code: Select all

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    Serial.print("abcd");
    delay(1000);
}
So print abcd on the serial connection and delay for a second.

I wrote a small amount of code on the Raspberry Pi to poll the serial connection. I measured the time difference while waiting for characters to be available to read from the serial connection. I expected the time difference to be approximately 1 second. However, the time difference was approximately 0.8 of a second.

...

And then I found the problem. I am using Gordon's configuration files for the Arduino IDE. I had accidentally selected Gertboard with ATmega328 (GPIO) rather than Gertuino with ATmega328 (GPIO) [sic]. It was writing code for a 12MHz processor rather than a 16MHz processor and this meant that the timing and baud rate was incorrect.

Thanks everyone for your help. Thanks ame you were on the right track, I just didn't understand how the baud rates could be different.

SpiderNZ
Posts: 2
Joined: Thu Dec 12, 2013 10:57 am

Re: Help with serial comms between the Pi and GertDuino [SOL

Thu Dec 12, 2013 11:07 am

Hi there,

I was having a similar problem with both a gertboard and gertduino. Just couldn't get any output from the boards on minicom.

Tried jumpers, searching google and the web and even jumping up and down.....all in vain until I found this thread!

It turns out that the sample code in the provided with the Arduino IDE just needs the following line at the start of any program wanting to write to the RS-232 buffer.

void send_serial_string(char *str);

a now I can get the output via minicom! :-)

Beron
Posts: 12
Joined: Tue Feb 05, 2013 11:41 pm

Re: Help with serial comms between the Pi and GertDuino [SOL

Thu Dec 12, 2013 9:10 pm

Thanks for the tip, I tried this as well but it's all dead for me.. not a single character I send over the serial port shows up. The other night I had some garbled text coming through but today, nothing. I've double and triple checked every setting, I just cant understand what I'm doing wrong!

Return to “HATs and other add-ons”