ftdi_sio.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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