From 55481e99a90dbc122ff279d580f5219094f5c444 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Sat, 21 Jul 2012 09:26:26 +0200 Subject: add Serial setup code --- actuators/motor/atmostripe.vala | 2 +- hw/serial-device.vala | 99 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/actuators/motor/atmostripe.vala b/actuators/motor/atmostripe.vala index 23f0fda..4485c8c 100644 --- a/actuators/motor/atmostripe.vala +++ b/actuators/motor/atmostripe.vala @@ -24,7 +24,7 @@ public class AtmostripeMotorController : SerialDevice, MotorController { public void init(KeyFile cfg) throws Error { var device = cfg.get_string("AtmostripeMotorController", "device"); - setup(device); + setup(device, 9600, 8, 1); } public bool set_single(uint8 engine, uint8 speed) { diff --git a/hw/serial-device.vala b/hw/serial-device.vala index cff68b9..a428506 100644 --- a/hw/serial-device.vala +++ b/hw/serial-device.vala @@ -19,18 +19,111 @@ public errordomain SerialError { } public class SerialDevice : Device { + private Posix.termios newtio; + private Posix.termios restoretio; private int fd; - public void setup(string device) throws SerialError { + public void setup(string device, int rate, int bits, int stopbits) throws SerialError { + Posix.speed_t baudrate = Posix.B9600; fd = Posix.open(device, Posix.O_RDWR); if(fd < 0) throw new SerialError.IO_ERROR("Could not open device!"); - /* TODO */ + Posix.tcflush(fd, Posix.TCIOFLUSH); + Posix.tcgetattr(fd, out restoretio); + + /* apply settings */ + switch(rate) { + case 300: + baudrate = Posix.B300; + break; + case 600: + baudrate = Posix.B600; + break; + case 1200: + baudrate = Posix.B1200; + break; + case 2400: + baudrate = Posix.B2400; + break; + case 4800: + baudrate = Posix.B4800; + break; + case 9600: + baudrate = Posix.B9600; + break; + case 19200: + baudrate = Posix.B19200; + break; + case 38400: + baudrate = Posix.B38400; + break; + case 57600: + baudrate = Posix.B57600; + break; + case 115200: + baudrate = Posix.B115200; + break; + case 230400: + baudrate = Posix.B230400; + break; + default: + /* not supported */ + rate = 9600; + break; + } + + Posix.cfsetospeed(ref newtio, baudrate); + Posix.cfsetispeed(ref newtio, baudrate); + + switch(bits) { + case 5: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS5; + break; + case 6: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS6; + break; + case 7: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS7; + break; + case 8: + default: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS8; + break; + } + + newtio.c_cflag |= Posix.CLOCAL | Posix.CREAD; + newtio.c_cflag &= ~(Posix.PARENB | Posix.PARODD); + + /* TODO: parity */ + + newtio.c_cflag &= ~Linux.Termios.CRTSCTS; + + if(stopbits == 2) + newtio.c_cflag |= Posix.CSTOPB; + else + newtio.c_cflag &= ~Posix.CSTOPB; + + newtio.c_iflag = Posix.IGNBRK; + + newtio.c_lflag = 0; + newtio.c_oflag = 0; + + newtio.c_cc[Posix.VTIME]=1; + newtio.c_cc[Posix.VMIN]=1; + + newtio.c_lflag &= ~(Posix.ECHONL|Posix.NOFLSH); + + int mcs=0; + Posix.ioctl(fd, Linux.Termios.TIOCMGET, out mcs); + mcs |= Linux.Termios.TIOCM_RTS; + Posix.ioctl(fd, Linux.Termios.TIOCMSET, out mcs); + + Posix.tcsetattr(fd, Posix.TCSANOW, newtio); } - ~I2CDevice() { + ~SerialDevice() { Posix.close(fd); } -- cgit v1.2.3