Ticket #11563: ftdi_sio.h

File ftdi_sio.h, 16.7 KB (added by shepsoft@…, 6 years ago)

ftdi driver header

Line 
1/*
2 * Definitions for the FTDI USB Single Port Serial Converter -
3 * known as FTDI_SIO (Serial Input/Output application of the chipset)
4 *
5 * The example I have is known as the USC-1000 which is available from
6 * http://www.dse.co.nz - cat no XH4214 It looks similar to this:
7 * http://www.dansdata.com/usbser.htm but I can't be sure There are other
8 * USC-1000s which don't look like my device though so beware!
9 *
10 * The device is based on the FTDI FT8U100AX chip. It has a DB25 on one side,
11 * USB on the other.
12 *
13 * Thanx to FTDI (http://www.ftdi.co.uk) for so kindly providing details
14 * of the protocol required to talk to the device and ongoing assistence
15 * during development.
16 *
17 * Bill Ryder - bryder@sgi.com formerly of Silicon Graphics, Inc.- wrote the
18 * FTDI_SIO implementation.
19 *
20 * Philipp Gühring - pg@futureware.at - added the Device ID of the USB relais
21 * from Rudolf Gugler
22 *
23 */
24
25/* Commands */
26#define FTDI_SIO_RESET          0 /* Reset the port */
27#define FTDI_SIO_MODEM_CTRL     1 /* Set the modem control register */
28#define FTDI_SIO_SET_FLOW_CTRL  2 /* Set flow control register */
29#define FTDI_SIO_SET_BAUD_RATE  3 /* Set baud rate */
30#define FTDI_SIO_SET_DATA       4 /* Set the data characteristics of the port */
31#define FTDI_SIO_GET_MODEM_STATUS       5 /* Retrieve current value of modern status register */
32#define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */
33#define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */
34#define FTDI_SIO_SET_LATENCY_TIMER      9 /* Set the latency timer */
35#define FTDI_SIO_GET_LATENCY_TIMER      10 /* Get the latency timer */
36
37/* Interface indices for FT2232, FT2232H and FT4232H devices */
38#define INTERFACE_A             1
39#define INTERFACE_B             2
40#define INTERFACE_C             3
41#define INTERFACE_D             4
42
43/*
44 *   BmRequestType:  1100 0000b
45 *   bRequest:       FTDI_E2_READ
46 *   wValue:         0
47 *   wIndex:         Address of word to read
48 *   wLength:        2
49 *   Data:           Will return a word of data from E2Address
50 *
51 */
52
53/* FTDI_SIO_RESET */
54#define FTDI_SIO_RESET_REQUEST FTDI_SIO_RESET
55#define FTDI_SIO_RESET_REQUEST_TYPE 0x40
56#define FTDI_SIO_RESET_SIO 0
57#define FTDI_SIO_RESET_PURGE_RX 1
58#define FTDI_SIO_RESET_PURGE_TX 2
59
60/*
61 * BmRequestType:  0100 0000B
62 * bRequest:       FTDI_SIO_RESET
63 * wValue:         Control Value
64 *                   0 = Reset SIO
65 *                   1 = Purge RX buffer
66 *                   2 = Purge TX buffer
67 * wIndex:         Port
68 * wLength:        0
69 * Data:           None
70 *
71 * The Reset SIO command has this effect:
72 *
73 *    Sets flow control set to 'none'
74 *    Event char = $0D
75 *    Event trigger = disabled
76 *    Purge RX buffer
77 *    Purge TX buffer
78 *    Clear DTR
79 *    Clear RTS
80 *    baud and data format not reset
81 *
82 * The Purge RX and TX buffer commands affect nothing except the buffers
83 *
84   */
85
86/* FTDI_SIO_SET_BAUDRATE */
87#define FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE 0x40
88#define FTDI_SIO_SET_BAUDRATE_REQUEST 3
89
90/*
91 * BmRequestType:  0100 0000B
92 * bRequest:       FTDI_SIO_SET_BAUDRATE
93 * wValue:         BaudDivisor value - see below
94 * wIndex:         Port
95 * wLength:        0
96 * Data:           None
97 * The BaudDivisor values are calculated as follows:
98 * - BaseClock is either 12000000 or 48000000 depending on the device. FIXME: I wish
99 *   I knew how to detect old chips to select proper base clock!
100 * - BaudDivisor is a fixed point number encoded in a funny way.
101 *   (--WRONG WAY OF THINKING--)
102 *   BaudDivisor is a fixed point number encoded with following bit weighs:
103 *   (-2)(-1)(13..0). It is a radical with a denominator of 4, so values
104 *   end with 0.0 (00...), 0.25 (10...), 0.5 (01...), and 0.75 (11...).
105 *   (--THE REALITY--)
106 *   The both-bits-set has quite different meaning from 0.75 - the chip designers
107 *   have decided it to mean 0.125 instead of 0.75.
108 *   This info looked up in FTDI application note "FT8U232 DEVICES \ Data Rates
109 *   and Flow Control Consideration for USB to RS232".
110 * - BaudDivisor = (BaseClock / 16) / BaudRate, where the (=) operation should
111 *   automagically re-encode the resulting value to take fractions into consideration.
112 * As all values are integers, some bit twiddling is in order:
113 *   BaudDivisor = (BaseClock / 16 / BaudRate) |
114 *   (((BaseClock / 2 / BaudRate) & 4) ? 0x4000    // 0.5
115 *    : ((BaseClock / 2 / BaudRate) & 2) ? 0x8000  // 0.25
116 *    : ((BaseClock / 2 / BaudRate) & 1) ? 0xc000  // 0.125
117 *    : 0)
118 *
119 * For the FT232BM, a 17th divisor bit was introduced to encode the multiples
120 * of 0.125 missing from the FT8U232AM.  Bits 16 to 14 are coded as follows
121 * (the first four codes are the same as for the FT8U232AM, where bit 16 is
122 * always 0):
123 *   000 - add .000 to divisor
124 *   001 - add .500 to divisor
125 *   010 - add .250 to divisor
126 *   011 - add .125 to divisor
127 *   100 - add .375 to divisor
128 *   101 - add .625 to divisor
129 *   110 - add .750 to divisor
130 *   111 - add .875 to divisor
131 * Bits 15 to 0 of the 17-bit divisor are placed in the urb value.  Bit 16 is
132 * placed in bit 0 of the urb index.
133 *
134 * Note that there are a couple of special cases to support the highest baud
135 * rates.  If the calculated divisor value is 1, this needs to be replaced with
136 * 0.  Additionally for the FT232BM, if the calculated divisor value is 0x4001
137 * (1.5), this needs to be replaced with 0x0001 (1) (but this divisor value is
138 * not supported by the FT8U232AM).
139 */
140
141typedef enum {
142        SIO = 1,
143        FT8U232AM = 2,
144        FT232BM = 3,
145        FT2232C = 4,
146        FT232RL = 5,
147        FT2232H = 6,
148        FT4232H = 7,
149        FT232H  = 8,
150        FTX     = 9,
151} ftdi_chip_type_t;
152
153typedef enum {
154 ftdi_sio_b300 = 0, 
155 ftdi_sio_b600 = 1, 
156 ftdi_sio_b1200 = 2,
157 ftdi_sio_b2400 = 3,
158 ftdi_sio_b4800 = 4,
159 ftdi_sio_b9600 = 5,
160 ftdi_sio_b19200 = 6,
161 ftdi_sio_b38400 = 7,
162 ftdi_sio_b57600 = 8,
163 ftdi_sio_b115200 = 9
164} FTDI_SIO_baudrate_t ;
165
166/*
167 * The ftdi_8U232AM_xxMHz_byyy constants have been removed. The encoded divisor values
168 * are calculated internally.
169 */
170
171#define FTDI_SIO_SET_DATA_REQUEST FTDI_SIO_SET_DATA
172#define FTDI_SIO_SET_DATA_REQUEST_TYPE 0x40
173#define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8 )
174#define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8 )
175#define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8 )
176#define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8 )
177#define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8 )
178#define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11 )
179#define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11 )
180#define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11 )
181#define FTDI_SIO_SET_BREAK (0x1 << 14)
182/* FTDI_SIO_SET_DATA */
183
184/*
185 * BmRequestType:  0100 0000B
186 * bRequest:       FTDI_SIO_SET_DATA
187 * wValue:         Data characteristics (see below)
188 * wIndex:         Port
189 * wLength:        0
190 * Data:           No
191 *
192 * Data characteristics
193 *
194 *   B0..7   Number of data bits
195 *   B8..10  Parity
196 *           0 = None
197 *           1 = Odd
198 *           2 = Even
199 *           3 = Mark
200 *           4 = Space
201 *   B11..13 Stop Bits
202 *           0 = 1
203 *           1 = 1.5
204 *           2 = 2
205 *   B14
206 *           1 = TX ON (break)
207 *           0 = TX OFF (normal state)
208 *   B15 Reserved
209 *
210 */
211
212
213
214/* FTDI_SIO_MODEM_CTRL */
215#define FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE 0x40
216#define FTDI_SIO_SET_MODEM_CTRL_REQUEST FTDI_SIO_MODEM_CTRL
217
218/*
219 * BmRequestType:   0100 0000B
220 * bRequest:        FTDI_SIO_MODEM_CTRL
221 * wValue:          ControlValue (see below)
222 * wIndex:          Port
223 * wLength:         0
224 * Data:            None
225 *
226 * NOTE: If the device is in RTS/CTS flow control, the RTS set by this
227 * command will be IGNORED without an error being returned
228 * Also - you can not set DTR and RTS with one control message
229 */
230
231#define FTDI_SIO_SET_DTR_MASK 0x1
232#define FTDI_SIO_SET_DTR_HIGH ( 1 | ( FTDI_SIO_SET_DTR_MASK  << 8))
233#define FTDI_SIO_SET_DTR_LOW  ( 0 | ( FTDI_SIO_SET_DTR_MASK  << 8))
234#define FTDI_SIO_SET_RTS_MASK 0x2
235#define FTDI_SIO_SET_RTS_HIGH ( 2 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
236#define FTDI_SIO_SET_RTS_LOW ( 0 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
237
238/*
239 * ControlValue
240 * B0    DTR state
241 *          0 = reset
242 *          1 = set
243 * B1    RTS state
244 *          0 = reset
245 *          1 = set
246 * B2..7 Reserved
247 * B8    DTR state enable
248 *          0 = ignore
249 *          1 = use DTR state
250 * B9    RTS state enable
251 *          0 = ignore
252 *          1 = use RTS state
253 * B10..15 Reserved
254 */
255
256/* FTDI_SIO_SET_FLOW_CTRL */
257#define FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE 0x40
258#define FTDI_SIO_SET_FLOW_CTRL_REQUEST FTDI_SIO_SET_FLOW_CTRL
259#define FTDI_SIO_DISABLE_FLOW_CTRL 0x0
260#define FTDI_SIO_RTS_CTS_HS (0x1 << 8)
261#define FTDI_SIO_DTR_DSR_HS (0x2 << 8)
262#define FTDI_SIO_XON_XOFF_HS (0x4 << 8)
263/*
264 *   BmRequestType:  0100 0000b
265 *   bRequest:       FTDI_SIO_SET_FLOW_CTRL
266 *   wValue:         Xoff/Xon
267 *   wIndex:         Protocol/Port - hIndex is protocl / lIndex is port
268 *   wLength:        0
269 *   Data:           None
270 *
271 * hIndex protocol is:
272 *   B0 Output handshaking using RTS/CTS
273 *       0 = disabled
274 *       1 = enabled
275 *   B1 Output handshaking using DTR/DSR
276 *       0 = disabled
277 *       1 = enabled
278 *   B2 Xon/Xoff handshaking
279 *       0 = disabled
280 *       1 = enabled
281 *
282 * A value of zero in the hIndex field disables handshaking
283 *
284 * If Xon/Xoff handshaking is specified, the hValue field should contain the XOFF character
285 * and the lValue field contains the XON character.
286 */ 
287 
288/*
289 * FTDI_SIO_GET_LATENCY_TIMER
290 *
291 * Set the timeout interval. The FTDI collects data from the slave
292 * device, transmitting it to the host when either A) 62 bytes are
293 * received, or B) the timeout interval has elapsed and the buffer
294 * contains at least 1 byte.  Setting this value to a small number
295 * can dramatically improve performance for applications which send
296 * small packets, since the default value is 16ms.
297 */
298#define  FTDI_SIO_GET_LATENCY_TIMER_REQUEST FTDI_SIO_GET_LATENCY_TIMER
299#define  FTDI_SIO_GET_LATENCY_TIMER_REQUEST_TYPE 0xC0
300
301/*
302 *  BmRequestType:   1100 0000b
303 *  bRequest:        FTDI_SIO_GET_LATENCY_TIMER
304 *  wValue:          0
305 *  wIndex:          Port
306 *  wLength:         0
307 *  Data:            latency (on return)
308 */
309
310/*
311 * FTDI_SIO_SET_LATENCY_TIMER
312 *
313 * Set the timeout interval. The FTDI collects data from the slave
314 * device, transmitting it to the host when either A) 62 bytes are
315 * received, or B) the timeout interval has elapsed and the buffer
316 * contains at least 1 byte.  Setting this value to a small number
317 * can dramatically improve performance for applications which send
318 * small packets, since the default value is 16ms.
319 */
320#define  FTDI_SIO_SET_LATENCY_TIMER_REQUEST FTDI_SIO_SET_LATENCY_TIMER
321#define  FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE 0x40
322
323/*
324 *  BmRequestType:   0100 0000b
325 *  bRequest:        FTDI_SIO_SET_LATENCY_TIMER
326 *  wValue:          Latency (milliseconds)
327 *  wIndex:          Port
328 *  wLength:         0
329 *  Data:            None
330 *
331 * wValue:
332 *   B0..7   Latency timer
333 *   B8..15  0
334 *
335 */
336
337/*
338 * FTDI_SIO_SET_EVENT_CHAR
339 *
340 * Set the special event character for the specified communications port.
341 * If the device sees this character it will immediately return the
342 * data read so far - rather than wait 40ms or until 62 bytes are read
343 * which is what normally happens.
344 */
345
346
347#define  FTDI_SIO_SET_EVENT_CHAR_REQUEST FTDI_SIO_SET_EVENT_CHAR
348#define  FTDI_SIO_SET_EVENT_CHAR_REQUEST_TYPE 0x40
349
350
351/*
352 *  BmRequestType:   0100 0000b
353 *  bRequest:        FTDI_SIO_SET_EVENT_CHAR
354 *  wValue:          EventChar
355 *  wIndex:          Port
356 *  wLength:         0
357 *  Data:            None
358 *
359 * wValue:
360 *   B0..7   Event Character
361 *   B8      Event Character Processing
362 *             0 = disabled
363 *             1 = enabled
364 *   B9..15  Reserved
365 *
366 */
367         
368/* FTDI_SIO_SET_ERROR_CHAR */
369
370/* Set the parity error replacement character for the specified communications port */
371
372/*
373 *  BmRequestType:  0100 0000b
374 *  bRequest:       FTDI_SIO_SET_EVENT_CHAR
375 *  wValue:         Error Char
376 *  wIndex:         Port
377 *  wLength:        0
378 *  Data:           None
379 *
380 *Error Char
381 *  B0..7  Error Character
382 *  B8     Error Character Processing
383 *           0 = disabled
384 *           1 = enabled
385 *  B9..15 Reserved
386 *
387 */
388
389/* FTDI_SIO_GET_MODEM_STATUS */
390/* Retrieve the current value of the modem status register */
391
392#define FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE 0xc0
393#define FTDI_SIO_GET_MODEM_STATUS_REQUEST FTDI_SIO_GET_MODEM_STATUS
394#define FTDI_SIO_CTS_MASK 0x10
395#define FTDI_SIO_DSR_MASK 0x20
396#define FTDI_SIO_RI_MASK  0x40
397#define FTDI_SIO_RLSD_MASK 0x80
398/*
399 *   BmRequestType:   1100 0000b
400 *   bRequest:        FTDI_SIO_GET_MODEM_STATUS
401 *   wValue:          zero
402 *   wIndex:          Port
403 *   wLength:         1
404 *   Data:            Status
405 *
406 * One byte of data is returned
407 * B0..3 0
408 * B4    CTS
409 *         0 = inactive
410 *         1 = active
411 * B5    DSR
412 *         0 = inactive
413 *         1 = active
414 * B6    Ring Indicator (RI)
415 *         0 = inactive
416 *         1 = active
417 * B7    Receive Line Signal Detect (RLSD)
418 *         0 = inactive
419 *         1 = active
420 */
421
422
423
424/* Descriptors returned by the device
425 *
426 *  Device Descriptor
427 *
428 * Offset       Field           Size    Value   Description
429 * 0    bLength         1       0x12    Size of descriptor in bytes
430 * 1    bDescriptorType 1       0x01    DEVICE Descriptor Type
431 * 2    bcdUSB          2       0x0110  USB Spec Release Number
432 * 4    bDeviceClass    1       0x00    Class Code
433 * 5    bDeviceSubClass 1       0x00    SubClass Code
434 * 6    bDeviceProtocol 1       0x00    Protocol Code
435 * 7    bMaxPacketSize0 1       0x08    Maximum packet size for endpoint 0
436 * 8    idVendor        2       0x0403  Vendor ID
437 * 10   idProduct       2       0x8372  Product ID (FTDI_SIO_PID)
438 * 12   bcdDevice       2       0x0001  Device release number
439 * 14   iManufacturer   1       0x01    Index of man. string desc
440 * 15   iProduct        1       0x02    Index of prod string desc
441 * 16   iSerialNumber   1       0x02    Index of serial nmr string desc
442 * 17   bNumConfigurations 1    0x01    Number of possible configurations
443 *
444 * Configuration Descriptor
445 *
446 * Offset       Field                   Size    Value
447 * 0    bLength                 1       0x09    Size of descriptor in bytes
448 * 1    bDescriptorType         1       0x02    CONFIGURATION Descriptor Type
449 * 2    wTotalLength            2       0x0020  Total length of data
450 * 4    bNumInterfaces          1       0x01    Number of interfaces supported
451 * 5    bConfigurationValue     1       0x01    Argument for SetCOnfiguration() req
452 * 6    iConfiguration          1       0x02    Index of config string descriptor
453 * 7    bmAttributes            1       0x20    Config characteristics Remote Wakeup
454 * 8    MaxPower                1       0x1E    Max power consumption
455 *
456 * Interface Descriptor
457 *
458 * Offset       Field                   Size    Value
459 * 0    bLength                 1       0x09    Size of descriptor in bytes
460 * 1    bDescriptorType         1       0x04    INTERFACE Descriptor Type
461 * 2    bInterfaceNumber        1       0x00    Number of interface
462 * 3    bAlternateSetting       1       0x00    Value used to select alternate
463 * 4    bNumEndpoints           1       0x02    Number of endpoints
464 * 5    bInterfaceClass         1       0xFF    Class Code
465 * 6    bInterfaceSubClass      1       0xFF    Subclass Code
466 * 7    bInterfaceProtocol      1       0xFF    Protocol Code
467 * 8    iInterface              1       0x02    Index of interface string description
468 *
469 * IN Endpoint Descriptor
470 *
471 * Offset       Field                   Size    Value
472 * 0    bLength                 1       0x07    Size of descriptor in bytes
473 * 1    bDescriptorType         1       0x05    ENDPOINT descriptor type
474 * 2    bEndpointAddress        1       0x82    Address of endpoint
475 * 3    bmAttributes            1       0x02    Endpoint attributes - Bulk
476 * 4    bNumEndpoints           2       0x0040  maximum packet size
477 * 5    bInterval               1       0x00    Interval for polling endpoint
478 *
479 * OUT Endpoint Descriptor
480 *
481 * Offset       Field                   Size    Value
482 * 0    bLength                 1       0x07    Size of descriptor in bytes
483 * 1    bDescriptorType         1       0x05    ENDPOINT descriptor type
484 * 2    bEndpointAddress        1       0x02    Address of endpoint
485 * 3    bmAttributes            1       0x02    Endpoint attributes - Bulk
486 * 4    bNumEndpoints           2       0x0040  maximum packet size
487 * 5    bInterval               1       0x00    Interval for polling endpoint
488 *     
489 * DATA FORMAT
490 *
491 * IN Endpoint
492 *
493 * The device reserves the first two bytes of data on this endpoint to contain the current
494 * values of the modem and line status registers. In the absence of data, the device
495 * generates a message consisting of these two status bytes every 40 ms
496 *
497 * Byte 0: Modem Status
498 *
499 * Offset       Description
500 * B0   Reserved - must be 1
501 * B1   Reserved - must be 0
502 * B2   Reserved - must be 0
503 * B3   Reserved - must be 0
504 * B4   Clear to Send (CTS)
505 * B5   Data Set Ready (DSR)
506 * B6   Ring Indicator (RI)
507 * B7   Receive Line Signal Detect (RLSD)
508 *
509 * Byte 1: Line Status
510 *
511 * Offset       Description
512 * B0   Data Ready (DR)
513 * B1   Overrun Error (OE)
514 * B2   Parity Error (PE)
515 * B3   Framing Error (FE)
516 * B4   Break Interrupt (BI)
517 * B5   Transmitter Holding Register (THRE)
518 * B6   Transmitter Empty (TEMT)
519 * B7   Error in RCVR FIFO
520 *
521 */
522#define FTDI_RS0_CTS    (1 << 4)
523#define FTDI_RS0_DSR    (1 << 5)
524#define FTDI_RS0_RI     (1 << 6)
525#define FTDI_RS0_RLSD   (1 << 7)
526
527#define FTDI_RS_DR  1
528#define FTDI_RS_OE (1<<1)
529#define FTDI_RS_PE (1<<2)
530#define FTDI_RS_FE (1<<3)
531#define FTDI_RS_BI (1<<4)
532#define FTDI_RS_THRE (1<<5)
533#define FTDI_RS_TEMT (1<<6)
534#define FTDI_RS_FIFO  (1<<7)
535
536/*
537 * OUT Endpoint
538 *
539 * This device reserves the first bytes of data on this endpoint contain the length
540 * and port identifier of the message. For the FTDI USB Serial converter the port
541 * identifier is always 1.
542 *
543 * Byte 0: Line Status
544 *
545 * Offset       Description
546 * B0   Reserved - must be 1
547 * B1   Reserved - must be 0
548 * B2..7        Length of message - (not including Byte 0)
549 *
550 */
551