forked from lorf/csr-spi-ftdi
-
Notifications
You must be signed in to change notification settings - Fork 6
/
spi.c
618 lines (501 loc) · 15.9 KB
/
spi.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#ifdef SPI_STATS
#include <math.h>
#endif
#include "spi.h"
#include "compat.h"
#include "logging.h"
#include <windows.h>
#include "USBIOX.H"
/* Default SPI clock rate, in kHz */
#define SPIMAXCLOCK 750
static uint8_t *ch341_in_out_buf = NULL;
static size_t ch341_buf_size = 0;
static unsigned int ch341_in_out_buf_offset;
static int spi_dev_open = 0;
static int spi_nrefs = 0;
static int ch341_index = -1;
static uint8_t ch341_pin_state = 0;
#ifdef SPI_STATS
static struct spi_stats {
long reads, writes;
long read_bytes, write_bytes;
struct timeval tv_xfer_begin, tv_xfer;
struct timeval tv_open_begin, tv_open;
unsigned long spi_clock_max, spi_clock_min;
unsigned long slowdowns;
} spi_stats;
#endif
#define SPI_MAX_PORTS 16
static struct spi_port spi_ports[SPI_MAX_PORTS];
static int spi_nports = 0;
unsigned long spi_clock = 0, spi_max_clock = SPIMAXCLOCK;
static char *spi_err_buf = NULL;
static size_t spi_err_buf_sz = 0;
static struct spi_pins *spi_pins;
static struct spi_pins spi_pin_presets[] = SPI_PIN_PRESETS;
static enum spi_pinouts spi_pinout = SPI_PINOUT_DEFAULT;
void spi_set_err_buf(char *buf, size_t sz)
{
if (buf && sz) {
spi_err_buf = buf;
spi_err_buf_sz = sz;
} else {
spi_err_buf = NULL;
spi_err_buf_sz = 0;
}
}
#define SPI_ERR(...) do { \
LOG(ERR, __VA_ARGS__); \
spi_err(__VA_ARGS__); \
} while (0)
static void spi_err(const char *fmt, ...) {
static char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
if (spi_err_buf) {
strncpy(spi_err_buf, buf, spi_err_buf_sz);
spi_err_buf[spi_err_buf_sz - 1] = '\0';
}
va_end(args);
}
/*
* CH341 transfer data forth and back in synchronous mode
*/
static int spi_ch_xfer(uint8_t *in_out_buf, int size)
{
BOOL rc;
rc = USBIO_BitStreamSPI(ch341_index, size, in_out_buf);
if (!rc) {
SPI_ERR("CH341: write data failed: [%d]", rc);
return -1;
}
return 0;
}
/*
* spi_xfer_*() use global output and input buffers. Output buffer is flushed
* on the following conditions:
* * when buffer becomes full;
* * on the clock change;
* * before read operation in spi_xfer();
* * if the running status of the CPU is requested from spi_xfer_begin();
* * at the closure of CH341 device.
* Read operations are only done in spi_xfer() and spi_xfer_begin(), in other
* situations we may safely discard what was read into the input buffer buffer
* by spi_ch_xfer().
*/
int spi_xfer_begin(int get_status)
{
unsigned int status_offset = 0;
int status;
LOG(DEBUG, "");
if (spi_clock == 0) {
SPI_ERR("SPI clock not initialized");
return -1;
}
#ifdef SPI_STATS
if (gettimeofday(&spi_stats.tv_xfer_begin, NULL) < 0)
LOG(WARN, "gettimeofday failed: %s", strerror(errno));
#endif
/* Check if there is enough space in the buffer */
if (ch341_buf_size - ch341_in_out_buf_offset < 3) {
/* There is no room in the buffer for the following operations, flush
* the buffer */
if (spi_ch_xfer(ch341_in_out_buf, ch341_in_out_buf_offset) < 0)
return -1;
/* The data in the buffer is useless, discard it */
ch341_in_out_buf_offset = 0;
}
/* BlueCore chip SPI port reset sequence: deassert CS, wait at least two
* clock cycles */
ch341_pin_state |= spi_pins->ncs;
ch341_in_out_buf[ch341_in_out_buf_offset++] = ch341_pin_state;
// At least two clock.
ch341_in_out_buf[ch341_in_out_buf_offset++] = ch341_pin_state;
/* Start transfer */
ch341_pin_state &= ~spi_pins->ncs;
if (get_status) {
/*
* Read the stopped status of the CPU. From CSR8645 datasheet: "When
* CSR8645 BGA is deselected (SPI_CS# = 1), the SPI_MISO line does not
* float. Instead, CSR8645 BGA outputs 0 if the processor is running or
* 1 if it is stopped". However in practice this is not entirely true.
* Reading MISO while the CPU is deselected gives wrong result. But
* reading it just after selecting gives the actual status. Also both
* sources I consulted (CsrSpiDrivers and CsrUsbSpiDeviceRE) are
* reading the status after setting CS# to 0.
*/
status_offset = ch341_in_out_buf_offset - 1;
if (spi_ch_xfer(ch341_in_out_buf, ch341_in_out_buf_offset) < 0)
return -1;
if (ch341_in_out_buf[status_offset] & spi_pins->miso)
status = SPI_CPU_STOPPED;
else
status = SPI_CPU_RUNNING;
/* Other data in the buffer is useless, discard it */
ch341_in_out_buf_offset = 0;
return status;
}
return 0;
}
int spi_xfer_end(void)
{
LOG(DEBUG, "");
/* Commit the last ch_pin_state after spi_xfer() */
/* Buffer flush is done on close */
#ifdef SPI_STATS
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0)
LOG(WARN, "gettimeofday failed: %s", strerror(errno));
timersub(&tv, &spi_stats.tv_xfer_begin, &tv);
timeradd(&spi_stats.tv_xfer, &tv, &spi_stats.tv_xfer);
}
#endif
return 0;
}
int spi_xfer(int cmd, int iosize, void *buf, int size)
{
unsigned int write_offset, read_offset, ch341_in_buf_offset;
uint16_t bit, word;
LOG(DEBUG, "(%d, %d, %p, %d)", cmd, iosize, buf, size);
write_offset = 0;
read_offset = 0;
do {
/* In FTDI sync bitbang mode we need to write something to device to
* toggle a read. */
/* The read, if any, will start at current buffer offset */
ch341_in_buf_offset = ch341_in_out_buf_offset;
while (write_offset < size) {
/* 2 bytes per bit */
if (ch341_buf_size - ch341_in_out_buf_offset < iosize) {
/* There is no room in the buffer for following word write,
* flush the buffer */
if (spi_ch_xfer(ch341_in_out_buf, ch341_in_out_buf_offset) < 0)
return -1;
ch341_in_out_buf_offset = 0;
/* Let following part to parse from ftdi_in_buf if needed */
break;
}
if (iosize == 8)
word = ((uint8_t *)buf)[write_offset];
else
word = ((uint16_t *)buf)[write_offset];
/* MOSI is sensed by BlueCore on the rising edge of CLK, MISO is
* changed on the falling edge of CLK. */
for (bit = (1 << (iosize - 1)); bit != 0; bit >>= 1) {
if (cmd & SPI_XFER_WRITE) {
/* Set output bit */
if (word & bit)
ch341_pin_state |= spi_pins->mosi;
else
ch341_pin_state &= ~spi_pins->mosi;
} else {
/* Write 0 during a read */
ch341_pin_state &= ~spi_pins->mosi;
}
ch341_in_out_buf[ch341_in_out_buf_offset++] = ch341_pin_state;
}
write_offset++;
}
if (cmd & SPI_XFER_READ) {
if (ch341_in_out_buf_offset) {
if (spi_ch_xfer(ch341_in_out_buf, ch341_in_out_buf_offset) < 0)
return -1;
ch341_in_out_buf_offset = 0;
}
while (read_offset < write_offset) {
word = 0;
for (bit = (1 << (iosize - 1)); bit != 0; bit >>= 1) {
/* Input bit */
if (ch341_in_out_buf[ch341_in_buf_offset] & spi_pins->miso)
word |= bit;
ch341_in_buf_offset++;
}
if (iosize == 8)
((uint8_t *)buf)[read_offset] = (uint8_t)word;
else
((uint16_t *)buf)[read_offset] = word;
read_offset++;
}
/* Reading done, reset buffer */
ch341_in_out_buf_offset = 0;
ch341_in_buf_offset = 0;
}
} while (write_offset < size);
#ifdef SPI_STATS
if (cmd & SPI_XFER_WRITE) {
spi_stats.writes++;
spi_stats.write_bytes += size * iosize / 8;
} else {
spi_stats.reads++;
spi_stats.read_bytes += size * iosize / 8;
}
#endif
return size;
}
/* Fills spi_ports array with discovered devices, sets spi_nports */
static int spi_enumerate_ports(void)
{
int index;
const char* name = NULL;
spi_nports = 0;
for (index = 0; (name = USBIO_GetDeviceName(index)) && spi_nports < SPI_MAX_PORTS; index++) {
spi_ports[spi_nports].ch341_index = index;
strcpy(spi_ports[spi_nports].name, name);
LOG(INFO, "Found device: name=\"%s\", id=0x%04x", spi_ports[spi_nports].name, spi_ports[spi_nports].ch341_index);
spi_nports++;
}
return 0;
}
int spi_init(void)
{
LOG(DEBUG, "spi_nrefs=%d, spi_dev_open=%d", spi_nrefs, spi_dev_open);
spi_nrefs++;
if (spi_nrefs > 1) {
LOG(WARN, "Superfluos call to spi_init()");
return 0;
}
if (spi_enumerate_ports() < 0) {
spi_deinit();
return -1;
}
spi_pins = &spi_pin_presets[spi_pinout];
return 0;
}
int spi_get_port_list(struct spi_port **pportlist, int *pnports)
{
if (spi_nrefs < 1) {
SPI_ERR("CH341: spi not initialized");
return -1;
}
if (pportlist)
*pportlist = spi_ports;
if (pnports)
*pnports = spi_nports;
return 0;
}
int spi_deinit(void)
{
LOG(DEBUG, "spi_nrefs=%d, spi_dev_open=%d", spi_nrefs, spi_dev_open);
if (spi_nrefs) {
if (spi_dev_open)
if (spi_close() < 0)
return -1;
spi_nrefs = 0;
}
return 0;
}
int spi_set_clock(unsigned long spi_clk) {
uint16_t mode_word = 0x81;
BOOL rc;
LOG(DEBUG, "(%lu)", spi_clk);
if (!spi_isopen()) {
SPI_ERR("CH341: setting SPI clock failed: SPI device is not open");
return -1;
}
if (spi_clk >= spi_max_clock) {
mode_word = 0x83;
spi_clk = spi_max_clock;
} else if (spi_clk >= 400) {
spi_clk = 400;
mode_word = 0x82;
} else if (spi_clk >= 100) {
spi_clk = 100;
mode_word = 0x81;
} else {
spi_clk = 20;
mode_word = 0x80;
}
spi_clock = spi_clk;
LOG(INFO, "CH341: setting SPI clock to %lu (CH341 mode %lu)", spi_clk, mode_word);
rc = USBIO_SetStream(ch341_index, mode_word);
if (!rc) {
SPI_ERR("CH341: set baudrate %lu failed.", mode_word);
return -1;
}
#ifdef SPI_STATS
if (spi_stats.spi_clock_max == 0)
spi_stats.spi_clock_max = spi_max_clock;
if (spi_stats.spi_clock_min == 0)
spi_stats.spi_clock_min = spi_max_clock;
/* Don't account for slow cmds, that are executing at 20 kHz,
* they are short and not representative */
if (spi_clock > 20 && spi_clock < spi_stats.spi_clock_min)
spi_stats.spi_clock_min = spi_clock;
#endif
return 0;
}
void spi_set_max_clock(unsigned long clk) {
LOG(INFO, "CH341: setting SPI max clock: %lu", clk);
spi_max_clock = clk;
}
int spi_clock_slowdown(void) {
unsigned long clk = spi_clock;
/* Slow SPI clock down by 1.5 */
if (clk >= 750) {
clk = 400;
} else if (clk >= 400) {
clk = 100;
} else if (clk >= 100) {
clk = 20;
} else {
// No way to slow down.
spi_clock = 20;
}
#ifdef SPI_STATS
spi_stats.slowdowns++;
#endif
LOG(INFO, "CH341: SPI clock slowdown, set SPI clock to %lu", clk);
return spi_set_clock(clk);
}
unsigned long spi_get_max_clock(void) {
return spi_max_clock;
}
unsigned long spi_get_clock(void) {
return spi_clock;
}
int spi_open(int nport)
{
BOOL rc;
uint8_t output_pins;
LOG(DEBUG, "(%d) spi_dev_open=%d", nport, spi_dev_open);
if (spi_dev_open > 0) {
LOG(WARN, "Superfluos call to spi_open()");
return 0;
}
if (spi_nports == 0 || nport < spi_nports - 1) {
SPI_ERR("No CH341 device found");
goto open_err;
}
#ifdef SPI_STATS
memset(&spi_stats, 0, sizeof(spi_stats));
if (gettimeofday(&spi_stats.tv_open_begin, NULL) < 0)
LOG(WARN, "gettimeofday failed: %s", strerror(errno));
#endif
if (!USBIO_OpenDevice(spi_ports[nport].ch341_index)) {
SPI_ERR("CH341: USBIO_OpenDevice() failed");
goto open_err;
}
rc = USBIO_SetStream(spi_ports[nport].ch341_index, 0x83);
if (!rc)
{
SPI_ERR("CH341: USBIO_SetStream() failed.");
goto open_err;
}
spi_dev_open++;
LOG(INFO, "CH341: using CH341 device: \"%s\"", spi_ports[nport].name);
ch341_index = spi_ports[nport].ch341_index;
/* Set pins direction */
output_pins = spi_pins->mosi | spi_pins->clk | spi_pins->ncs;
/* Set initial pin state: CS high, MISO high as pullup, MOSI and CLK low, LEDs off */
rc = USBIO_Set_D5_D0(spi_ports[nport].ch341_index, output_pins, spi_pins->ncs | spi_pins->miso);
if (rc < 0) {
SPI_ERR("CH341: set synchronous bitbang mode failed.");
goto open_err;
}
/* Initialize xfer buffers */
ch341_buf_size = 896;
ch341_in_out_buf = malloc(ch341_buf_size);
if (ch341_in_out_buf == NULL) {
SPI_ERR("Not enough memory");
goto open_err;
}
ch341_in_out_buf_offset = 0;
return 0;
open_err:
if (spi_dev_open > 0)
USBIO_CloseDevice(spi_ports[nport].ch341_index);
spi_dev_open = 0;
return -1;
}
int spi_isopen(void)
{
return spi_dev_open ? 1 : 0;
}
#ifdef SPI_STATS
void spi_output_stats(void)
{
double xfer_pct = NAN, avg_read = NAN, avg_write = NAN, rate = NAN, iops = NAN;
struct timeval tv;
long inxfer_ms;
FILE *fp;
fp = log_get_dest();
if (!fp)
return;
/* Calculate timeranges until now */
if (gettimeofday(&tv, NULL) < 0)
LOG(WARN, "gettimeofday failed: %s", strerror(errno));
timersub(&tv, &spi_stats.tv_open_begin, &tv);
timeradd(&spi_stats.tv_open, &tv, &spi_stats.tv_open);
xfer_pct = avg_read = avg_write = rate = iops = NAN;
if (spi_stats.tv_open.tv_sec || spi_stats.tv_open.tv_usec) {
xfer_pct = (spi_stats.tv_xfer.tv_sec * 1000 + spi_stats.tv_xfer.tv_usec / 1000);
xfer_pct *= 100;
xfer_pct /= (spi_stats.tv_open.tv_sec * 1000 + spi_stats.tv_open.tv_usec / 1000);
}
if (spi_stats.reads) {
avg_read = spi_stats.read_bytes;
avg_read /= spi_stats.reads;
}
if (spi_stats.writes) {
avg_write = spi_stats.write_bytes;
avg_write /= spi_stats.writes;
}
inxfer_ms = spi_stats.tv_xfer.tv_sec * 1000 + spi_stats.tv_xfer.tv_usec / 1000;
if (inxfer_ms > 0) {
rate = ((spi_stats.read_bytes + spi_stats.write_bytes) * 1000) /
inxfer_ms;
rate /= 1024; /* In KB/s */
iops = ((spi_stats.reads + spi_stats.writes) * 1000) / inxfer_ms;
}
fprintf(fp,
"*** CH341 Statistics ********************************************************\n"
"csr-spi-ch341 version: " VERSION " (git rev " GIT_REVISION ")\n"
"Time open: %ld.%02ld s\n"
"Time in xfer: %ld.%02ld s (%.2f%% of open time)\n"
"Reads: %ld (%ld bytes, %.2f bytes avg read size)\n"
"Writes: %ld (%ld bytes, %.2f bytes avg write size)\n"
"Xfer data rate: %.2f KB/s (%ld bytes in %ld.%02ld s)\n"
"IOPS: %.2f IO/s (%ld IOs in %ld.%02ld s)\n"
"SPI max clock: %lu kHz, min clock: %lu kHz, slowdowns: %lu\n"
"****************************************************************************\n",
spi_stats.tv_open.tv_sec, spi_stats.tv_open.tv_usec / 10000,
spi_stats.tv_xfer.tv_sec, spi_stats.tv_xfer.tv_usec / 10000, xfer_pct,
spi_stats.reads, spi_stats.read_bytes, avg_read,
spi_stats.writes, spi_stats.write_bytes, avg_write,
rate, spi_stats.read_bytes + spi_stats.write_bytes,
spi_stats.tv_xfer.tv_sec, spi_stats.tv_xfer.tv_usec / 10000,
iops, spi_stats.reads + spi_stats.writes,
spi_stats.tv_xfer.tv_sec, spi_stats.tv_xfer.tv_usec / 10000,
spi_stats.spi_clock_max, spi_stats.spi_clock_min, spi_stats.slowdowns
);
}
#endif
int spi_close(void)
{
LOG(DEBUG, "spi_nrefs=%d, spi_dev_open=%d", spi_nrefs, spi_dev_open);
if (spi_dev_open) {
/* Flush and reset the buffers */
if (ch341_in_out_buf_offset) {
if (spi_ch_xfer(ch341_in_out_buf, ch341_in_out_buf_offset) < 0)
return -1;
ch341_in_out_buf_offset = 0;
}
USBIO_CloseDevice(ch341_index);
#ifdef SPI_STATS
spi_output_stats();
#endif
free(ch341_in_out_buf);
ch341_in_out_buf = NULL;
ch341_buf_size = 0;
spi_dev_open = 0;
}
return 0;
}