2 * iceprog -- simple programming tool for FTDI-based Lattice iCE programmers
4 * Copyright (C) 2015 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 2018 Piotr Esden-Tempski <piotr@esden.net>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * http://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
35 // ---------------------------------------------------------
36 // MPSSE / FTDI definitions
37 // ---------------------------------------------------------
39 /* FTDI bank pinout typically used for iCE dev boards
40 * BUS IO | Signal | Control
41 * -------+--------+--------------
42 * xDBUS0 | SCK | MPSSE
43 * xDBUS1 | MOSI | MPSSE
44 * xDBUS2 | MISO | MPSSE
48 * xDBUS6 | CDONE | GPIO
49 * xDBUS7 | CRESET | GPIO
52 struct ftdi_context mpsse_ftdic
;
53 bool mpsse_ftdic_open
= false;
54 bool mpsse_ftdic_latency_set
= false;
55 unsigned char mpsse_ftdi_latency
;
58 // ---------------------------------------------------------
59 // MPSSE / FTDI function implementations
60 // ---------------------------------------------------------
67 int rc
= ftdi_read_data(&mpsse_ftdic
, &data
, 1);
70 fprintf(stderr
, "unexpected rx byte: %02X\n", data
);
78 void mpsse_error(int status
)
81 fprintf(stderr
, "ABORT.\n");
82 if (mpsse_ftdic_open
) {
83 if (mpsse_ftdic_latency_set
)
84 ftdi_set_latency_timer(&mpsse_ftdic
, mpsse_ftdi_latency
);
85 ftdi_usb_close(&mpsse_ftdic
);
87 ftdi_deinit(&mpsse_ftdic
);
91 uint8_t mpsse_recv_byte()
95 int rc
= ftdi_read_data(&mpsse_ftdic
, &data
, 1);
97 fprintf(stderr
, "Read error.\n");
107 void mpsse_send_byte(uint8_t data
)
109 int rc
= ftdi_write_data(&mpsse_ftdic
, &data
, 1);
111 fprintf(stderr
, "Write error (single byte, rc=%d, expected %d)(%s).\n", rc
, 1, ftdi_get_error_string(&mpsse_ftdic
));
116 void mpsse_purge(void){
117 int rc
= ftdi_usb_purge_buffers(&mpsse_ftdic
);
119 fprintf(stderr
, "Purge error.\n");
124 void mpsse_xfer(uint8_t* data_buffer
, uint16_t send_length
, uint16_t receive_length
)
127 int rc
= ftdi_write_data(&mpsse_ftdic
, data_buffer
, send_length
);
128 if (rc
!= send_length
) {
129 fprintf(stderr
, "Write error (rc=%d, expected %d)[%s]\n", rc
, 1, ftdi_get_error_string(&mpsse_ftdic
));
135 /* Calls to ftdi_read_data may return with less data than requested if it wasn't ready.
136 * We stay in this while loop to collect all the data that we expect. */
138 while(rx_len
!= receive_length
){
139 int rc
= ftdi_read_data(&mpsse_ftdic
, data_buffer
+ rx_len
, receive_length
- rx_len
);
141 fprintf(stderr
, "Read error (rc=%d)[%s]\n", rc
, ftdi_get_error_string(&mpsse_ftdic
));
150 void mpsse_jtag_init(){
151 mpsse_send_byte(MC_SETB_LOW
);
152 mpsse_send_byte(0x08); /* Value */
153 mpsse_send_byte(0x0B); /* Direction */
155 /* Reset JTAG State machine */
159 void mpsse_jtag_tms(uint8_t bits
, uint8_t pattern
){
160 mpsse_send_byte(MC_DATA_TMS
| MC_DATA_LSB
| MC_DATA_BITS
);
161 mpsse_send_byte(bits
-1);
162 mpsse_send_byte(pattern
);
165 void mpsse_init(int ifnum
, const char *devstr
, bool slow_clock
)
167 enum ftdi_interface ftdi_ifnum
= INTERFACE_A
;
171 ftdi_ifnum
= INTERFACE_A
;
174 ftdi_ifnum
= INTERFACE_B
;
177 ftdi_ifnum
= INTERFACE_C
;
180 ftdi_ifnum
= INTERFACE_D
;
183 ftdi_ifnum
= INTERFACE_A
;
187 ftdi_init(&mpsse_ftdic
);
188 ftdi_set_interface(&mpsse_ftdic
, ftdi_ifnum
);
190 if (devstr
!= NULL
) {
191 if (ftdi_usb_open_string(&mpsse_ftdic
, devstr
)) {
192 fprintf(stderr
, "Can't find iCE FTDI USB device (device string %s).\n", devstr
);
196 if (ftdi_usb_open(&mpsse_ftdic
, 0x0403, 0x6010) && ftdi_usb_open(&mpsse_ftdic
, 0x0403, 0x6014)) {
197 fprintf(stderr
, "Can't find iCE FTDI USB device (vendor_id 0x0403, device_id 0x6010 or 0x6014).\n");
202 mpsse_ftdic_open
= true;
204 if (ftdi_usb_reset(&mpsse_ftdic
)) {
205 fprintf(stderr
, "Failed to reset iCE FTDI USB device.\n");
209 if (ftdi_usb_purge_buffers(&mpsse_ftdic
)) {
210 fprintf(stderr
, "Failed to purge buffers on iCE FTDI USB device.\n");
214 if (ftdi_get_latency_timer(&mpsse_ftdic
, &mpsse_ftdi_latency
) < 0) {
215 fprintf(stderr
, "Failed to get latency timer (%s).\n", ftdi_get_error_string(&mpsse_ftdic
));
219 /* 1 is the fastest polling, it means 1 kHz polling */
220 if (ftdi_set_latency_timer(&mpsse_ftdic
, 1) < 0) {
221 fprintf(stderr
, "Failed to set latency timer (%s).\n", ftdi_get_error_string(&mpsse_ftdic
));
225 mpsse_ftdic_latency_set
= true;
227 /* Enter MPSSE (Multi-Protocol Synchronous Serial Engine) mode. Set all pins to output. */
228 if (ftdi_set_bitmode(&mpsse_ftdic
, 0xff, BITMODE_MPSSE
) < 0) {
229 fprintf(stderr
, "Failed to set BITMODE_MPSSE on FTDI USB device.\n");
233 int rc
= ftdi_usb_purge_buffers(&mpsse_ftdic
);
235 fprintf(stderr
, "Purge error.\n");
241 mpsse_send_byte(MC_SET_CLK_DIV
);
242 mpsse_send_byte(119);
243 mpsse_send_byte(0x00);
246 mpsse_send_byte(MC_SET_CLK_DIV
);
248 mpsse_send_byte(0x00);
252 void mpsse_close(void)
254 ftdi_set_latency_timer(&mpsse_ftdic
, mpsse_ftdi_latency
);
255 ftdi_disable_bitbang(&mpsse_ftdic
);
256 ftdi_usb_close(&mpsse_ftdic
);
257 ftdi_deinit(&mpsse_ftdic
);