forked from devttys0/libmpsse
-
Notifications
You must be signed in to change notification settings - Fork 25
/
README.C
330 lines (186 loc) · 8.17 KB
/
README.C
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
MPSSE FUNCTIONS
struct mpsse_context *MPSSE.Open(int vid, int pid, enum modes mode, int freq, int endianess, int interface, const char *description, const char *serial)
Opens the device matching the given vendor and product ID, description, or serial number.
This can be used to create an MPSSE context for any supported mode.
@vid - Device vendor ID.
@pid - Device product ID.
@mode - MPSSE mode, one of: SPI0, SPI1, SPI2, SPI3, I2C, MCU8, MCU16, GPIO, BITBANG.
@freq - Clock frequency to use for the specified mode.
@endianess - Specifies how data is clocked in/out (MSB, LSB).
@interface - FTDI interface to use, one of: IFACE_ANY, IFACE_A - IFACE_D.
@description - Device product description (set to NULL if not needed).
@serial - Serial number of the FTDI (set to NULL if not needed).
Returns a pointer to an MPSSE context structure.
On success, mpsse->open will be set to 1.
On failure, mpsse->open will be set to 0.
void MPSSE.Close(struct mpsse_context *mpsse)
Closes the FTDI device and deinitializes libftdi.
@mpsse - MPSSE context pointer.
Returns void.
char *MPSSE.ErrorString(struct mpsse_context *mpsse)
Get the last error string as reported by libftdi. This data should not be freed by the caller.
@mpsse - MPSSE context pointer.
Returns a pointer to the last error string reported by libftdi.
int MPSSE.GetVid(struct mpsse_context *mpsse)
Get the USB vendor ID of the connected FTDI device.
@mpsse - MPSSE context pointer.
Returns the FTDI vendor ID.
int MPSSE.GetPid(struct mpsse_context *mpsse)
Get the USB product ID of the connected FTDI device.
@mpsse - MPSSE context pointer.
Returns the FTDI product ID.
char *MPSSE.GetDescription(struct mpsse_context *mpsse)
Get the description, as listed in the internal supported_devices structure, of the connected FTDI device.
The caller must not free the return value.
@mpsse - MPSSE context pointer.
Returns a pointer to the device description string.
int MPSSE.GetClock(struct mpsse_context *mpsse)
Gets the currently configured clock frequency.
@mpsse - MPSSE context pointer.
Returns an unsigned 32 bit value representing the clock frequency.
int MPSSE.SetClock(struct mpsse_context *mpsse, int freq)
Sets the appropriate divisor for the desired clock frequency.
Called internally by Open().
@mpsse - MPSSE context pointer.
@freq - Desired clock frequency in hertz.
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
int MPSSE.SetMode(struct mpsse_context *mpsse, enum modes mode, int endianess)
Sets the appropriate transmit and receive commands based on the requested mode and byte order.
Called internally by Open().
@mpsse - MPSSE context pointer.
@mode - The desired mode, as listed in enum modes.
@endianess - Most or least significant byte first (MSB / LSB).
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
int MPSSE.SetLoopback(struct mpsse_context *mpsse, int enable)
Enable / disable internal loopback.
Called internally by SetMode().
@mpsse - MPSSE context pointer.
@enable - Zero to disable the FTDI internal loopback, 1 to enable loopback.
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
SPI FUNCTIONS
int MPSSE.SPI.Start(struct mpsse_context *mpsse)
Asserts the chip select pin.
@mpsse - MPSSE context pointer.
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
int MPSSE.SPI.Stop(struct mpsse_context *mpsse)
Deasserts the chip select pin.
@mpsse - MPSSE context pointer.
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
char *MPSSE.SPI.Read(struct mpsse_context *mpsse, int size)
Reads SPI data.
@mpsse - MPSSE context pointer.
@size - Number of bytes to read.
Returns a pointer to the read data on success.
Returns NULL on failure.
int MPSSE.SPI.Write(struct mpsse_context *mpsse, char *data, int size)
Writes SPI data.
@mpsse - MPSSE context pointer.
@data - Buffer of data to send.
@size - Size of data.
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
char *MPSSE.SPI.Transfer(struct mpsse_context *mpsse, char *outbuf, int size)
Performs a bi-directional data transfer of size bytes.
@mpsse - MPSSE context pointer.
@outbuf - Buffer of bytes to send.
@size - Size of outbuf.
Returns a pointer to the read data on success.
Returns NULL on failure.
I2C FUNCTIONS
int MPSSE.I2C.GetAck(struct mpsse_context *mpsse)
Return the status of the last received acknolwedgement bit.
@mpsse - MPSSE context pointer.
Returns 0 if an ACK was received.
Returns 1 if a NACK was received.
void MPSSE.I2C.SetAck(struct mpsse_context *mpsse, int ack)
Set the ACK bit to send when Read() receives a byte from the I2C slave device.
@mpsse - MPSSE context pointer.
@ack - Set to 0 to send ACKs (default).
Set to 1 to send NACKs.
Returns void.
void MPSSE.I2C.SendAcks(struct mpsse_context *mpsse)
Causes libmpsse to send ACKs after each read byte in I2C mode.
@mpsse - MPSSE context pointer.
Returns void.
void MPSSE.I2C.SendNacks(struct mpsse_context *mpsse)
Causes libmpsse to send NACKs after each read byte in I2C mode.
@mpsse - MPSSE context pointer.
Returns void.
void SetCSIdle(struct mpsse_context *mpsse, int idle)
Set the idle state of the chip select pin. CS idles high by default.
@mpsse - MPSSE context pointer.
@idle - Set to 1 to idle high, 0 to idle low.
Returns void.
int PinHigh(struct mpsse_context *mpsse, int pin)
Set the specified GPIO pin high (1).
Note that the state of the GPOL pins can only be changed prior to a Start()
or after a Stop() condition is sent.
@mpsse - MPSSE context pointer.
@pin - Pin number, one of: 0 - 11.
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
int PinLow(struct mpsse_context *mpsse, int pin)
Set the specified GPIO pin low (0).
Note that the state of GPIO pins 0-3 can only be changed prior to a Start()
or after a Stop() condition is sent.
@mpsse - MPSSE context pointer.
@pin - Pin number, one of: 0 - 11.
Returns MPSSE_OK on success.
Returns MPSSE_FAIL on failure.
int ReadPins(struct mpsse_context *mpsse);
Reads the state of the chip's pins.
@mpsse - MPSSE context pointer.
Returns a byte with the corresponding pin's bits set to 1 or 0.
int PinState(struct mpsse_context *mpsse, int pin, int state)
Checks if a specific pin is high or low.
@mpsse - MPSSE context pointer.
@pin - The pin number.
@state - The state of the pins, as returned by ReadPins.
If set to -1, ReadPins will automatically be called.
Returns a 1 if the pin is high, 0 if the pin is low.
DEFINITIONS
Interface definitions, used for the 'interface' argument to Open():
IFACE_ANY = INTERFACE_ANY,
IFACE_A = INTERFACE_A,
IFACE_B = INTERFACE_B,
IFACE_C = INTERFACE_C,
IFACE_D = INTERFACE_D
Clock rate definitions, provided for convenience, used for the 'frequency' argument of MPSSE() and Open():
ONE_HUNDRED_KHZ = 100000,
FOUR_HUNDRED_KHZ = 400000,
ONE_MHZ = 1000000,
TWO_MHZ = 2000000,
FIVE_MHZ = 5000000,
SIX_MHZ = 6000000,
TEN_MHZ = 10000000,
TWELVE_MHZ = 12000000,
THIRTY_MHZ = 30000000,
Mode definitions, used for the 'mode' argument of MPSSE() and Open():
SPI0 = 1,
SPI1 = 2,
SPI2 = 3,
SPI3 = 4,
I2C = 5,
GPIO = 6
BITBANG = 7
GPIO pin definitions:
GPIOL0 = 0,
GPIOL1 = 1,
GPIOL2 = 2,
GPIOL3 = 3,
GPIOH0 = 4,
GPIOH1 = 5,
GPIOH2 = 6,
GPIOH3 = 7,
GPIOH4 = 8,
GPIOH5 = 9,
GPIOH6 = 10,
GPIOH7 = 11
MPSSE return codes; most functions that return an integer value return one of these unless otherwise specified:
MPSSE_OK = 0,
MPSSE_FAIL = -1