-
Notifications
You must be signed in to change notification settings - Fork 2
/
iceprog.c
286 lines (219 loc) · 5.89 KB
/
iceprog.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
/*
* iceprog -- simple programming tool for FTDI-based Lattice iCE programmers
*
* Copyright (C) 2015 Clifford Wolf <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#define _GNU_SOURCE
#include <ftdi.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
struct ftdi_context ftdic;
bool ftdic_open = false;
bool verbose = false;
void check_rx()
{
while (1) {
uint8_t data;
int rc = ftdi_read_data(&ftdic, &data, 1);
if (rc <= 0) break;
fprintf(stderr, "unexpected rx byte: %02X\n", data);
}
}
void error()
{
check_rx();
fprintf(stderr, "ABORT.\n");
if (ftdic_open)
ftdi_usb_close(&ftdic);
ftdi_deinit(&ftdic);
exit(1);
}
uint8_t recv_byte()
{
uint8_t data;
while (1) {
int rc = ftdi_read_data(&ftdic, &data, 1);
if (rc < 0) {
fprintf(stderr, "Read error.\n");
error();
}
if (rc == 1)
break;
usleep(100);
}
return data;
}
void send_byte(uint8_t data)
{
int rc = ftdi_write_data(&ftdic, &data, 1);
if (rc != 1) {
fprintf(stderr, "Write error (single byte, rc=%d, expected %d).\n", rc, 1);
error();
}
}
void send_spi(uint8_t *data, int n)
{
if (n < 1)
return;
// max size check...
// 3.3.2 Clock Data Bytes Out on - ve clo ck edge MSB first (no read) Use if CLK starts at '0
send_byte(0x11);
send_byte(n-1);
send_byte((n-1) >> 8);
int rc = ftdi_write_data(&ftdic, data, n);
if (rc != n) {
fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n);
error();
}
}
void xfer_spi(uint8_t *data, int n)
{
if (n < 1)
return;
send_byte(0x31);
send_byte(n-1);
send_byte((n-1) >> 8);
int rc = ftdi_write_data(&ftdic, data, n);
if (rc != n) {
fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n);
error();
}
for (int i = 0; i < n; i++)
data[i] = recv_byte();
}
void set_gpio(int a, int b, int c)
{
// sets two bytes - intial led_values and i/o direction
// what does the one indicate?
// one is the clock ... maybe set high by default?
uint8_t gpio = 1;
if (a) {
// (GPIOL0)
gpio |= 0x10; // 1 << 4
}
if (b) {
// (GPIOL1)
gpio |= 0x20; // 1 << 5
}
if (c) {
// (GPIOL2)
gpio |= 0x40; // 1 << 6
}
// GPIOL3 not connected to fpga.
/*
At this point, the MPSSE is ready for commands . Each command consists of an
op - code followed by any necessary parameters or data.
The following commands are used to set the initial direction and logic state of
the pins when first entering MPSSE mode. They are also use to set or re a d
GPIO pins.
0x80 is a op-code to set initial led_values and direction.
*/
send_byte(0x80); // must be a command,,,
send_byte(gpio);
// 0xfb.toString(2);
// 0xFB = "11111011" should be correct enables miso as only input
send_byte(0xFB); // this is the direction a bit mask?
}
int get_cdone()
{
uint8_t data;
send_byte(0x81);
data = recv_byte();
// ADBUS6 (GPIOL2)
return (data & 0x40) != 0;
}
int main(int argc, char **argv)
{
const char *devstr = NULL;
enum ftdi_interface ifnum = INTERFACE_B;
int led_value;
if(strcmp(argv[1], "on") == 0)
led_value = 1;
else if (strcmp(argv[1], "off") == 0)
led_value = 0;
// ---------------------------------------------------------
// iNITIalize USB connection to FT2232H
// ---------------------------------------------------------
fprintf(stderr, "init..\n");
ftdi_init(&ftdic);
ftdi_set_interface(&ftdic, ifnum);
if (devstr != NULL) {
if (ftdi_usb_open_string(&ftdic, devstr)) {
fprintf(stderr, "Can't find iCE FTDI USB device (device string %s).\n", devstr);
error();
}
} else {
if (ftdi_usb_open(&ftdic, 0x0403, 0x6010)) {
fprintf(stderr, "Can't find iCE FTDI USB device (vedor_id 0x0403, device_id 0x6010).\n");
error();
}
}
ftdic_open = true;
if (ftdi_usb_reset(&ftdic)) {
fprintf(stderr, "Failed to reset iCE FTDI USB device.\n");
error();
}
if (ftdi_usb_purge_buffers(&ftdic)) {
fprintf(stderr, "Failed to purge buffers on iCE FTDI USB device.\n");
error();
}
if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_MPSSE) < 0) {
fprintf(stderr, "Failed set BITMODE_MPSSE on iCE FTDI USB device.\n");
error();
}
// enable clock divide by 5
send_byte(0x8b);
// set 6 MHz clock
send_byte(0x86);
send_byte(0x02); // 1 or 2 MHz ?
send_byte(0x00);
fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
// initialize data out
set_gpio(1, 1, 1);
fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
uint8_t data[1];
while(true) {
// assert CS
fprintf(stderr, "writing data led_value %d\n", led_value);
led_value = !led_value;
set_gpio(0, 1, 1);
if(led_value == 1)
data[0] = 0xcc;
else if(led_value == 0)
data[0] = 0xdd;
// write
send_spi( data, 1 );
// deassert CS
set_gpio(1, 1, 1);
fprintf(stderr, "done\n");
sleep(1);
}
// ---------------------------------------------------------
// Exit
// ---------------------------------------------------------
fprintf(stderr, "Bye.\n");
ftdi_disable_bitbang(&ftdic);
ftdi_usb_close(&ftdic);
ftdi_deinit(&ftdic);
return 0;
}