From cf185b61689311d155020fe7f57087aa71a8f37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergi=20Hern=C3=A0ndez=20Juan?= <shernand@iri.upc.edu> Date: Thu, 21 Jun 2012 21:24:42 +0000 Subject: [PATCH] Changed the driver to use the CFTDI class instead of the CRS232 class --- trunk/src/examples/CMakeLists.txt | 4 +-- trunk/src/examples/test_usb_i2c.cpp | 6 ++-- trunk/src/usb_i2c.cpp | 46 ++++++++++++++++++++++------- trunk/src/usb_i2c.h | 7 +++-- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/trunk/src/examples/CMakeLists.txt b/trunk/src/examples/CMakeLists.txt index 48bc0bb..f41d1fd 100644 --- a/trunk/src/examples/CMakeLists.txt +++ b/trunk/src/examples/CMakeLists.txt @@ -5,7 +5,7 @@ ADD_EXECUTABLE(test_usb_i2c test_usb_i2c.cpp) TARGET_LINK_LIBRARIES(test_usb_i2c usb_i2c_adapter ${iriutils_LIBRARY} ${comm_LIBRARY}) # edit the following line to add the source code for the example and the name of the executable -ADD_EXECUTABLE(test_battery battery_test.cpp) +ADD_EXECUTABLE(test_camera test_camera.cpp) # edit the following line to add the necessary libraries -TARGET_LINK_LIBRARIES(test_battery usb_i2c_adapter ${iriutils_LIBRARY} ${comm_LIBRARY}) +TARGET_LINK_LIBRARIES(test_camera usb_i2c_adapter ${iriutils_LIBRARY} ${comm_LIBRARY}) diff --git a/trunk/src/examples/test_usb_i2c.cpp b/trunk/src/examples/test_usb_i2c.cpp index 534af53..f8cb7f1 100644 --- a/trunk/src/examples/test_usb_i2c.cpp +++ b/trunk/src/examples/test_usb_i2c.cpp @@ -2,8 +2,8 @@ #include "usb_i2c.h" #include <iostream> -const std::string usb_port="/dev/ttyUSB0"; -const unsigned char dev_id=0x22; +const std::string serial="A700ettt"; +const unsigned char dev_id=0x21; int main(int argc, char *argv[]) { @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) int i=0; try{ - adapter.open(usb_port); + adapter.open(serial); std::cout << "Firmware revision: " << (int)adapter.get_revision() << std::endl; std::cout << "Turning the led off ..." << std::endl; adapter.turn_led_off(); diff --git a/trunk/src/usb_i2c.cpp b/trunk/src/usb_i2c.cpp index 8deee1d..0340dea 100644 --- a/trunk/src/usb_i2c.cpp +++ b/trunk/src/usb_i2c.cpp @@ -16,7 +16,7 @@ CUSBI2C::CUSBI2C(const std::string &adapter_id) else { this->adapter_id=adapter_id; - this->comm_dev=new CRS232(comm_dev_id); + this->comm_dev=NULL; this->gpio_config[gpio1]=digital_input; this->gpio_config[gpio2]=i2c; this->gpio_config[gpio3]=i2c; @@ -25,18 +25,42 @@ CUSBI2C::CUSBI2C(const std::string &adapter_id) } } -void CUSBI2C::open(const std::string &port_id) +void CUSBI2C::open(const std::string &serial) { - TRS232_config config; + CFTDIServer *ftdi_server=CFTDIServer::instance(); + TFTDIconfig ftdi_config; + int i=0; if(this->comm_dev!=NULL) - this->comm_dev->open((void *)&port_id); - config.baud=19200; - config.num_bits=8; - config.parity=none; - config.stop_bits=2; - this->comm_dev->config(&config); - this->rx_event_id=this->comm_dev->get_rx_event_id(); + { + this->comm_dev->close(); + delete this->comm_dev; + this->comm_dev=NULL; + } + if(ftdi_server->get_num_devices()>0) + { + for(i=0;i<ftdi_server->get_num_devices();i++) + { + if(serial==ftdi_server->get_serial_number(i)) + { + this->comm_dev=ftdi_server->get_device(ftdi_server->get_serial_number(i)); + ftdi_config.baud_rate = 19200; + ftdi_config.word_length = 8; + ftdi_config.stop_bits = 2; + ftdi_config.parity = 0; + ftdi_config.read_timeout = 2; + ftdi_config.write_timeout = 2; + ftdi_config.latency_timer = 2; + this->comm_dev->config(&ftdi_config); + this->rx_event_id=this->comm_dev->get_rx_event_id(); + } + } + if(this->comm_dev==NULL)/* the device has not been initialized */ + { + /* handle exceptions */ + throw CUSBI2CException(_HERE_,"No FTDI device available with the desired serial number."); + } + } } unsigned char CUSBI2C::get_revision(void) @@ -385,7 +409,7 @@ int CUSBI2C::write_reg(unsigned char dev_id, short int address, unsigned char *d cmd[1]=dev_id<<1; cmd[2]=address; cmd[3]=num; - memcpy(&data[total_written],&cmd[4],num); + memcpy(&cmd[4],&data[total_written],num); num+=4; } else diff --git a/trunk/src/usb_i2c.h b/trunk/src/usb_i2c.h index 74045a6..c59d4f1 100644 --- a/trunk/src/usb_i2c.h +++ b/trunk/src/usb_i2c.h @@ -1,7 +1,8 @@ #ifndef _USB_I2C_H #define _USB_I2C_H -#include "rs232.h" +#include "ftdimodule.h" +#include "ftdiserver.h" #include "eventserver.h" typedef enum {gpio1=0,gpio2=1,gpio3=2} gpio_pins; @@ -11,14 +12,14 @@ typedef enum {digital_input=0x01,digital_output=0x02,analog_input=0x04,i2c=0x08} class CUSBI2C { private: - CRS232 *comm_dev; + CFTDI *comm_dev; std::string adapter_id; std::string rx_event_id; unsigned char gpio_config[3]; CEventServer *event_server; public: CUSBI2C(const std::string &adapter_id); - void open(const std::string &port_id); + void open(const std::string &serial); unsigned char get_revision(void); void config_gpio(gpio_pins pin, gpio_types type); void set_gpio(gpio_pins pin, bool value); -- GitLab