forked from mds5000/goftdi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftdi.go
166 lines (146 loc) · 5.03 KB
/
ftdi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package ftdi
import (
"errors"
)
type BitMode byte
const (
RESET BitMode = 0x00
ASYNC_BITBANG = 0x01
MPSSE = 0x02
SYNC_BITBANG = 0x04
HOST_EMU = 0x08
FAST_OPTO = 0x10
CBUS_BITBANG = 0x20
SYNCHRONOUS = 0x40
)
type FlowControl uint16
const (
DISABLED FlowControl = 0x0000
RTS_CTS = 0x0100
DTR_DSR = 0x0200
XON_XOFF = 0x0400
)
type LineProperties struct {
Bits bitsPerWord
StopBits stopBits
Parity parity
}
type bitsPerWord byte
type stopBits byte
type parity byte
const (
BITS_8 bitsPerWord = 8
BIST_7 bitsPerWord = 7
STOP_1 stopBits = 0
STOP_2 stopBits = 2
NONE parity = 0
ODD parity = 1
EVEN parity = 2
MARK parity = 3
SPACE parity = 4
)
const (
CHECK_RX_DELAY_MS = 200
)
//FT_STATUS (DWORD)
const (
FT_OK = 0
FT_INVALID_HANDLE = 1
FT_DEVICE_NOT_FOUND = 2
FT_DEVICE_NOT_OPENED = 3
FT_IO_ERROR = 4
FT_INSUFFICIENT_RESOURCES = 5
FT_INVALID_PARAMETER = 6
FT_INVALID_BAUD_RATE = 7
FT_DEVICE_NOT_OPENED_FOR_ERASE = 8
FT_DEVICE_NOT_OPENED_FOR_WRITE = 9
FT_FAILED_TO_WRITE_DEVICE = 10
FT_EEPROM_READ_FAILED = 11
FT_EEPROM_WRITE_FAILED = 12
FT_EEPROM_ERASE_FAILED = 13
FT_EEPROM_NOT_PRESENT = 14
FT_EEPROM_NOT_PROGRAMMED = 15
FT_INVALID_ARGS = 16
FT_NOT_SUPPORTED = 17
FT_OTHER_ERROR = 18
)
var (
ErrInvalidHandle = errors.New("FTDI :: Invalid device handle")
ErrDeviceNotFound = errors.New("FTDI :: Device not found")
ErrDeviceNotOpened = errors.New("FTDI :: Device not opened")
ErrIO = errors.New("FTDI :: IO failed")
ErrInsufficientResources = errors.New("FTDI :: Insufficient resources")
ErrInvalidParameter = errors.New("FTDI :: Invalid parameter")
ErrInvalidBaudRate = errors.New("FTDI :: Invalid baud rate")
ErrDeviceNotOpenedForErase = errors.New("FTDI :: Device not opened for erase")
ErrDeviceNotOpenedForWrite = errors.New("FTDI :: Device not opened for write")
ErrFailedToWriteDevice = errors.New("FTDI :: Failed to write device")
ErrEReadFailed = errors.New("FTDI :: EEPROM read failed")
ErrEWriteFailed = errors.New("FTDI :: EEPROM write failed")
ErrEEraseFailed = errors.New("FTDI :: EEPROM erase failed")
ErrENotPresent = errors.New("FTDI :: EEPROM not present")
ErrENotProgrammed = errors.New("FTDI :: EEPROM not programmed")
ErrInvalidArgs = errors.New("FTDI :: Invalid arguments")
ErrNotSupported = errors.New("FTDI :: Device not supported")
ErrOther = errors.New("FTDI :: Unknown FTDI error")
)
var errorList map[uintptr]error
func init() {
errorList = map[uintptr]error{
FT_INVALID_HANDLE: ErrInvalidHandle,
FT_DEVICE_NOT_FOUND: ErrDeviceNotFound,
FT_DEVICE_NOT_OPENED: ErrDeviceNotOpened,
FT_IO_ERROR: ErrIO,
FT_INSUFFICIENT_RESOURCES: ErrInsufficientResources,
FT_INVALID_PARAMETER: ErrInvalidParameter,
FT_INVALID_BAUD_RATE: ErrInvalidBaudRate,
FT_DEVICE_NOT_OPENED_FOR_ERASE: ErrDeviceNotOpenedForErase,
FT_DEVICE_NOT_OPENED_FOR_WRITE: ErrDeviceNotOpenedForWrite,
FT_FAILED_TO_WRITE_DEVICE: ErrFailedToWriteDevice,
FT_EEPROM_READ_FAILED: ErrEReadFailed,
FT_EEPROM_WRITE_FAILED: ErrEWriteFailed,
FT_EEPROM_ERASE_FAILED: ErrEEraseFailed,
FT_EEPROM_NOT_PRESENT: ErrENotPresent,
FT_EEPROM_NOT_PROGRAMMED: ErrENotProgrammed,
FT_INVALID_ARGS: ErrInvalidArgs,
FT_NOT_SUPPORTED: ErrNotSupported,
FT_OTHER_ERROR: ErrOther,
}
}
func ftdiError(e uintptr) error {
err, ok := errorList[e]
if ok {
return err
} else {
return ErrOther
}
}
/* API
// Search the system for all connected FTDI devices.
// Returns a slice of `DeviceInfo` objects for each.
func GetDeviceList() []DeviceInfo {}
// Open the device described by DeviceInfo
func Open(di *DeviceInfo) *Device, error {}
// Close the device
// Return nil on success, error otherwise.
func (d *Device) Close() error {}
// Read from the device. Implements io.Reader.
func (d *Device) Read([]byte) int, error {}
// Write to the device. Implements io.Writer.
func (d *Device) Write([]byte) int, error {}
// Set the baudrate/bitrate in bits-per-second.
// Return nil on success, error otherwise.
func (d *Device) SetBaudrate(baud uint) error {}
// Set the device's bit mode.
func (d *Device) SetBitMode(mode BitMode) error {}
func (d *Device) SetFlowControl() error{}
func (d *Device) SetLatency() {}
func (d *Device) SetLineProperty() {}
func (d *Device) SetTimeout() {}
func (d *Device) SetTransferSize() {}
func (d *Device) SetChars() {}
// Reset the device. Returns nil on success,
// error otherwise.
func (d *Device) Reset() error {}
func (d *Device) Purge() error {}
*/