Main MRPT website > C++ reference
MRPT logo
CInterfaceFTDI.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 
00029 #ifndef CInterfaceFTDI_H
00030 #define CInterfaceFTDI_H
00031 
00032 #include <mrpt/config.h>
00033 #include <mrpt/utils/CStream.h>
00034 #include <mrpt/utils/stl_extensions.h>
00035 
00036 #include <mrpt/hwdrivers/link_pragmas.h>
00037 
00038 namespace mrpt
00039 {
00040         namespace hwdrivers
00041         {
00042 
00043                 /** A list of FTDI devices and their descriptors.
00044                   * \sa CInterfaceFTDI::ListAllDevices
00045                   * \ingroup mrpt_hwdrivers_grp
00046                   */
00047                 struct HWDRIVERS_IMPEXP TFTDIDevice
00048                 {
00049                         std::string     ftdi_manufacturer;
00050                         std::string     ftdi_description;
00051                         std::string     ftdi_serial;
00052 
00053                         uint16_t                usb_idVendor;
00054                         uint16_t                usb_idProduct;
00055                         uint8_t                 usb_serialNumber;
00056 
00057         #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE)
00058                         /** Only for Linux: the corresponding libusb's  "usb_device*" */
00059                         void                    *usb_device_struct;
00060         #endif
00061                 };
00062 
00063                 /** Print out all the information of a FTDI device in textual form. */
00064                 std::ostream HWDRIVERS_IMPEXP  &operator << ( std::ostream &o, const TFTDIDevice &d);
00065 
00066                 /** Used in  CInterfaceFTDI::ListAllDevices */
00067                 typedef  std::deque<TFTDIDevice> TFTDIDeviceList;
00068 
00069                 /** A definition of a CStream actually representing a USB connection to a FTDI chip.
00070                  *
00071                  *  This class implements the communication with FT245BM / FT245RL chips.
00072                  *   Using this class makes a program to depend on:
00073                  *              - Windows: "FT2XX.DLL" and the device drivers (see FTDI website).
00074                  *              - Linux: "libusb.so" (quite standard!), and "libftdi.so" only if linking against the dynamic library.
00075                  *
00076                  *  If there is any error during the communications (or loading the Windows DLL), a std::exception will be raised.
00077                  *
00078                  *  To write bulk data, use CStream::ReadBuffer and CStream::WriteBuffer. See also the derived classes for
00079                  *   higher level communication: CInterfaceFTDIMessages
00080                  *
00081                  * Warning: Avoid defining an object of this class in a global scope if you want to catch all potential
00082                  *      exceptions during the constructors (like DLL not found, etc...)
00083                  *
00084                  * VERSIONS:
00085                  *              - 11/APR/2005: Initial development. JLBC
00086                  *              - 16/FEB/2007: Integration into the MRPT framework. Support for device serial numbers. JLBC
00087                  *              - 15/APR/2008: Implemented for Linux using libftdi. JLBC
00088                  *
00089                  * \sa CInterfaceFTDIMessages, CStream
00090                   * \ingroup mrpt_hwdrivers_grp
00091                  */
00092                 class HWDRIVERS_IMPEXP CInterfaceFTDI : public utils::CStream
00093                 {
00094                 public:
00095                         /** Constructor, which loads driver interface (the DLL under Windows).
00096                           */
00097                         CInterfaceFTDI();
00098 
00099                         /** Destructor, which closes the connection with the chip and unloads the driver interface.
00100                           */
00101                         virtual ~CInterfaceFTDI();
00102 
00103                         /** This object cannot be copied */
00104                         CInterfaceFTDI(const CInterfaceFTDI &o);
00105 
00106                         /** This object cannot be copied */
00107                         CInterfaceFTDI& operator =(const CInterfaceFTDI &o);
00108 
00109                         /** Checks whether the chip has been successfully open.
00110                           * \sa OpenBySerialNumber, OpenByDescription
00111                           */
00112                         bool  isOpen();
00113 
00114                         /** Open by device serial number
00115                           */
00116                         void  OpenBySerialNumber( const std::string &serialNumber );
00117 
00118                         /** Open by device description
00119                           */
00120                         void  OpenByDescription( const std::string &description );
00121 
00122                         /** Close the USB device */
00123                         void  Close();
00124 
00125                         /** Reset the USB device */
00126                         void  ResetDevice();
00127 
00128                         /** Purge the I/O buffers */
00129                         void  Purge();
00130 
00131                         /** Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms, data is not sent to the PC waiting for possible more data, to save USB trafic. */
00132                         void  SetLatencyTimer (unsigned char latency_ms);
00133 
00134                         /** Change read & write timeouts, in milliseconds. */
00135                         void  SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms);
00136 
00137 
00138                         /** Generates a list with all FTDI devices connected right now.
00139                           */
00140                         void ListAllDevices( TFTDIDeviceList &outList );
00141 
00142                         /** Tries to read, raising no exception if not all the bytes are available, but raising one if there is some communication error.
00143                          */
00144                         size_t  ReadSync(void *Buffer, size_t Count)
00145                         {
00146                                 return Read(Buffer,Count);
00147                         }
00148 
00149                         /** Tries to write, raising no exception if not all the bytes are available, but raising one if there is some communication error.
00150                          */
00151                         size_t  WriteSync(const void *Buffer, size_t Count)
00152                         {
00153                                 return Write(Buffer,Count);
00154                         }
00155 
00156                         /** Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream).
00157                          *  In this class this method actually behaves as expected and does not fallback to ReadBuffer().
00158                          *      \exception std::exception On any error, or if ZERO bytes are read.
00159                          */
00160                         virtual size_t  ReadBufferImmediate(void *Buffer, size_t Count);
00161 
00162                 protected:
00163                         /** Introduces a pure virtual method responsible for reading from the stream.
00164                           *  It integrates a cache buffer to speed-up sequences of many, small readings.
00165                           */
00166                         size_t  Read(void *Buffer, size_t Count);
00167 
00168                         mrpt::utils::circular_buffer<uint8_t>    m_readBuffer;  //!< Used in Read
00169 
00170                         /** Introduces a pure virtual method responsible for writing to the stream.
00171                          *  Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
00172                          */
00173                         size_t  Write(const void *Buffer, size_t Count);
00174 
00175                         /** This virtual method does nothing in this class.
00176                          */
00177                         uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning);
00178 
00179                         /** This virtual method does nothing in this class.
00180                          */
00181                         uint64_t getTotalBytesCount();
00182 
00183                         /** This virtual method does nothing in this class.
00184                          */
00185                         uint64_t getPosition();
00186 
00187 
00188                         void  ftdi_read(void  *lpvBuffer, unsigned long dwBuffSize, unsigned long  *lpdwBytesRead);
00189                         void  ftdi_write(const void  *lpvBuffer, unsigned long dwBuffSize, unsigned long  *lpdwBytes);
00190 
00191         #if defined(MRPT_OS_WINDOWS)
00192                 private:
00193                         void  checkErrorAndRaise(int errorCode);
00194 
00195                         void  ftdi_open(void* pvDevice);
00196                         void  ftdi_openEx(void* pArg1, unsigned long dwFlags);
00197                         void  ftdi_listDevices(void *pArg1, void *pArg2, unsigned long dwFlags);
00198                         void  ftdi_getQueueStatus(unsigned long  *lpdwAmountInRxQueue);
00199 
00200                         void                            *m_hmodule;
00201                         unsigned long           m_ftHandle;
00202 
00203                         void            loadDriver();
00204 
00205                         enum FT_STATUS
00206                         {
00207                                 dummy
00208                         };
00209 
00210                         typedef FT_STATUS (__stdcall *PtrToOpen)(void*, unsigned long  *);
00211                         PtrToOpen m_pOpen;
00212 
00213                         typedef FT_STATUS (__stdcall *PtrToOpenEx)(void*, unsigned long, unsigned long  *);
00214                         PtrToOpenEx m_pOpenEx;
00215 
00216                         typedef FT_STATUS (__stdcall *PtrToListDevices)(void*, void*, unsigned long);
00217                         PtrToListDevices m_pListDevices;
00218 
00219                         typedef FT_STATUS (__stdcall *PtrToClose)(unsigned long );
00220                         PtrToClose m_pClose;
00221 
00222                         typedef FT_STATUS (__stdcall *PtrToRead)(unsigned long , void  *, unsigned long, unsigned long  *);
00223                         PtrToRead m_pRead;
00224 
00225                         typedef FT_STATUS (__stdcall *PtrToWrite)(unsigned long , const void  *, unsigned long, unsigned long  *);
00226                         PtrToWrite m_pWrite;
00227 
00228                         typedef FT_STATUS (__stdcall *PtrToResetDevice)(unsigned long );
00229                         PtrToResetDevice m_pResetDevice;
00230 
00231                         typedef FT_STATUS (__stdcall *PtrToPurge)(unsigned long , unsigned long);
00232                         PtrToPurge m_pPurge;
00233 
00234                         typedef FT_STATUS (__stdcall *PtrToSetTimeouts)(unsigned long , unsigned long, unsigned long);
00235                         PtrToSetTimeouts m_pSetTimeouts;
00236 
00237                         typedef FT_STATUS (__stdcall *PtrToGetQueueStatus)(unsigned long , unsigned long  *);
00238                         PtrToGetQueueStatus m_pGetQueueStatus;
00239 
00240                         typedef FT_STATUS (__stdcall *PtrToSetLatencyTimer )(unsigned long , unsigned char);
00241                         PtrToSetLatencyTimer m_pSetLatencyTimer;
00242 
00243         #else
00244                 // Declarations for Linux:
00245                         void            *m_ftdi_context;
00246 
00247                         /** Process recursively a USB device and its children: */
00248                         void recursive_fill_list_devices( void *usb_device_structure , TFTDIDeviceList &outList );
00249 
00250 
00251         #endif
00252 
00253 
00254                 }; // end of class
00255 
00256         } // end of namespace
00257 } // end of namespace
00258 
00259 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011