00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
00060
00061 #ifdef _USB_
00062 FT_STATUS ftStatus;
00063 FT_HANDLE ftHandle;
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
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
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
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 }