Main Page | Namespace List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

os_dep_usb.c

Go to the documentation of this file.
00001 /*  Implémentation Linux de la bibliotheque XADLL de Sidena
00002  *  Copyright (C) 2005 ENSTAR
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *  You can contact us:
00019  *    Association ENSTAR
00020  *    Ecole Nationale Superieure de Techniques Avancees
00021  *    32 boulevard Victor
00022  *    75739 Paris Cedex 15
00023  *    FRANCE
00024  *
00025  *    enstar@ensta.fr
00026  */
00027 
00028 /** @file    os_dep_usb.c
00029  *  @brief   Bibliothèque de fonctions de communication avec le port USB
00030  *  @author  Francois Fevotte <francois.fevotte@ensta.org>
00031  *  $Revision: 17 $
00032  *  $Date: 2006-02-14 21:13:32 +0100 (Tue, 14 Feb 2006) $
00033  *
00034  *  Ce code implémente les fonctions de communications spécifiques à la liaison USB (sous LINUX).
00035  *  Les fonctions de communications avec les périphériques USB-FTDI sont tirés de la bibliothèque
00036  *  'ftd2xx' fournie par le constructeur FTDI
00037  */
00038 
00039 /* $Id: os_dep_usb.c 17 2006-02-14 20:13:32Z francois $ */
00040 
00041 #define OS_DEP_USB_C
00042 
00043 #include <unistd.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <fcntl.h>
00047 #include <time.h>
00048 #include "os_dep_usb.h"
00049 #include "statique.h"
00050 
00051 #ifdef _USB_
00052 #include "ftd2xx.h"
00053 #endif
00054 
00055 #define SOURCE_ERREUR "os_dep_usb"
00056 #include "erreur.h"
00057 
00058 
00059 /* Variables globales */
00060 
00061 #ifdef _USB_
00062 FT_STATUS  ftStatus;        /** Le retour de la dernière fonction FTDI */
00063 FT_HANDLE  ftHandle;        /** Le pointeur vers le device */
00064 #endif
00065 
00066 int usb::open()
00067 {
00068 #ifdef _USB_
00069   int iNumDevs;
00070 
00071   char* pcBufLD[2];
00072   char cBufLD[64];
00073 
00074   pcBufLD[0] = cBufLD;
00075   pcBufLD[1] = NULL;
00076 
00077   erreur::debug( "Ouverture du périphérique" );
00078 
00079   /* Recherche de l'adresse de notre device */
00080   ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER);
00081   if(ftStatus != FT_OK)
00082     {
00083       erreur::fatale( "FT_ListDevices" );
00084       return 1;
00085     }
00086 
00087   /* Ouverture du device */
00088   if((ftStatus = FT_OpenEx(cBufLD, FT_OPEN_BY_SERIAL_NUMBER, &ftHandle)) != FT_OK)
00089     {
00090       erreur::fatale( "FT_Open" );
00091       return 1;
00092     }
00093   erreur::debug( "Périphérique ouvert" );
00094 
00095   
00096   /* Paramétrage du device */
00097   if( (ftStatus = FT_ResetDevice( ftHandle )) != FT_OK )
00098     {
00099       erreur::warning( "FT_ResetDevice" );
00100     }
00101   
00102   if( (ftStatus = FT_SetTimeouts(ftHandle, 1000, 100)) != FT_OK )
00103     {
00104       erreur::warning( "FT_SetTimeouts" );
00105     }
00106   erreur::debug( "Périphérique paramétré" );
00107 #endif
00108   return 0;
00109 }
00110 
00111 void usb::close()
00112 {
00113 #ifdef _USB_
00114   erreur::debug( "Fermeture du périphérique" );
00115   FT_Close(ftHandle);
00116 #endif
00117 }
00118 
00119 int usb::write(short length)
00120 {
00121 #ifdef _USB_
00122   DWORD dwBytesWritten;
00123   
00124   if((ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
00125     {
00126       erreur::warning( "FT_Purge" );
00127     }
00128 
00129   if((ftStatus = FT_Write(ftHandle, Out_buffer, length, &dwBytesWritten)) != FT_OK)
00130     {
00131       erreur::warning( "FT_Write" );
00132       return 0;
00133     }
00134 #endif
00135   return 1;
00136 }
00137 
00138 int usb::read(short length)
00139 {
00140 #ifdef _USB_
00141   DWORD lu=0;
00142 
00143   if( (ftStatus = FT_Read(ftHandle, Rec_buffer+XA_lastcar_in, length, &lu)) != FT_OK )
00144     {
00145       erreur::warning( "FT_Read" );
00146       return 0;
00147     }
00148 
00149   XA_lastcar_in += lu;
00150   return lu;
00151 #else
00152   return 0;
00153 #endif
00154 }

Project hosted on SourceForge.net
Documentation generated on Mon Apr 2 14:52:55 2007 for libXA by doxygen 1.4.4