summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2012-07-21 09:26:26 +0200
committerSebastian Reichel <sre@ring0.de>2012-07-21 09:26:26 +0200
commit55481e99a90dbc122ff279d580f5219094f5c444 (patch)
tree84e6b67811c0d432aaed94ca5d90140d92b5aa3f
parent1e123c3670209132fa82cf02e95b8425a0c070aa (diff)
downloadmicrocopterd-55481e99a90dbc122ff279d580f5219094f5c444.tar.bz2
add Serial setup code
-rw-r--r--actuators/motor/atmostripe.vala2
-rw-r--r--hw/serial-device.vala99
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);
}