Arduino Forum

Products => Arduino Yún => Topic started by: scrot on Oct 06, 2013, 08:43 am

Title: Tty for serial port to Arduino from Linino
Post by: scrot on Oct 06, 2013, 08:43 am
All,

I'm trying to implement Firmata using pyfirmata on linino, and the Standard Firmata on the arduino.  I know on the Arduino side I need to use Serial1.

My question is: What /dev/tty to I use to talk to the arduino?  I know that the OS launches a terminal on that TTY which is how you can talk to the Linino over Serial1 from Arduino.  Simply want to treat the arduino as a dumb slave to my Python program running on the Linino.

Title: Re: Tty for serial port to Arduino from Linino
Post by: priority on Oct 09, 2013, 04:17 am
Needed this myself.  Just need to ask Linino "nicely". 

Code: [Select]
root@arduino:/# dmesg | grep tty
[    0.000000] Kernel command line:  board=Linino console=ttyATH0,250000 mtdparts=spi0.0:256k(u-boot)ro,64k(u-boot-env)ro,14656k(rootfs),1280k(kernel),64k(nvram),64k(art),15936k@0x50000(firmware) rootfstype=squashfs,jffs2 noinitrd
[    0.620000] ar933x-uart: ttyATH0 at MMIO 0x18020000 (irq = 11) is a AR933X UART
[    0.630000] console [ttyATH0] enabled, bootconsole disabled


Found this by searching through the OpenWRT forums.
Title: Re: Tty for serial port to Arduino from Linino
Post by: scrot on Oct 09, 2013, 04:35 am
Excellent!  I didn't even think of using dmesg.  The next question is how to disable the console that runs on that serial port?  I spend some time looking through init.d and didn't find anything.  I'll need to dig some more.  If I can disable the console then I should have a nice clean connection from python to arduino.
Title: Re: Tty for serial port to Arduino from Linino
Post by: priority on Oct 09, 2013, 06:22 am
Edit /etc/inittab to disable the serial console.  Specifically, comment or delete:
Code: [Select]
ttyATH0::askfirst:/bin/ash --login

More info: https://forum.openwrt.org/viewtopic.php?id=15165 (https://forum.openwrt.org/viewtopic.php?id=15165)

Looking forward to seeing how fast your clean connection python code runs.
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Oct 09, 2013, 11:35 am
Me too. Keep us posted. It looks like a much better approach
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Oct 09, 2013, 04:48 pm
Hi,
with the tips in this thread I was able to do some very basic IO from python to the atmega:

I disabled the login on ttyATH0 by editing the the file /etc/inittab
Code: [Select]

::sysinit:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K shutdown
#disable the login console on this serial by commenting it out
#ttyATH0::askfirst:/bin/ash --login


[font=Verdana]I'm not sure if disabling the console will cause any  sideeffects, but as long as I can still ssh into the Yun I feel safe[/font]

Here is a very basic sketch to run on the atmega, which reads a character and if it is one of the digits 0-8 it echos the digit value +1, otherwise it simply echos the character just read.
Code: [Select]

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

void loop() {
  char c;

  while (Serial1.available() > 0) {
    c=Serial1.read();
    if(c>='0' && c<'9')
      c++;
    Serial1.print(c);
   }
}

The I wrote the most basic python code to do some IO over serial: It opens the serial given on the first commandline argument and then sends the first 5 characters from the second argument. Then it reads 5 bytes from the serial and closes it.
Code: [Select]

#File st.py run like "python st.py /dev/ttyATH0 '123abc'
import serial
import sys

s=serial.Serial(sys.argv[1],9600,timeout=1)
data =sys.argv[2]
s.write(data[0:5])
result=s.read(5)
print result
s.close()


This is what I get, seems to work fine
Code: [Select]

root@YunYun:/mnt/sda1/python# python st.py /dev/ttyATH0 'ab3deohg'
ab4de
root@YunYun:/mnt/sda1/python#


Reference to PySerial : http://pyserial.sourceforge.net (http://pyserial.sourceforge.net)
Title: Re: Tty for serial port to Arduino from Linino
Post by: scrot on Oct 09, 2013, 06:53 pm
I was able to reproduce your results.  This is exciting.  I'm working on making Firmata work.  My first experiment didn't succeed.  I think it just a matter of some tinkering.  I'll keep you in the loop.
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Oct 09, 2013, 07:28 pm
Very well done wayoda. This is become really interesting. Out of curiosity: could you set up a stress test case?
Something like flooding the 32u4 with random chars (in a 1.000.000 rounds "for" loop) and checking if the output is the expected one
Title: Re: Tty for serial port to Arduino from Linino
Post by: robertofc on Oct 09, 2013, 10:39 pm
Great job!!! This is what I was looking for.
I'll try.
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Oct 10, 2013, 09:52 am

Out of curiosity: could you set up a stress test case?
Something like flooding the 32u4 with random chars (in a 1.000.000 rounds "for" loop) and checking if the output is the expected one

I did some quick tests. In short...


[font=Verdana]The baudrate in the python script and the Arduino Sketch must (obviously) match to make this work[/font]

Here is the python script for testing
Code: [Select]

"""
Usage :
    python st.py PORT BAUDRATE KILO_BYTES

example :
python st.py /dev/ttyATH0 115200 10
Will send and receive 10kB of data

turn on profiling:
python -m cProfile st.py /dev/ttyATH0 115200 10
"""

import serial
import sys

data='01234567012345670123456701234567'
expected='12345678123456781234567812345678'

#one packet is 32 bytes so 1kByte is 32 packets
packets=int(sys.argv[3])*32
s=serial.Serial(sys.argv[1],sys.argv[2],timeout=5)
for i in range(packets):
    s.write(data)
    result=s.read(32)
    if result!=expected:
        raise ValueError('Mismatch on run : '+str(i)+'\ndata = "'+data+'"\nresult = "'+result+'"')
print "Done!"
s.close()


On the Arduino I run this Sketch. It blinks the Led after coming out of a reset.
Code: [Select]

void setup() {
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  delay(500);
  digitalWrite(13,LOW);
  delay(500);
  digitalWrite(13,HIGH);
  delay(500);
  digitalWrite(13,LOW);
  Serial1.begin(115200);
 
}

void loop() {
  char c;
 
  while (Serial1.available() > 0) {
    c=Serial1.read();
    c++;
    Serial1.print(c);
  }
 
}



Here is the output of the longest test I ran  (4MB of data).
Takes about 7 minutes to complete!
Code: [Select]

root@YunYun:/mnt/sda1/python# python -m cProfile st.py /dev/ttyATH0 115200 4096
Done!
         5392556 function calls in 431.015 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.005    0.005    0.026    0.026 __init__.py:8(<module>)
        1    0.017    0.017    0.022    0.022 serialposix.py:13(<module>)
        1    0.000    0.000    0.000    0.000 serialposix.py:163(Serial)
        1    0.000    0.000    0.001    0.001 serialposix.py:168(open)
        1    0.001    0.001    0.001    0.001 serialposix.py:192(_reconfigurePort)
        1    0.000    0.000    0.000    0.000 serialposix.py:321(close)
   131072  189.347    0.001  392.045    0.003 serialposix.py:340(read)
   131072   11.837    0.000   25.662    0.000 serialposix.py:359(write)
        1    0.001    0.001    0.001    0.001 serialutil.py:111(SerialBase)
        1    0.001    0.001    0.002    0.002 serialutil.py:123(__init__)
        1    0.000    0.000    0.000    0.000 serialutil.py:196(setPort)
        1    0.000    0.000    0.000    0.000 serialutil.py:221(setBaudrate)
        1    0.000    0.000    0.000    0.000 serialutil.py:240(setByteSize)
        1    0.000    0.000    0.000    0.000 serialutil.py:253(setParity)
        1    0.000    0.000    0.000    0.000 serialutil.py:266(setStopbits)
        1    0.000    0.000    0.000    0.000 serialutil.py:279(setTimeout)
        1    0.000    0.000    0.000    0.000 serialutil.py:298(setWriteTimeout)
        1    0.000    0.000    0.000    0.000 serialutil.py:30(SerialException)
        1    0.000    0.000    0.000    0.000 serialutil.py:317(setXonXoff)
        1    0.000    0.000    0.000    0.000 serialutil.py:328(setRtsCts)
        1    0.000    0.000    0.000    0.000 serialutil.py:339(setDsrDtr)
        1    0.000    0.000    0.000    0.000 serialutil.py:35(SerialTimeoutException)
        1    0.000    0.000    0.000    0.000 serialutil.py:355(setInterCharTimeout)
        1    0.000    0.000    0.000    0.000 serialutil.py:40(FileLike)
        1    0.003    0.003    0.004    0.004 serialutil.py:8(<module>)
        1   13.242   13.242  431.015  431.015 st.py:14(<module>)
        3    0.000    0.000    0.000    0.000 {_struct.pack}
        2    0.000    0.000    0.000    0.000 {chr}
        3    0.000    0.000    0.000    0.000 {getattr}
       21    0.000    0.000    0.000    0.000 {hasattr}
   131072    1.061    0.000    1.061    0.000 {isinstance}
  2565176   21.640    0.000   21.640    0.000 {len}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'lower' of 'str' objects}
        1    0.000    0.000    0.000    0.000 {posix.close}
        1    0.000    0.000    0.000    0.000 {posix.open}
  1151516   62.691    0.000   62.691    0.000 {posix.read}
   131072   12.070    0.000   12.070    0.000 {posix.write}
        1    0.038    0.038    0.038    0.038 {range}
  1151516  119.062    0.000  119.062    0.000 {select.select}
        1    0.000    0.000    0.000    0.000 {termios.tcgetattr}
        1    0.000    0.000    0.000    0.000 {termios.tcsetattr}
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Oct 10, 2013, 10:11 am
Can you retry with 250000 baud? That's the speed we set for ttyATH0
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Oct 10, 2013, 12:55 pm

Can you retry with 250000 baud? That's the speed we set for ttyATH0

Doesn't work. For some reason I get incomplete readings for all speeds above 115200 baud. (230400, 250000 etc.)
I also tried to write smaller data packets of 16 bytes instead of 32 bytes, but no luck. Every baudrate above 115200 baud fails on the first write/read cycle.

But 115200 baud (10kB/s) is fast enough for most usecases where data from the ATmega32u4 is  being forwarded to a webservice.
I can live with that limitation.
Title: Re: Tty for serial port to Arduino from Linino
Post by: scrot on Oct 10, 2013, 04:47 pm
I got Firmata to work between a Python script running on Linino and the Arduino.  I was able to get about 400 messages per second which is sufficient for my needs.  I made the following changes to the StandardFirmata sketch "begin" section.

Code: [Select]
void setup()
{
 
  Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);

  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
  Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.attach(START_SYSEX, sysexCallback);
  Firmata.attach(SYSTEM_RESET, systemResetCallback);

  Serial1.begin(9600); // Set the baud.
  Firmata.begin(Serial1);
  systemResetCallback();  // reset to default config
}


On the python I installed PyFirmata and pyserial.  The following code turns on the LED:

Code: [Select]
from pyfirmata import Arduino, util
board = Arduino('/dev/ttyATH0', baudrate=9600)
board.digital[13].write(1)
Title: Re: Tty for serial port to Arduino from Linino
Post by: GreyCon on Oct 10, 2013, 04:51 pm
Hi Wayoda,

   when you say a BAUD rate of 250000 fails - do you get characters from Serial, or nothing at all? The Arduino UART should definitely do 250000 , and higher. And since the hard-wired serial connection is very short, there shouldn't be any signal degradation issues.

Maybe you have a marginal board. I will give it a go tonight and let you know results. If necessary we can look at the UART registers for over-run or parity errors....

Con
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Oct 10, 2013, 05:45 pm

Hi Wayoda,
   when you say a BAUD rate of 250000 fails - do you get characters from Serial, or nothing at all?


The python script writes 32 chars to the ATmega and then reads 32 bytes with a five second timeout.
When used with baudrates above 115200 the read returns unexpected characters and also too few of them.
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Oct 10, 2013, 06:33 pm

The Arduino UART should definitely do 250000 , and higher. And since the hard-wired serial connection is very short, there shouldn't be any signal degradation issues.
Maybe you have a marginal board. I will give it a go tonight and let you know results. If necessary we can look at the UART registers for over-run or parity errors....

I tried everything to create a bad environment for the Yun to provoke an error at 115200 baud...
- Wind an active Lan-cable several times round the Yun
- Place the Yun on to of my cellphone, and call myself
...but the python script runs without problems

Raise the speed to 250000 and it fails .

BTW:
Sketch programming from the Yun side (not over USB) does not use the serial port. It uses the avrdude -c linuxgpio option which emulates a SPI programmer. See file /etc/avrdude.conf line 976
 
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Oct 10, 2013, 08:11 pm
Hi,
there is a serious problem with my little echo sketch on the ATmega.

The changes in /etc/inittab detach the serial line to the ATmega, but this happens very late in the boot process.
Until just before the login prompt for the shell is shown, all kernel messages are still forwarded to the ATmega.
Because my test sketch more or less echos what is read from the serial, the  boot process seems to be stopped by the random data coming back over Serial1.
The effect is that because of the echo sketch the WiFi setup never completes. Login over ssh is [font=Verdana]not possible[/font].

When the simple Blink Sketch or even the Yún Serial Terminal runs on the ATmega the Yun boots fine because these Sketches don't throw any random data back at the linux machine. If you are stuck like that because you tried the test at home, simply upload a Sketch not writing to Serial1. http://arduino.cc/en/Tutorial/YunSerialTerminal (http://arduino.cc/en/Tutorial/YunSerialTerminal)

For an application using the Serial1 to communicate with the ATMega this means, you are only allowed to listen on the Serial1 until the Linux machine was sucessfully booted.
 
Title: Re: Tty for serial port to Arduino from Linino
Post by: scrot on Oct 10, 2013, 08:30 pm
If you look in the Bridge.cpp source code you'll se that they deal with this.

Code: [Select]
// Wait for U-boot to finish startup
  do {
    dropAll();
    delay(1000);
  } while (stream.available()>0);


This doesn't seem very fool proof.  Its counting on the fact that during the boot process you see some data at least once per second come over serial.  If there was a > 1 sec pause then you'd be out of luck.

I'd be happy to see a robust code snippet I could insert into my sketches.

Good catch.
Title: Re: Tty for serial port to Arduino from Linino
Post by: priority on Oct 11, 2013, 03:51 am
Hmmm....I have used the serial console of the early boot process before to debug a bad dd-wrt (openwrt fork) image on a router. I suspect that this "feature" is enabled by default in the openwrt build process since this is a relatively good way to unbrick a board with a corrupt flash image.

A bootlog for linino is at:  https://github.com/arduino/linino/blob/master/docs/hardware/danube/bootlog (https://github.com/arduino/linino/blob/master/docs/hardware/danube/bootlog).  There are several opportunities that echoing the console is not a good idea!

Took a bit of digging, but it appears the only way to stop the "terminal chatter" early in the boot process is to rebuild the kernel to disable "Early printk".  Not something that most folks (including me) will be excited about for many reasons.   Seems like the best workaround is exactly what scrot and wayoda are doing: find a way to make sure the 32u4 does not respond to this "terminal chatter".
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Oct 11, 2013, 08:22 am
Indeed, disabling printk doesn't seem a very good idea: I fear we may lose dmesg output, making device debugging (wifi in particular) impossible
Title: Re: Tty for serial port to Arduino from Linino
Post by: noblepepper on Oct 11, 2013, 08:27 pm
Just a warning to the wise, it looks like this breaks the WiFi reset button, re-enabling
Code: [Select]
ttyATH0::askfirst:/bin/ash --login in /etc/inittab makes it work again.

If you lose your Yun this way check out my post about "is my WiFi dead?"
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Oct 17, 2013, 12:06 pm
Hi,
I had a little accident in my workshop, so I will not be able to do any serious work with my yun, but I took some time to read the whole documentation that is available, and there might be a nice way to signal the atmega when the linux machine is available. The original Yun docs mention a heartbeat line from the mips-processor (GPIO 19) to the arduino Pin 7.

I should be possible to write a Linux Init-script that switches this handshake to a certain state when the Linux part is fully booted and the Console is free to use for an application. The same is possible for situations where the Linux part is shutting down or in a Reboot.

An Arduino Sketch would simply have to monitor Pin 7 and thereby knows when serial IO with som Python code is possible.
One-handed typing is a bit arkward, so here are just the references:
http://arduino.cc/en/Main/ArduinoBoardYun  (http://arduino.cc/en/Main/ArduinoBoardYun)
http://arduino.cc/en/uploads/Main/arduino-Yun-schematic.pdf (http://arduino.cc/en/uploads/Main/arduino-Yun-schematic.pdf)
in both document search for "handshake"
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Oct 17, 2013, 04:27 pm
While you are investigating on your own, do you mind opening on issue on https://github.com/arduino/YunBridge ? This is pretty cool stuff and it would be a shame to forgot it just because we are stuck with other tasks at the moment
Title: Re: Tty for serial port to Arduino from Linino
Post by: mundosimaginados on Oct 21, 2013, 12:42 am

It works for me... thank you. I've working on that and at the end you gives the solution.


I got Firmata to work between a Python script running on Linino and the Arduino.  I was able to get about 400 messages per second which is sufficient for my needs.  I made the following changes to the StandardFirmata sketch "begin" section.

Code: [Select]
void setup()
{
 
  Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);

  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
  Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.attach(START_SYSEX, sysexCallback);
  Firmata.attach(SYSTEM_RESET, systemResetCallback);

  Serial1.begin(9600); // Set the baud.
  Firmata.begin(Serial1);
  systemResetCallback();  // reset to default config
}


On the python I installed PyFirmata and pyserial.  The following code turns on the LED:

Code: [Select]
from pyfirmata import Arduino, util
board = Arduino('/dev/ttyATH0', baudrate=9600)
board.digital[13].write(1)

Title: Re: Tty for serial port to Arduino from Linino
Post by: scrot on Oct 21, 2013, 02:50 am
Thanks for the update.  Here's my Firmata setup().  Its very similar to yours.  The only difference is a section to wait for the U-boot to complete, and I set the baud to 115200 which works like a champ.  I stole the U-boot section from Bridge.cpp.

Code: [Select]
void setup()
{
  Serial1.begin(115200); // Set the baud.
 
   // Wait for U-boot to finish startup.  Consume all bytes until we are done.
  do {
     while (Serial1.available() > 0) {
        Serial1.read();
        }
   
    delay(1000);
  } while (Serial1.available()>0);
 
  Serial.begin(9600); // For logging.
 
  Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);

  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
  Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.attach(START_SYSEX, sysexCallback);
  Firmata.attach(SYSTEM_RESET, systemResetCallback);

  Firmata.begin(Serial1);
  systemResetCallback();  // reset to default config
}
Title: Re: Tty for serial port to Arduino from Linino
Post by: GreyCon on Oct 22, 2013, 12:30 am
Hi,
  I use the Linino to handle REST Web Services, and JSON data. These services talk to an API layer, which maps the JSON data to "device level" calls, which my Arduino understands. (Eg replace Timers for Monday, read all timers, etc.)

This API layer takes a sequence of bytes (A private command or the Arduino API), wraps it in a frame (SOM BYTE, frame datalen, DATA, EOM, and Checksum) and sends it to the Arduino over the serial port.

I have a C++ class (YUNListener) on the Arduino which waits for SOM, parses the data, ensures there's an EOM, and validates the Checksum. So ant comms errors, or other unwanted / unexpected incoming data on the serial bridge are discarded. The C++ class has 2 Virtual methods, which do nothing in the base class, but are designed to be over-ridden in real subclasses. (Eg TimerListener : YUNListener)

The methods are:
1. handleCommand( data ) which you can implement, and will be given a correct byte array from incoming commands.
2. debug( data ) which will be called at various points in the command parsing process. I use this method to check for an input pin being pulled high, and if it is, I output serial debugging info to an output pin . So I have a cable that connects the output pin to a serial terminal, and straps the other pin high. So I automatically get debug output when I connect the cable to my laptop.

Happy to supply the code if anyone is interested.

Title: Re: Tty for serial port to Arduino from Linino
Post by: scrot on Oct 22, 2013, 02:16 am
Please show us the code, or post a link to GitHub.  I'd be very interested in seeing your code.  Its probably simpler than Firmata which would be great.
Title: Re: Tty for serial port to Arduino from Linino
Post by: GreyCon on Oct 22, 2013, 09:50 pm
No problem, it will be tomorrow or thursday night before I am home and able to upload it. I have never actually put code on Github but how hard can it be :-)

Title: Re: Tty for serial port to Arduino from Linino
Post by: GreyCon on Oct 25, 2013, 01:19 am
Hi,
  sorry for the delay. I have the arduino code running just fine (It was already running on another Arduino) but I have been in a rabbit-hole trying to get C code to cross-compile for the Yun. Anyway, I have it working now.

So, I will finish off the Linux part of the protocol classes, and put them on Github. Hopefully this weekend.

Cheers,
Con
Title: Re: Tty for serial port to Arduino from Linino
Post by: violinuxer on Nov 13, 2013, 11:46 pm
Hi everybody!

Sorry if I'm necroing this post... just wanted to add my 2 cents and confirm that things don't work well at 250k baud.

I too have been working on bypassing the Bridge library which (in my experience) is VERY slow. With regards to the baud rate, I agree that 250k baud seems awfully high. I also had trouble with dropped characters at that rate when reading them on the device. What if it's purely a matter of CPU Speed? At a rate like 250k baud, the CPU shouldn't have much time to empty the UART buffers before they overflow. Might the dropped characters just be lost in a buffer overflow because the CPU cant quite keep up?

Anyways, I disabled the tty and connected at 115k. It works great :)

violinuxer
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Nov 14, 2013, 06:33 am
What do you exactly mean by you disabled tty?
Thought tty is required for Bridge.
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Nov 16, 2013, 03:32 pm

Hi,
  I use the Linino to handle REST Web Services, and JSON data. These services talk to an API layer, which maps the JSON data to "device level" calls, which my Arduino understands. (Eg replace Timers for Monday, read all timers, etc.)

This API layer takes a sequence of bytes (A private command or the Arduino API), wraps it in a frame (SOM BYTE, frame datalen, DATA, EOM, and Checksum) and sends it to the Arduino over the serial port.

I have a C++ class (YUNListener) on the Arduino which waits for SOM, parses the data, ensures there's an EOM, and validates the Checksum. So ant comms errors, or other unwanted / unexpected incoming data on the serial bridge are discarded. The C++ class has 2 Virtual methods, which do nothing in the base class, but are designed to be over-ridden in real subclasses. (Eg TimerListener : YUNListener)

The methods are:
1. handleCommand( data ) which you can implement, and will be given a correct byte array from incoming commands.
2. debug( data ) which will be called at various points in the command parsing process. I use this method to check for an input pin being pulled high, and if it is, I output serial debugging info to an output pin . So I have a cable that connects the output pin to a serial terminal, and straps the other pin high. So I automatically get debug output when I connect the cable to my laptop.

Happy to supply the code if anyone is interested.

Sounds promising.
Anything to share?
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Nov 16, 2013, 03:49 pm
Maybe someone following this thread can help me regarding my problem posted over there: http://forum.arduino.cc/index.php?topic=194934.msg1470401#msg1470401
Title: Re: Tty for serial port to Arduino from Linino
Post by: violinuxer on Nov 16, 2013, 04:49 pm

What do you exactly mean by you disabled tty?
Thought tty is required for Bridge.


Sorry about the confusion... let me clarify.

I'm attempting to get real time data from an I2C sensor that I want to send to a computer over Wi-Fi. I attempted to use the Console class to send data from the microcontroller to the Linino processor. It was reliable and all, but it was WAY too slow (it sent only 2 or 3 samples per second). I then decided to bypass bridge altogether and write a very simple Serial to TCP Bridge using pyserial. Without bridge enabled, I could send values to the linux processor via Serial1 (at 250k baud) to the shell quite quickly, but there were many issues with dropped characters because of the high UART speed. I disabled the tty in inittab so I could set my own baud rate for communications, after which the transmission worked great.

To everybody else:

Does anyone know how to change the tty baud rate at boot time? The speed appears to be coded into the kernel args. Do I have to build my own kernel to change the speed? Is there a file somewhere I can edit?

Also, I would love it if future releases of Linino adopt a 115k baud TTY speed as it appears to be MUCH more stable.

Anyways, thanks for all the help!

violinuxer
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Nov 16, 2013, 04:55 pm
Thanks for the clarification.
Could you share your python code?

BTW: Does it count whether one connects Yun via USB and uses TTY from IDE or WiFi only? Looks like when doing Serial1.println() Serial Monitor throws back something from BuysBox:
Code: [Select]
@arduino:/#
oot@ arduino:/# ->test=[[1,2],[3,4]]
sh: -: not found

Where "->test=[[1,2],[3,4]]" is my data.
Title: Re: Tty for serial port to Arduino from Linino
Post by: violinuxer on Nov 16, 2013, 05:12 pm


Hi Wayoda,
   when you say a BAUD rate of 250000 fails - do you get characters from Serial, or nothing at all?


The python script writes 32 chars to the ATmega and then reads 32 bytes with a five second timeout.
When used with baudrates above 115200 the read returns unexpected characters and also too few of them.


I can confirm this. At 250k, characters are sent, but it drops characters pretty often. At 115200 and below, it works great.

violinuxer
Title: Re: Tty for serial port to Arduino from Linino
Post by: violinuxer on Nov 16, 2013, 10:14 pm
Hello everybody!

Just in case anybody might find this useful, here is the simple, fast python script I've been working on to send anything written to Serial1 to a TCP port for access over the network. It doesn't do any receiving, but it should be relatively trivial to add that functionality. As many mathematics journals say, "its left as an exercise to the reader." :)

Note: make sure to install pyserial via opkg

Hope this helps someone....

violinuxer

Code: [Select]

import serial
import socket

TCP_IP = "0.0.0.0"
TCP_PORT = 5454

serial_conn = serial.Serial("/dev/ttyATH0", 115200, timeout=1)

server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sock.bind((TCP_IP, TCP_PORT))
server_sock.listen(1)

while True:

print "Waiting for connection..."
conn, addr = server_sock.accept()

print "Client " + str(addr) + " connected."
serial_conn.flushOutput()
serial_conn.flushInput()
try:
while True:
text = serial_conn.readline()
print "Sending: " + text
conn.send(text)
except socket.error as e:
print e
finally:
conn.close()
Title: Re: Tty for serial port to Arduino from Linino
Post by: NewLine on Nov 24, 2013, 02:09 pm
Sorry for my ignorance, but I have some very basic questions:

- why do you need to disable the Console to get this to work?
- why is the Linino sending characters over that link whilst booting?

It would be great if I could threat the Arduino-Linino connection as a very simple object like (hopefully) a Serial.
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Nov 24, 2013, 02:21 pm
Newline, are you referring to the /etc/inittab modifications?
If yes, I had to do this or a connection to ttyATH0 would result in a login which could not be done by my server process. But to be honest, I would like to see more detailed information and documentation how this all works together.

Another thing I had to do in my sketch is wait until Linino is done with its boot process and after that change connection speed to 115200. It looks like that Linino starts using 250000 on Serial1 and you can only change to a different connection speed after the boot sequence completed.
Title: Re: Tty for serial port to Arduino from Linino
Post by: noblepepper on Nov 24, 2013, 02:49 pm
As far as the boot process, Openwrt (linino) goes through a few steps while booting up. The console is attached to the serial port to allow interaction in case bad things happen so you can fix them, without this you could end up "bricked" with no way to "unbrick".

Specifically first there is an opportunity to stop uboot so you can flash/boot if the image is bad. Next is failsafe mode which allows you to fix things that are messed up in the linux system configuration, next is the console started in inittab which allows you access if you lose network connectivity. While you could run for years without needing these things they are nice to have, kinda like airbags in a car, if you need them you are VERY glad you had them.

I believe the only way to disable the first two early stages would be to compile an image after appropriate changes have been made to uboot and kernel parameters. 
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Nov 26, 2013, 09:17 am
super clear explanation noblepepper and I love the airbag metaphor. It would be nice to have a "yun boot up explained" paragraph on the playground, do you mind pasting your post there?
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Nov 26, 2013, 12:32 pm
I like the explanation too.
And I do not feel very comfortable about changing inittab and removing the tty entry.
But what is a better solution if I want to attach to Serial1 and listen for incoming data?
Title: Re: Tty for serial port to Arduino from Linino
Post by: mediamad on Dec 20, 2013, 04:37 pm
So I'm not sure after reading through the posts if I definitively understand the process.

1) Does the tty HAVE to be disabled in inittab for the connection between the two sides to work?  If not, do I correctly understand that once the boot process is finished you then must reset the baud rate?

2) Can the Bridge app be left alone or should it be disabled from starting as well?
Title: Re: Tty for serial port to Arduino from Linino
Post by: noblepepper on Dec 21, 2013, 02:47 am
1)a No, definitely not true, the bridge operates fine with this enabled. I haven't studied it enough to tell you exactly how it does this, but this is where I would start. I believe Federico said that the bridge filters out some "chatter" that the kernel will send to the console from time to time.
1)b There are some that say 250,000 is too fast but the bridge seems to operate there fine.

2) I think if you want to use this connection without going through the bridge you will need to disable the bridge since it is using it. You will lose all the functionality of the bridge if you do.



Title: Re: Tty for serial port to Arduino from Linino
Post by: mediamad on Dec 21, 2013, 05:00 am
@noblepepper Thanks for comments.  I realize I wasn't clear on 1) I was asking this question with regard to establishing a serial connection from Python, not using the Bridge.
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Dec 21, 2013, 08:29 am
Regarding my experience from doing a bi-directional / full duplex Serial1 based communication between a python script and an Arduino sketch, I had to comment the following line in iinittab to get the communication to work without problems:
Code: [Select]
ttyATH0::askfirst:/bin/ash --login
The reason is that with leaving the above line in place, some parts of my data send from Arduino to  Linino got lost. The other way round works perfectly.
Currently I'm making tests with leaving the line in inittab and disabling the tty echo. I'll post about my progress here.
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Dec 21, 2013, 02:11 pm
Only get duplex Serial1 communication to work properly when modifying inttab by commenting the ttyATH0 line.
Maybe some of the tty experts can explain it to me. I still don't understand how it all plays together.
Title: Re: Tty for serial port to Arduino from Linino
Post by: priority on Dec 24, 2013, 03:29 am

Only get duplex Serial1 communication to work properly when modifying inttab by commenting the ttyATH0 line.
Maybe some of the tty experts can explain it to me. I still don't understand how it all plays together.


The answer to your question is quite clear: 
If the Bridge is running while your Python program is running, there is no mechanism to decide whether your Python program or the Bridge gets incoming data on the serial port resulting in data loss.



Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Dec 24, 2013, 08:43 am

The answer to your question is quite clear:  
If the Bridge is running while your Python program is running, there is no mechanism to decide whether your Python program or the Bridge gets incoming data on the serial port resulting in data loss.


So I will give it a try and stop bridge before launching my python script. Maybe this will make it unnecessary to modify inittab.

Edit: No, it's not the bridge, because calling /usr/bin/kill-bridge right before starting my python script does not solve my problem. Looks like I really have to get rid of the "ash --login".
Title: Re: Tty for serial port to Arduino from Linino
Post by: spdonegan on Dec 24, 2013, 03:28 pm
I have not found where bridge.py is being started in the system - given what I am trying to do I prefer to completely defuse the Bridge/Process/etc code and simply let the 32U4 and the AR9331 talk directly via ttyATH0/Serial1.

TIA

steve at donegan dot org
Title: Re: Tty for serial port to Arduino from Linino
Post by: priority on Dec 24, 2013, 04:00 pm

Edit: No, it's not the bridge, because calling /usr/bin/kill-bridge right before starting my python script does not solve my problem. Looks like I really have to get rid of the "ash --login".


Sorry, I should have been more clear. 

If the ANYTHING (including the console, Bridge, or any other program) is using tty on the linux side while your Python program that is trying to use tty is running, there is no mechanism to decide whether your Python program or the other program gets incoming data on the serial port resulting in data loss.

So, YES, you must disable console on tty in order for your Python program to reliably receive data from the 32u4.
Title: Re: Tty for serial port to Arduino from Linino
Post by: priority on Dec 24, 2013, 04:09 pm

I have not found where bridge.py is being started in the system - given what I am trying to do I prefer to completely defuse the Bridge/Process/etc code and simply let the 32U4 and the AR9331 talk directly via ttyATH0/Serial1.

TIA

steve at donegan dot org

The 32u4 sends the bridge startup command over the serial port to start the bridge process.   If the serial console is enabled, the linux side will execute this command just as if someone had typed it from a keyboard and hit enter.  Thus, if the serial console is disabled, the Bridge is effectively disabled.

The relevant portion of code from bridge.cpp in the Bridge library:
Code: [Select]

  ...
    // Bridge startup:
    // - If the bridge is not running starts it safely
    stream.print(CTRL_C);
    delay(250);
    stream.print(F("\n"));
    delay(250);
    stream.print(F("\n"));
    delay(500);
    // Wait for OpenWRT message
    // "Press enter to activate console"
    stream.print(F("run-bridge\n"));
    delay(500);
    dropAll();
...
Title: Re: Tty for serial port to Arduino from Linino
Post by: spdonegan on Dec 25, 2013, 12:37 pm
Thank you Priority. So if I do not include bridge.h/do any 32U4 side bridge related calls, and comment out the serial port in inittab then no process should run on either processor which uses Serial1? If so I will proceed with my little project - Arduino captures GPS data, Hall sensor data (vehicle speed) and calculates some basic speed/distance/time/location data and sends that as a single line to the AR9331/Linino side. I am trying to replace a Raspberry Pi for my application (Open Road Rally computer).

Thanks! And Merry Christmas!
Title: Re: Tty for serial port to Arduino from Linino
Post by: priority on Dec 26, 2013, 12:41 am

Thank you Priority. So if I do not include bridge.h/do any 32U4 side bridge related calls, and comment out the serial port in inittab then no process should run on either processor which uses Serial1? ....

Thanks! And Merry Christmas!


Merry Christmas as well!

Glad this was some help; took me a while to get my head wrapped around how the Bridge did its magic and then another bit of time to understand how to prevent the Bridge from doing its magic.  I wish the Bridge worked better; the transfer rate between Linux and 32U4 is just abysmal from my experience.

The only other pitfall that I am aware of wrt to using the serial connection between the 32U4 and Linino is during the period of time that Linino is booting.  During u-boot, printk is enabled and will spew quite a bit of text on the serial port.  The inittab disabling does not affect printk.  The big issue here is that you do NOT want to respond to any of the uboot text as you will cause the boot process to halt and wait for further input.   The second page of this thread has very good info (wayoda/scrot/me -- replies 16-18) about this issue and what the Bridge tries to do to ensure it is not affected.  Scrot has code that works for his needs in Reply 23 that leverages the Bridge u-boot pitfall avoidance loop.
Title: Re: Tty for serial port to Arduino from Linino
Post by: mediamad on Dec 29, 2013, 04:50 am
Thanks to everyone for the input here.  Scratching my head on something though... now that I have this set up how can I monitor the incoming data to Serial1 on the Arduino?
Title: Re: Tty for serial port to Arduino from Linino
Post by: Gernby on Jan 02, 2014, 09:42 pm
I intended to post in this thread first, since it's really what got me thinking about this, but since so many of you guys proved that the serial connection between the 2 Yun processors doesn't work well above 115200, it seems to me that maybe the whole reason why the Bridge library is so slow and flaky.  When I looked in the source code for the Bridge library, the default baudrate for Bridge.Begin() is 250000.  However, it looks like you can specify override that with Bridge.Begin(115200).  Has anyone tried this to see if it might improve the performance of the Bridge by reducing the number of CRC failures and retries?
Title: Re: Tty for serial port to Arduino from Linino
Post by: nickgammon on Jan 03, 2014, 11:16 pm
I think I deleted your other 10 or so posts on the exact same subject. Stick to one thread please.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

- Moderator
Title: Re: Tty for serial port to Arduino from Linino
Post by: Gernby on Jan 04, 2014, 02:39 am
Sorry for cross-posting, but I really think my posts were relevant to those threads.  I think the OP's of all of those threads would have wanted to see it ... :(

On another note, I was able to test the bridge at various baud rates, but I haven't determined for sure if it is more "stable" at 115200.  It took a while for me to figure out how to programatically change the speed of the UART on the Linino side, since I'm a total NOOB to Arduino and Linux "stuff", but it was a whole lot easier than customizing the Kernel!

I wrote a simple app in .NET that would repeatedly call REST commands in a loop, and executed that at 4800, 115200, and 250000 for hundreds of iterations.  The average time for each loop at 4800 wound up at 1.6 seconds, and the other 2 speeds were identical at 0.56 seconds.  This was over WiFi, so I will think about writing a test that eliminates some of the network latency.
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Jan 05, 2014, 09:07 am
The latency in REST calls is not related to Bridge speed but rather to how each http request in handled: each http call needs to start a new lua process (linux side)., that handles the request and then ends. Such process start-end loop is what is slowing down the REST api: the bottleneck is on the linux side.
Title: Re: Tty for serial port to Arduino from Linino
Post by: mwalimu on Jan 16, 2014, 02:29 am
How exactly does one install pyfirmata?  I can't do it from opkg (or can I?).  I've tried copying and pasting the source, but that doesn't seem to work.

EDIT: I figured it out "pip install pyfirmata"
Title: Re: Tty for serial port to Arduino from Linino
Post by: thomas-c on Jan 18, 2014, 06:30 pm
EDIT:  please also see mwalimu's question in reply # 59.

I started a new thread for doing tty through the SPI bus, realizing afterwards that my questions were off topic.
http://forum.arduino.cc/index.php?topic=211823.0
Title: Re: Tty for serial port to Arduino from Linino
Post by: mwalimu on Jan 19, 2014, 07:48 pm
New Problem!

I can't upload a sketch to the arduino over USB.  I can over the Yun interface (192.168.1.140 on my network).  I've tried uncommeting "ttyATH0::askfirst:/bin/ash --login" and rebooting, but nothing seems to be working.

I'm thinking I need to reflash the Yun image, but I'm afraid that i run the risk of bricking it since I can't program the arduino and there is no serial connection directly to the linux system.

Help?
Title: Re: Tty for serial port to Arduino from Linino
Post by: noblepepper on Jan 19, 2014, 09:01 pm
I don't think this has anything to do with the /dev/ttyATH0<->Serial1 connection, the USB is Serial on the AVR side of things. What do you mean by
Quote
I can't upload a sketch to the arduino over USB.
? What prevents it? Do you get errors? Do you not see the Yun in the IDE? You don't have a USB cable?

This solved one person's problems but without more information than you gave it may not help you:
flash bootloader-
Code: [Select]

/usr/bin/run-avrdude    /etc/arduino/Caterina-Yun.hex
Title: Re: Tty for serial port to Arduino from Linino
Post by: esbjerg on Feb 22, 2014, 05:00 pm
Going back to the beginning here, reading this and everything I can find has left me totally confused..

I managed to get firmata working through the usb connection, but when first reading this I thought it was for a wireless firmata, but on my third full read through I'm not sure, mostly because I'm not sure what some of this is or how to find actual information on it.
If someone could help me out that would be amazing.

Basically what I'm trying to do is get a wireless I/O into max. (I started a topic about it here but no love http://forum.arduino.cc/index.php?topic=216775.0 )
As I said in there, I have limited experience with arduino IDE (and to an extent Java, although I guess that's not helpful..) and no experience with python or linux.

Can anyone help me?

Also:
1. What's pyfirmata for on the arduino, from what I can gather it's an interface for on a computer?
2. When it's installed on the linino side, it's running linux, but installed with python?

I'm sorry about my ignorance here, I still think this is not that difficult, but I just can't get my head around it.
Thanks in advance!
Title: Re: Tty not working at 250000
Post by: Thomas33 on Mar 26, 2014, 01:59 pm
Just got my first yun. The HW/wlan side is very promissing (in terms of stability and most of all latency), but the brige code brings in disapointing latency of around 300 ms per round trip. So I looked for alternatives and found this very interessting thread.

Now I want to throw in my 2 cent:

I am sure, the data coming in at 250000 is to fast for the arduino side, as violinuxer in reply #29 already suspected. But this is not (not only) due to the CPU beeing so slow, but due to arduino library for serial.

All you are probably not aware of some serial speed issues being investigated while writing an arduino lib to connect AX-12 digital servos at 1 Mbit/s. I quote from http://www.pablogindel.com/informacion/the-arduinodynamixel-resource-page/:

Quote
Be sure to go into the hardware/arduino/cores/HardwareSerial.cpp and make the following changes::

// modificación para usar la biblioteca AX12

/*SIGNAL(SIG_USART1_RECV)
{
unsigned char c = UDR1;
store_char(c, &rx_buffer1);
}*/

^^ COMMENT THIS OUT!

To put it in one sentence: The ISR stuff in HardwareSerial.cpp just takes to long, so one needs to replace it to be able to use higher speed rates.

I am sure, this is the reason, why it is not working here with 250000. And why is it working during the boot, you may ask. I guess it is not working, but the incoming data is thrown away anyway (see reply #17), so nobody cares.

BTW: Federico Fissore in reply #58 is not quite right about where the 1.5 ms come from. It is not the process start, but just the WiFi delays (which are amazing short, to have that said). I know this, because i did some speed testing with a single threaded (=not threaded) python server on the linux side., and I got the same average 1.5 ms for one send-package-receive-package-cycle (on an already opened socket).

Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Mar 26, 2014, 06:03 pm
Hm, in my Arduino 1.5.6 HardwareSerial.cpp I can not find the given lines.
Title: Re: Tty for serial port to Arduino from Linino
Post by: tdicola on Mar 26, 2014, 09:50 pm
I also was just doing some speed intensive stuff and ran into terrible performance and latency issues with the bridge.  In my case I had a lot of output being quickly generated by a process and an arduino sketch trying to consume that output.  Not only was it very slow, but it was also dropping a lot of data.  I think the various levels of buffering in the bridge library need to be investigated, perhaps there are underflows/overflows occuring when things get into tight loops of sending/receiving data.  I also noticed the python process running the bridge would spike up to 99% CPU--might be interesting to see if there's a way to profile it and see where all the time is being spent.

In the end I disabled the bridge and had great luck just talking directly with Serial1.  In my case I found I had to wait about 90 seconds in the setup of my sketch before trusting any input on Serial1, otherwise I was getting bootup noise.  I tried the wait for 1 full second of no Serial1 traffic like in the bridge code and found it didn't actually work and bootup traffic was still getting spammed to my sketch.

Has anyone looked into using GPIO 19 on the Linino/MIPS processor, which appears in the schematic to be connected to ATmega INT6/AIN0/PE6 pin, as a means of signaling between the two processors?  I'm thinking of perhaps modifying the bridge to look for a high or low signal on this line and completely disable itself if it finds it.  For example in your sketch if you want to disable the bridge you would set PE6 high in the setup and sit waiting.  On the Linino side when the bridge starts up it would look for a high signal on GPIO 19 and if it finds it set the line low and immediately abort/stop running.  Then on the ATmega side it would be waiting for the line to go low as a signal that it's safe to start using Serial1 exclusively.  There's still a race condition if the linino processor gets reset but the ATmega does not or vice versa, but maybe there's a way to work around that (or just live with resetting both at the same time).  Consider this mod a 'draw bridge' that lets you disable and enable the bridge library from a sketch. :)
Title: Re: Tty for serial port to Arduino from Linino
Post by: wayoda on Mar 27, 2014, 09:58 am


Has anyone looked into using GPIO 19 on the Linino/MIPS processor, which appears in the schematic to be connected to ATmega INT6/AIN0/PE6 pin, as a means of signaling between the two processors? 

Here is some basic code and documentation concerning the use of GPIO 19 as a kind of linux boot monitor.

https://gist.github.com/wayoda/db3c023417757f726088

Eberhard
Title: Re: Tty for serial port to Arduino from Linino
Post by: Thomas33 on Mar 28, 2014, 12:06 pm
Quote
Hm, in my Arduino 1.5.6 HardwareSerial.cpp I can not find the given lines.


Yes, you are right, it has changed a bit. I just checked it: The ISR code is now called"HardwareSerial::_rx_complete_irq" an can be found in the file HardwareSerial_private.h. Anyway, the arduino style serial receive ISR can be deactivated e.g. in HardwareSerial1.cpp, it is the whole block around the line
Code: [Select]
Serial1._rx_complete_irq();


Anyway, the principle remains: Probably  the arduino style serial receive ISR is too slow, and must be replaced by an own (faster) ISR to be able to use more than 115200 bit/s.

========================

After thinking about all this, I made a theorie:
Why is there so much hassle in the bridge code with CRC and internal retries?? The "cable length" between the linino and the AVR side is so short, that there shouln't be any errors at all. Can it be, that the arduino people realized all these errors, but weren't aware off the problematic ISR timing, and then found a (bad) solution in doing CRC and retries?

Title: Re: Tty for serial port to Arduino from Linino
Post by: DarkCloud on Apr 02, 2014, 07:51 pm
As always, the problem with serial communications is the size of the receive buffer, not the speed of the link or the response of the interrupt service routine.  A 250kbaud serial link can deliver slightly more than 2kBytes per second - that's about 500microseconds between bytes.  However, it only takes about 4 microseconds for an interrupt service routine on a 16MHz Mega board to store that byte in the receiver buffer.

I had to write my own serial receiver for a project recently, with incoming data at close to 1Megabaud, with message lengths in the order of 500 bytes.  That required 4K of sram devoted to one receive buffer - rather more than the library allocates.  The main loop has to be very streamlined to process the messages, and any delay can cause a buffer overrun - that's bad news as the message must be sent again, or discarded.

And that's the problem - writing a library that probably supports *four* serial ports with receive and transmit buffers for each channel eats up sram - and that's not appreciated by users who use only one channel to output logging to a terminal.

@tdicola:- The reason for the 99% cpu usage mentioned previously is probably the serial transmit routine blocking when the transmit buffer becomes full.
Title: Re: Tty for serial port to Arduino from Linino
Post by: DarkCloud on Apr 04, 2014, 12:57 pm

Happy to supply the code if anyone is interested.

Ooooh yes please :smiley-mr-green:
Title: Re: Tty for serial port to Arduino from Linino
Post by: brewmonkey on Apr 08, 2014, 10:14 pm
Hi!
I'm about to start a project in which I *need* to have pure serial com between linux and the AVR. And so, I ended up here...
In order to make serial port free to AVR, kernel messages and console needs to be disabled.
But as pointed out, by removing the inittab entry and removing kernel messages at boot, will leave you in a bad situation if linux side should end up in a bad state.
So, my solution so far is to change inittab as such:
Code: [Select]

::sysinit:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K shutdown
#ttyATH0::askfirst:/bin/ash --login
ttyATH0::once:/bin/ash --login


This will start one shell at boot up, leaving the possibility to connect.
The sketch will have to wait for boot to pass in a similar manner as shown before, It will then have to connect
and send "echo 0 > /proc/sys/kernel/printk" to disable kernel output to serial, and finally an "exit" to kill the ash shell.
Now, the serial is free for use, without loosing the valuable boot messages and shell.
If anyone knows of any better solution, I'd very much appreciate if you share the info.
Title: Re: Tty for serial port to Arduino from Linino
Post by: noblepepper on Apr 09, 2014, 06:28 pm
I gave brewmonkey's approach a whirl, all seems to work as expected.

The only observations I have is you can modify inittab like this to never even start the console on ttyATH0:
Code: [Select]
::sysinit:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K shutdown
#ttyATH0::askfirst:/bin/ash --login


kill the kernel chatter in rc.local:
Code: [Select]
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
echo 0 > /proc/sys/kernel/printk
wifi-live-or-reset
exit 0


Now the sketch just needs to wait patiently until all is ready, if there are problems later the you can use failsafe to recover:
Code: [Select]
Press the [f] key and hit [enter] to enter failsafe mode
f
- failsafe -
/etc/preinit: line 1: telnetd: not found


BusyBox v1.19.4 (2013-08-07 16:16:02 CEST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

     ___                   ___                       ___           ___    
    /\__\      ___        /\__\          ___        /\__\         /\  \    
   /:/  /     /\  \      /::|  |        /\  \      /::|  |       /::\  \  
  /:/  /      \:\  \    /:|:|  |        \:\  \    /:|:|  |      /:/\:\  \  
 /:/  /       /::\__\  /:/|:|  |__      /::\__\  /:/|:|  |__   /:/  \:\  \
/:/__/     __/:/\/__/ /:/ |:| /\__\  __/:/\/__/ /:/ |:| /\__\ /:/__/ \:\__\
\:\  \    /\/:/  /    \/__|:|/:/  / /\/:/  /    \/__|:|/:/  / \:\  \ /:/  /
 \:\  \   \::/__/         |:/:/  /  \::/__/         |:/:/  /   \:\  /:/  /
  \:\  \   \:\__\         |::/  /    \:\__\         |::/  /     \:\/:/  /  
   \:\__\   \/__/         /:/  /      \/__/         /:/  /       \::/  /  
    \/__/                 \/__/                     \/__/         \/__/  
                                                                                     
           _______                     ________        __
          |       |.-----.-----.-----.|  |  |  |.----.|  |_
          |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
          |_______||   __|_____|__|__||________||__|  |____|
                   |__| W I R E L E S S   F R E E D O M

root@(none):/# mount_root
[   17.300000] jffs2: notice: (534) jffs2_build_xattr_subsystem: complete building xattr subsystem, 1 of xdatum (1 unchecked, 0 orphan) and 12 of xref (0 dead, 0 orphan) found.
[   18.930000] sd 0:0:0:0: [sda] 7716864 512-byte logical blocks: (3.95 GB/3.67 GiB)
[   18.930000] sd 0:0:0:0: [sda] Write Protect is off
[   18.930000] sd 0:0:0:0: [sda] No Caching mode page present
[   18.940000] sd 0:0:0:0: [sda] Assuming drive cache: write through
[   18.940000] sd 0:0:0:0: [sda] No Caching mode page present
[   18.940000] sd 0:0:0:0: [sda] Assuming drive cache: write through
[   18.950000]  sda: sda1
[   18.960000] sd 0:0:0:0: [sda] No Caching mode page present
[   18.960000] sd 0:0:0:0: [sda] Assuming drive cache: write through
[   18.960000] sd 0:0:0:0: [sda] Attached SCSI removable disk
switching to jffs2
root@(none):/#
root@(none):/# vim /etc/inittab
root@(none):/# vim /etc/rc.local
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Apr 09, 2014, 08:12 pm
Sorry for my ignorance, but I do not get the point of what noblepepper is suggesting:

Changing inittab to comment ash start, was stated to be dangerous (I don't know why because I still was able to connect via YunTerminal). And having a sketch sitting and waiting to get Linino started is also known practice. In what way is changing rc.local to reduce chatter an improvement?
Title: Re: Tty for serial port to Arduino from Linino
Post by: brewmonkey on Apr 09, 2014, 09:36 pm
Ah, that is a lot better.
I will try it out! Thanks!
Well, it is better because we still recieve boot messages that could be useful in case something goes wrong, but a sufficiently long wait or even an mcu-reset after linino boot might do it if sketch is well written. So not so much special handling from sketch.
And if failsafe mode enables console, then that could be used as last resort.
Title: Re: Tty for serial port to Arduino from Linino
Post by: noblepepper on Apr 10, 2014, 03:56 pm
Quote
I do not get the point


The point is that this deals with two issues when a sketch tries to use the serial connection between the AR9331 and the 32u4 outside of the bridge:
1. There is a shell or console connected to the serial port on the AR9331, this intercepts some communication on the connection. This is useful to send shell commands to the AR9331 but if you want a program on the AR9331 to use the data directly it is a problem.
Quote
In what way is changing rc.local to reduce chatter an improvement?

2. The kernel considers this to be the "system console" so it sends messages there on occasion. For example, plugging and unplugging an ethernet connection to the Yun causes
Code: [Select]
[   71.050000] eth1: link up (100Mbps/Full duplex)
[   79.050000] eth1: link down
to be sent to the serial port. This happens regardless of whether there is a shell running there. A sketch on the 32u4 needs to discriminate between this and the data which it needs.

Quote
dangerous
? Not really. Scary sometimes? Perhaps. The first time I disabled the shell and had some network issues it was very disconcerting. But you can still use YunSerialTerminal to fix things, either with failsafe or if really needed, uboot and sysupgrade. This works because the Yun goes through three stages when it boots, uboot which is kind of like the bios on a desktop, preinit which mounts the overlay filesystem and does other needed work, and init which brings up the final system. Uboot and preinit are not affected by these changes so they are still available (and you have to wait for them to get out of the way)

Quote
I don't know why because I still was able to connect via YunTerminal
You still see the uboot and preinit stuff but on my yun when I comment the
Code: [Select]
ttyATH0::askfirst:/bin/ash --login in inittab there is no shell available once the init stage takes over.

All in all it just amounts to what you want, for most people none of this would be useful, in fact would be a bad thing since it disables a lot of the stuff the Arduino people worked very hard to put in place to make the Yun easier to use.
Title: Re: Tty for serial port to Arduino from Linino
Post by: mamu on Apr 10, 2014, 04:38 pm
Many thanks for your explanations.
Maybe you forgot that I'm one of the guys who already uses serial communication between my sketch and my python web sever doing Web Sockets for client interaction.
So I already modified my inittab and got it to work.
I was just courious about the rc.local mod for which I do not see the advantage.
I'm a little disappointed about the Yun Team not natively giving the choice using tty or spi for doing the communication.
Blocking serial with a slow Bridge implementation is not what I would prefer.
Title: Re: Tty for serial port to Arduino from Linino
Post by: sonnyyu on Apr 14, 2014, 03:39 pm

...
If anyone knows of any better solution, I'd very much appreciate if you share the info.


Not better solution but work around.

Rootfs on External Storage microSD card (extroot)

http://wiki.openwrt.org/doc/howto/extroot (http://wiki.openwrt.org/doc/howto/extroot)

http://forum.arduino.cc/index.php?topic=228204.msg1651254#msg1651254 (http://forum.arduino.cc/index.php?topic=228204.msg1651254#msg1651254)

Edit /etc/inittab at Rootfs on microSD card, comment out or delete:

Code: [Select]
#ttyATH0::askfirst:/bin/ash --login

Whenever you need access serial port again, just remove microSD card and reboot.
Title: Re: Tty for serial port to Arduino from Linino
Post by: sonnyyu on May 09, 2014, 07:57 pm
an other work around:

Force AR9331 to device mode, then redirect tty to USB port, release UART port. 

http://forum.arduino.cc/index.php?topic=238834.0 (http://forum.arduino.cc/index.php?topic=238834.0)
Title: Re: Tty for serial port to Arduino from Linino
Post by: sonnyyu on May 14, 2014, 05:55 am
Plan C:

Wire one more PHY/Mac  ( ethernet port ) from AR9331 and set it as static IP address. then we could release UART port.
Title: Re: Tty for serial port to Arduino from Linino
Post by: sonnyyu on May 14, 2014, 05:59 am
Plan D:

Wire out i2c from  AR9331  for communication between CPU and MCU. Leave UART port  alone.
Title: Re: Tty for serial port to Arduino from Linino
Post by: sonnyyu on May 14, 2014, 06:00 am
Plan E:

Wire SPI from AR9331 for communication between CPU and MCU. Leave UART port  alone.
Title: Re: Tty for serial port to Arduino from Linino
Post by: chaos_2k on Jun 30, 2014, 10:18 am
Regarding the instability of the UART interface that the bridge library uses at higher baudrates: It appears that there is a bug in the linux driver for the AR9331 UART that can lead to imprecise bus timings. Alex Walker has created a patch http://permalink.gmane.org/gmane.comp.embedded.openwrt.devel/23013 (http://permalink.gmane.org/gmane.comp.embedded.openwrt.devel/23013) (based on some excellent research!), but unfortunately it hasn't made its way into the kernel or openwrt for that matter yet.

Has anyone tried the patch and checked how this affects the bridge performance?
Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Jun 30, 2014, 10:29 am
It would be great if someone could give this a spin. In order to test it, once you're almost ready to build the image, create a patch in folder
Code: [Select]

~/openwrt-yun/target/linux/ar71xx/patches-3.3

Title: Re: Tty for serial port to Arduino from Linino
Post by: federicofissore on Jun 30, 2014, 11:02 am
Also note that 32u4 Serial1 (when used by Bridge) runs exactly at 250k, the ar9331 uses a number that is almost 250k. See this patch https://github.com/arduino/openwrt-yun/blob/master/target/linux/ar71xx/patches-3.3/619-MIPS-ath79-Linino-uart-fix-baudrate.patch#L23
Title: Re: Tty for serial port to Arduino from Linino
Post by: born2net on Oct 23, 2014, 03:52 am
yes I had the same issue..

using node.js and Firmata.

FIXED:

in /usr/lib/node_modules/firmata/lib/firmata.js edit the line

    if(typeof port === 'object'){
        this.sp = port;
    } else {
        this.sp = new SerialPort(port, {
            baudrate: XXXX,
            buffersize: 1

        });
    }


and set XXXX to be 9600

and in StandardFirmata set the same:

Serial1.begin(9600); // Set the baud.

now Node.js speaks to Firmata no problem!!!!!!!
Title: Re: Tty for serial port to Arduino from Linino
Post by: marinne on Feb 08, 2015, 10:52 pm
I'm using node.js + Johnny-Five running on Yun (same with @born2net) to communicate with the hardware and having problem connecting to the board. Most of the times (about 95%, but not always) Johnny-Five throws an exception saying timeout while trying to connect to Board. After setting the baudrate to 9600 on both sides (StandardFirmata and my node.js code), it worked! :)

But instead of directly modifying the library source, which IMHO is not that appropriate, I'm using this in my node.js code:

Code: [Select]
var serialPort = new serialport.SerialPort('/dev/ttyATH0', {
  baudrate: 9600,
  buffersize: 1
});
var board = new five.Board({
  port: serialPort
});
board.on('ready', function(){
  // Read sensor data
});
Title: Re: Tty for serial port to Arduino from Linino
Post by: jessemonroy650 on Feb 09, 2015, 01:35 am
@marinne,
please start a new thread. Subject: node.js + Johnny-Five serial port problems.

TIA
Jesse
Title: Re: Tty for serial port to Arduino from Linino
Post by: lucabarbierato on Sep 03, 2015, 01:12 am
I'm using Serial1 to comunicate between ATmega and AVR at 115200 baud with node.js (with node-serialport). When disabling tty line in inittab, it start to work fine. I can receive buffer from node to arduino sketch without problem. When I try to send back responses from arduino to node, it works for short frame. With long frame, the first messages are received from node, then node start to receive only parts of packet..and then Serial1 become unresponsive. Someone could help?
Title: Re: Tty for serial port to Arduino from Linino
Post by: jessemonroy650 on Sep 03, 2015, 06:04 am
@lucabarbierato,

Please start a new thread. Subject: node.js serial port problems.
Please post your Arduino sketch.

FWIW: your issue is far afield from the original post. One of the goals of forums is to respond to an issue quickly so other may learn from it. Attaching to old threads does not allow this.

TIA
Jesse
Title: Re: Tty for serial port to Arduino from Linino
Post by: sonnyyu on Sep 03, 2015, 06:02 pm
Code: [Select]
root@Arduino:/dev# free
             total         used         free       shared      buffers
Mem:         61116        28496        32620            0         3672
-/+ buffers:              24824        36292
Swap:            0            0            0



Yun only has 32 MiB RAM left, and might not good candidate for node.