Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / lib / usb_dev_board_drivers / ftdi_sio.h @ 11

History | View | Annotate | Download (14.3 KB)

1 11 emarinel
/*
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 of Silicon Graphics, Inc.- wrote the
18
 * FTDI_SIO implementation.
19
 *
20
 */
21
22
#define FTDI_VID        0x0403        /* Vendor Id */
23
#define FTDI_SIO_PID        0x8372        /* Product Id SIO application of 8U100AX  */
24
#define FTDI_8U232AM_PID 0x6001 /* Similar device to SIO above */
25
#define FTDI_NF_RIC_VID        0x0DCD        /* Vendor Id */
26
#define FTDI_NF_RIC_PID        0x0001        /* Product Id */
27
28
#define FTDI_SIO_RESET                 0 /* Reset the port */
29
#define FTDI_SIO_MODEM_CTRL         1 /* Set the modem control register */
30
#define FTDI_SIO_SET_FLOW_CTRL        2 /* Set flow control register */
31
#define FTDI_SIO_SET_BAUD_RATE        3 /* Set baud rate */
32
#define FTDI_SIO_SET_DATA        4 /* Set the data characteristics of the port */
33
#define FTDI_SIO_GET_MODEM_STATUS        5 /* Retrieve current value of modern status register */
34
#define FTDI_SIO_SET_EVENT_CHAR        6 /* Set the event character */
35
#define FTDI_SIO_SET_ERROR_CHAR        7 /* Set the error character */
36
37
/* Port Identifier Table */
38
#define PIT_DEFAULT                 0 /* SIOA */
39
#define PIT_SIOA                1 /* SIOA */
40
/* The device this driver is tested with one has only one port */
41
#define PIT_SIOB                2 /* SIOB */
42
#define PIT_PARALLEL                3 /* Parallel */
43
44
/* FTDI_SIO_RESET */
45
#define FTDI_SIO_RESET_REQUEST FTDI_SIO_RESET
46
#define FTDI_SIO_RESET_REQUEST_TYPE 0x40
47
#define FTDI_SIO_RESET_SIO 0
48
#define FTDI_SIO_RESET_PURGE_RX 1
49
#define FTDI_SIO_RESET_PURGE_TX 2
50
51
/*
52
 * BmRequestType:  0100 0000B
53
 * bRequest:       FTDI_SIO_RESET
54
 * wValue:         Control Value
55
 *                   0 = Reset SIO
56
 *                   1 = Purge RX buffer
57
 *                   2 = Purge TX buffer
58
 * wIndex:         Port
59
 * wLength:        0
60
 * Data:           None
61
 *
62
 * The Reset SIO command has this effect:
63
 *
64
 *    Sets flow control set to 'none'
65
 *    Event char = $0D
66
 *    Event trigger = disabled
67
 *    Purge RX buffer
68
 *    Purge TX buffer
69
 *    Clear DTR
70
 *    Clear RTS
71
 *    baud and data format not reset
72
 *
73
 * The Purge RX and TX buffer commands affect nothing except the buffers
74
 *
75
   */
76
77
/* FTDI_SIO_SET_BAUDRATE */
78
#define FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE 0x40
79
#define FTDI_SIO_SET_BAUDRATE_REQUEST 3
80
81
/*
82
 * BmRequestType:  0100 0000B
83
 * bRequest:       FTDI_SIO_SET_BAUDRATE
84
 * wValue:         BaudDivisor value - see below
85
 * wIndex:         Port
86
 * wLength:        0
87
 * Data:           None
88
 * The BaudDivisor values are calculated as follows:
89
 * - BaseClock is either 12000000 or 48000000 depending on the device. FIXME: I wish
90
 *   I knew how to detect old chips to select proper base clock!
91
 * - BaudDivisor is a fixed point number encoded in a funny way.
92
 *   (--WRONG WAY OF THINKING--)
93
 *   BaudDivisor is a fixed point number encoded with following bit weighs:
94
 *   (-2)(-1)(13..0). It is a radical with a denominator of 4, so values
95
 *   end with 0.0 (00...), 0.25 (10...), 0.5 (01...), and 0.75 (11...).
96
 *   (--THE REALITY--)
97
 *   The both-bits-set has quite different meaning from 0.75 - the chip designers
98
 *   have decided it to mean 0.125 instead of 0.75.
99
 *   This info looked up in FTDI application note "FT8U232 DEVICES \ Data Rates
100
 *   and Flow Control Consideration for USB to RS232".
101
 * - BaudDivisor = (BaseClock / 16) / BaudRate, where the (=) operation should
102
 *   automagically re-encode the resulting value to take fractions into consideration.
103
 * As all values are integers, some bit twiddling is in order:
104
 *   BaudDivisor = (BaseClock / 16 / BaudRate) |
105
 *   (((BaseClock / 2 / BaudRate) & 2) ? 0x8000 : 0) | // 0.25
106
 *   (((BaseClock / 2 / BaudRate) & 4) ? 0x4000 : 0) | // 0.5
107
 *   (((BaseClock / 2 / BaudRate) & 0x7) == 1 ? 0xc000) // 0.125 - this line due to funny encoding only
108
 */
109
110
typedef enum {
111
        SIO = 1,
112
        FT8U232AM = 2,
113
} ftdi_chip_type_t;
114
115
typedef enum {
116
 ftdi_sio_b300 = 0,
117
 ftdi_sio_b600 = 1,
118
 ftdi_sio_b1200 = 2,
119
 ftdi_sio_b2400 = 3,
120
 ftdi_sio_b4800 = 4,
121
 ftdi_sio_b9600 = 5,
122
 ftdi_sio_b19200 = 6,
123
 ftdi_sio_b38400 = 7,
124
 ftdi_sio_b57600 = 8,
125
 ftdi_sio_b115200 = 9
126
} FTDI_SIO_baudrate_t ;
127
128
#define FTDI_SIO_BASE_BAUD_TO_DIVISOR(base, baud) ( \
129
((base/2/baud) >> 3) | \
130
(((base/2/baud) & 2) ? 0x8000 : 0) | \
131
(((base/2/baud) & 4) ? 0x4000 : 0) | \
132
((((base/2/baud) & 0x7) == 1) ? 0xc000 : 0) )
133
134
#define FTDI_SIO_BAUD_TO_DIVISOR(baud) FTDI_SIO_BASE_BAUD_TO_DIVISOR(48000000, baud)
135
136
/*
137
 * The ftdi_8U232AM_xxMHz_byyy constans have been removed. Their values can
138
 * be calculated as follows: FTDI_SIO_BAUD_TO_DIVISOR(xx000000, yyy)
139
 */
140
141
#define FTDI_SIO_SET_DATA_REQUEST FTDI_SIO_SET_DATA
142
#define FTDI_SIO_SET_DATA_REQUEST_TYPE 0x40
143
#define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8 )
144
#define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8 )
145
#define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8 )
146
#define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8 )
147
#define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8 )
148
#define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11 )
149
#define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11 )
150
#define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11 )
151
#define FTDI_SIO_SET_BREAK (0x1 << 14)
152
/* FTDI_SIO_SET_DATA */
153
154
/*
155
 * BmRequestType:  0100 0000B
156
 * bRequest:       FTDI_SIO_SET_DATA
157
 * wValue:         Data characteristics (see below)
158
 * wIndex:         Port
159
 * wLength:        0
160
 * Data:           No
161
 *
162
 * Data characteristics
163
 *
164
 *   B0..7   Number of data bits
165
 *   B8..10  Parity
166
 *           0 = None
167
 *           1 = Odd
168
 *           2 = Even
169
 *           3 = Mark
170
 *           4 = Space
171
 *   B11..13 Stop Bits
172
 *           0 = 1
173
 *           1 = 1.5
174
 *           2 = 2
175
 *   B14
176
 *           1 = TX ON (break)
177
 *           0 = TX OFF (normal state)
178
 *   B15 Reserved
179
 *
180
 */
181
182
183
184
/* FTDI_SIO_MODEM_CTRL */
185
#define FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE 0x40
186
#define FTDI_SIO_SET_MODEM_CTRL_REQUEST FTDI_SIO_MODEM_CTRL
187
188
/*
189
 * BmRequestType:   0100 0000B
190
 * bRequest:        FTDI_SIO_MODEM_CTRL
191
 * wValue:          ControlValue (see below)
192
 * wIndex:          Port
193
 * wLength:         0
194
 * Data:            None
195
 *
196
 * NOTE: If the device is in RTS/CTS flow control, the RTS set by this
197
 * command will be IGNORED without an error being returned
198
 * Also - you can not set DTR and RTS with one control message
199
 */
200
201
#define FTDI_SIO_SET_DTR_MASK 0x1
202
#define FTDI_SIO_SET_DTR_HIGH ( 1 | ( FTDI_SIO_SET_DTR_MASK  << 8))
203
#define FTDI_SIO_SET_DTR_LOW  ( 0 | ( FTDI_SIO_SET_DTR_MASK  << 8))
204
#define FTDI_SIO_SET_RTS_MASK 0x2
205
#define FTDI_SIO_SET_RTS_HIGH ( 2 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
206
#define FTDI_SIO_SET_RTS_LOW ( 0 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
207
208
/*
209
 * ControlValue
210
 * B0    DTR state
211
 *          0 = reset
212
 *          1 = set
213
 * B1    RTS state
214
 *          0 = reset
215
 *          1 = set
216
 * B2..7 Reserved
217
 * B8    DTR state enable
218
 *          0 = ignore
219
 *          1 = use DTR state
220
 * B9    RTS state enable
221
 *          0 = ignore
222
 *          1 = use RTS state
223
 * B10..15 Reserved
224
 */
225
226
/* FTDI_SIO_SET_FLOW_CTRL */
227
#define FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE 0x40
228
#define FTDI_SIO_SET_FLOW_CTRL_REQUEST FTDI_SIO_SET_FLOW_CTRL
229
#define FTDI_SIO_DISABLE_FLOW_CTRL 0x0
230
#define FTDI_SIO_RTS_CTS_HS (0x1 << 8)
231
#define FTDI_SIO_DTR_DSR_HS (0x2 << 8)
232
#define FTDI_SIO_XON_XOFF_HS (0x4 << 8)
233
/*
234
 *   BmRequestType:  0100 0000b
235
 *   bRequest:       FTDI_SIO_SET_FLOW_CTRL
236
 *   wValue:         Xoff/Xon
237
 *   wIndex:         Protocol/Port - hIndex is protocl / lIndex is port
238
 *   wLength:        0
239
 *   Data:           None
240
 *
241
 * hIndex protocol is:
242
 *   B0 Output handshaking using RTS/CTS
243
 *       0 = disabled
244
 *       1 = enabled
245
 *   B1 Output handshaking using DTR/DSR
246
 *       0 = disabled
247
 *       1 = enabled
248
 *   B2 Xon/Xoff handshaking
249
 *       0 = disabled
250
 *       1 = enabled
251
 *
252
 * A value of zero in the hIndex field disables handshaking
253
 *
254
 * If Xon/Xoff handshaking is specified, the hValue field should contain the XOFF character
255
 * and the lValue field contains the XON character.
256
 */
257
258
/*
259
 * FTDI_SIO_SET_EVENT_CHAR
260
 *
261
 * Set the special event character for the specified communications port.
262
 * If the device sees this character it will immediately return the
263
 * data read so far - rather than wait 40ms or until 62 bytes are read
264
 * which is what normally happens.
265
 */
266
267
268
#define  FTDI_SIO_SET_EVENT_CHAR_REQUEST FTDI_SIO_SET_EVENT_CHAR
269
#define  FTDI_SIO_SET_EVENT_CHAR_REQUEST_TYPE 0x40
270
271
272
/*
273
 *  BmRequestType:   0100 0000b
274
 *  bRequest:        FTDI_SIO_SET_EVENT_CHAR
275
 *  wValue:          EventChar
276
 *  wIndex:          Port
277
 *  wLength:         0
278
 *  Data:            None
279
 *
280
 * wValue:
281
 *   B0..7   Event Character
282
 *   B8      Event Character Processing
283
 *             0 = disabled
284
 *             1 = enabled
285
 *   B9..15  Reserved
286
 *
287
 */
288
289
/* FTDI_SIO_SET_ERROR_CHAR */
290
291
/* Set the parity error replacement character for the specified communications port */
292
293
/*
294
 *  BmRequestType:  0100 0000b
295
 *  bRequest:       FTDI_SIO_SET_EVENT_CHAR
296
 *  wValue:         Error Char
297
 *  wIndex:         Port
298
 *  wLength:        0
299
 *  Data:           None
300
 *
301
 *Error Char
302
 *  B0..7  Error Character
303
 *  B8     Error Character Processing
304
 *           0 = disabled
305
 *           1 = enabled
306
 *  B9..15 Reserved
307
 *
308
 */
309
310
/* FTDI_SIO_GET_MODEM_STATUS */
311
/* Retreive the current value of the modem status register */
312
313
#define FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE 0xc0
314
#define FTDI_SIO_GET_MODEM_STATUS_REQUEST FTDI_SIO_GET_MODEM_STATUS
315
#define FTDI_SIO_CTS_MASK 0x10
316
#define FTDI_SIO_DSR_MASK 0x20
317
#define FTDI_SIO_RI_MASK  0x40
318
#define FTDI_SIO_RLSD_MASK 0x80
319
/*
320
 *   BmRequestType:   1100 0000b
321
 *   bRequest:        FTDI_SIO_GET_MODEM_STATUS
322
 *   wValue:          zero
323
 *   wIndex:          Port
324
 *   wLength:         1
325
 *   Data:            Status
326
 *
327
 * One byte of data is returned
328
 * B0..3 0
329
 * B4    CTS
330
 *         0 = inactive
331
 *         1 = active
332
 * B5    DSR
333
 *         0 = inactive
334
 *         1 = active
335
 * B6    Ring Indicator (RI)
336
 *         0 = inactive
337
 *         1 = active
338
 * B7    Receive Line Signal Detect (RLSD)
339
 *         0 = inactive
340
 *         1 = active
341
 */
342
343
344
345
/* Descriptors returned by the device
346
 *
347
 *  Device Descriptor
348
 *
349
 * Offset        Field                Size        Value        Description
350
 * 0        bLength                1        0x12        Size of descriptor in bytes
351
 * 1        bDescriptorType        1        0x01        DEVICE Descriptor Type
352
 * 2        bcdUSB                2        0x0110        USB Spec Release Number
353
 * 4        bDeviceClass        1        0x00        Class Code
354
 * 5        bDeviceSubClass        1        0x00        SubClass Code
355
 * 6        bDeviceProtocol        1        0x00        Protocol Code
356
 * 7        bMaxPacketSize0 1        0x08        Maximum packet size for endpoint 0
357
 * 8        idVendor        2        0x0403        Vendor ID
358
 * 10        idProduct        2        0x8372        Product ID (FTDI_SIO_PID)
359
 * 12        bcdDevice        2        0x0001        Device release number
360
 * 14        iManufacturer        1        0x01        Index of man. string desc
361
 * 15        iProduct        1        0x02        Index of prod string desc
362
 * 16        iSerialNumber        1        0x02        Index of serial nmr string desc
363
 * 17        bNumConfigurations 1    0x01        Number of possible configurations
364
 *
365
 * Configuration Descriptor
366
 *
367
 * Offset        Field                        Size        Value
368
 * 0        bLength                        1        0x09        Size of descriptor in bytes
369
 * 1        bDescriptorType                1        0x02        CONFIGURATION Descriptor Type
370
 * 2        wTotalLength                2        0x0020        Total length of data
371
 * 4        bNumInterfaces                1        0x01        Number of interfaces supported
372
 * 5        bConfigurationValue        1        0x01        Argument for SetCOnfiguration() req
373
 * 6        iConfiguration                1        0x02        Index of config string descriptor
374
 * 7        bmAttributes                1        0x20        Config characteristics Remote Wakeup
375
 * 8        MaxPower                1        0x1E        Max power consumption
376
 *
377
 * Interface Descriptor
378
 *
379
 * Offset        Field                        Size        Value
380
 * 0        bLength                        1        0x09        Size of descriptor in bytes
381
 * 1        bDescriptorType                1        0x04        INTERFACE Descriptor Type
382
 * 2        bInterfaceNumber        1        0x00        Number of interface
383
 * 3        bAlternateSetting        1        0x00        Value used to select alternate
384
 * 4        bNumEndpoints                1        0x02        Number of endpoints
385
 * 5        bInterfaceClass                1        0xFF        Class Code
386
 * 6        bInterfaceSubClass        1        0xFF        Subclass Code
387
 * 7        bInterfaceProtocol        1        0xFF        Protocol Code
388
 * 8        iInterface                1        0x02        Index of interface string description
389
 *
390
 * IN Endpoint Descriptor
391
 *
392
 * Offset        Field                        Size        Value
393
 * 0        bLength                        1        0x07        Size of descriptor in bytes
394
 * 1        bDescriptorType                1        0x05        ENDPOINT descriptor type
395
 * 2        bEndpointAddress        1        0x82        Address of endpoint
396
 * 3        bmAttributes                1        0x02        Endpoint attributes - Bulk
397
 * 4        bNumEndpoints                2        0x0040        maximum packet size
398
 * 5        bInterval                1        0x00        Interval for polling endpoint
399
 *
400
 * OUT Endpoint Descriptor
401
 *
402
 * Offset        Field                        Size        Value
403
 * 0        bLength                        1        0x07        Size of descriptor in bytes
404
 * 1        bDescriptorType                1        0x05        ENDPOINT descriptor type
405
 * 2        bEndpointAddress        1        0x02        Address of endpoint
406
 * 3        bmAttributes                1        0x02        Endpoint attributes - Bulk
407
 * 4        bNumEndpoints                2        0x0040        maximum packet size
408
 * 5        bInterval                1        0x00        Interval for polling endpoint
409
 *
410
 * DATA FORMAT
411
 *
412
 * IN Endpoint
413
 *
414
 * The device reserves the first two bytes of data on this endpoint to contain the current
415
 * values of the modem and line status registers. In the absence of data, the device
416
 * generates a message consisting of these two status bytes every 40 ms
417
 *
418
 * Byte 0: Modem Status
419
 *
420
 * Offset        Description
421
 * B0        Reserved - must be 1
422
 * B1        Reserved - must be 0
423
 * B2        Reserved - must be 0
424
 * B3        Reserved - must be 0
425
 * B4        Clear to Send (CTS)
426
 * B5        Data Set Ready (DSR)
427
 * B6        Ring Indicator (RI)
428
 * B7        Receive Line Signal Detect (RLSD)
429
 *
430
 * Byte 1: Line Status
431
 *
432
 * Offset        Description
433
 * B0        Data Ready (DR)
434
 * B1        Overrun Error (OE)
435
 * B2        Parity Error (PE)
436
 * B3        Framing Error (FE)
437
 * B4        Break Interrupt (BI)
438
 * B5        Transmitter Holding Register (THRE)
439
 * B6        Transmitter Empty (TEMT)
440
 * B7        Error in RCVR FIFO
441
 *
442
 */
443
#define FTDI_RS0_CTS        (1 << 4)
444
#define FTDI_RS0_DSR        (1 << 5)
445
#define FTDI_RS0_RI        (1 << 6)
446
#define FTDI_RS0_RLSD        (1 << 7)
447
448
#define FTDI_RS_DR  1
449
#define FTDI_RS_OE (1<<1)
450
#define FTDI_RS_PE (1<<2)
451
#define FTDI_RS_FE (1<<3)
452
#define FTDI_RS_BI (1<<4)
453
#define FTDI_RS_THRE (1<<5)
454
#define FTDI_RS_TEMT (1<<6)
455
#define FTDI_RS_FIFO  (1<<7)
456
457
/*
458
 * OUT Endpoint
459
 *
460
 * This device reserves the first bytes of data on this endpoint contain the length
461
 * and port identifier of the message. For the FTDI USB Serial converter the port
462
 * identifier is always 1.
463
 *
464
 * Byte 0: Line Status
465
 *
466
 * Offset        Description
467
 * B0        Reserved - must be 1
468
 * B1        Reserved - must be 0
469
 * B2..7        Length of message - (not including Byte 0)
470
 *
471
 */