Difference between revisions of "How to use RS485"

From IGEP - ISEE Wiki

Jump to: navigation, search
(Created page with '= Overview of How-To = This How-To is meant to be a starting point for people to learn to use the RS-485 port on IGEP0020 board. Below this How-To is a short .c file which de…')
 
m (IGEP BERLIN)
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Overview of How-To =
+
= Overview =
 +
<section begin=overview />
 +
This How-To is meant to be a starting point for people to learn to use RS-485 port on IGEP devices as quickly and easily as possible. We use [http://labs.isee.biz/index.php/IGEP_firmware_Yocto IGEP YOCTO Firmware] and RS485 example which describe how to setup and write data on the RS-485 port.
 +
<section end=overview />
  
This How-To is meant to be a starting point for people to learn to use the RS-485 port on IGEP0020 board.
+
= Requirements =
  
Below this How-To is a short .c file which describe how to setup and write data on the RS-485 port.
+
*[http://www.ubuntu.com/ Ubuntu] distribution or [http://labs.isee.biz/index.php/IGEP_SDK_Virtual_Machine IGEP SDK VM].
 +
*[http://www.isee.biz/component/zoo/item/igep-yocto-toolcahin-sdk IGEP YOCTO Toolchain SDK]
 +
*2 IGEP devices
  
== Feedback and Contributing  ==
+
= Add RS-485 support =
 +
== Flash memory==
 +
Boot IGEP with a [http://labs.isee.biz/index.php/How_to_create_a_SD-card_with_the_latest_software_image MicroSD card].
  
At any point, if you see a mistake you can contribute to this How-To.  
+
*Connect to [http://labs.isee.biz/index.php/IGEPv2#Log_into_IGEPv2_via_Ethernet_interface IGEP]
 +
<pre>mkdir /tmp/temp
 +
mount -t jffs2 /dev/mtdblock1 /tmp/temp </pre>
 +
Now, mtdblock1 partition is mounted at /tmp/temp directory. Modify igep.ini located in: /tmp/temp/igep.ini
  
= Add RS-485 support to Linux  =
+
*Add RS-485 support:
 +
<pre>board.ei485=yes</pre>
 +
Save changes, power down IGEP, unplug MicroSD card and power up your board.
  
=== Using kernel 2.6.33 series  ===
+
== Micro SD==
 +
Plug SD card in your computer, boot partition should be appear. Modify igep.ini located in: /media/boot/igep.ini
  
Follow http://www.igep.es/index.php?option=com_kunena&amp;Itemid=97&amp;func=view&amp;catid=13&amp;id=1195&amp;limit=6
+
*Add RS-485 support:
 +
<pre>board.ei485=yes</pre>
 +
Save changes, plug SD card in IGEP and power up your board.
  
=== Using kernel 2.6.28 series  ===
+
= IGEPv2 =
  
Tested on a 2.6.28.10-3 version kernel.  
+
RS-485 port is located at [http://labs.isee.biz/index.php/Connectors_Summary#J940_connector J940 connector], connect two IGEPv2 devices like this:
  
To use correctly the RS-485 interface chip we need to configure the serial port driver in the kernel to manage the RTS signal (Request To Send) to enable the TX section (DE pin) only when data are transmitted on TXD line (Transmit Data). This is a software only solution, requiring no special hardware, but timing control for delays is only accurate to the next nearest jiffy.
 
 
First of all, download the latest stable kernel version
 
 
$ wget http://downloads.igep.es/sources/kernel/linux-omap-2.6.28.10-3.tar.gz
 
$ tar xzf linux-omap-2.6.28.10-3.tar.gz
 
$ cd linux-omap-2.6.28.10-3
 
$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- igep0020_defconfig
 
 
Next, enable the RS-485 support.
 
 
$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- menuconfig
 
 
Configure the kernel to enable RS-485 support.
 
 
Device drivers ---&gt;
 
Character devices ---&gt;
 
Serial drivers  ---&gt;
 
*** serial port extensions ***
 
[*] Allow hand shake line half duplex direction signaling
 
 
Finally build kernel image with RS-485 support
 
 
$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- uImage
 
 
= Test RS-485 communication with two IGEP0020 boards  =
 
 
Connect two IGEP0020 boards using J940 connector like this:
 
 
<pre> J940                        J940
 
<pre> J940                        J940
 
  ---                          ---
 
  ---                          ---
| 1 |---------- GND ----------| 1 |
+
| 1 |---------- GND --------------| 1 |
| 2 |-X                     X-| 2 |
+
| 2 |-X                     X-| 2 |
| 3 |-X                     X-| 3 |
+
| 3 |-X                     X-| 3 |
| 4 |----------  A  ----------| 4 |
+
| 4 |----------  A  --------------| 4 |
| 5 |----------  B  ----------| 5 |
+
| 5 |----------  B  --------------| 5 |
  ---                          ---
+
  ---                          ---</pre>
</pre>  
 
Setup a NFS-TFTP environment (poky-image-sato) for each board using kernel image with RS-485 support, and power up
 
 
 
Build the example for your target boards,
 
 
 
$ cd linux-omap-2.6.28.10-3
 
$ arm-none-linux-gnueabi-gcc 485-example.c -o 485-example -Iinclude -Iarch/arm/include
 
 
 
and copy to target rootfs.
 
 
 
Now, open a ssh console to the first board and execute the microcom command
 
 
 
root@igep0020:~# microcom /dev/ttyS0
 
connected to '/dev/ttyS0' (9600 bps), exit with ctrl-X...
 
  
On second board, open a ssh and execute the 485 example
+
== Program ==
 +
[http://labs.isee.biz/index.php/How_to_setup_a_cross_compiler#Download.2FInstall_IGEP_SDK Download an Install] IGEP SDK if you don't have it.
  
root@igep0020:~# ./485-example
+
Create source code text file:
  
If all is ok, should appear "ABC" in microcom console.  
+
First of all you need to initialize a suitable environment in the bash shell console inside your machine. <br> You can do this sourcing once the environment-setup script.
 +
<pre>jdoe@ubuntu ~ $ source /opt/poky/1.2/environment-setup-armv7a-vfp-neon-poky-linux-gnueabi </pre>
 +
Create a single .c file (485-example.c), using your preferred editor (vi, nano, gedit, ...) <br> 485-example.c:
 +
<pre>/* 485-example.c : Basic example of RS485 half duplex transmission */
 +
#include <stdio.h>
 +
#include <string.h>
 +
#include <unistd.h>
 +
#include <fcntl.h>
 +
#include <errno.h>
 +
#include <termios.h>
  
root@igep0020:~# microcom /dev/ttyS0
+
#include <sys/ioctl.h>
connected to '/dev/ttyS0' (9600 bps), exit with ctrl-X...
+
#include <asm/ioctls.h>
ABC
 
  
= Appendix A: 485-example.c&nbsp;: Basic example of RS-485 half duplex transmission  =
+
#include <linux/serial.h>
<pre>#c
 
/* 485-example.c&nbsp;: Basic example of RS485 half duplex transmission */
 
#include &lt;stdio.h&gt;
 
#include &lt;string.h&gt;
 
#include &lt;unistd.h&gt;
 
#include &lt;fcntl.h&gt;
 
#include &lt;errno.h&gt;
 
#include &lt;termios.h&gt;
 
 
 
#include &lt;sys/ioctl.h&gt;
 
#include &lt;asm/ioctls.h&gt;
 
 
 
#include &lt;linux/rs485.h&gt;
 
  
 
int main(void)
 
int main(void)
 
{
 
{
char dev[] = "/dev/ttyS0";
+
  char dev[] = "/dev/ttyO0";
char tosend[] = {'A', 'B', 'C'};
+
  char tosend[] = {'A', 'B', 'C'};
struct serial_rs485_settings ctrl485;
+
  struct serial_rs485 ctrl485;
int status;
+
  int status;
int fd;
+
  int fd;
  
struct termios ti;
+
  struct termios ti;
speed_t speed;
+
  speed_t speed;
  
/* Open the port */
+
  /* Open the port */
fd = open(dev, O_RDWR);
+
  fd = open(dev, O_RDWR);
if (fd &lt; 0) {
+
  if (fd < 0) {
printf("%s: Unable to open.\n", dev);
+
      printf("%s: Unable to open.\n", dev);
return -1;
+
      return -1;
}
+
  }
  
/* Set the port in 485 mode */
+
  /* Set the port in 485 mode */
ctrl485.flags = SER_RS485_MODE | SER_RS485_MODE_RTS | SER_RS485_RTS_TX_LOW;
+
  ctrl485.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND;
ctrl485.delay_before_send = 0;
+
  ctrl485.delay_rts_before_send = 0;
ctrl485.delay_after_send = 0;
+
  ctrl485.delay_rts_after_send = 0;
  
status = ioctl(fd, TIOCSRS485, &amp;ctrl485);
+
  status = ioctl(fd, TIOCSRS485, &ctrl485);
if (status) {
+
  if (status) {
printf("%s: Unable to configure port in 485 mode, status (%i)\n", dev, status);
+
      printf("%s: Unable to configure port in 485 mode, status (%i)\n", dev, status);
return -1;
+
      return -1;
}
+
  }
  
/* Set the port speed */
+
  /* Set the port speed */
speed = B9600;
+
  speed = B9600;
tcgetattr(fd, &amp;ti);
+
  tcgetattr(fd, &ti);
cfsetospeed(&amp;ti, speed);
+
  cfsetospeed(&ti, speed);
cfsetispeed(&amp;ti, speed);
+
  cfsetispeed(&ti, speed);
tcsetattr(fd, TCSANOW, &amp;ti);
+
  tcsetattr(fd, TCSANOW, &ti);
  
/* Send content to RS485 */
+
  /* Send content to RS485 */
if (write(fd, tosend, sizeof(tosend))&nbsp;!= sizeof(tosend)) {
+
  if (write(fd, tosend, sizeof(tosend)) != sizeof(tosend)) {
printf("%s: write() failed\n", dev);
+
      printf("%s: write() failed\n", dev);
}
+
  }
  
return 0;
+
  return 0;
 
}
 
}
 +
</pre>
 +
*Build arm binary executable:
  
</pre>  
+
Cross toolchain tools are available into the built-in virtual machine Poky SDK. You only need open bash terminal prompt and write commands:
= References  =
+
<pre>jdoe@ubuntu ~/Desktop $ arm-poky-linux-gnueabi-gcc -o 485-example 485-example.c</pre>
  
*[http://markmail.org/message/hu4ydmvwa4g7xtec#query:Auto%20RS485%20direction%20control+page:1+mid:uyw4hqsipap5z4il+state:results PATCH/RFC Auto RS-485 direction control ]
+
== Test ==
 +
=== IGEP 1 ===
 +
*Get a remote shell and execute microcom program
 +
<pre>microcom -s 9600 -X /dev/ttyO0</pre>
  
*Discussions on RS-485 auto direction control seem to be split over a number of threads. Here are some threads:
+
=== IGEP 2 ===
**http://marc.info/?t=121805745200001&amp;r=1&amp;w=2
+
*Transfer arm executable file from virtual machine to IGEP Board
**http://marc.info/?t=121690034200007&amp;r=1&amp;w=2  
+
*Execute arm executable file inside IGEP Board (remote shell):<br>
**http://marc.info/?t=122821576400003&amp;r=1&amp;w=2
+
<pre>root@igep00x0:~# ./485-example</pre>
**http://marc.info/?t=122822457300004&amp;r=1&amp;w=2
 
**http://marc.info/?t=122954923400001&amp;r=1&amp;w=2
 
**http://marc.info/?t=122964426200001&amp;r=1&amp;w=2
 
**http://marc.info/?t=121566595900001&amp;r=1&amp;w=2
 
**http://marc.info/?t=121805198200004&amp;r=1&amp;w=2
 
  
=== ISEE References  ===
+
== Results ==
 +
If you follow steps correctly you can see the next result at IGEP1:
 +
<pre>root@igep00x0:~# microcom -s 9600 -X /dev/ttyO0
 +
ABC </pre>
 +
= IGEP BERLIN =
 +
'''under construction'''
  
Read the [http://igep.es/index.php?option=com_content&view=article&id=99&Itemid=129&dir=%2Fvar%2Fwww%2Fvhosts%2Figep.es%2Fhttpdocs%2Fdownloads%2F01-ISEE_Products%2FIGEPv2%2FHW_User_Manuals&download_file=%2Fvar%2Fwww%2Fvhosts%2Figep.es%2Fhttpdocs%2Fdownloads%2F01-ISEE_Products%2FIGEPv2%2FHW_User_Manuals%2FMAN-PR-IGEP.0020-001.23.HW_RC.pdf Official IGEP0020 Hardware Reference Manual] (chapter "5.3 CONNECTOR J940: POWER + RS-485" )<br>
 
  
[[Category:How_to_forge|RS485]]
+
[[Category:RS485]]

Latest revision as of 18:39, 10 September 2012

Overview

This How-To is meant to be a starting point for people to learn to use RS-485 port on IGEP devices as quickly and easily as possible. We use IGEP YOCTO Firmware and RS485 example which describe how to setup and write data on the RS-485 port.


Requirements

Add RS-485 support

Flash memory

Boot IGEP with a MicroSD card.

mkdir /tmp/temp
mount -t jffs2 /dev/mtdblock1 /tmp/temp 

Now, mtdblock1 partition is mounted at /tmp/temp directory. Modify igep.ini located in: /tmp/temp/igep.ini

  • Add RS-485 support:
board.ei485=yes

Save changes, power down IGEP, unplug MicroSD card and power up your board.

Micro SD

Plug SD card in your computer, boot partition should be appear. Modify igep.ini located in: /media/boot/igep.ini

  • Add RS-485 support:
board.ei485=yes

Save changes, plug SD card in IGEP and power up your board.

IGEPv2

RS-485 port is located at J940 connector, connect two IGEPv2 devices like this:

 J940                         J940
 ---                           ---
| 1 |---------- GND --------------| 1 |
| 2 |-X                      X-| 2 |
| 3 |-X                      X-| 3 |
| 4 |----------  A  --------------| 4 |
| 5 |----------  B  --------------| 5 |
 ---                           ---

Program

Download an Install IGEP SDK if you don't have it.

Create source code text file:

First of all you need to initialize a suitable environment in the bash shell console inside your machine.
You can do this sourcing once the environment-setup script.

jdoe@ubuntu ~ $ source /opt/poky/1.2/environment-setup-armv7a-vfp-neon-poky-linux-gnueabi 

Create a single .c file (485-example.c), using your preferred editor (vi, nano, gedit, ...)
485-example.c:

/* 485-example.c : Basic example of RS485 half duplex transmission */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

#include <sys/ioctl.h>
#include <asm/ioctls.h>

#include <linux/serial.h>

int main(void)
{
   char dev[] = "/dev/ttyO0";
   char tosend[] = {'A', 'B', 'C'};
   struct serial_rs485 ctrl485;
   int status;
   int fd;

   struct termios ti;
   speed_t speed;

   /* Open the port */
   fd = open(dev, O_RDWR);
   if (fd < 0) {
      printf("%s: Unable to open.\n", dev);
      return -1;
   }

   /* Set the port in 485 mode */
   ctrl485.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND;
   ctrl485.delay_rts_before_send = 0;
   ctrl485.delay_rts_after_send = 0;

   status = ioctl(fd, TIOCSRS485, &ctrl485);
   if (status) {
      printf("%s: Unable to configure port in 485 mode, status (%i)\n", dev, status);
      return -1;
   }

   /* Set the port speed */
   speed = B9600;
   tcgetattr(fd, &ti);
   cfsetospeed(&ti, speed);
   cfsetispeed(&ti, speed);
   tcsetattr(fd, TCSANOW, &ti);

   /* Send content to RS485 */
   if (write(fd, tosend, sizeof(tosend)) != sizeof(tosend)) {
      printf("%s: write() failed\n", dev);
   }

   return 0;
}
  • Build arm binary executable:

Cross toolchain tools are available into the built-in virtual machine Poky SDK. You only need open bash terminal prompt and write commands:

jdoe@ubuntu ~/Desktop $ arm-poky-linux-gnueabi-gcc -o 485-example 485-example.c

Test

IGEP 1

  • Get a remote shell and execute microcom program
microcom -s 9600 -X /dev/ttyO0

IGEP 2

  • Transfer arm executable file from virtual machine to IGEP Board
  • Execute arm executable file inside IGEP Board (remote shell):
root@igep00x0:~# ./485-example

Results

If you follow steps correctly you can see the next result at IGEP1:

root@igep00x0:~# microcom -s 9600 -X /dev/ttyO0
ABC 

IGEP BERLIN

under construction