User:JanosA

From ArmadeusWiki
Revision as of 21:23, 9 May 2013 by JanosA (Talk | contribs)

Jump to: navigation, search

About Me

I am IP Network and Datacenter engineer, currently I'm working for an Internet Service Provider in Hungary.

My hobby is programming embedded systems and creating my own small electronic boards.

Skills

  • Assembly programming: PIC 8bit family
  • C programming: PIC 8bit / PIC32MX family, Linux
  • Perl, PHP, Javascript, Html programming with MySQL
  • Linux system administration
  • RS-232/485 serial communications: Ascii, Modbus, Saia Sbus, etc.
  • IP networking

My ArmadeuS plans (APF28 based)

  • Creating an universal home automation hardware for my friends, with easy programming in perl or PHP. :)
  • Datacenter temperature monitoring, with RS-485 and OneWire communications.
  • Data logger for Saia Sbus based intelligent power meters.



RS485

Informations and mini HOWTOs for my APF28/iMX28 mxs-auart and busybox stty patches.


Linux stty

  • Setup APF28 serial port for RS485 communication:
# stty -F /dev/ttySP0 raw
# stty -F /dev/ttySP0 -echo -echoe -echok
# stty -F /dev/ttySP0 rs485
  • Set extra delays for TXEN/RTS signaling before and after the transmitted packet (in microseconds):
# stty -F /dev/ttySP0 rs485delaybefore 100
# stty -F /dev/ttySP0 rs485delayafter 100
  • To get current RS485 parameters. (If the port device supports the RS485 options you will see the last two lines)
# stty -F /dev/ttySP0
speed 9600 baud; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-brkint -imaxbel
rs485 -rs485rtsonsend -rs485rtsaftersend -rs485rxduringtx
rs485delaybefore = 100; rs485delayafter = 100; rs485delaylastchartx = 1250;

Please note:

  • You can't set the "rs485delaylastchartx" parameter, it's calculated automatically from the baudrate.

Special notes for the APF28/iMX28 mxs-auart driver:

  • All delays are in microseconds.
  • The "rs485delaylastchartx" parameter is not used in DMA mode.
  • The parameters: "rs485rtsonsend" "rs485rtsaftersend" "rs485rxduringtx" are not used. (Only used by atmel_serial.c and crisv10.c drivers)
  • If the RS485 mode is turned on, it will override (turn off) the RTS/CTS hardware flow settings of the uart, don't care what is configured.


C code

  • The RS485 mode can be turned on with ioctl calls from C code, here is the example:
#include <stdio.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <linux/serial.h>
#include <asm-generic/ioctls.h> /* TIOCGRS485 + TIOCSRS485 ioctl definitions */

int main(void) {
         struct serial_rs485 rs485conf;

         int fd = open ("/dev/ttySP0", O_RDWR);
         if (fd < 0) {
                 printf("Error: Can't open: /dev/ttySP0\n");
         }


         /* Don't forget to read first the current state of the RS485 options with ioctl.
            If You don't do this, You will destroy the rs485conf.delay_rts_last_char_tx
            parameter which is automatically calculated by the driver when You opens the
            port device. */
         if (ioctl (fd, TIOCGRS485, &rs485conf) < 0) {
                 printf("Error: TIOCGRS485 ioctl not supported.\n");
         }

         /* Enable RS485 mode: */
         rs485conf.flags |= SER_RS485_ENABLED;

         /* Set rts/txen delay before send, if needed: (in microseconds) */
         rs485conf.delay_rts_before_send = 0;

         /* Set rts/txen delay after send, if needed: (in microseconds) */
         rs485conf.delay_rts_after_send = 0;

         if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
                 printf("Error: TIOCSRS485 ioctl not supported.\n");
         }

         fcntl(fd, F_SETFL, 0);
         int n = write(fd, "ABC\r\n", 5);
         if (n < 0) {
                 /* Error handling */
         }

         if (close (fd) < 0) {
                 printf("Error: Can't close: /dev/ttySP0\n");
         }
}


Hardware Notes

  • The RS485 Transmit Enable (TXEN) output will be the RTS pin of the uart.
  • The TXEN/RTS output is _active low_, it must be inverted to use it with the popular RS485 transceiver ICs.
  • When the u-boot runs and the board start to boot, the RTS pin works as input, please add a pullup resistor to this pin before the inverter, to prevent RS485 bus lockups.



OneWire

Informations and mini HOWTOs for my APF28DEV OneWire patch.


Software installation

  • Recompile your Linux kernel with OneWire GPIO bus master (and some slave device) support.
  • The driver works only in static compiled mode. (Load as module did not work.)
$ make linux-menuconfig
Device Drivers  --->
    <*> Dallas's 1-wire support  --->
        1-wire Bus Masters  --->
            <*> GPIO 1-wire busmaster
        1-wire Slaves  --->
            <*> Thermal family implementation
$ make
  • Reflash your Linux kernel


Hardware Notes

  • Connect the OneWire slave directly to the APF28_DEV board J9 connector:
 * Vcc 3.3V is pin 1 on J9 connector
 * DQ is pin 31 on J9 (LCD_D18 / GPIO 1_18)
 * GND is pin 39 on J9
  • Add an 1.2k pullup resistor between Vcc and DQ


How it works

The OneWire bus master driver every 10 seconds scan the bus for new slave devices.

Each detected OneWire slave device have a sub-directory with it's unique ID in "/sys/devices/w1 bus master".

The sub-directory format is <2 digit family ID>-<12 digit unique ID>.

# ls -la /sys/devices/w1\ bus\ master/
total 0
drwxr-xr-x    4 root     root             0 Jan  1 01:06 .
drwxr-xr-x    6 root     root             0 Jan  1 01:05 ..
drwxr-xr-x    3 root     root             0 Jan  1 01:06 28-000001e68904
lrwxrwxrwx    1 root     root             0 Jan  1 01:07 driver -> ../../bus/w1/drivers/w1_master_driver
drwxr-xr-x    2 root     root             0 Jan  1 01:07 power
lrwxrwxrwx    1 root     root             0 Jan  1 01:07 subsystem -> ../../bus/w1
-rw-r--r--    1 root     root          4096 Jan  1 01:07 uevent
-rw-rw-rw-    1 root     root          4096 Jan  1 01:07 w1_master_add
-r--r--r--    1 root     root          4096 Jan  1 01:07 w1_master_attempts
-r--r--r--    1 root     root          4096 Jan  1 01:07 w1_master_max_slave_count
-r--r--r--    1 root     root          4096 Jan  1 01:07 w1_master_name
-r--r--r--    1 root     root          4096 Jan  1 01:07 w1_master_pointer
-rw-rw-rw-    1 root     root          4096 Jan  1 01:07 w1_master_pullup
-rw-rw-rw-    1 root     root          4096 Jan  1 01:07 w1_master_remove
-rw-rw-rw-    1 root     root          4096 Jan  1 01:07 w1_master_search
-r--r--r--    1 root     root          4096 Jan  1 01:07 w1_master_slave_count
-r--r--r--    1 root     root          4096 Jan  1 01:07 w1_master_slaves
-r--r--r--    1 root     root          4096 Jan  1 01:07 w1_master_timeout

You can list the detected slaves from this files:

# cat /sys/devices/w1\ bus\ master/w1_master_slave_count
1
# cat /sys/devices/w1\ bus\ master/w1_master_slaves
28-000001e68904

Let's read the temperature: (The 28-xxx family is a DS18B20 temperature sensor in this example.)

# cat /sys/devices/w1\ bus\ master/28-000001e68904/w1_slave
dd 01 4b 46 7f ff 03 10 1e : crc=1e YES
dd 01 4b 46 7f ff 03 10 1e t=29812

The t=29812 means, the temperature is: 29.812°C