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

Go to the source code of this file.

Classes

struct  pdata
 

Macros

#define STK500_XTAL   7372800U
 
#define MAX_SYNC_ATTEMPTS   10
 
#define PDATA(pgm)   ((struct pdata *)(pgm->cookie))
 

Functions

static int stk500_getparm (PROGRAMMER *pgm, unsigned parm, unsigned *value)
 
static int stk500_setparm (PROGRAMMER *pgm, unsigned parm, unsigned value)
 
static void stk500_print_parms1 (PROGRAMMER *pgm, const char *p)
 
static int stk500_send (PROGRAMMER *pgm, unsigned char *buf, size_t len)
 
static int stk500_recv (PROGRAMMER *pgm, unsigned char *buf, size_t len)
 
int stk500_drain (PROGRAMMER *pgm, int display)
 
int stk500_getsync (PROGRAMMER *pgm)
 
static int stk500_cmd (PROGRAMMER *pgm, const unsigned char *cmd, unsigned char *res)
 
static int stk500_chip_erase (PROGRAMMER *pgm, AVRPART *p)
 
static int stk500_program_enable (PROGRAMMER *pgm, AVRPART *p)
 
static int stk500_set_extended_parms (PROGRAMMER *pgm, int n, unsigned char *cmd)
 
static int mib510_isp (PROGRAMMER *pgm, unsigned char cmd)
 
static int stk500_initialize (PROGRAMMER *pgm, AVRPART *p)
 
static void stk500_disable (PROGRAMMER *pgm)
 
static void stk500_enable (PROGRAMMER *pgm)
 
static int stk500_open (PROGRAMMER *pgm, char *port)
 
static void stk500_close (PROGRAMMER *pgm)
 
static int stk500_loadaddr (PROGRAMMER *pgm, AVRMEM *mem, unsigned int addr)
 
static int stk500_paged_write (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
 
static int stk500_paged_load (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
 
static int stk500_set_vtarget (PROGRAMMER *pgm, double v)
 
static int stk500_set_varef (PROGRAMMER *pgm, unsigned int chan, double v)
 
static int stk500_set_fosc (PROGRAMMER *pgm, double v)
 
static int stk500_set_sck_period (PROGRAMMER *pgm, double v)
 
static void stk500_display (PROGRAMMER *pgm, const char *p)
 
static void stk500_print_parms (PROGRAMMER *pgm)
 
static void stk500_setup (PROGRAMMER *pgm)
 
static void stk500_teardown (PROGRAMMER *pgm)
 
void stk500_initpgm (PROGRAMMER *pgm)
 

Variables

const char stk500_desc [] = "Atmel STK500 Version 1.x firmware"
 

Macro Definition Documentation

◆ MAX_SYNC_ATTEMPTS

#define MAX_SYNC_ATTEMPTS   10

◆ PDATA

#define PDATA (   pgm)    ((struct pdata *)(pgm->cookie))

◆ STK500_XTAL

#define STK500_XTAL   7372800U

Function Documentation

◆ mib510_isp()

static int mib510_isp ( PROGRAMMER pgm,
unsigned char  cmd 
)
static
343{
344 unsigned char buf[9];
345 int tries = 0;
346
347 buf[0] = 0xaa;
348 buf[1] = 0x55;
349 buf[2] = 0x55;
350 buf[3] = 0xaa;
351 buf[4] = 0x17;
352 buf[5] = 0x51;
353 buf[6] = 0x31;
354 buf[7] = 0x13;
355 buf[8] = cmd;
356
357
358 retry:
359
360 tries++;
361
362 stk500_send(pgm, buf, 9);
363 if (stk500_recv(pgm, buf, 1) < 0)
364 return -1;
365 if (buf[0] == Resp_STK_NOSYNC) {
366 if (tries > 33) {
367 avrdude_message(MSG_INFO, "%s: mib510_isp(): can't get into sync\n",
368 progname);
369 return -1;
370 }
371 if (stk500_getsync(pgm) < 0)
372 return -1;
373 goto retry;
374 }
375 else if (buf[0] != Resp_STK_INSYNC) {
376 avrdude_message(MSG_INFO, "%s: mib510_isp(): protocol error, "
377 "expect=0x%02x, resp=0x%02x\n",
378 progname, Resp_STK_INSYNC, buf[0]);
379 return -1;
380 }
381
382 if (stk500_recv(pgm, buf, 1) < 0)
383 return -1;
384 if (buf[0] == Resp_STK_OK) {
385 return 0;
386 }
387 else if (buf[0] == Resp_STK_NODEVICE) {
388 avrdude_message(MSG_INFO, "%s: mib510_isp(): no device\n",
389 progname);
390 return -1;
391 }
392
393 if (buf[0] == Resp_STK_FAILED)
394 {
395 avrdude_message(MSG_INFO, "%s: mib510_isp(): command %d failed\n",
396 progname, cmd);
397 return -1;
398 }
399
400
401 avrdude_message(MSG_INFO, "%s: mib510_isp(): unknown response=0x%02x\n",
402 progname, buf[0]);
403
404 return -1;
405}
#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
static PROGRAMMER * pgm
Definition main.c:192
static int stk500_send(PROGRAMMER *pgm, unsigned char *buf, size_t len)
Definition stk500.c:62
static int stk500_recv(PROGRAMMER *pgm, unsigned char *buf, size_t len)
Definition stk500.c:68
int stk500_getsync(PROGRAMMER *pgm)
Definition stk500.c:88
#define Resp_STK_INSYNC
Definition stk500_private.h:22
#define Resp_STK_NOSYNC
Definition stk500_private.h:23
#define Resp_STK_NODEVICE
Definition stk500_private.h:21
#define Resp_STK_FAILED
Definition stk500_private.h:19
#define Resp_STK_OK
Definition stk500_private.h:18
struct command cmd[]
Definition term.c:94

References avrdude_message(), cmd, MSG_INFO, pgm, progname, Resp_STK_FAILED, Resp_STK_INSYNC, Resp_STK_NODEVICE, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_recv(), and stk500_send().

Referenced by stk500_close(), and stk500_open().

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

◆ stk500_chip_erase()

static int stk500_chip_erase ( PROGRAMMER pgm,
AVRPART p 
)
static
181{
182 unsigned char cmd[4];
183 unsigned char res[4];
184
185 if (pgm->cmd == NULL) {
186 avrdude_message(MSG_INFO, "%s: Error: %s programmer uses stk500_chip_erase() but does not\n"
187 "provide a cmd() method.\n",
188 progname, pgm->type);
189 return -1;
190 }
191
192 if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
193 avrdude_message(MSG_INFO, "chip erase instruction not defined for part \"%s\"\n",
194 p->desc);
195 return -1;
196 }
197
198 pgm->pgm_led(pgm, ON);
199
200 memset(cmd, 0, sizeof(cmd));
201
203 pgm->cmd(pgm, cmd, res);
205 pgm->initialize(pgm, p);
206
207 pgm->pgm_led(pgm, OFF);
208
209 return 0;
210}
int avr_set_bits(OPCODE *op, unsigned char *cmd)
Definition avrpart.c:80
#define OFF
Definition libavrdude.h:585
@ AVR_OP_CHIP_ERASE
Definition libavrdude.h:156
int chip_erase_delay
Definition libavrdude.h:222
char desc[AVR_DESCLEN]
Definition libavrdude.h:218
OPCODE * op[AVR_OP_MAX]
Definition libavrdude.h:275
#define ON
Definition libavrdude.h:584
int(* cmd)(struct programmer_t *pgm, const unsigned char *cmd, unsigned char *res)
Definition libavrdude.h:651
int(* pgm_led)(struct programmer_t *pgm, int value)
Definition libavrdude.h:641
char type[PGM_TYPELEN]
Definition libavrdude.h:619
int(* initialize)(struct programmer_t *pgm, AVRPART *p)
Definition libavrdude.h:643
int usleep(unsigned usec)
Definition unistd.cpp:13

References AVR_OP_CHIP_ERASE, avr_set_bits(), avrdude_message(), avrpart::chip_erase_delay, programmer_t::cmd, cmd, avrpart::desc, programmer_t::initialize, MSG_INFO, OFF, ON, avrpart::op, pgm, programmer_t::pgm_led, progname, programmer_t::type, and usleep().

Referenced by stk500_initpgm().

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

◆ stk500_close()

static void stk500_close ( PROGRAMMER pgm)
static
687{
688 // MIB510 close
689 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0)
690 (void)mib510_isp(pgm, 0);
691
693 pgm->fd.ifd = -1;
694}
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
#define serial_close
Definition libavrdude.h:576
void * ldata(LNODEID)
Definition lists.c:720
LNODEID lfirst(LISTID)
Definition lists.c:688
int ifd
Definition libavrdude.h:522
static int mib510_isp(PROGRAMMER *pgm, unsigned char cmd)
Definition stk500.c:342
union filedescriptor fd
Definition libavrdude.h:637
LISTID id
Definition libavrdude.h:617

References programmer_t::fd, programmer_t::id, filedescriptor::ifd, ldata(), lfirst(), mib510_isp(), pgm, serial_close, and void().

Referenced by stk500_initpgm().

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

◆ stk500_cmd()

static int stk500_cmd ( PROGRAMMER pgm,
const unsigned char *  cmd,
unsigned char *  res 
)
static
140{
141 unsigned char buf[32];
142
143 buf[0] = Cmnd_STK_UNIVERSAL;
144 buf[1] = cmd[0];
145 buf[2] = cmd[1];
146 buf[3] = cmd[2];
147 buf[4] = cmd[3];
148 buf[5] = Sync_CRC_EOP;
149
150 stk500_send(pgm, buf, 6);
151
152 if (stk500_recv(pgm, buf, 1) < 0)
153 return -1;
154 if (buf[0] != Resp_STK_INSYNC) {
155 avrdude_message(MSG_INFO, "%s: stk500_cmd(): programmer is out of sync\n", progname);
156 return -1;
157 }
158
159 res[0] = cmd[1];
160 res[1] = cmd[2];
161 res[2] = cmd[3];
162 if (stk500_recv(pgm, &res[3], 1) < 0)
163 return -1;
164
165 if (stk500_recv(pgm, buf, 1) < 0)
166 return -1;
167 if (buf[0] != Resp_STK_OK) {
168 avrdude_message(MSG_INFO, "%s: stk500_cmd(): protocol error\n", progname);
169 return -1;
170 }
171
172 return 0;
173}
#define Cmnd_STK_UNIVERSAL
Definition stk500_private.h:49
#define Sync_CRC_EOP
Definition stk500_private.h:32

References avrdude_message(), cmd, Cmnd_STK_UNIVERSAL, MSG_INFO, pgm, progname, Resp_STK_INSYNC, Resp_STK_OK, stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_initpgm(), and stk500_loadaddr().

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

◆ stk500_disable()

static void stk500_disable ( PROGRAMMER pgm)
static
602{
603 unsigned char buf[16];
604 int tries=0;
605
606 retry:
607
608 tries++;
609
611 buf[1] = Sync_CRC_EOP;
612
613 stk500_send(pgm, buf, 2);
614 if (stk500_recv(pgm, buf, 1) < 0)
615 return;
616 if (buf[0] == Resp_STK_NOSYNC) {
617 if (tries > 33) {
618 avrdude_message(MSG_INFO, "%s: stk500_disable(): can't get into sync\n",
619 progname);
620 return;
621 }
622 if (stk500_getsync(pgm) < 0)
623 return;
624 goto retry;
625 }
626 else if (buf[0] != Resp_STK_INSYNC) {
627 avrdude_message(MSG_INFO, "%s: stk500_disable(): protocol error, expect=0x%02x, "
628 "resp=0x%02x\n",
629 progname, Resp_STK_INSYNC, buf[0]);
630 return;
631 }
632
633 if (stk500_recv(pgm, buf, 1) < 0)
634 return;
635 if (buf[0] == Resp_STK_OK) {
636 return;
637 }
638 else if (buf[0] == Resp_STK_NODEVICE) {
639 avrdude_message(MSG_INFO, "%s: stk500_disable(): no device\n",
640 progname);
641 return;
642 }
643
644 avrdude_message(MSG_INFO, "%s: stk500_disable(): unknown response=0x%02x\n",
645 progname, buf[0]);
646
647 return;
648}
#define Cmnd_STK_LEAVE_PROGMODE
Definition stk500_private.h:45

References avrdude_message(), Cmnd_STK_LEAVE_PROGMODE, MSG_INFO, pgm, progname, Resp_STK_INSYNC, Resp_STK_NODEVICE, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_initpgm().

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

◆ stk500_display()

static void stk500_display ( PROGRAMMER pgm,
const char *  p 
)
static
1220{
1221 unsigned maj, min, hdw, topcard;
1222
1227
1228 avrdude_message(MSG_INFO, "%sHardware Version: %d\n", p, hdw);
1229 avrdude_message(MSG_INFO, "%sFirmware Version: %d.%d\n", p, maj, min);
1230 if (topcard < 3) {
1231 const char *n = "Unknown";
1232
1233 switch (topcard) {
1234 case 1:
1235 n = "STK502";
1236 break;
1237
1238 case 2:
1239 n = "STK501";
1240 break;
1241 }
1242 avrdude_message(MSG_INFO, "%sTopcard : %s\n", p, n);
1243 }
1245
1246 return;
1247}
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() min(const half &a, const half &b)
Definition Half.h:507
static void stk500_print_parms1(PROGRAMMER *pgm, const char *p)
Definition stk500.c:1250
static int stk500_getparm(PROGRAMMER *pgm, unsigned parm, unsigned *value)
Definition stk500.c:1107
#define Parm_STK_SW_MINOR
Definition stk500_private.h:73
#define Param_STK500_TOPCARD_DETECT
Definition stk500_private.h:89
#define Parm_STK_SW_MAJOR
Definition stk500_private.h:72
#define Parm_STK_HW_VER
Definition stk500_private.h:71

References avrdude_message(), MSG_INFO, Param_STK500_TOPCARD_DETECT, Parm_STK_HW_VER, Parm_STK_SW_MAJOR, Parm_STK_SW_MINOR, pgm, stk500_getparm(), and stk500_print_parms1().

Referenced by stk500_initpgm().

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

◆ stk500_drain()

int stk500_drain ( PROGRAMMER pgm,
int  display 
)
83{
84 return serial_drain(&pgm->fd, display);
85}
#define serial_drain
Definition libavrdude.h:579

References programmer_t::fd, pgm, and serial_drain.

Referenced by arduino_open(), stk500_getsync(), and stk500_open().

+ Here is the caller graph for this function:

◆ stk500_enable()

static void stk500_enable ( PROGRAMMER pgm)
static
651{
652 return;
653}

Referenced by stk500_initpgm().

+ Here is the caller graph for this function:

◆ stk500_getparm()

static int stk500_getparm ( PROGRAMMER pgm,
unsigned  parm,
unsigned *  value 
)
static
1108{
1109 unsigned char buf[16];
1110 unsigned v;
1111 int tries = 0;
1112
1113 retry:
1114 tries++;
1115 buf[0] = Cmnd_STK_GET_PARAMETER;
1116 buf[1] = parm;
1117 buf[2] = Sync_CRC_EOP;
1118
1119 stk500_send(pgm, buf, 3);
1120
1121 if (stk500_recv(pgm, buf, 1) < 0)
1122 return -1;
1123 if (buf[0] == Resp_STK_NOSYNC) {
1124 if (tries > 33) {
1125 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): can't get into sync\n",
1126 progname);
1127 return -1;
1128 }
1129 if (stk500_getsync(pgm) < 0)
1130 return -1;
1131 goto retry;
1132 }
1133 else if (buf[0] != Resp_STK_INSYNC) {
1134 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): (a) protocol error, "
1135 "expect=0x%02x, resp=0x%02x\n",
1136 progname, Resp_STK_INSYNC, buf[0]);
1137 return -2;
1138 }
1139
1140 if (stk500_recv(pgm, buf, 1) < 0)
1141 return -1;
1142 v = buf[0];
1143
1144 if (stk500_recv(pgm, buf, 1) < 0)
1145 return -1;
1146 if (buf[0] == Resp_STK_FAILED) {
1147 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): parameter 0x%02x failed\n",
1148 progname, v);
1149 return -3;
1150 }
1151 else if (buf[0] != Resp_STK_OK) {
1152 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): (a) protocol error, "
1153 "expect=0x%02x, resp=0x%02x\n",
1154 progname, Resp_STK_INSYNC, buf[0]);
1155 return -3;
1156 }
1157
1158 *value = v;
1159
1160 return 0;
1161}
#define Cmnd_STK_GET_PARAMETER
Definition stk500_private.h:40

References avrdude_message(), Cmnd_STK_GET_PARAMETER, MSG_INFO, pgm, progname, Resp_STK_FAILED, Resp_STK_INSYNC, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_display(), stk500_initialize(), stk500_print_parms1(), stk500_set_varef(), and stk500_set_vtarget().

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

◆ stk500_getsync()

int stk500_getsync ( PROGRAMMER pgm)
89{
90 unsigned char buf[32], resp[32];
91 int attempt;
92
93 /*
94 * get in sync */
95 buf[0] = Cmnd_STK_GET_SYNC;
96 buf[1] = Sync_CRC_EOP;
97
98 /*
99 * First send and drain a few times to get rid of line noise
100 */
101
102 stk500_send(pgm, buf, 2);
103 stk500_drain(pgm, 0);
104 stk500_send(pgm, buf, 2);
105 stk500_drain(pgm, 0);
106
107 for (attempt = 0; attempt < MAX_SYNC_ATTEMPTS; attempt++) {
108 stk500_send(pgm, buf, 2);
109 stk500_recv(pgm, resp, 1);
110 if (resp[0] == Resp_STK_INSYNC){
111 break;
112 }
113 avrdude_message(MSG_INFO, "%s: stk500_getsync() attempt %d of %d: not in sync: resp=0x%02x\n",
114 progname, attempt + 1, MAX_SYNC_ATTEMPTS, resp[0]);
115 }
116 if (attempt == MAX_SYNC_ATTEMPTS) {
117 stk500_drain(pgm, 0);
118 return -1;
119 }
120
121 if (stk500_recv(pgm, resp, 1) < 0)
122 return -1;
123 if (resp[0] != Resp_STK_OK) {
124 avrdude_message(MSG_INFO, "%s: stk500_getsync(): can't communicate with device: "
125 "resp=0x%02x\n",
126 progname, resp[0]);
127 return -1;
128 }
129
130 return 0;
131}
int stk500_drain(PROGRAMMER *pgm, int display)
Definition stk500.c:82
#define MAX_SYNC_ATTEMPTS
Definition stk500.c:46
#define Cmnd_STK_GET_SYNC
Definition stk500_private.h:36

References avrdude_message(), Cmnd_STK_GET_SYNC, MAX_SYNC_ATTEMPTS, MSG_INFO, pgm, progname, Resp_STK_INSYNC, Resp_STK_OK, stk500_drain(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by arduino_open(), mib510_isp(), stk500_disable(), stk500_getparm(), stk500_initialize(), stk500_loadaddr(), stk500_open(), stk500_paged_load(), stk500_paged_write(), stk500_program_enable(), stk500_set_extended_parms(), and stk500_setparm().

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

◆ stk500_initialize()

static int stk500_initialize ( PROGRAMMER pgm,
AVRPART p 
)
static
412{
413 unsigned char buf[32];
414 AVRMEM * m;
415 int tries;
416 unsigned maj, min;
417 int rc;
418 int n_extparms;
419
422
423 // MIB510 does not need extparams
424 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0)
425 n_extparms = 0;
426 else if ((maj > 1) || ((maj == 1) && (min > 10)))
427 n_extparms = 4;
428 else
429 n_extparms = 3;
430
431 tries = 0;
432
433 retry:
434 tries++;
435
436 memset(buf, 0, sizeof(buf));
437
438 /*
439 * set device programming parameters
440 */
441 buf[0] = Cmnd_STK_SET_DEVICE;
442
443 buf[1] = p->stk500_devcode;
444 buf[2] = 0; /* device revision */
445
446 if ((p->flags & AVRPART_SERIALOK) && (p->flags & AVRPART_PARALLELOK))
447 buf[3] = 0; /* device supports parallel and serial programming */
448 else
449 buf[3] = 1; /* device supports parallel only */
450
451 if (p->flags & AVRPART_PARALLELOK) {
452 if (p->flags & AVRPART_PSEUDOPARALLEL) {
453 buf[4] = 0; /* pseudo parallel interface */
454 n_extparms = 0;
455 }
456 else {
457 buf[4] = 1; /* full parallel interface */
458 }
459 }
460
461#if 0
462 avrdude_message(MSG_INFO, "%s: stk500_initialize(): n_extparms = %d\n",
463 progname, n_extparms);
464#endif
465
466 buf[5] = 1; /* polling supported - XXX need this in config file */
467 buf[6] = 1; /* programming is self-timed - XXX need in config file */
468
469 m = avr_locate_mem(p, "lock");
470 if (m)
471 buf[7] = m->size;
472 else
473 buf[7] = 0;
474
475 /*
476 * number of fuse bytes
477 */
478 buf[8] = 0;
479 m = avr_locate_mem(p, "fuse");
480 if (m)
481 buf[8] += m->size;
482 m = avr_locate_mem(p, "lfuse");
483 if (m)
484 buf[8] += m->size;
485 m = avr_locate_mem(p, "hfuse");
486 if (m)
487 buf[8] += m->size;
488 m = avr_locate_mem(p, "efuse");
489 if (m)
490 buf[8] += m->size;
491
492 m = avr_locate_mem(p, "flash");
493 if (m) {
494 buf[9] = m->readback[0];
495 buf[10] = m->readback[1];
496 if (m->paged) {
497 buf[13] = (m->page_size >> 8) & 0x00ff;
498 buf[14] = m->page_size & 0x00ff;
499 }
500 buf[17] = (m->size >> 24) & 0xff;
501 buf[18] = (m->size >> 16) & 0xff;
502 buf[19] = (m->size >> 8) & 0xff;
503 buf[20] = m->size & 0xff;
504 }
505 else {
506 buf[9] = 0xff;
507 buf[10] = 0xff;
508 buf[13] = 0;
509 buf[14] = 0;
510 buf[17] = 0;
511 buf[18] = 0;
512 buf[19] = 0;
513 buf[20] = 0;
514 }
515
516 m = avr_locate_mem(p, "eeprom");
517 if (m) {
518 buf[11] = m->readback[0];
519 buf[12] = m->readback[1];
520 buf[15] = (m->size >> 8) & 0x00ff;
521 buf[16] = m->size & 0x00ff;
522 }
523 else {
524 buf[11] = 0xff;
525 buf[12] = 0xff;
526 buf[15] = 0;
527 buf[16] = 0;
528 }
529
530 buf[21] = Sync_CRC_EOP;
531
532 stk500_send(pgm, buf, 22);
533 if (stk500_recv(pgm, buf, 1) < 0)
534 return -1;
535 if (buf[0] == Resp_STK_NOSYNC) {
536 avrdude_message(MSG_INFO, "%s: stk500_initialize(): programmer not in sync, resp=0x%02x\n",
537 progname, buf[0]);
538 if (tries > 33)
539 return -1;
540 if (stk500_getsync(pgm) < 0)
541 return -1;
542 goto retry;
543 }
544 else if (buf[0] != Resp_STK_INSYNC) {
545 avrdude_message(MSG_INFO, "%s: stk500_initialize(): (a) protocol error, "
546 "expect=0x%02x, resp=0x%02x\n",
547 progname, Resp_STK_INSYNC, buf[0]);
548 return -1;
549 }
550
551 if (stk500_recv(pgm, buf, 1) < 0)
552 return -1;
553 if (buf[0] != Resp_STK_OK) {
554 avrdude_message(MSG_INFO, "%s: stk500_initialize(): (b) protocol error, "
555 "expect=0x%02x, resp=0x%02x\n",
556 progname, Resp_STK_OK, buf[0]);
557 return -1;
558 }
559
560 if (n_extparms) {
561 if ((p->pagel == 0) || (p->bs2 == 0)) {
562 avrdude_message(MSG_NOTICE2, "%s: PAGEL and BS2 signals not defined in the configuration "
563 "file for part %s, using dummy values\n",
564 progname, p->desc);
565 buf[2] = 0xD7; /* they look somehow possible, */
566 buf[3] = 0xA0; /* don't they? ;) */
567 }
568 else {
569 buf[2] = p->pagel;
570 buf[3] = p->bs2;
571 }
572 buf[0] = n_extparms+1;
573
574 /*
575 * m is currently pointing to eeprom memory if the part has it
576 */
577 if (m)
578 buf[1] = m->page_size;
579 else
580 buf[1] = 0;
581
582
583 if (n_extparms == 4) {
585 buf[4] = 0;
586 else
587 buf[4] = 1;
588 }
589
590 rc = stk500_set_extended_parms(pgm, n_extparms+1, buf);
591 if (rc) {
592 avrdude_message(MSG_INFO, "%s: stk500_initialize(): failed\n", progname);
593 return -1;
594 }
595 }
596
597 return pgm->program_enable(pgm, p);
598}
#define MSG_NOTICE2
Definition avrdude.h:53
AVRMEM * avr_locate_mem(AVRPART *p, char *desc)
Definition avrpart.c:354
#define AVRPART_PSEUDOPARALLEL
Definition libavrdude.h:197
unsigned char pagel
Definition libavrdude.h:223
unsigned char readback[2]
Definition libavrdude.h:296
int size
Definition libavrdude.h:286
#define AVRPART_PARALLELOK
Definition libavrdude.h:196
int paged
Definition libavrdude.h:285
int page_size
Definition libavrdude.h:287
#define AVRPART_SERIALOK
Definition libavrdude.h:195
int reset_disposition
Definition libavrdude.h:227
@ RESET_DEDICATED
Definition libavrdude.h:171
unsigned char bs2
Definition libavrdude.h:224
unsigned flags
Definition libavrdude.h:230
int stk500_devcode
Definition libavrdude.h:220
Definition libavrdude.h:283
static int stk500_set_extended_parms(PROGRAMMER *pgm, int n, unsigned char *cmd)
Definition stk500.c:274
#define Cmnd_STK_SET_DEVICE
Definition stk500_private.h:41
int(* program_enable)(struct programmer_t *pgm, AVRPART *p)
Definition libavrdude.h:649

References avr_locate_mem(), avrdude_message(), AVRPART_PARALLELOK, AVRPART_PSEUDOPARALLEL, AVRPART_SERIALOK, avrpart::bs2, Cmnd_STK_SET_DEVICE, avrpart::desc, avrpart::flags, programmer_t::id, ldata(), lfirst(), MSG_INFO, MSG_NOTICE2, avrmem::page_size, avrmem::paged, avrpart::pagel, Parm_STK_SW_MAJOR, Parm_STK_SW_MINOR, pgm, progname, programmer_t::program_enable, avrmem::readback, RESET_DEDICATED, avrpart::reset_disposition, Resp_STK_INSYNC, Resp_STK_NOSYNC, Resp_STK_OK, avrmem::size, avrpart::stk500_devcode, stk500_getparm(), stk500_getsync(), stk500_recv(), stk500_send(), stk500_set_extended_parms(), and Sync_CRC_EOP.

Referenced by stk500_initpgm().

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

◆ stk500_initpgm()

void stk500_initpgm ( PROGRAMMER pgm)
1322{
1323 strcpy(pgm->type, "STK500");
1324
1325 /*
1326 * mandatory functions
1327 */
1334 pgm->cmd = stk500_cmd;
1335 pgm->open = stk500_open;
1339
1340 /*
1341 * optional functions
1342 */
1352 pgm->page_size = 256;
1353}
int avr_write_byte_default(PROGRAMMER *pgm, AVRPART *p, AVRMEM *mem, unsigned long addr, unsigned char data)
Definition avr.c:530
int avr_read_byte_default(PROGRAMMER *pgm, AVRPART *p, AVRMEM *mem, unsigned long addr, unsigned char *value)
Definition avr.c:183
static int stk500_paged_load(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
Definition stk500.c:882
static int stk500_chip_erase(PROGRAMMER *pgm, AVRPART *p)
Definition stk500.c:180
static int stk500_initialize(PROGRAMMER *pgm, AVRPART *p)
Definition stk500.c:411
static int stk500_set_vtarget(PROGRAMMER *pgm, double v)
Definition stk500.c:982
static int stk500_cmd(PROGRAMMER *pgm, const unsigned char *cmd, unsigned char *res)
Definition stk500.c:138
static void stk500_disable(PROGRAMMER *pgm)
Definition stk500.c:601
static void stk500_display(PROGRAMMER *pgm, const char *p)
Definition stk500.c:1219
static int stk500_set_sck_period(PROGRAMMER *pgm, double v)
Definition stk500.c:1084
static int stk500_set_varef(PROGRAMMER *pgm, unsigned int chan, double v)
Definition stk500.c:1005
static void stk500_print_parms(PROGRAMMER *pgm)
Definition stk500.c:1297
static void stk500_close(PROGRAMMER *pgm)
Definition stk500.c:686
static int stk500_set_fosc(PROGRAMMER *pgm, double v)
Definition stk500.c:1028
static void stk500_setup(PROGRAMMER *pgm)
Definition stk500.c:1302
static int stk500_program_enable(PROGRAMMER *pgm, AVRPART *p)
Definition stk500.c:215
static int stk500_paged_write(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
Definition stk500.c:765
static void stk500_teardown(PROGRAMMER *pgm)
Definition stk500.c:1314
static void stk500_enable(PROGRAMMER *pgm)
Definition stk500.c:650
static int stk500_open(PROGRAMMER *pgm, char *port)
Definition stk500.c:656
void(* display)(struct programmer_t *pgm, const char *p)
Definition libavrdude.h:644
int(* paged_write)(struct programmer_t *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int baseaddr, unsigned int n_bytes)
Definition libavrdude.h:659
int(* open)(struct programmer_t *pgm, char *port)
Definition libavrdude.h:657
int(* read_byte)(struct programmer_t *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char *value)
Definition libavrdude.h:670
int(* paged_load)(struct programmer_t *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int baseaddr, unsigned int n_bytes)
Definition libavrdude.h:662
int(* set_vtarget)(struct programmer_t *pgm, double v)
Definition libavrdude.h:674
void(* teardown)(struct programmer_t *pgm)
Definition libavrdude.h:685
int(* set_varef)(struct programmer_t *pgm, unsigned int chan, double v)
Definition libavrdude.h:675
void(* enable)(struct programmer_t *pgm)
Definition libavrdude.h:645
int(* write_byte)(struct programmer_t *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char value)
Definition libavrdude.h:668
void(* setup)(struct programmer_t *pgm)
Definition libavrdude.h:684
int(* set_fosc)(struct programmer_t *pgm, double v)
Definition libavrdude.h:676
void(* print_parms)(struct programmer_t *pgm)
Definition libavrdude.h:673
void(* close)(struct programmer_t *pgm)
Definition libavrdude.h:658
int(* set_sck_period)(struct programmer_t *pgm, double v)
Definition libavrdude.h:677
int(* chip_erase)(struct programmer_t *pgm, AVRPART *p)
Definition libavrdude.h:650
void(* disable)(struct programmer_t *pgm)
Definition libavrdude.h:646
int page_size
Definition libavrdude.h:638

References avr_read_byte_default(), avr_write_byte_default(), programmer_t::chip_erase, programmer_t::close, programmer_t::cmd, programmer_t::disable, programmer_t::display, programmer_t::enable, programmer_t::initialize, programmer_t::open, programmer_t::page_size, programmer_t::paged_load, programmer_t::paged_write, pgm, programmer_t::print_parms, programmer_t::program_enable, programmer_t::read_byte, programmer_t::set_fosc, programmer_t::set_sck_period, programmer_t::set_varef, programmer_t::set_vtarget, programmer_t::setup, stk500_chip_erase(), stk500_close(), stk500_cmd(), stk500_disable(), stk500_display(), stk500_enable(), stk500_initialize(), stk500_open(), stk500_paged_load(), stk500_paged_write(), stk500_print_parms(), stk500_program_enable(), stk500_set_fosc(), stk500_set_sck_period(), stk500_set_varef(), stk500_set_vtarget(), stk500_setup(), stk500_teardown(), programmer_t::teardown, programmer_t::type, and programmer_t::write_byte.

Referenced by arduino_initpgm(), and stk500generic_open().

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

◆ stk500_loadaddr()

static int stk500_loadaddr ( PROGRAMMER pgm,
AVRMEM mem,
unsigned int  addr 
)
static
698{
699 unsigned char buf[16];
700 int tries;
701 unsigned char ext_byte;
702 OPCODE * lext;
703
704 tries = 0;
705 retry:
706 tries++;
707
708 /* To support flash > 64K words the correct Extended Address Byte is needed */
709 lext = mem->op[AVR_OP_LOAD_EXT_ADDR];
710 if (lext != NULL) {
711 ext_byte = (addr >> 16) & 0xff;
712 if (ext_byte != PDATA(pgm)->ext_addr_byte) {
713 /* Either this is the first addr load, or a 64K word boundary is
714 * crossed, so set the ext addr byte */
715 avr_set_bits(lext, buf);
716 avr_set_addr(lext, buf, addr);
717 stk500_cmd(pgm, buf, buf);
718 PDATA(pgm)->ext_addr_byte = ext_byte;
719 }
720 }
721
722 buf[0] = Cmnd_STK_LOAD_ADDRESS;
723 // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
724 // Send the binary data by nibbles to avoid transmitting the ';' character.
725 buf[1] = addr & 0x0f;
726 buf[2] = addr & 0xf0;
727 buf[3] = (addr >> 8) & 0x0f;
728 buf[4] = (addr >> 8) & 0xf0;
729 buf[5] = Sync_CRC_EOP;
730 stk500_send(pgm, buf, 6);
731
732 if (stk500_recv(pgm, buf, 1) < 0)
733 return -1;
734 if (buf[0] == Resp_STK_NOSYNC) {
735 if (tries > 33) {
736 avrdude_message(MSG_INFO, "%s: stk500_loadaddr(): can't get into sync\n",
737 progname);
738 return -1;
739 }
740 if (stk500_getsync(pgm) < 0)
741 return -1;
742 goto retry;
743 }
744 else if (buf[0] != Resp_STK_INSYNC) {
745 avrdude_message(MSG_INFO, "%s: stk500_loadaddr(): (a) protocol error, "
746 "expect=0x%02x, resp=0x%02x\n",
747 progname, Resp_STK_INSYNC, buf[0]);
748 return -1;
749 }
750
751 if (stk500_recv(pgm, buf, 1) < 0)
752 return -1;
753 if (buf[0] == Resp_STK_OK) {
754 return 0;
755 }
756
757 avrdude_message(MSG_INFO, "%s: loadaddr(): (b) protocol error, "
758 "expect=0x%02x, resp=0x%02x\n",
759 progname, Resp_STK_INSYNC, buf[0]);
760
761 return -1;
762}
int avr_set_addr(OPCODE *op, unsigned char *cmd, unsigned long addr)
Definition avrpart.c:107
@ AVR_OP_LOAD_EXT_ADDR
Definition libavrdude.h:154
OPCODE * op[AVR_OP_MAX]
Definition libavrdude.h:306
Definition libavrdude.h:190
#define PDATA(pgm)
Definition stk500.c:54
#define Cmnd_STK_LOAD_ADDRESS
Definition stk500_private.h:48

References AVR_OP_LOAD_EXT_ADDR, avr_set_addr(), avr_set_bits(), avrdude_message(), Cmnd_STK_LOAD_ADDRESS, MSG_INFO, avrmem::op, PDATA, pgm, progname, Resp_STK_INSYNC, Resp_STK_NOSYNC, Resp_STK_OK, stk500_cmd(), stk500_getsync(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_paged_load(), and stk500_paged_write().

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

◆ stk500_open()

static int stk500_open ( PROGRAMMER pgm,
char *  port 
)
static
657{
658 union pinfo pinfo;
659 strcpy(pgm->port, port);
660 pinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
661 if (serial_open(port, pinfo, &pgm->fd)==-1) {
662 return -1;
663 }
664
665 /*
666 * drain any extraneous input
667 */
668 stk500_drain(pgm, 0);
669
670 // MIB510 init
671 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0 &&
672 mib510_isp(pgm, 1) != 0) {
674 return -1;
675 }
676
677 if (stk500_getsync(pgm) < 0) {
679 return -1;
680 }
681
682 return 0;
683}
#define serial_open
Definition libavrdude.h:574
long baud
Definition libavrdude.h:537
Definition libavrdude.h:536
char port[PGM_PORTLEN]
Definition libavrdude.h:620
int baudrate
Definition libavrdude.h:630

References pinfo::baud, programmer_t::baudrate, programmer_t::fd, programmer_t::id, ldata(), lfirst(), mib510_isp(), pgm, programmer_t::port, serial_close, serial_open, stk500_drain(), and stk500_getsync().

Referenced by stk500_initpgm().

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

◆ stk500_paged_load()

static int stk500_paged_load ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned int  page_size,
unsigned int  addr,
unsigned int  n_bytes 
)
static
885{
886 unsigned char buf[16];
887 int memtype;
888 int a_div;
889 int tries;
890 unsigned int n;
891 int block_size;
892
893 if (strcmp(m->desc, "flash") == 0) {
894 memtype = 'F';
895 }
896 else if (strcmp(m->desc, "eeprom") == 0) {
897 memtype = 'E';
898 }
899 else {
900 return -2;
901 }
902
903 if ((m->op[AVR_OP_LOADPAGE_LO]) || (m->op[AVR_OP_READ_LO]))
904 a_div = 2;
905 else
906 a_div = 1;
907
908 n = addr + n_bytes;
909 for (; addr < n; addr += block_size) {
910 // MIB510 uses fixed blocks size of 256 bytes
911 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0) {
912 block_size = 256;
913 } else {
914 if (n - addr < page_size)
915 block_size = n - addr;
916 else
917 block_size = page_size;
918 }
919
920 tries = 0;
921 retry:
922 tries++;
923 stk500_loadaddr(pgm, m, addr/a_div);
924 buf[0] = Cmnd_STK_READ_PAGE;
925 // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
926 // Send the binary data by nibbles to avoid transmitting the ';' character.
927 buf[1] = (block_size >> 8) & 0xf0;
928 buf[2] = (block_size >> 8) & 0x0f;
929 buf[3] = block_size & 0xf0;
930 buf[4] = block_size & 0x0f;
931 buf[5] = memtype;
932 buf[6] = Sync_CRC_EOP;
933 stk500_send(pgm, buf, 7);
934
935 if (stk500_recv(pgm, buf, 1) < 0)
936 return -1;
937 if (buf[0] == Resp_STK_NOSYNC) {
938 if (tries > 33) {
939 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): can't get into sync\n",
940 progname);
941 return -3;
942 }
943 if (stk500_getsync(pgm) < 0)
944 return -1;
945 goto retry;
946 }
947 else if (buf[0] != Resp_STK_INSYNC) {
948 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): (a) protocol error, "
949 "expect=0x%02x, resp=0x%02x\n",
950 progname, Resp_STK_INSYNC, buf[0]);
951 return -4;
952 }
953
954 if (stk500_recv(pgm, &m->buf[addr], block_size) < 0)
955 return -1;
956
957 if (stk500_recv(pgm, buf, 1) < 0)
958 return -1;
959
960 if(strcmp(ldata(lfirst(pgm->id)), "mib510") == 0) {
961 if (buf[0] != Resp_STK_INSYNC) {
962 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): (a) protocol error, "
963 "expect=0x%02x, resp=0x%02x\n",
964 progname, Resp_STK_INSYNC, buf[0]);
965 return -5;
966 }
967 }
968 else {
969 if (buf[0] != Resp_STK_OK) {
970 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): (a) protocol error, "
971 "expect=0x%02x, resp=0x%02x\n",
972 progname, Resp_STK_OK, buf[0]);
973 return -5;
974 }
975 }
976 }
977
978 return n_bytes;
979}
@ AVR_OP_LOADPAGE_LO
Definition libavrdude.h:152
@ AVR_OP_READ_LO
Definition libavrdude.h:148
unsigned char * buf
Definition libavrdude.h:304
char desc[AVR_MEMDESCLEN]
Definition libavrdude.h:284
static int stk500_loadaddr(PROGRAMMER *pgm, AVRMEM *mem, unsigned int addr)
Definition stk500.c:697
#define Cmnd_STK_READ_PAGE
Definition stk500_private.h:63

References AVR_OP_LOADPAGE_LO, AVR_OP_READ_LO, avrdude_message(), avrmem::buf, Cmnd_STK_READ_PAGE, avrmem::desc, programmer_t::id, ldata(), lfirst(), MSG_INFO, avrmem::op, pgm, progname, Resp_STK_INSYNC, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_loadaddr(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_initpgm().

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

◆ stk500_paged_write()

static int stk500_paged_write ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned int  page_size,
unsigned int  addr,
unsigned int  n_bytes 
)
static
768{
769 unsigned char *buf = alloca(page_size + 16);
770 int memtype;
771 int a_div;
772 int block_size;
773 int tries;
774 unsigned int n;
775 unsigned int i, j;
776 unsigned int prusa3d_semicolon_workaround_round = 0;
777 bool has_semicolon = false;
778
779 if (strcmp(m->desc, "flash") == 0) {
780 memtype = 'F';
781 }
782 else if (strcmp(m->desc, "eeprom") == 0) {
783 memtype = 'E';
784 }
785 else {
786 return -2;
787 }
788
789 if ((m->op[AVR_OP_LOADPAGE_LO]) || (m->op[AVR_OP_READ_LO]))
790 a_div = 2;
791 else
792 a_div = 1;
793
794 n = addr + n_bytes;
795#if 0
796 avrdude_message(MSG_INFO, "n_bytes = %d\n"
797 "n = %u\n"
798 "a_div = %d\n"
799 "page_size = %d\n",
800 n_bytes, n, a_div, page_size);
801#endif
802
803 for (; addr < n; addr += block_size) {
804 // MIB510 uses fixed blocks size of 256 bytes
805 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0) {
806 block_size = 256;
807 } else {
808 if (n - addr < page_size)
809 block_size = n - addr;
810 else
811 block_size = page_size;
812 }
813 tries = 0;
814 retry:
815 tries++;
816 stk500_loadaddr(pgm, m, addr/a_div);
817
818 for (i = 0; i < n_bytes; ++ i)
819 if (m->buf[addr + i] == ';') {
820 has_semicolon = true;
821 break;
822 }
823
824 for (prusa3d_semicolon_workaround_round = 0; prusa3d_semicolon_workaround_round < (has_semicolon ? 2u : 1u); prusa3d_semicolon_workaround_round++) {
825 /* build command block and avoid multiple send commands as it leads to a crash
826 of the silabs usb serial driver on mac os x */
827 i = 0;
828 buf[i++] = Cmnd_STK_PROG_PAGE;
829 // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
830 // Send the binary data by nibbles to avoid transmitting the ';' character.
831 buf[i++] = (block_size >> 8) & 0xf0;
832 buf[i++] = (block_size >> 8) & 0x0f;
833 buf[i++] = block_size & 0xf0;
834 buf[i++] = block_size & 0x0f;
835 buf[i++] = memtype;
836 if (has_semicolon) {
837 for (j = 0; j < (unsigned)block_size; ++i, ++ j) {
838 buf[i] = m->buf[addr + j];
839 if (buf[i] == ';')
840 buf[i] |= (prusa3d_semicolon_workaround_round ? 0xf0 : 0x0f);
841 }
842 } else {
843 memcpy(&buf[i], &m->buf[addr], block_size);
844 i += block_size;
845 }
846 buf[i++] = Sync_CRC_EOP;
847 stk500_send( pgm, buf, i);
848
849 if (stk500_recv(pgm, buf, 1) < 0)
850 return -1;
851 if (buf[0] == Resp_STK_NOSYNC) {
852 if (tries > 33) {
853 avrdude_message(MSG_INFO, "\n%s: stk500_paged_write(): can't get into sync\n",
854 progname);
855 return -3;
856 }
857 if (stk500_getsync(pgm) < 0)
858 return -1;
859 goto retry;
860 }
861 else if (buf[0] != Resp_STK_INSYNC) {
862 avrdude_message(MSG_INFO, "\n%s: stk500_paged_write(): (a) protocol error, "
863 "expect=0x%02x, resp=0x%02x\n",
864 progname, Resp_STK_INSYNC, buf[0]);
865 return -4;
866 }
867
868 if (stk500_recv(pgm, buf, 1) < 0)
869 return -1;
870 if (buf[0] != Resp_STK_OK) {
871 avrdude_message(MSG_INFO, "\n%s: stk500_paged_write(): (a) protocol error, "
872 "expect=0x%02x, resp=0x%02x\n",
873 progname, Resp_STK_INSYNC, buf[0]);
874 return -5;
875 }
876 }
877 }
878
879 return n_bytes;
880}
#define Cmnd_STK_PROG_PAGE
Definition stk500_private.h:56

References AVR_OP_LOADPAGE_LO, AVR_OP_READ_LO, avrdude_message(), avrmem::buf, Cmnd_STK_PROG_PAGE, avrmem::desc, programmer_t::id, ldata(), lfirst(), MSG_INFO, avrmem::op, pgm, progname, Resp_STK_INSYNC, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_loadaddr(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_initpgm().

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

◆ stk500_print_parms()

static void stk500_print_parms ( PROGRAMMER pgm)
static
1298{
1300}

References pgm, and stk500_print_parms1().

Referenced by stk500_initpgm().

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

◆ stk500_print_parms1()

static void stk500_print_parms1 ( PROGRAMMER pgm,
const char *  p 
)
static
1251{
1252 unsigned vtarget, vadjust, osc_pscale, osc_cmatch, sck_duration;
1253
1256 stk500_getparm(pgm, Parm_STK_OSC_PSCALE, &osc_pscale);
1257 stk500_getparm(pgm, Parm_STK_OSC_CMATCH, &osc_cmatch);
1258 stk500_getparm(pgm, Parm_STK_SCK_DURATION, &sck_duration);
1259
1260 avrdude_message(MSG_INFO, "%sVtarget : %.1f V\n", p, vtarget / 10.0);
1261 avrdude_message(MSG_INFO, "%sVaref : %.1f V\n", p, vadjust / 10.0);
1262 avrdude_message(MSG_INFO, "%sOscillator : ", p);
1263 if (osc_pscale == 0)
1264 avrdude_message(MSG_INFO, "Off\n");
1265 else {
1266 int prescale = 1;
1267 double f = STK500_XTAL / 2;
1268 const char *unit;
1269
1270 switch (osc_pscale) {
1271 case 2: prescale = 8; break;
1272 case 3: prescale = 32; break;
1273 case 4: prescale = 64; break;
1274 case 5: prescale = 128; break;
1275 case 6: prescale = 256; break;
1276 case 7: prescale = 1024; break;
1277 }
1278 f /= prescale;
1279 f /= (osc_cmatch + 1);
1280 if (f > 1e6) {
1281 f /= 1e6;
1282 unit = "MHz";
1283 } else if (f > 1e3) {
1284 f /= 1000;
1285 unit = "kHz";
1286 } else
1287 unit = "Hz";
1288 avrdude_message(MSG_INFO, "%.3f %s\n", f, unit);
1289 }
1290 avrdude_message(MSG_INFO, "%sSCK period : %.1f us\n", p,
1291 sck_duration * 8.0e6 / STK500_XTAL + 0.05);
1292
1293 return;
1294}
#define STK500_XTAL
Definition stk500.c:45
#define Parm_STK_VTARGET
Definition stk500_private.h:75
#define Parm_STK_VADJUST
Definition stk500_private.h:76
#define Parm_STK_OSC_PSCALE
Definition stk500_private.h:77
#define Parm_STK_SCK_DURATION
Definition stk500_private.h:80
#define Parm_STK_OSC_CMATCH
Definition stk500_private.h:78

References avrdude_message(), MSG_INFO, Parm_STK_OSC_CMATCH, Parm_STK_OSC_PSCALE, Parm_STK_SCK_DURATION, Parm_STK_VADJUST, Parm_STK_VTARGET, pgm, stk500_getparm(), and STK500_XTAL.

Referenced by stk500_display(), and stk500_print_parms().

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

◆ stk500_program_enable()

static int stk500_program_enable ( PROGRAMMER pgm,
AVRPART p 
)
static
216{
217 unsigned char buf[16];
218 int tries=0;
219
220 retry:
221
222 tries++;
223
225 buf[1] = Sync_CRC_EOP;
226
227 stk500_send(pgm, buf, 2);
228 if (stk500_recv(pgm, buf, 1) < 0)
229 return -1;
230 if (buf[0] == Resp_STK_NOSYNC) {
231 if (tries > 33) {
232 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): can't get into sync\n",
233 progname);
234 return -1;
235 }
236 if (stk500_getsync(pgm) < 0)
237 return -1;
238 goto retry;
239 }
240 else if (buf[0] != Resp_STK_INSYNC) {
241 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): protocol error, "
242 "expect=0x%02x, resp=0x%02x\n",
243 progname, Resp_STK_INSYNC, buf[0]);
244 return -1;
245 }
246
247 if (stk500_recv(pgm, buf, 1) < 0)
248 return -1;
249 if (buf[0] == Resp_STK_OK) {
250 return 0;
251 }
252 else if (buf[0] == Resp_STK_NODEVICE) {
253 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): no device\n",
254 progname);
255 return -1;
256 }
257
258 if(buf[0] == Resp_STK_FAILED)
259 {
260 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): failed to enter programming mode\n",
261 progname);
262 return -1;
263 }
264
265
266 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): unknown response=0x%02x\n",
267 progname, buf[0]);
268
269 return -1;
270}
#define Cmnd_STK_ENTER_PROGMODE
Definition stk500_private.h:44

References avrdude_message(), Cmnd_STK_ENTER_PROGMODE, MSG_INFO, pgm, progname, Resp_STK_FAILED, Resp_STK_INSYNC, Resp_STK_NODEVICE, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_initpgm().

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

◆ stk500_recv()

static int stk500_recv ( PROGRAMMER pgm,
unsigned char *  buf,
size_t  len 
)
static
69{
70 int rv;
71
72 rv = serial_recv(&pgm->fd, buf, len);
73 if (rv < 0) {
74 avrdude_message(MSG_INFO, "%s: stk500_recv(): programmer is not responding\n",
75 progname);
76 return -1;
77 }
78 return 0;
79}
#define serial_recv
Definition libavrdude.h:578

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

Referenced by mib510_isp(), stk500_cmd(), stk500_disable(), stk500_getparm(), stk500_getsync(), stk500_initialize(), stk500_loadaddr(), stk500_paged_load(), stk500_paged_write(), stk500_program_enable(), stk500_set_extended_parms(), and stk500_setparm().

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

◆ stk500_send()

static int stk500_send ( PROGRAMMER pgm,
unsigned char *  buf,
size_t  len 
)
static
63{
64 return serial_send(&pgm->fd, buf, len);
65}
#define serial_send
Definition libavrdude.h:577

References programmer_t::fd, pgm, and serial_send.

Referenced by mib510_isp(), stk500_cmd(), stk500_disable(), stk500_getparm(), stk500_getsync(), stk500_initialize(), stk500_loadaddr(), stk500_paged_load(), stk500_paged_write(), stk500_program_enable(), stk500_set_extended_parms(), and stk500_setparm().

+ Here is the caller graph for this function:

◆ stk500_set_extended_parms()

static int stk500_set_extended_parms ( PROGRAMMER pgm,
int  n,
unsigned char *  cmd 
)
static
276{
277 unsigned char buf[16];
278 int tries=0;
279 int i;
280
281 retry:
282
283 tries++;
284
286 for (i=0; i<n; i++) {
287 buf[1+i] = cmd[i];
288 }
289 i++;
290 buf[i] = Sync_CRC_EOP;
291
292 stk500_send(pgm, buf, i+1);
293 if (stk500_recv(pgm, buf, 1) < 0)
294 return -1;
295 if (buf[0] == Resp_STK_NOSYNC) {
296 if (tries > 33) {
297 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): can't get into sync\n",
298 progname);
299 return -1;
300 }
301 if (stk500_getsync(pgm) < 0)
302 return -1;
303 goto retry;
304 }
305 else if (buf[0] != Resp_STK_INSYNC) {
306 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): protocol error, "
307 "expect=0x%02x, resp=0x%02x\n",
308 progname, Resp_STK_INSYNC, buf[0]);
309 return -1;
310 }
311
312 if (stk500_recv(pgm, buf, 1) < 0)
313 return -1;
314 if (buf[0] == Resp_STK_OK) {
315 return 0;
316 }
317 else if (buf[0] == Resp_STK_NODEVICE) {
318 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): no device\n",
319 progname);
320 return -1;
321 }
322
323 if(buf[0] == Resp_STK_FAILED)
324 {
325 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): failed to set extended "
326 "device programming parameters\n",
327 progname);
328 return -1;
329 }
330
331
332 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): unknown response=0x%02x\n",
333 progname, buf[0]);
334
335 return -1;
336}
#define Cmnd_STK_SET_DEVICE_EXT
Definition stk500_private.h:42

References avrdude_message(), cmd, Cmnd_STK_SET_DEVICE_EXT, MSG_INFO, pgm, progname, Resp_STK_FAILED, Resp_STK_INSYNC, Resp_STK_NODEVICE, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_initialize().

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

◆ stk500_set_fosc()

static int stk500_set_fosc ( PROGRAMMER pgm,
double  v 
)
static
1029{
1030 unsigned prescale, cmatch, fosc;
1031 static unsigned ps[] = {
1032 1, 8, 32, 64, 128, 256, 1024
1033 };
1034 int idx, rc;
1035
1036 prescale = cmatch = 0;
1037 if (v > 0.0) {
1038 if (v > STK500_XTAL / 2) {
1039 const char *unit;
1040 if (v > 1e6) {
1041 v /= 1e6;
1042 unit = "MHz";
1043 } else if (v > 1e3) {
1044 v /= 1e3;
1045 unit = "kHz";
1046 } else
1047 unit = "Hz";
1048 avrdude_message(MSG_INFO, "%s: stk500_set_fosc(): f = %.3f %s too high, using %.3f MHz\n",
1049 progname, v, unit, STK500_XTAL / 2e6);
1050 fosc = STK500_XTAL / 2;
1051 } else
1052 fosc = (unsigned)v;
1053
1054 for (idx = 0; idx < sizeof(ps) / sizeof(ps[0]); idx++) {
1055 if (fosc >= STK500_XTAL / (256 * ps[idx] * 2)) {
1056 /* this prescaler value can handle our frequency */
1057 prescale = idx + 1;
1058 cmatch = (unsigned)(STK500_XTAL / (2 * fosc * ps[idx])) - 1;
1059 break;
1060 }
1061 }
1062 if (idx == sizeof(ps) / sizeof(ps[0])) {
1063 avrdude_message(MSG_INFO, "%s: stk500_set_fosc(): f = %u Hz too low, %u Hz min\n",
1064 progname, fosc, STK500_XTAL / (256 * 1024 * 2));
1065 return -1;
1066 }
1067 }
1068
1069 if ((rc = stk500_setparm(pgm, Parm_STK_OSC_PSCALE, prescale)) != 0
1070 || (rc = stk500_setparm(pgm, Parm_STK_OSC_CMATCH, cmatch)) != 0)
1071 return rc;
1072
1073 return 0;
1074}
static int stk500_setparm(PROGRAMMER *pgm, unsigned parm, unsigned value)
Definition stk500.c:1164

References avrdude_message(), MSG_INFO, Parm_STK_OSC_CMATCH, Parm_STK_OSC_PSCALE, pgm, progname, stk500_setparm(), and STK500_XTAL.

Referenced by stk500_initpgm().

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

◆ stk500_set_sck_period()

static int stk500_set_sck_period ( PROGRAMMER pgm,
double  v 
)
static
1085{
1086 int dur;
1087 double min, max;
1088
1089 min = 8.0 / STK500_XTAL;
1090 max = 255 * min;
1091 dur = (int)(v / min + 0.5);
1092
1093 if (v < min) {
1094 dur = 1;
1095 avrdude_message(MSG_INFO, "%s: stk500_set_sck_period(): p = %.1f us too small, using %.1f us\n",
1096 progname, v / 1e-6, dur * min / 1e-6);
1097 } else if (v > max) {
1098 dur = 255;
1099 avrdude_message(MSG_INFO, "%s: stk500_set_sck_period(): p = %.1f us too large, using %.1f us\n",
1100 progname, v / 1e-6, dur * min / 1e-6);
1101 }
1102
1104}
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() max(const half &a, const half &b)
Definition Half.h:516

References avrdude_message(), MSG_INFO, Parm_STK_SCK_DURATION, pgm, progname, stk500_setparm(), and STK500_XTAL.

Referenced by stk500_initpgm().

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

◆ stk500_set_varef()

static int stk500_set_varef ( PROGRAMMER pgm,
unsigned int  chan,
double  v 
)
static
1007{
1008 unsigned uaref, utarg;
1009
1010 uaref = (unsigned)((v + 0.049) * 10);
1011
1012 if (stk500_getparm(pgm, Parm_STK_VTARGET, &utarg) != 0) {
1013 avrdude_message(MSG_INFO, "%s: stk500_set_varef(): cannot obtain V[target]\n",
1014 progname);
1015 return -1;
1016 }
1017
1018 if (uaref > utarg) {
1019 avrdude_message(MSG_INFO, "%s: stk500_set_varef(): V[aref] must not be greater than "
1020 "V[target] = %.1f\n",
1021 progname, utarg / 10.0);
1022 return -1;
1023 }
1024 return stk500_setparm(pgm, Parm_STK_VADJUST, uaref);
1025}

References avrdude_message(), MSG_INFO, Parm_STK_VADJUST, Parm_STK_VTARGET, pgm, progname, stk500_getparm(), and stk500_setparm().

Referenced by stk500_initpgm().

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

◆ stk500_set_vtarget()

static int stk500_set_vtarget ( PROGRAMMER pgm,
double  v 
)
static
983{
984 unsigned uaref, utarg;
985
986 utarg = (unsigned)((v + 0.049) * 10);
987
988 if (stk500_getparm(pgm, Parm_STK_VADJUST, &uaref) != 0) {
989 avrdude_message(MSG_INFO, "%s: stk500_set_vtarget(): cannot obtain V[aref]\n",
990 progname);
991 return -1;
992 }
993
994 if (uaref > utarg) {
995 avrdude_message(MSG_INFO, "%s: stk500_set_vtarget(): reducing V[aref] from %.1f to %.1f\n",
996 progname, uaref / 10.0, v);
998 != 0)
999 return -1;
1000 }
1001 return stk500_setparm(pgm, Parm_STK_VTARGET, utarg);
1002}

References avrdude_message(), MSG_INFO, Parm_STK_VADJUST, Parm_STK_VTARGET, pgm, progname, stk500_getparm(), and stk500_setparm().

Referenced by stk500_initpgm().

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

◆ stk500_setparm()

static int stk500_setparm ( PROGRAMMER pgm,
unsigned  parm,
unsigned  value 
)
static
1165{
1166 unsigned char buf[16];
1167 int tries = 0;
1168
1169 retry:
1170 tries++;
1171 buf[0] = Cmnd_STK_SET_PARAMETER;
1172 buf[1] = parm;
1173 buf[2] = value;
1174 buf[3] = Sync_CRC_EOP;
1175
1176 stk500_send(pgm, buf, 4);
1177
1178 if (stk500_recv(pgm, buf, 1) < 0)
1179 return -1;
1180 if (buf[0] == Resp_STK_NOSYNC) {
1181 if (tries > 33) {
1182 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): can't get into sync\n",
1183 progname);
1184 return -1;
1185 }
1186 if (stk500_getsync(pgm) < 0)
1187 return -1;
1188 goto retry;
1189 }
1190 else if (buf[0] != Resp_STK_INSYNC) {
1191 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): (a) protocol error, "
1192 "expect=0x%02x, resp=0x%02x\n",
1193 progname, Resp_STK_INSYNC, buf[0]);
1194 return -2;
1195 }
1196
1197 if (stk500_recv(pgm, buf, 1) < 0)
1198 return -1;
1199 if (buf[0] == Resp_STK_OK)
1200 return 0;
1201
1202 parm = buf[0]; /* if not STK_OK, we've been echoed parm here */
1203 if (stk500_recv(pgm, buf, 1) < 0)
1204 return -1;
1205 if (buf[0] == Resp_STK_FAILED) {
1206 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): parameter 0x%02x failed\n",
1207 progname, parm);
1208 return -3;
1209 }
1210 else {
1211 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): (a) protocol error, "
1212 "expect=0x%02x, resp=0x%02x\n",
1213 progname, Resp_STK_INSYNC, buf[0]);
1214 return -3;
1215 }
1216}
#define Cmnd_STK_SET_PARAMETER
Definition stk500_private.h:39

References avrdude_message(), Cmnd_STK_SET_PARAMETER, MSG_INFO, pgm, progname, Resp_STK_FAILED, Resp_STK_INSYNC, Resp_STK_NOSYNC, Resp_STK_OK, stk500_getsync(), stk500_recv(), stk500_send(), and Sync_CRC_EOP.

Referenced by stk500_set_fosc(), stk500_set_sck_period(), stk500_set_varef(), and stk500_set_vtarget().

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

◆ stk500_setup()

static void stk500_setup ( PROGRAMMER pgm)
static
1303{
1304 if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
1305 avrdude_message(MSG_INFO, "%s: stk500_setup(): Out of memory allocating private data\n",
1306 progname);
1307 return;
1308 }
1309 memset(pgm->cookie, 0, sizeof(struct pdata));
1310 PDATA(pgm)->ext_addr_byte = 0xff; /* Ensures it is programmed before
1311 * first memory address */
1312}
void * malloc(YYSIZE_T)
Definition avr910.c:50
void * cookie
Definition libavrdude.h:689

References avrdude_message(), programmer_t::cookie, malloc(), MSG_INFO, PDATA, pgm, and progname.

Referenced by stk500_initpgm().

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

◆ stk500_teardown()

static void stk500_teardown ( PROGRAMMER pgm)
static
1315{
1316 free(pgm->cookie);
1317}
void free(void *)

References programmer_t::cookie, free(), and pgm.

Referenced by stk500_initpgm().

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

Variable Documentation

◆ stk500_desc

const char stk500_desc[] = "Atmel STK500 Version 1.x firmware"