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$ */
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 (void)p;
45
46 /* Signature byte reads are always 3 bytes. */
47
48 if (m->size < 3) {
49 avrdude_message(MSG_INFO, "%s: memsize too small for sig byte read", progname);
50 return -1;
51 }
52
53 buf[0] = Cmnd_STK_READ_SIGN;
54 buf[1] = Sync_CRC_EOP;
55
56 serial_send(&pgm->fd, buf, 2);
57
58 if (serial_recv(&pgm->fd, buf, 5) < 0)
59 return -1;
60 if (buf[0] == Resp_STK_NOSYNC) {
61 avrdude_message(MSG_INFO, "%s: stk500_cmd(): programmer is out of sync\n",
62 progname);
63 return -1;
64 } else if (buf[0] != Resp_STK_INSYNC) {
65 avrdude_message(MSG_INFO, "\n%s: arduino_read_sig_bytes(): (a) protocol error, "
66 "expect=0x%02x, resp=0x%02x\n",
67 progname, Resp_STK_INSYNC, buf[0]);
68 return -2;
69 }
70 if (buf[4] != Resp_STK_OK) {
71 avrdude_message(MSG_INFO, "\n%s: arduino_read_sig_bytes(): (a) protocol error, "
72 "expect=0x%02x, resp=0x%02x\n",
73 progname, Resp_STK_OK, buf[4]);
74 return -3;
75 }
76
77 m->buf[0] = buf[1];
78 m->buf[1] = buf[2];
79 m->buf[2] = buf[3];
80
81 return 3;
82 }
83
prusa_init_external_flash(PROGRAMMER * pgm)84 static int prusa_init_external_flash(PROGRAMMER * pgm)
85 {
86 // Note: send/receive as in _the firmare_ send & receives
87 const char entry_magic_send[] = "start\n";
88 const unsigned char entry_magic_receive[] = "w25x20cl_enter\n";
89 const char entry_magic_cfm[] = "w25x20cl_cfm\n";
90 const size_t buffer_len = 32; // Should be large enough for the above messages
91
92 int res;
93 size_t recv_size;
94 char *buffer = alloca(buffer_len);
95
96 // 1. receive the "start" command
97 recv_size = sizeof(entry_magic_send) - 1;
98 res = serial_recv(&pgm->fd, (unsigned char *)buffer, recv_size);
99 if (res < 0) {
100 avrdude_message(MSG_INFO, "%s: prusa_init_external_flash(): MK3 printer did not boot up on time or serial communication failed\n", progname);
101 return -1;
102 } else if (strncmp(buffer, entry_magic_send, recv_size) != 0) {
103 avrdude_message(MSG_INFO, "%s: prusa_init_external_flash(): MK3 printer emitted incorrect start code: `%*s`\n", progname, recv_size, buffer);
104 return -1;
105 }
106
107 // 2. Send the external flash programmer enter command
108 if (serial_send(&pgm->fd, entry_magic_receive, sizeof(entry_magic_receive) - 1) < 0) {
109 avrdude_message(MSG_INFO, "%s: prusa_init_external_flash(): Failed to send command to the printer\n",progname);
110 return -1;
111 }
112
113 // 3. Receive the entry confirmation command
114 recv_size = sizeof(entry_magic_cfm) - 1;
115 res = serial_recv(&pgm->fd, (unsigned char *)buffer, recv_size);
116 if (res < 0) {
117 avrdude_message(MSG_INFO, "%s: prusa_init_external_flash(): MK3 printer did not boot up on time or serial communication failed\n", progname);
118 return -1;
119 } else if (strncmp(buffer, entry_magic_cfm, recv_size) != 0) {
120 avrdude_message(MSG_INFO, "%s: prusa_init_external_flash(): MK3 printer emitted incorrect cfm code: `%*s`\n", progname, recv_size, buffer);
121 return -1;
122 }
123
124 return 0;
125 }
126
arduino_open(PROGRAMMER * pgm,char * port)127 static int arduino_open(PROGRAMMER * pgm, char * port)
128 {
129 union pinfo pinfo;
130 strcpy(pgm->port, port);
131 pinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
132 if (serial_open(port, pinfo, &pgm->fd)==-1) {
133 return -1;
134 }
135
136 /* Clear DTR and RTS to unload the RESET capacitor
137 * (for example in Arduino) */
138 serial_set_dtr_rts(&pgm->fd, 0);
139 usleep(250*1000);
140 /* Set DTR and RTS back to high */
141 serial_set_dtr_rts(&pgm->fd, 1);
142 usleep(50*1000);
143
144 // Sometimes there may be line noise generating input on the printer's USB-to-serial IC
145 // Here we try to clean its input buffer with a sequence of newlines (a minimum of 9 is needed):
146 const unsigned char cleanup_newlines[] = "\n\n\n\n\n\n\n\n\n\n";
147 if (serial_send(&pgm->fd, cleanup_newlines, sizeof(cleanup_newlines) - 1) < 0) {
148 return -1;
149 }
150
151 /*
152 * drain any extraneous input
153 */
154 stk500_drain(pgm, 0);
155
156 // Initialization sequence for programming the external FLASH on the Prusa MK3
157 if (prusa_init_external_flash(pgm) < 0) {
158 avrdude_message(MSG_INFO, "%s: arduino_open(): Failed to initialize MK3 external flash programming mode\n", progname);
159 return -1;
160 }
161
162 if (stk500_getsync(pgm) < 0) {
163 serial_close(&pgm->fd);
164 return -1;
165 }
166
167 return 0;
168 }
169
arduino_close(PROGRAMMER * pgm)170 static void arduino_close(PROGRAMMER * pgm)
171 {
172 serial_set_dtr_rts(&pgm->fd, 0);
173 serial_close(&pgm->fd);
174 pgm->fd.ifd = -1;
175 }
176
177 const char arduino_desc[] = "Arduino programmer";
178
arduino_initpgm(PROGRAMMER * pgm)179 void arduino_initpgm(PROGRAMMER * pgm)
180 {
181 /* This is mostly a STK500; just the signature is read
182 differently than on real STK500v1
183 and the DTR signal is set when opening the serial port
184 for the Auto-Reset feature */
185 stk500_initpgm(pgm);
186
187 strcpy(pgm->type, "Arduino");
188 pgm->read_sig_bytes = arduino_read_sig_bytes;
189 pgm->open = arduino_open;
190 pgm->close = arduino_close;
191 }
192