Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
arduino.c File Reference
#include "ac_cfg.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "avrdude.h"
#include "libavrdude.h"
#include "stk500_private.h"
#include "stk500.h"
#include "arduino.h"
+ Include dependency graph for arduino.c:

Go to the source code of this file.

Functions

static int arduino_read_sig_bytes (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m)
 
static int prusa_init_external_flash (PROGRAMMER *pgm)
 
static int arduino_open (PROGRAMMER *pgm, char *port)
 
static void arduino_close (PROGRAMMER *pgm)
 
void arduino_initpgm (PROGRAMMER *pgm)
 

Variables

const char arduino_desc [] = "Arduino programmer"
 

Function Documentation

◆ arduino_close()

static void arduino_close ( PROGRAMMER pgm)
static
171{
174 pgm->fd.ifd = -1;
175}
#define serial_close
Definition libavrdude.h:576
#define serial_set_dtr_rts
Definition libavrdude.h:580
int ifd
Definition libavrdude.h:522
static PROGRAMMER * pgm
Definition main.c:192
union filedescriptor fd
Definition libavrdude.h:637

References programmer_t::fd, filedescriptor::ifd, pgm, serial_close, and serial_set_dtr_rts.

Referenced by arduino_initpgm().

+ Here is the caller graph for this function:

◆ arduino_initpgm()

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 */
186
187 strcpy(pgm->type, "Arduino");
191}
static int arduino_read_sig_bytes(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m)
Definition arduino.c:41
static void arduino_close(PROGRAMMER *pgm)
Definition arduino.c:170
static int arduino_open(PROGRAMMER *pgm, char *port)
Definition arduino.c:127
void stk500_initpgm(PROGRAMMER *pgm)
Definition stk500.c:1321
int(* read_sig_bytes)(struct programmer_t *pgm, AVRPART *p, AVRMEM *m)
Definition libavrdude.h:672
int(* open)(struct programmer_t *pgm, char *port)
Definition libavrdude.h:657
char type[PGM_TYPELEN]
Definition libavrdude.h:619
void(* close)(struct programmer_t *pgm)
Definition libavrdude.h:658

References arduino_close(), arduino_open(), arduino_read_sig_bytes(), programmer_t::close, programmer_t::open, pgm, programmer_t::read_sig_bytes, stk500_initpgm(), and programmer_t::type.

+ Here is the call graph for this function:

◆ arduino_open()

static int arduino_open ( PROGRAMMER pgm,
char *  port 
)
static
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) */
139 usleep(250*1000);
140 /* Set DTR and RTS back to high */
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
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) {
164 return -1;
165 }
166
167 return 0;
168}
static int prusa_init_external_flash(PROGRAMMER *pgm)
Definition arduino.c:84
#define MSG_INFO
Definition avrdude.h:51
char * progname
Definition main.c:61
int avrdude_message(const int msglvl, const char *format,...)
Definition main.c:93
#define serial_open
Definition libavrdude.h:574
#define serial_send
Definition libavrdude.h:577
long baud
Definition libavrdude.h:537
Definition libavrdude.h:536
int stk500_drain(PROGRAMMER *pgm, int display)
Definition stk500.c:82
int stk500_getsync(PROGRAMMER *pgm)
Definition stk500.c:88
char port[PGM_PORTLEN]
Definition libavrdude.h:620
int baudrate
Definition libavrdude.h:630
int usleep(unsigned usec)
Definition unistd.cpp:13

References avrdude_message(), pinfo::baud, programmer_t::baudrate, programmer_t::fd, MSG_INFO, pgm, programmer_t::port, progname, prusa_init_external_flash(), serial_close, serial_open, serial_send, serial_set_dtr_rts, stk500_drain(), stk500_getsync(), and usleep().

Referenced by arduino_initpgm().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ arduino_read_sig_bytes()

static int arduino_read_sig_bytes ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m 
)
static
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}
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
int size
Definition libavrdude.h:286
#define serial_recv
Definition libavrdude.h:578
unsigned char * buf
Definition libavrdude.h:304
#define Sync_CRC_EOP
Definition stk500_private.h:32
#define Resp_STK_INSYNC
Definition stk500_private.h:22
#define Resp_STK_NOSYNC
Definition stk500_private.h:23
#define Cmnd_STK_READ_SIGN
Definition stk500_private.h:64
#define Resp_STK_OK
Definition stk500_private.h:18

References avrdude_message(), avrmem::buf, Cmnd_STK_READ_SIGN, programmer_t::fd, MSG_INFO, pgm, progname, Resp_STK_INSYNC, Resp_STK_NOSYNC, Resp_STK_OK, serial_recv, serial_send, avrmem::size, Sync_CRC_EOP, and void().

Referenced by arduino_initpgm().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ prusa_init_external_flash()

static int prusa_init_external_flash ( PROGRAMMER pgm)
static
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}

References avrdude_message(), programmer_t::fd, MSG_INFO, pgm, progname, serial_recv, and serial_send.

Referenced by arduino_open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ arduino_desc

const char arduino_desc[] = "Arduino programmer"