-
Notifications
You must be signed in to change notification settings - Fork 138
/
linuxspi.c
485 lines (399 loc) · 13.2 KB
/
linuxspi.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
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Support for using spidev userspace drivers to communicate directly over SPI
*
* Copyright (C) 2013 Kevin Cuzner <[email protected]>
* Copyright (C) 2018 Ralf Ramsauer <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Support for inversion of reset pin, Tim Chilton 02/05/2014
* Review code, rebase to latest trunk, add linux/gpio.h support, Ralf Ramsauer 2018-09-07
*/
#include <ac_cfg.h>
#include "avrdude.h"
#include "libavrdude.h"
#include "linuxspi.h"
#if HAVE_LINUXSPI
/*
* Linux Kernel SPI Drivers
*
* Copyright (C) 2006 SWAPP
* Andrea Paterniani <[email protected]>
* Copyright (C) 2007 David Brownell (simplification, cleanup)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <linux/gpio.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define LINUXSPI "linuxspi"
// Private data for this programmer
struct pdata {
int disable_no_cs;
int fd_spidev, fd_gpiochip, fd_linehandle;
};
// Use private programmer data as if they were a global structure my
#define my (*(struct pdata *)(pgm->cookie))
/*
* @brief Sends/receives a message in full duplex mode
* @return -1 on failure, otherwise number of bytes sent/received
*/
static int linuxspi_spi_duplex(const PROGRAMMER *pgm, const unsigned char *tx, unsigned char *rx, int len) {
struct spi_ioc_transfer tr;
int ret;
tr = (struct spi_ioc_transfer) {
.tx_buf = (unsigned long) tx,
.rx_buf = (unsigned long) rx,
.len = len,
.delay_usecs = 1,
.speed_hz = 1.0/pgm->bitclock,
.bits_per_word = 8,
};
errno = 0;
ret = ioctl(my.fd_spidev, SPI_IOC_MESSAGE(1), &tr);
if(ret != len) {
int ioctl_errno = errno;
msg_error("\n");
pmsg_error("unable to send SPI message");
if(ioctl_errno)
msg_error("%s", strerror(ioctl_errno));
msg_error("\n");
}
return ret == -1? -1: 0;
}
static void linuxspi_setup(PROGRAMMER *pgm) {
pgm->cookie = mmt_malloc(sizeof(struct pdata));
}
static void linuxspi_teardown(PROGRAMMER *pgm) {
mmt_free(pgm->cookie);
pgm->cookie = NULL;
}
static int linuxspi_reset_mcu(const PROGRAMMER *pgm, bool active) {
struct gpiohandle_data data;
int ret;
/*
* Set the reset state and keep it. The pin will be released and set back to
* its initial value, once the my.fd_gpiochip is closed.
*/
data.values[0] = active ^ !(pgm->pinno[PIN_AVR_RESET] & PIN_INVERSE);
ret = ioctl(my.fd_linehandle, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
#ifdef GPIO_V2_LINE_SET_VALUES_IOCTL
if(ret == -1) {
struct gpio_v2_line_values val;
val.mask = 1;
val.bits = active ^ !(pgm->pinno[PIN_AVR_RESET] & PIN_INVERSE);
ret = ioctl(my.fd_linehandle, GPIO_V2_LINE_SET_VALUES_IOCTL, &val);
}
#endif
if(ret == -1) {
ret = -errno;
pmsg_ext_error("unable to set GPIO line %d value: %s\n", pgm->pinno[PIN_AVR_RESET] & PIN_MASK, strerror(errno));
return ret;
}
return 0;
}
static int linuxspi_open(PROGRAMMER *pgm, const char *pt) {
const char *port_error = "unknown port specification, "
"please use the format /dev/spidev:/dev/gpiochip[:resetno]\n";
char port_default[] = "/dev/spidev0.0:/dev/gpiochip0";
char *spidev, *gpiochip, *reset_pin;
char *port = mmt_strdup(pt);
struct gpiohandle_request req;
int ret;
if(str_eq(port, "unknown")) {
port = port_default;
}
spidev = strtok(port, ":");
if(!spidev) {
pmsg_error("%s", port_error);
return -1;
}
gpiochip = strtok(NULL, ":");
if(!gpiochip) {
pmsg_error("%s", port_error);
return -1;
}
// Optional: override reset pin in configuration
reset_pin = strtok(NULL, ":");
if(reset_pin) {
const char *errstr;
pgm->pinno[PIN_AVR_RESET] = str_int(reset_pin, STR_UINT32, &errstr);
if(errstr) {
pmsg_error("pin number %s: %s", reset_pin, errstr);
return -1;
}
}
pgm->port = port;
my.fd_spidev = open(pgm->port, O_RDWR);
if(my.fd_spidev < 0) {
pmsg_ext_error("unable to open the spidev device %s: %s\n", pgm->port, strerror(errno));
return -1;
}
uint32_t mode = SPI_MODE_0;
if(!my.disable_no_cs)
mode |= SPI_NO_CS;
ret = ioctl(my.fd_spidev, SPI_IOC_WR_MODE32, &mode);
if(ret == -1) {
int ioctl_errno = errno;
pmsg_ext_error("unable to set SPI mode %02X on %s: %s\n", mode, spidev, strerror(errno));
if(ioctl_errno == EINVAL && !my.disable_no_cs)
pmsg_error("try -x disable_no_cs\n");
goto close_spidev;
}
my.fd_gpiochip = open(gpiochip, 0);
if(my.fd_gpiochip < 0) {
pmsg_ext_error("unable to open the gpiochip %s: %s\n", gpiochip, strerror(errno));
ret = -1;
goto close_spidev;
}
strcpy(req.consumer_label, progname);
req.lines = 1;
req.lineoffsets[0] = pgm->pinno[PIN_AVR_RESET] & PIN_MASK;
req.default_values[0] = !!(pgm->pinno[PIN_AVR_RESET] & PIN_INVERSE);
req.flags = GPIOHANDLE_REQUEST_OUTPUT;
ret = ioctl(my.fd_gpiochip, GPIO_GET_LINEHANDLE_IOCTL, &req);
if(ret != -1)
my.fd_linehandle = req.fd;
#ifdef GPIO_V2_GET_LINE_IOCTL
if(ret == -1) {
struct gpio_v2_line_request reqv2;
memset(&reqv2, 0, sizeof(reqv2));
reqv2.offsets[0] = pgm->pinno[PIN_AVR_RESET] & PIN_MASK;
strncpy(reqv2.consumer, progname, sizeof(reqv2.consumer) - 1);
reqv2.config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
reqv2.config.num_attrs = 1;
reqv2.config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES;
reqv2.config.attrs[0].attr.values = !!(pgm->pinno[PIN_AVR_RESET] & PIN_INVERSE);
reqv2.config.attrs[0].mask = 1;
reqv2.num_lines = 1;
ret = ioctl(my.fd_gpiochip, GPIO_V2_GET_LINE_IOCTL, &reqv2);
if(ret != -1)
my.fd_linehandle = reqv2.fd;
}
#endif
if(ret == -1) {
ret = -errno;
pmsg_ext_error("unable to get GPIO line %d. %s\n", pgm->pinno[PIN_AVR_RESET] & PIN_MASK, strerror(errno));
goto close_gpiochip;
}
ret = linuxspi_reset_mcu(pgm, true);
if(ret)
goto close_out;
if(pgm->baudrate != 0) {
pmsg_warning("obsolete use of -b <clock> option for bit clock; use -B <clock>\n");
pgm->bitclock = 1.0/pgm->baudrate;
}
if(pgm->bitclock == 0) {
pmsg_notice("defaulting bit clock to 200 kHz\n");
pgm->bitclock = 5E-6; // 200 kHz - 5 µs
}
return 0;
close_out:
close(my.fd_linehandle);
close_gpiochip:
close(my.fd_gpiochip);
close_spidev:
close(my.fd_spidev);
return ret;
}
static void linuxspi_close(PROGRAMMER *pgm) {
switch(pgm->exit_reset) {
case EXIT_RESET_ENABLED:
linuxspi_reset_mcu(pgm, true);
break;
case EXIT_RESET_DISABLED:
linuxspi_reset_mcu(pgm, false);
break;
default:
break;
}
close(my.fd_linehandle);
close(my.fd_spidev);
close(my.fd_gpiochip);
}
static void linuxspi_disable(const PROGRAMMER *pgm) {
}
static void linuxspi_enable(PROGRAMMER *pgm, const AVRPART *p) {
}
static void linuxspi_display(const PROGRAMMER *pgm, const char *p) {
}
static int linuxspi_initialize(const PROGRAMMER *pgm, const AVRPART *p) {
int tries, ret;
if(is_tpi(p)) {
// We do not support TPI; this is a dedicated SPI thing
pmsg_error("programmer " LINUXSPI " does not support TPI\n");
return -1;
}
// Enable programming on the part
tries = 0;
do {
ret = pgm->program_enable(pgm, p);
if(ret == 0 || ret == -1)
break;
} while(tries++ < 65);
if(ret)
pmsg_error("AVR device not responding\n");
return ret;
}
static int linuxspi_cmd(const PROGRAMMER *pgm, const unsigned char *cmd, unsigned char *res) {
return linuxspi_spi_duplex(pgm, cmd, res, 4);
}
static int linuxspi_program_enable(const PROGRAMMER *pgm, const AVRPART *p) {
unsigned char cmd[4], res[4];
if(!p->op[AVR_OP_PGM_ENABLE]) {
pmsg_error("program enable instruction not defined for part %s\n", p->desc);
return -1;
}
memset(cmd, 0, sizeof(cmd));
avr_set_bits(p->op[AVR_OP_PGM_ENABLE], cmd); // Set the cmd
pgm->cmd(pgm, cmd, res);
if(res[2] != cmd[1]) {
/*
* From ATtiny441 datasheet:
*
* In some systems, the programmer can not guarantee that SCK is held low
* during power-up. In this case, RESET must be given a positive pulse
* after SCK has been set to '0'. The duration of the pulse must be at
* least t RST plus two CPU clock cycles. See Table 25-5 on page 240 for
* definition of minimum pulse width on RESET pin, t RST 2. Wait for at
* least 20 ms and then enable serial programming by sending the
* Programming Enable serial instruction to the SDO pin 3. The serial
* programming instructions will not work if the communication is out of
* synchronization. When in sync, the second byte (0x53) will echo back
* when issuing the third byte of the Programming Enable instruction ... If
* the 0x53 did not echo back, give RESET a positive pulse and issue a new
* Programming Enable command
*/
if(linuxspi_reset_mcu(pgm, false))
return -1;
usleep(5);
if(linuxspi_reset_mcu(pgm, true))
return -1;
usleep(20000);
return -2;
}
return 0;
}
static int linuxspi_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) {
unsigned char cmd[4], res[4];
if(!p->op[AVR_OP_CHIP_ERASE]) {
pmsg_error("chip erase instruction not defined for part %s\n", p->desc);
return -1;
}
memset(cmd, 0, sizeof(cmd));
avr_set_bits(p->op[AVR_OP_CHIP_ERASE], cmd);
pgm->cmd(pgm, cmd, res);
usleep(p->chip_erase_delay);
pgm->initialize(pgm, p);
return 0;
}
static int linuxspi_parseexitspecs(PROGRAMMER *pgm, const char *sp) {
char *cp, *s, *str = mmt_strdup(sp);
int rv = 0;
bool help = false;
s = str;
while((cp = strtok(s, ","))) {
s = NULL;
if(str_eq(cp, "reset")) {
pgm->exit_reset = EXIT_RESET_ENABLED;
continue;
}
if(str_eq(cp, "noreset")) {
pgm->exit_reset = EXIT_RESET_DISABLED;
continue;
}
if(str_eq(cp, "help")) {
help = true;
rv = LIBAVRDUDE_EXIT;
}
if(!help) {
pmsg_error("invalid exitspec parameter -E %s\n", cp);
rv = -1;
}
msg_error("%s -c %s exitspec parameter options:\n", progname, pgmid);
msg_error(" -E reset Programmer will keep the reset line low after programming session\n");
msg_error(" -E noreset Programmer will not keep the reset line low after programming session\n");
msg_error(" -E help Show this help menu and exit\n");
mmt_free(str);
return rv;
}
mmt_free(str);
return rv;
}
static int linuxspi_parseextparams(const PROGRAMMER *pgm, const LISTID extparms) {
int rc = 0;
bool help = false;
for(LNODEID ln = lfirst(extparms); ln; ln = lnext(ln)) {
const char *extended_param = ldata(ln);
if(str_eq(extended_param, "disable_no_cs")) {
my.disable_no_cs = 1;
continue;
}
if(str_eq(extended_param, "help")) {
help = true;
rc = LIBAVRDUDE_EXIT;
}
if(!help) {
pmsg_error("invalid extended parameter -x %s\n", extended_param);
rc = -1;
}
msg_error("%s -c %s extended options:\n", progname, pgmid);
msg_error(" -x disable_no_cs Do not use the SPI_NO_CS bit for the SPI driver\n");
msg_error(" -x help Show this help menu and exit\n");
return rc;
}
return rc;
}
void linuxspi_initpgm(PROGRAMMER *pgm) {
strcpy(pgm->type, LINUXSPI);
pgm_fill_old_pins(pgm); // TODO to be removed if old pin data no longer needed
// Mandatory functions
pgm->initialize = linuxspi_initialize;
pgm->display = linuxspi_display;
pgm->enable = linuxspi_enable;
pgm->disable = linuxspi_disable;
pgm->program_enable = linuxspi_program_enable;
pgm->chip_erase = linuxspi_chip_erase;
pgm->cmd = linuxspi_cmd;
pgm->open = linuxspi_open;
pgm->close = linuxspi_close;
pgm->read_byte = avr_read_byte_default;
pgm->write_byte = avr_write_byte_default;
// Optional functions
pgm->setup = linuxspi_setup;
pgm->teardown = linuxspi_teardown;
pgm->parseexitspecs = linuxspi_parseexitspecs;
pgm->parseextparams = linuxspi_parseextparams;
}
const char linuxspi_desc[] = "SPI using Linux spidev driver";
#else // ! HAVE_LINUXSPI
void linuxspi_initpgm(PROGRAMMER *pgm) {
pmsg_error("Linux SPI driver not available in this configuration\n");
}
const char linuxspi_desc[] = "SPI using Linux spidev driver (not available)";
#endif // HAVE_LINUXSPI