Friday, August 12, 2016

Accessing Intel Edison UART1 on Arduino Kit

Intel Edison came with two add on model, one is mini breakout board and the other is Arduino compatible kit. The benefit with Arduino kit are:
1.      Lots of existing Arduino Uno shield compatible can be use
2.      IO signal can be select between 3.3v and 5v. While in mini breakout board IO signal is 1.8v to use this you need level shifter and its quiet difficult for beginner who wants to learn Intel Edison.
*See Intel Edison Kit for Arduino Hardware Guide
If you look at the picture, Intel Edison have two serial port, UART1 connect IO socket (red box) and UART2 is use for debugging. With Yocto Linux coming from Intel Edison UART1 is recognize as /dev/ttyMFD1 and UART2 as /dev/ttyMFD2. It’s easy to use UART1 with Arduino IDE for Intel Edison you just use:
void setup() {
Serial1.begin(baudrate)
}
void loop() {
               Serial1.println(“Hello World of Edison!!!”);
}

What if we want to access it through linux directly without Arduino IDE?
I made this note, because I’m having trouble connecting my serial device when using Intel Edison for the first time. And for an example I use Netbeans java with JSSC (Java Simple Serial Connector) to access UART1.  
Now, look at the GPIO linux table in the Hardware Guide


In table 3 we will see the address of GPIO, Mode and Control. Here the scheme to configure UART1_RxD and TxD:
1.      Prepare all GPIO address before we use it
2.      TRI_STATE_ALL: set GPIO Linux 214 = LOW
3.      UART1_RxD: set Mode for address 130 = 1, Output enable = LOW and Pullup enable = IN
4.      UART1_TxD: set Mode for address 130 = 1, Output enable = HIGH and Pullup enable = OUT
5.      TRI_STATE_ALL: set GPIO Linux 214 = HIGH
Ok, now start your SSH remote tools (putty) and logon to your Intel Edison.

Now start type:



# Prepare all GPIO address, if you find error when you running it means this IO already use or created.
echo –n “214” > /sys/class/gpio/export
echo –n “130” > /sys/class/gpio/export
echo –n “248” > /sys/class/gpio/export
echo –n “216” > /sys/class/gpio/export
echo –n “131” > /sys/class/gpio/export
echo –n “249” > /sys/class/gpio/export
echo –n “217” > /sys/class/gpio/export
# set TRI_STATE_ALL to LOW
echo low > /sys/class/gpio/gpio214/direction
# set RxD
echo low > /sys/class/gpio/gpio248/direction
echo in > /sys/class/gpio/gpio216/direction
echo mode1 > /sys/kernel/debug/gpio_debug/gpio130/current_pinmux
echo in > /sys/class/gpio/gpio130/direction
# set TxD
echo high > /sys/class/gpio/gpio249/direction
echo out > /sys/class/gpio/gpio217/direction
echo mode1 > /sys/kernel/debug/gpio_debug/gpio131/current_pinmux
echo out > /sys/class/gpio/gpio131/direction
# set TRI_STATE_ALL to HIGH
echo high > /sys/class/gpio/gpio214/direction
Now, you’re ready to use UART1 or /dev/ttyMFD1 in linux without Arduino IDE. Before using it, you need to configure the baudrate of UART1 by typing “sty –F /dev/ttyMFD1 9600” it shows you the baudrate set to 9600.
Open another putty session with serial port. Make sure, you’ve connected your USB to Serial adapter to your computer and set the same baudrate (9600). You will have two putty session, one is connect remotely to your Edison board (I’m using WiFi) and the other connect to USB Serial adapter.
In Edison remote session, start testing by typing
echo “Hello World of Edison !!!” > /dev/ttyMFD1
To test receiving message from your computer to Edison, just type “cat /dev/ttyMFD1” on you remote edison session. Now, start type message in your computer COMx session.
Next, I’ll show you how can use it in you java application.
1. Start your Netbeans, If you don’t know how to connect Netbeans with your Edison you can see my Intel Edison with Netbeans tutorial.
2. Create new project “EdisonSerial”
3. Set your run remotely to your Intel Edison configuration in Netbeans
4. Add jSSC library
5. Here are the sample code
package edisonserial;

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

public class Edisonserial {
    static SerialPort oSerialPort;
    public static void main(String[] args) {
        oSerialPort = new SerialPort("/dev/ttyMFD1");
        openSerialPort();
        sendMessage("Hello World of Edison from Netbeans!!!");
        closeSerialPort();
    }
   
    private static void openSerialPort() {
        try {
            oSerialPort.openPort();          
            oSerialPort.setParams(
                    oSerialPort.BAUDRATE_9600,
                    oSerialPort.DATABITS_8,
                    oSerialPort.STOPBITS_1,
                    oSerialPort.PARITY_NONE);
        }
        catch (SerialPortException ex) {
            closeSerialPort();
            System.out.println(ex);
        }      
    }
   
    private static void closeSerialPort() {
        try {
            oSerialPort.closePort();
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
   
    private static void sendMessage(String msgValue) {
        try {
            oSerialPort.writeString(msgValue);
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
}
6. Run this application, you will see your message send to your COMx-Putty session.
Hope, my note can be useful.










No comments:

Post a Comment