1 /*
2 * avrdude - A Downloader/Uploader for AVR device programmers
3 * Copyright (C) 2009 Lars Immisch
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* $Id: arduino.c 1321 2014-06-13 20:07:40Z awachtler $ */
20
21 /*
22 * avrdude interface for Arduino programmer
23 *
24 * The Arduino programmer is mostly a STK500v1, just the signature bytes
25 * are read differently.
26 */
27
28 #include "ac_cfg.h"
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include "avrdude.h"
35 #include "libavrdude.h"
36 #include "stk500_private.h"
37 #include "stk500.h"
38 #include "arduino.h"
39
40 /* read signature bytes - arduino version */
arduino_read_sig_bytes(PROGRAMMER * pgm,AVRPART * p,AVRMEM * m)41 static int arduino_read_sig_bytes(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m)
42 {
43 unsigned char buf[32];
44
45 /* Signature byte reads are always 3 bytes. */
46
47 if (m->size < 3) {
48 avrdude_message(MSG_INFO, "%s: memsize too small for sig byte read", progname);
49 return -1;
50 }
51
52 buf[0] = Cmnd_STK_READ_SIGN;
53 buf[1] = Sync_CRC_EOP;
54
55 serial_send(&pgm->fd, buf, 2);
56
57 if (serial_recv(&pgm->fd, buf, 5) < 0)
58 return -1;
59 if (buf[0] == Resp_STK_NOSYNC) {
60 avrdude_message(MSG_INFO, "%s: stk500_cmd(): programmer is out of sync\n",
61 progname);
62 return -1;
63 } else if (buf[0] != Resp_STK_INSYNC) {
64 avrdude_message(MSG_INFO, "\n%s: arduino_read_sig_bytes(): (a) protocol error, "
65 "expect=0x%02x, resp=0x%02x\n",
66 progname, Resp_STK_INSYNC, buf[0]);
67 return -2;
68 }
69 if (buf[4] != Resp_STK_OK) {
70 avrdude_message(MSG_INFO, "\n%s: arduino_read_sig_bytes(): (a) protocol error, "
71 "expect=0x%02x, resp=0x%02x\n",
72 progname, Resp_STK_OK, buf[4]);
73 return -3;
74 }
75
76 m->buf[0] = buf[1];
77 m->buf[1] = buf[2];
78 m->buf[2] = buf[3];
79
80 return 3;
81 }
82
arduino_open(PROGRAMMER * pgm,char * port)83 static int arduino_open(PROGRAMMER * pgm, char * port)
84 {
85 union pinfo pinfo;
86 strcpy(pgm->port, port);
87 pinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
88 if (serial_open(port, pinfo, &pgm->fd)==-1) {
89 return -1;
90 }
91
92 /* Clear DTR and RTS to unload the RESET capacitor
93 * (for example in Arduino) */
94 serial_set_dtr_rts(&pgm->fd, 0);
95 usleep(250*1000);
96 /* Set DTR and RTS back to high */
97 serial_set_dtr_rts(&pgm->fd, 1);
98 usleep(50*1000);
99
100 /*
101 * drain any extraneous input
102 */
103 stk500_drain(pgm, 0);
104
105 if (stk500_getsync(pgm) < 0)
106 return -1;
107
108 return 0;
109 }
110
arduino_close(PROGRAMMER * pgm)111 static void arduino_close(PROGRAMMER * pgm)
112 {
113 serial_set_dtr_rts(&pgm->fd, 0);
114 serial_close(&pgm->fd);
115 pgm->fd.ifd = -1;
116 }
117
118 const char arduino_desc[] = "Arduino programmer";
119
arduino_initpgm(PROGRAMMER * pgm)120 void arduino_initpgm(PROGRAMMER * pgm)
121 {
122 /* This is mostly a STK500; just the signature is read
123 differently than on real STK500v1
124 and the DTR signal is set when opening the serial port
125 for the Auto-Reset feature */
126 stk500_initpgm(pgm);
127
128 strcpy(pgm->type, "Arduino");
129 pgm->read_sig_bytes = arduino_read_sig_bytes;
130 pgm->open = arduino_open;
131 pgm->close = arduino_close;
132 }
133