OSDN Git Service

FTDI_linux.h: standard libftdi
[stock/stock.osdn.git] / FTDI / FTDI_linux.h
1 // FTDI class, header file
2
3 #ifndef _FTDI_H
4 #define _FTDI_H
5
6 #include <assert.h>
7 #include "int_types.h"
8 #include "ftdi.h"
9
10 enum {
11   FT_OK = 0,
12   FT_PURGE_RX = 1,
13   FT_BITMODE_CBUS_BITBANG = BITMODE_CBUS,
14 };
15
16 class FTDI
17 {
18 //private:
19 public:
20   struct ftdi_context ftdic;
21   int status;
22
23 public:
24   FTDI() : status(0) {
25      assert(ftdi_init(&ftdic) >= 0);
26   }
27   ~FTDI() {
28      ftdi_deinit(&ftdic);
29   }
30   operator bool() const {
31      return (status >= 0);
32   }
33   int open(int index, unsigned product = 0x6001) {
34 //     status = ftdi_usb_open(&ftdic, 0x0403, product);
35      status = ftdi_usb_open_desc_index(&ftdic, 0x0403, product, NULL, NULL, index);
36      return status;
37   }
38   int open(int index, int vendor, int product) {
39      status = ftdi_usb_open_desc_index(&ftdic, vendor, product, NULL, NULL, index);
40      return status;
41   }
42   int open_by_serial(const char * serial) {
43      status = ftdi_usb_open_desc(&ftdic, 0x0403, 0x6001, NULL, serial);
44      return status;
45   }
46   bool close() {
47      status = ftdi_usb_close(&ftdic);
48      return status >= 0;
49   }
50   bool reset_device() {
51      return ftdi_usb_reset(&ftdic) >= 0;
52   }
53   bool set_flow_control_off() {
54      return ftdi_setflowctrl(&ftdic, SIO_DISABLE_FLOW_CTRL) == 0;
55   }
56   bool set_serial(int data_bits, char parity, int stop_bits) {
57      enum ftdi_bits_type e_data;
58      switch (data_bits) {
59         case 7:  e_data = BITS_7; break;
60         case 8:  e_data = BITS_8; break;
61         default: return false;
62      }
63      enum ftdi_stopbits_type e_stop;
64      switch (stop_bits) {
65         case 1:  e_stop = STOP_BIT_1;  break;
66         case 15: e_stop = STOP_BIT_15; break; // 1.5 stop bits
67         case 2:  e_stop = STOP_BIT_2;  break;
68         default: return false;
69      }
70      enum ftdi_parity_type e_parity;
71      switch (parity) {
72         case 'N': e_parity = NONE;  break;
73         case 'O': e_parity = ODD;   break;
74         case 'E': e_parity = EVEN;  break;
75         case 'M': e_parity = MARK;  break;
76         case 'S': e_parity = SPACE; break;
77         default: return false;
78      }
79      return ftdi_set_line_property2(&ftdic, e_data, e_stop, e_parity, BREAK_OFF) == 0;
80   }
81   bool set_baud_rate(unsigned baud_rate) {
82      return ftdi_set_baudrate(&ftdic, baud_rate) >= 0;
83   }
84   bool set_divisor(unsigned divisor) {
85      // FIXME
86      return ftdi_set_baudrate(&ftdic, 3000000/divisor) >= 0;
87   }
88   int set_bit_mode(uint8_t mask, uint8_t mode) {
89      return ftdi_set_bitmode(&ftdic, mask, mode);
90   }
91   inline int read(void * buffer, unsigned len) {
92      return ftdi_read_data(&ftdic, (uint8_t *)buffer, len);
93   }
94   inline int write(const void * buffer, unsigned len) {
95      return ftdi_write_data(&ftdic, (uint8_t *)buffer, len);
96   }
97   inline uint8_t get_bit_mode() {
98      uint8_t pins;
99      ftdi_read_pins(&ftdic, &pins);
100      return pins;
101   }
102   bool purge(unsigned mode) {
103      if (mode && FT_PURGE_RX)
104         return ftdi_usb_purge_rx_buffer(&ftdic) >= 0;
105      return 0;
106   }
107   void set_latency_timer(unsigned latency) {
108      ftdi_set_latency_timer(&ftdic, latency);
109   }
110   uint8_t get_latency_timer() {
111      uint8_t latency;
112      ftdi_get_latency_timer(&ftdic, &latency);
113      return latency;
114   }
115   void set_usb_parameters(unsigned rx_chunksize, unsigned tx_chunksize) {
116      ftdi_read_data_set_chunksize(&ftdic, rx_chunksize);
117      ftdi_write_data_set_chunksize(&ftdic, tx_chunksize);
118   }
119   void set_timeouts(unsigned rx, unsigned tx) {
120     ftdic.usb_read_timeout = rx;
121     ftdic.usb_write_timeout = tx;
122   }
123   unsigned get_modem_status() {
124      unsigned short status;
125      ftdi_poll_modem_status(&ftdic, &status);
126      return status;
127   }
128   bool set_dtr(int state) {
129      return (ftdi_setdtr(&ftdic, state) == 0);
130   }
131   bool set_rts(int state) {
132      return (ftdi_setrts(&ftdic, state) == 0);
133   }
134   int eeprom_read(int addr) {
135      unsigned short value;
136      if (ftdi_read_eeprom_location(&ftdic, addr, &value) == 0)
137         return value;
138      else
139         return -1;
140   }
141   bool eeprom_read_all(unsigned char * eeprom) {
142      return ftdi_read_eeprom(&ftdic, eeprom) == 0;
143   }
144   bool eeprom_write(int addr, unsigned short value) {
145      return ftdi_write_eeprom_location(&ftdic, addr, value) == 0;
146   }
147   bool eeprom_write_all(const unsigned char * eeprom) {
148      return ftdi_write_eeprom(&ftdic, (unsigned char *)eeprom) == 0;
149   }
150 #if 0
151   FT_STATUS list_devices(VOID * arg1, VOID * arg2, DWORD flags);
152   FT_STATUS open_ex();  //OpenEx(PVOID, DWORD);
153   FT_STATUS get_device_info(FT_DEVICE * type, DWORD * id, CHAR * SerialNumber, CHAR * Description);
154   FT_STATUS reset_port();       // FT_ResetPort()
155   FT_STATUS cycle_port();       // FT_CyclePort()
156   FT_STATUS purge(ULONG mode);  // FT_Purge()
157   FT_STATUS stop_rx();    // FT_StopInTask()
158   FT_STATUS restart_rx(); // FT_RestartInTask()
159   FT_STATUS set_timeouts(ULONG rx, ULONG tx); // the duration in ms
160   DWORD     get_queue_status();  // get rx queue status
161   FT_STATUS get_status(DWORD *RxBytes, DWORD *TxBytes, DWORD *Event);
162   DWORD     get_library_version();
163   DWORD     get_driver_version();
164   FT_STATUS set_event_notification(DWORD EventMask, PVOID hEvent);
165   FT_STATUS set_reset_pipe_retry_count(DWORD count); // FT_SetResetPipeRetryCount()
166   FT_STATUS set_data_characteristics(UCHAR uWordLength, UCHAR uStopBits, UCHAR uParity);
167   FT_STATUS set_flow_control(USHORT usFlowControl, UCHAR uXon, UCHAR uXoff);
168 #endif
169 public:
170 //  operator bool() const { return (status == FT_OK); }
171   const char * status_msg() {
172      return ftdi_get_error_string(&ftdic);
173   }
174 #if 0
175   bool is_open() const {
176       return (handle != NULL);
177   }
178 #endif
179 };
180
181 #endif // _FTDI_H