From dfbb403fcd4d3d6ceec22d2e70091686c53606f3 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Thu, 7 Jun 2012 19:54:38 +0200 Subject: initial code import --- hw/device.vala | 17 ++++++++++ hw/i2c-device.vala | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/serial-device.vala | 20 ++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 hw/device.vala create mode 100644 hw/i2c-device.vala create mode 100644 hw/serial-device.vala (limited to 'hw') diff --git a/hw/device.vala b/hw/device.vala new file mode 100644 index 0000000..adee58a --- /dev/null +++ b/hw/device.vala @@ -0,0 +1,17 @@ +/* Copyright 2012, Sebastian Reichel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +public class Device { +} diff --git a/hw/i2c-device.vala b/hw/i2c-device.vala new file mode 100644 index 0000000..21193f2 --- /dev/null +++ b/hw/i2c-device.vala @@ -0,0 +1,90 @@ +/* Copyright 2012, Sebastian Reichel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +public errordomain I2CError { + IO_ERROR, + ERRNO, +} + +public class I2CDevice : Device { + private int fd; + + public I2CDevice(uint8 dev, uint8 addr) throws I2CError { + string file = "/dev/i2c-%u".printf(dev); + fd = Posix.open(file, Posix.O_RDWR); + + if(fd < 0) + throw new I2CError.IO_ERROR("Could not open device!"); + + setup_address(addr); + } + + ~I2CDevice() { + Posix.close(fd); + } + + protected void setup_address(uint8 addr) throws I2CError { + if(Posix.ioctl(fd, Linux.I2C.SLAVE, addr) < 0) + throw new I2CError.IO_ERROR("Could not seek to I2C device 0x%02x!", addr); + } + + protected void write_byte(uint8 byte) throws I2CError { + if(Linux.I2C.SMBUS.write_byte(fd, byte) < 0) { + throw new I2CError.ERRNO("Could not write to I2C device: %s!", Posix.strerror(Posix.errno)); + } + } + + protected uint8 read_byte() throws I2CError { + int rc = Linux.I2C.SMBUS.read_byte(fd); + if(rc < 0) + throw new I2CError.ERRNO("Could not read from I2C device: %s!", Posix.strerror(Posix.errno)); + return (uint8) rc; + } + + protected uint8 get_byte(uint8 addr) throws I2CError { + int rc = Linux.I2C.SMBUS.read_word_data(fd, addr); + if(rc < 0) + throw new I2CError.ERRNO("Could not get byte %d from I2C device: %s!", addr, Posix.strerror(Posix.errno)); + return (uint8) rc; + } + + protected void set_byte(uint8 addr, uint8 val) throws I2CError { + int rc = Linux.I2C.SMBUS.write_word_data(fd, addr, val); + if(rc < 0) + throw new I2CError.ERRNO("Could not set byte %d to %d from I2C device: %s!", addr, val, Posix.strerror(Posix.errno)); + } + + protected uint16 get_big_word(uint8 addr) throws I2CError { + int rc = Linux.I2C.SMBUS.read_word_data(fd, addr); + if(rc < 0) + throw new I2CError.ERRNO("Could get word %d from I2C device: %s!", addr, Posix.strerror(Posix.errno)); + return uint16.from_big_endian((uint16) rc); + } + + protected uint16 get_little_word(uint8 addr) throws I2CError { + int rc = Linux.I2C.SMBUS.read_word_data(fd, addr); + if(rc < 0) + throw new I2CError.ERRNO("Could get word %d from I2C device: %s!", addr, Posix.strerror(Posix.errno)); + return uint16.from_little_endian((uint16) rc); + } + + protected uint8[] get_block(uint8 addr, uint8 size) throws I2CError { + uint8[] data = new uint8[size]; + int rc = Linux.I2C.SMBUS.read_i2c_block_data(fd, addr, data); + if(rc < 0) + throw new I2CError.ERRNO("Could get word %d from I2C device: %s!", addr, Posix.strerror(Posix.errno)); + return data; + } +} diff --git a/hw/serial-device.vala b/hw/serial-device.vala new file mode 100644 index 0000000..bbd7698 --- /dev/null +++ b/hw/serial-device.vala @@ -0,0 +1,20 @@ +/* Copyright 2012, Sebastian Reichel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +public class SerialDevice : Device { + + /* TODO */ + +} -- cgit v1.2.3