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

Go to the source code of this file.

Classes

struct  pdata
 

Macros

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

Functions

static void butterfly_setup (PROGRAMMER *pgm)
 
static void butterfly_teardown (PROGRAMMER *pgm)
 
static int butterfly_send (PROGRAMMER *pgm, char *buf, size_t len)
 
static int butterfly_recv (PROGRAMMER *pgm, char *buf, size_t len)
 
static int butterfly_drain (PROGRAMMER *pgm, int display)
 
static int butterfly_vfy_cmd_sent (PROGRAMMER *pgm, char *errmsg)
 
static int butterfly_rdy_led (PROGRAMMER *pgm, int value)
 
static int butterfly_err_led (PROGRAMMER *pgm, int value)
 
static int butterfly_pgm_led (PROGRAMMER *pgm, int value)
 
static int butterfly_vfy_led (PROGRAMMER *pgm, int value)
 
static int butterfly_chip_erase (PROGRAMMER *pgm, AVRPART *p)
 
static void butterfly_enter_prog_mode (PROGRAMMER *pgm)
 
static void butterfly_leave_prog_mode (PROGRAMMER *pgm)
 
static int butterfly_program_enable (PROGRAMMER *pgm, AVRPART *p)
 
static void butterfly_powerup (PROGRAMMER *pgm)
 
static void butterfly_powerdown (PROGRAMMER *pgm)
 
static int butterfly_initialize (PROGRAMMER *pgm, AVRPART *p)
 
static void butterfly_disable (PROGRAMMER *pgm)
 
static void butterfly_enable (PROGRAMMER *pgm)
 
static int butterfly_open (PROGRAMMER *pgm, char *port)
 
static void butterfly_close (PROGRAMMER *pgm)
 
static void butterfly_display (PROGRAMMER *pgm, const char *p)
 
static void butterfly_set_addr (PROGRAMMER *pgm, unsigned long addr)
 
static void butterfly_set_extaddr (PROGRAMMER *pgm, unsigned long addr)
 
static int butterfly_write_byte (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char value)
 
static int butterfly_read_byte_flash (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char *value)
 
static int butterfly_read_byte_eeprom (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char *value)
 
static int butterfly_page_erase (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int addr)
 
static int butterfly_read_byte (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char *value)
 
static int butterfly_paged_write (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
 
static int butterfly_paged_load (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
 
static int butterfly_read_sig_bytes (PROGRAMMER *pgm, AVRPART *p, AVRMEM *m)
 
void butterfly_initpgm (PROGRAMMER *pgm)
 
void butterfly_mk_initpgm (PROGRAMMER *pgm)
 

Variables

const char butterfly_desc [] = "Atmel Butterfly evaluation board; Atmel AppNotes AVR109, AVR911"
 
const char butterfly_mk_desc [] = "Mikrokopter.de Butterfly"
 

Macro Definition Documentation

◆ IS_BUTTERFLY_MK

#define IS_BUTTERFLY_MK   0x0001

◆ PDATA

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

Function Documentation

◆ butterfly_chip_erase()

static int butterfly_chip_erase ( PROGRAMMER pgm,
AVRPART p 
)
static
155{
156 butterfly_send(pgm, "e", 1);
157 if (butterfly_vfy_cmd_sent(pgm, "chip erase") < 0)
158 return -1;
159
160 return 0;
161}
static int butterfly_send(PROGRAMMER *pgm, char *buf, size_t len)
Definition butterfly.c:79
static int butterfly_vfy_cmd_sent(PROGRAMMER *pgm, char *errmsg)
Definition butterfly.c:105
static PROGRAMMER * pgm
Definition main.c:192

References butterfly_send(), butterfly_vfy_cmd_sent(), and pgm.

Referenced by butterfly_initpgm().

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

◆ butterfly_close()

static void butterfly_close ( PROGRAMMER pgm)
static
410{
411 /* "exit programmer" */
412 butterfly_send(pgm, "E", 1);
413 butterfly_vfy_cmd_sent(pgm, "exit bootloader");
414
416 pgm->fd.ifd = -1;
417}
#define serial_close
Definition libavrdude.h:576
int ifd
Definition libavrdude.h:522
union filedescriptor fd
Definition libavrdude.h:637

References butterfly_send(), butterfly_vfy_cmd_sent(), programmer_t::fd, filedescriptor::ifd, pgm, and serial_close.

Referenced by butterfly_initpgm().

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

◆ butterfly_disable()

static void butterfly_disable ( PROGRAMMER pgm)
static
372{
374
375 return;
376}
static void butterfly_leave_prog_mode(PROGRAMMER *pgm)
Definition butterfly.c:171

References butterfly_leave_prog_mode(), and pgm.

Referenced by butterfly_initpgm().

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

◆ butterfly_display()

static void butterfly_display ( PROGRAMMER pgm,
const char *  p 
)
static
421{
422 return;
423}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_drain()

static int butterfly_drain ( PROGRAMMER pgm,
int  display 
)
static
100{
101 return serial_drain(&pgm->fd, display);
102}
#define serial_drain
Definition libavrdude.h:579

References programmer_t::fd, pgm, and serial_drain.

Referenced by butterfly_initialize(), and butterfly_open().

+ Here is the caller graph for this function:

◆ butterfly_enable()

static void butterfly_enable ( PROGRAMMER pgm)
static
380{
381 return;
382}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_enter_prog_mode()

static void butterfly_enter_prog_mode ( PROGRAMMER pgm)
static
165{
166 butterfly_send(pgm, "P", 1);
167 butterfly_vfy_cmd_sent(pgm, "enter prog mode");
168}

References butterfly_send(), butterfly_vfy_cmd_sent(), and pgm.

Referenced by butterfly_initialize().

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

◆ butterfly_err_led()

static int butterfly_err_led ( PROGRAMMER pgm,
int  value 
)
static
128{
129 /* Do nothing. */
130
131 return 0;
132}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_initialize()

static int butterfly_initialize ( PROGRAMMER pgm,
AVRPART p 
)
static
214{
215 char id[8];
216 char sw[2];
217 char hw[2];
218 char buf[10];
219 char type;
220 char c, devtype_1st;
221
222 /*
223 * Send some ESC to activate butterfly bootloader. This is not needed
224 * for plain avr109 bootloaders but does not harm there either.
225 */
226 avrdude_message(MSG_INFO, "Connecting to programmer: ");
227 if (pgm->flag & IS_BUTTERFLY_MK)
228 {
229 char mk_reset_cmd[6] = {"#aR@S\r"};
230 unsigned char mk_timeout = 0;
231
232 putc('.', stderr);
233 butterfly_send(pgm, mk_reset_cmd, sizeof(mk_reset_cmd));
234 usleep(20000);
235
236 do
237 {
238 c = 27;
239 butterfly_send(pgm, &c, 1);
240 usleep(20000);
241 c = 0xaa;
242 usleep(80000);
243 butterfly_send(pgm, &c, 1);
244 if (mk_timeout % 10 == 0) putc('.', stderr);
245 } while (mk_timeout++ < 10);
246
247 butterfly_recv(pgm, &c, 1);
248 if ( c != 'M' && c != '?')
249 {
250 avrdude_message(MSG_INFO, "\nConnection FAILED.");
251 return -1;
252 }
253 else
254 {
255 id[0] = 'M'; id[1] = 'K'; id[2] = '2'; id[3] = 0;
256 }
257 }
258 else
259 {
260 do {
261 putc('.', stderr);
262 butterfly_send(pgm, "\033", 1);
264 butterfly_send(pgm, "S", 1);
265 butterfly_recv(pgm, &c, 1);
266 if (c != '?') {
267 putc('\n', stderr);
268 /*
269 * Got a useful response, continue getting the programmer
270 * identifier. Programmer returns exactly 7 chars _without_
271 * the null.
272 */
273 id[0] = c;
274 butterfly_recv(pgm, &id[1], sizeof(id)-2);
275 id[sizeof(id)-1] = '\0';
276 }
277 } while (c == '?');
278 }
279
280 /* Get the HW and SW versions to see if the programmer is present. */
282
283 butterfly_send(pgm, "V", 1);
284 butterfly_recv(pgm, sw, sizeof(sw));
285
286 butterfly_send(pgm, "v", 1);
287 butterfly_recv(pgm, hw, 1); /* first, read only _one_ byte */
288 if (hw[0]!='?') {
289 butterfly_recv(pgm, &hw[1], 1);/* now, read second byte */
290 };
291
292 /* Get the programmer type (serial or parallel). Expect serial. */
293
294 butterfly_send(pgm, "p", 1);
295 butterfly_recv(pgm, &type, 1);
296
297 avrdude_message(MSG_INFO, "Found programmer: Id = \"%s\"; type = %c\n", id, type);
298 avrdude_message(MSG_INFO, " Software Version = %c.%c; ", sw[0], sw[1]);
299 if (hw[0]=='?') {
300 avrdude_message(MSG_INFO, "No Hardware Version given.\n");
301 } else {
302 avrdude_message(MSG_INFO, "Hardware Version = %c.%c\n", hw[0], hw[1]);
303 };
304
305 /* See if programmer supports autoincrement of address. */
306
307 butterfly_send(pgm, "a", 1);
308 butterfly_recv(pgm, &PDATA(pgm)->has_auto_incr_addr, 1);
309 if (PDATA(pgm)->has_auto_incr_addr == 'Y')
310 avrdude_message(MSG_INFO, "Programmer supports auto addr increment.\n");
311
312 /* Check support for buffered memory access, abort if not available */
313
314 butterfly_send(pgm, "b", 1);
315 butterfly_recv(pgm, &c, 1);
316 if (c != 'Y') {
317 avrdude_message(MSG_INFO, "%s: error: buffered memory access not supported. Maybe it isn't\n"\
318 "a butterfly/AVR109 but a AVR910 device?\n", progname);
319 return -1;
320 };
321 butterfly_recv(pgm, &c, 1);
322 PDATA(pgm)->buffersize = (unsigned int)(unsigned char)c<<8;
323 butterfly_recv(pgm, &c, 1);
324 PDATA(pgm)->buffersize += (unsigned int)(unsigned char)c;
325 avrdude_message(MSG_INFO, "Programmer supports buffered memory access with buffersize=%i bytes.\n",
326 PDATA(pgm)->buffersize);
327
328 /* Get list of devices that the programmer supports. */
329
330 butterfly_send(pgm, "t", 1);
331 avrdude_message(MSG_INFO, "\nProgrammer supports the following devices:\n");
332 devtype_1st = 0;
333 while (1) {
334 butterfly_recv(pgm, &c, 1);
335 if (devtype_1st == 0)
336 devtype_1st = c;
337
338 if (c == 0)
339 break;
340 avrdude_message(MSG_INFO, " Device code: 0x%02x\n", (unsigned int)(unsigned char)c);
341 };
343
344 /* Tell the programmer which part we selected.
345 According to the AVR109 code, this is ignored by the bootloader. As
346 some early versions might not properly ignore it, rather pick up the
347 first device type as reported above than anything out of avrdude.conf,
348 so to avoid a potential conflict. There appears to be no general
349 agreement on AVR910 device IDs beyond the ones from the original
350 appnote 910. */
351
352 buf[0] = 'T';
353 buf[1] = devtype_1st;
354
355 butterfly_send(pgm, buf, 2);
356 if (butterfly_vfy_cmd_sent(pgm, "select device") < 0)
357 return -1;
358
359 if (verbose)
360 avrdude_message(MSG_INFO, "%s: devcode selected: 0x%02x\n",
361 progname, (unsigned)buf[1]);
362
365
366 return 0;
367}
int verbose
Definition main.c:198
#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 int butterfly_drain(PROGRAMMER *pgm, int display)
Definition butterfly.c:99
static int butterfly_recv(PROGRAMMER *pgm, char *buf, size_t len)
Definition butterfly.c:85
static void butterfly_enter_prog_mode(PROGRAMMER *pgm)
Definition butterfly.c:164
#define PDATA(pgm)
Definition butterfly.c:61
#define IS_BUTTERFLY_MK
Definition butterfly.c:208
char flag
Definition libavrdude.h:690
int usleep(unsigned usec)
Definition unistd.cpp:13

References avrdude_message(), butterfly_drain(), butterfly_enter_prog_mode(), butterfly_recv(), butterfly_send(), butterfly_vfy_cmd_sent(), programmer_t::flag, IS_BUTTERFLY_MK, MSG_INFO, PDATA, pgm, progname, usleep(), and verbose.

Referenced by butterfly_initpgm().

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

◆ butterfly_initpgm()

void butterfly_initpgm ( PROGRAMMER pgm)
718{
719 strcpy(pgm->type, "butterfly");
720
721 /*
722 * mandatory functions
723 */
740
741 /*
742 * optional functions
743 */
744
748
750
753 pgm->flag = 0;
754}
static int butterfly_paged_write(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
Definition butterfly.c:595
static int butterfly_open(PROGRAMMER *pgm, char *port)
Definition butterfly.c:385
static int butterfly_err_led(PROGRAMMER *pgm, int value)
Definition butterfly.c:127
static int butterfly_pgm_led(PROGRAMMER *pgm, int value)
Definition butterfly.c:135
static void butterfly_close(PROGRAMMER *pgm)
Definition butterfly.c:409
static void butterfly_display(PROGRAMMER *pgm, const char *p)
Definition butterfly.c:420
static void butterfly_setup(PROGRAMMER *pgm)
Definition butterfly.c:63
static void butterfly_teardown(PROGRAMMER *pgm)
Definition butterfly.c:74
static int butterfly_rdy_led(PROGRAMMER *pgm, int value)
Definition butterfly.c:119
static int butterfly_initialize(PROGRAMMER *pgm, AVRPART *p)
Definition butterfly.c:213
static int butterfly_page_erase(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int addr)
Definition butterfly.c:548
static int butterfly_read_byte(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char *value)
Definition butterfly.c:559
static void butterfly_powerdown(PROGRAMMER *pgm)
Definition butterfly.c:201
static int butterfly_program_enable(PROGRAMMER *pgm, AVRPART *p)
Definition butterfly.c:181
static void butterfly_disable(PROGRAMMER *pgm)
Definition butterfly.c:371
static int butterfly_paged_load(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes)
Definition butterfly.c:650
static void butterfly_enable(PROGRAMMER *pgm)
Definition butterfly.c:379
static int butterfly_vfy_led(PROGRAMMER *pgm, int value)
Definition butterfly.c:143
static int butterfly_read_sig_bytes(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m)
Definition butterfly.c:696
static int butterfly_chip_erase(PROGRAMMER *pgm, AVRPART *p)
Definition butterfly.c:154
static int butterfly_write_byte(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char value)
Definition butterfly.c:454
static void butterfly_powerup(PROGRAMMER *pgm)
Definition butterfly.c:190
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(* 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
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
void(* powerup)(struct programmer_t *pgm)
Definition libavrdude.h:647
void(* teardown)(struct programmer_t *pgm)
Definition libavrdude.h:685
int(* vfy_led)(struct programmer_t *pgm, int value)
Definition libavrdude.h:642
int(* pgm_led)(struct programmer_t *pgm, int value)
Definition libavrdude.h:641
void(* enable)(struct programmer_t *pgm)
Definition libavrdude.h:645
int(* rdy_led)(struct programmer_t *pgm, int value)
Definition libavrdude.h:639
int(* err_led)(struct programmer_t *pgm, int value)
Definition libavrdude.h:640
int(* program_enable)(struct programmer_t *pgm, AVRPART *p)
Definition libavrdude.h:649
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(* page_erase)(struct programmer_t *pgm, AVRPART *p, AVRMEM *m, unsigned int baseaddr)
Definition libavrdude.h:665
char type[PGM_TYPELEN]
Definition libavrdude.h:619
int(* initialize)(struct programmer_t *pgm, AVRPART *p)
Definition libavrdude.h:643
void(* close)(struct programmer_t *pgm)
Definition libavrdude.h:658
int(* chip_erase)(struct programmer_t *pgm, AVRPART *p)
Definition libavrdude.h:650
void(* disable)(struct programmer_t *pgm)
Definition libavrdude.h:646
void(* powerdown)(struct programmer_t *pgm)
Definition libavrdude.h:648

References butterfly_chip_erase(), butterfly_close(), butterfly_disable(), butterfly_display(), butterfly_enable(), butterfly_err_led(), butterfly_initialize(), butterfly_open(), butterfly_page_erase(), butterfly_paged_load(), butterfly_paged_write(), butterfly_pgm_led(), butterfly_powerdown(), butterfly_powerup(), butterfly_program_enable(), butterfly_rdy_led(), butterfly_read_byte(), butterfly_read_sig_bytes(), butterfly_setup(), butterfly_teardown(), butterfly_vfy_led(), butterfly_write_byte(), programmer_t::chip_erase, programmer_t::close, programmer_t::disable, programmer_t::display, programmer_t::enable, programmer_t::err_led, programmer_t::flag, programmer_t::initialize, programmer_t::open, programmer_t::page_erase, programmer_t::paged_load, programmer_t::paged_write, pgm, programmer_t::pgm_led, programmer_t::powerdown, programmer_t::powerup, programmer_t::program_enable, programmer_t::rdy_led, programmer_t::read_byte, programmer_t::read_sig_bytes, programmer_t::setup, programmer_t::teardown, programmer_t::type, programmer_t::vfy_led, and programmer_t::write_byte.

Referenced by butterfly_mk_initpgm().

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

◆ butterfly_leave_prog_mode()

static void butterfly_leave_prog_mode ( PROGRAMMER pgm)
static
172{
173 butterfly_send(pgm, "L", 1);
174 butterfly_vfy_cmd_sent(pgm, "leave prog mode");
175}

References butterfly_send(), butterfly_vfy_cmd_sent(), and pgm.

Referenced by butterfly_disable().

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

◆ butterfly_mk_initpgm()

void butterfly_mk_initpgm ( PROGRAMMER pgm)
759{
761 strcpy(pgm->type, "butterfly_mk");
763}
void butterfly_initpgm(PROGRAMMER *pgm)
Definition butterfly.c:717

References butterfly_initpgm(), programmer_t::flag, IS_BUTTERFLY_MK, pgm, and programmer_t::type.

+ Here is the call graph for this function:

◆ butterfly_open()

static int butterfly_open ( PROGRAMMER pgm,
char *  port 
)
static
386{
387 union pinfo pinfo;
388 strcpy(pgm->port, port);
389 /*
390 * If baudrate was not specified use 19200 Baud
391 */
392 if(pgm->baudrate == 0) {
393 pgm->baudrate = 19200;
394 }
396 if (serial_open(port, pinfo, &pgm->fd)==-1) {
397 return -1;
398 }
399
400 /*
401 * drain any extraneous input
402 */
403 butterfly_drain (pgm, 0);
404
405 return 0;
406}
#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, butterfly_drain(), programmer_t::fd, pgm, programmer_t::port, and serial_open.

Referenced by butterfly_initpgm().

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

◆ butterfly_page_erase()

static int butterfly_page_erase ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned int  addr 
)
static
549{
550 if (strcmp(m->desc, "flash") == 0)
551 return -1; /* not supported */
552 if (strcmp(m->desc, "eeprom") == 0)
553 return 0; /* nothing to do */
554 avrdude_message(MSG_INFO, "%s: butterfly_page_erase() called on memory type \"%s\"\n",
555 progname, m->desc);
556 return -1;
557}
char desc[AVR_MEMDESCLEN]
Definition libavrdude.h:284

References avrdude_message(), avrmem::desc, MSG_INFO, and progname.

Referenced by butterfly_initpgm().

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

◆ butterfly_paged_load()

static int butterfly_paged_load ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned int  page_size,
unsigned int  addr,
unsigned int  n_bytes 
)
static
653{
654 unsigned int max_addr = addr + n_bytes;
655 int rd_size = 2;
656 int blocksize = PDATA(pgm)->buffersize;
657 int use_ext_addr = m->op[AVR_OP_LOAD_EXT_ADDR] != NULL;
658
659 /* check parameter syntax: only "flash" or "eeprom" is allowed */
660 if (strcmp(m->desc, "flash") && strcmp(m->desc, "eeprom"))
661 return -2;
662
663 if (m->desc[0] == 'e')
664 rd_size = blocksize = 1; /* Read from eeprom single bytes only */
665
666 { /* use buffered mode */
667 char cmd[4];
668
669 cmd[0] = 'g';
670 cmd[3] = toupper((int)(m->desc[0]));
671
672 if (use_ext_addr) {
673 butterfly_set_extaddr(pgm, addr / rd_size);
674 } else {
675 butterfly_set_addr(pgm, addr / rd_size);
676 }
677 while (addr < max_addr) {
678 if ((max_addr - addr) < (unsigned)blocksize) {
679 blocksize = max_addr - addr;
680 };
681 cmd[1] = (blocksize >> 8) & 0xff;
682 cmd[2] = blocksize & 0xff;
683
685 butterfly_recv(pgm, (char *)&m->buf[addr], blocksize);
686
687 addr += blocksize;
688 } /* while */
689 }
690
691 return addr * rd_size;
692}
static void butterfly_set_addr(PROGRAMMER *pgm, unsigned long addr)
Definition butterfly.c:426
static void butterfly_set_extaddr(PROGRAMMER *pgm, unsigned long addr)
Definition butterfly.c:439
@ AVR_OP_LOAD_EXT_ADDR
Definition libavrdude.h:154
unsigned char * buf
Definition libavrdude.h:304
OPCODE * op[AVR_OP_MAX]
Definition libavrdude.h:306
struct command cmd[]
Definition term.c:94

References AVR_OP_LOAD_EXT_ADDR, avrmem::buf, butterfly_recv(), butterfly_send(), butterfly_set_addr(), butterfly_set_extaddr(), cmd, avrmem::desc, avrmem::op, PDATA, and pgm.

Referenced by butterfly_initpgm().

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

◆ butterfly_paged_write()

static int butterfly_paged_write ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned int  page_size,
unsigned int  addr,
unsigned int  n_bytes 
)
static
598{
599 unsigned int max_addr = addr + n_bytes;
600 char *cmd;
601 unsigned int blocksize = PDATA(pgm)->buffersize;
602 int use_ext_addr = m->op[AVR_OP_LOAD_EXT_ADDR] != NULL;
603 unsigned int wr_size = 2;
604
605 if (strcmp(m->desc, "flash") && strcmp(m->desc, "eeprom"))
606 return -2;
607
608 if (m->desc[0] == 'e')
609 wr_size = blocksize = 1; /* Write to eeprom single bytes only */
610
611 if (use_ext_addr) {
612 butterfly_set_extaddr(pgm, addr / wr_size);
613 } else {
614 butterfly_set_addr(pgm, addr / wr_size);
615 }
616
617#if 0
618 usleep(1000000);
619 butterfly_send(pgm, "y", 1);
620 if (butterfly_vfy_cmd_sent(pgm, "clear LED") < 0)
621 return -1;
622#endif
623
624 cmd = malloc(4+blocksize);
625 if (!cmd) return -1;
626 cmd[0] = 'B';
627 cmd[3] = toupper((int)(m->desc[0]));
628
629 while (addr < max_addr) {
630 if ((max_addr - addr) < blocksize) {
631 blocksize = max_addr - addr;
632 };
633 memcpy(&cmd[4], &m->buf[addr], blocksize);
634 cmd[1] = (blocksize >> 8) & 0xff;
635 cmd[2] = blocksize & 0xff;
636
637 butterfly_send(pgm, cmd, 4+blocksize);
638 if (butterfly_vfy_cmd_sent(pgm, "write block") < 0)
639 return -1;
640
641 addr += blocksize;
642 } /* while */
643 free(cmd);
644
645 return addr;
646}
void * malloc(YYSIZE_T)
void free(void *)

References AVR_OP_LOAD_EXT_ADDR, avrmem::buf, butterfly_send(), butterfly_set_addr(), butterfly_set_extaddr(), butterfly_vfy_cmd_sent(), cmd, avrmem::desc, free(), malloc(), avrmem::op, PDATA, pgm, and usleep().

Referenced by butterfly_initpgm().

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

◆ butterfly_pgm_led()

static int butterfly_pgm_led ( PROGRAMMER pgm,
int  value 
)
static
136{
137 /* Do nothing. */
138
139 return 0;
140}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_powerdown()

static void butterfly_powerdown ( PROGRAMMER pgm)
static
202{
203 /* Do nothing. */
204
205 return;
206}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_powerup()

static void butterfly_powerup ( PROGRAMMER pgm)
static
191{
192 /* Do nothing. */
193
194 return;
195}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_program_enable()

static int butterfly_program_enable ( PROGRAMMER pgm,
AVRPART p 
)
static
182{
183 return -1;
184}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_rdy_led()

static int butterfly_rdy_led ( PROGRAMMER pgm,
int  value 
)
static
120{
121 /* Do nothing. */
122
123 return 0;
124}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_read_byte()

static int butterfly_read_byte ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned long  addr,
unsigned char *  value 
)
static
561{
562 char cmd;
563
564 if (strcmp(m->desc, "flash") == 0) {
565 return butterfly_read_byte_flash(pgm, p, m, addr, value);
566 }
567
568 if (strcmp(m->desc, "eeprom") == 0) {
569 return butterfly_read_byte_eeprom(pgm, p, m, addr, value);
570 }
571
572 if (strcmp(m->desc, "lfuse") == 0) {
573 cmd = 'F';
574 }
575 else if (strcmp(m->desc, "hfuse") == 0) {
576 cmd = 'N';
577 }
578 else if (strcmp(m->desc, "efuse") == 0) {
579 cmd = 'Q';
580 }
581 else if (strcmp(m->desc, "lock") == 0) {
582 cmd = 'r';
583 }
584 else
585 return -1;
586
587 butterfly_send(pgm, &cmd, 1);
588 butterfly_recv(pgm, (char *)value, 1);
589
590 return *value == '?'? -1: 0;
591}
static int butterfly_read_byte_eeprom(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char *value)
Definition butterfly.c:539
static int butterfly_read_byte_flash(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m, unsigned long addr, unsigned char *value)
Definition butterfly.c:498

References butterfly_read_byte_eeprom(), butterfly_read_byte_flash(), butterfly_recv(), butterfly_send(), cmd, avrmem::desc, and pgm.

Referenced by butterfly_initpgm().

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

◆ butterfly_read_byte_eeprom()

static int butterfly_read_byte_eeprom ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned long  addr,
unsigned char *  value 
)
static
541{
542 butterfly_set_addr(pgm, addr);
543 butterfly_send(pgm, "g\000\001E", 4);
544 butterfly_recv(pgm, (char *)value, 1);
545 return 0;
546}

References butterfly_recv(), butterfly_send(), butterfly_set_addr(), and pgm.

Referenced by butterfly_read_byte().

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

◆ butterfly_read_byte_flash()

static int butterfly_read_byte_flash ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned long  addr,
unsigned char *  value 
)
static
500{
501 static int cached = 0;
502 static unsigned char cvalue;
503 static unsigned long caddr;
504 int use_ext_addr = m->op[AVR_OP_LOAD_EXT_ADDR] != NULL;
505
506 if (cached && ((caddr + 1) == addr)) {
507 *value = cvalue;
508 cached = 0;
509 }
510 else {
511 char buf[2];
512
513 if (use_ext_addr) {
514 butterfly_set_extaddr(pgm, addr >> 1);
515 } else {
516 butterfly_set_addr(pgm, addr >> 1);
517 }
518
519 butterfly_send(pgm, "g\000\002F", 4);
520
521 /* Read back the program mem word (MSB first) */
522 butterfly_recv(pgm, buf, sizeof(buf));
523
524 if ((addr & 0x01) == 0) {
525 *value = buf[0];
526 cached = 1;
527 cvalue = buf[1];
528 caddr = addr;
529 }
530 else {
531 *value = buf[1];
532 }
533 }
534
535 return 0;
536}

References AVR_OP_LOAD_EXT_ADDR, butterfly_recv(), butterfly_send(), butterfly_set_addr(), butterfly_set_extaddr(), avrmem::op, and pgm.

Referenced by butterfly_read_byte().

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

◆ butterfly_read_sig_bytes()

static int butterfly_read_sig_bytes ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m 
)
static
697{
698 unsigned char tmp;
699
700 if (m->size < 3) {
701 avrdude_message(MSG_INFO, "%s: memsize too small for sig byte read", progname);
702 return -1;
703 }
704
705 butterfly_send(pgm, "s", 1);
706 butterfly_recv(pgm, (char *)m->buf, 3);
707 /* Returned signature has wrong order. */
708 tmp = m->buf[2];
709 m->buf[2] = m->buf[0];
710 m->buf[0] = tmp;
711
712 return 3;
713}
int size
Definition libavrdude.h:286

References avrdude_message(), avrmem::buf, butterfly_recv(), butterfly_send(), MSG_INFO, pgm, progname, and avrmem::size.

Referenced by butterfly_initpgm().

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

◆ butterfly_recv()

static int butterfly_recv ( PROGRAMMER pgm,
char *  buf,
size_t  len 
)
static
86{
87 int rv;
88
89 rv = serial_recv(&pgm->fd, (unsigned char *)buf, len);
90 if (rv < 0) {
91 avrdude_message(MSG_INFO, "%s: butterfly_recv(): programmer is not responding\n",
92 progname);
93 return -1;
94 }
95 return 0;
96}
#define serial_recv
Definition libavrdude.h:578

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

Referenced by butterfly_initialize(), butterfly_paged_load(), butterfly_read_byte(), butterfly_read_byte_eeprom(), butterfly_read_byte_flash(), butterfly_read_sig_bytes(), and butterfly_vfy_cmd_sent().

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

◆ butterfly_send()

static int butterfly_send ( PROGRAMMER pgm,
char *  buf,
size_t  len 
)
static
80{
81 return serial_send(&pgm->fd, (unsigned char *)buf, len);
82}
#define serial_send
Definition libavrdude.h:577

References programmer_t::fd, pgm, and serial_send.

Referenced by butterfly_chip_erase(), butterfly_close(), butterfly_enter_prog_mode(), butterfly_initialize(), butterfly_leave_prog_mode(), butterfly_paged_load(), butterfly_paged_write(), butterfly_read_byte(), butterfly_read_byte_eeprom(), butterfly_read_byte_flash(), butterfly_read_sig_bytes(), butterfly_set_addr(), butterfly_set_extaddr(), and butterfly_write_byte().

+ Here is the caller graph for this function:

◆ butterfly_set_addr()

static void butterfly_set_addr ( PROGRAMMER pgm,
unsigned long  addr 
)
static
427{
428 char cmd[3];
429
430 cmd[0] = 'A';
431 cmd[1] = (addr >> 8) & 0xff;
432 cmd[2] = addr & 0xff;
433
434 butterfly_send(pgm, cmd, sizeof(cmd));
435 butterfly_vfy_cmd_sent(pgm, "set addr");
436}

References butterfly_send(), butterfly_vfy_cmd_sent(), cmd, and pgm.

Referenced by butterfly_paged_load(), butterfly_paged_write(), butterfly_read_byte_eeprom(), butterfly_read_byte_flash(), and butterfly_write_byte().

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

◆ butterfly_set_extaddr()

static void butterfly_set_extaddr ( PROGRAMMER pgm,
unsigned long  addr 
)
static
440{
441 char cmd[4];
442
443 cmd[0] = 'H';
444 cmd[1] = (addr >> 16) & 0xff;
445 cmd[2] = (addr >> 8) & 0xff;
446 cmd[3] = addr & 0xff;
447
448 butterfly_send(pgm, cmd, sizeof(cmd));
449 butterfly_vfy_cmd_sent(pgm, "set extaddr");
450}

References butterfly_send(), butterfly_vfy_cmd_sent(), cmd, and pgm.

Referenced by butterfly_paged_load(), butterfly_paged_write(), butterfly_read_byte_flash(), and butterfly_write_byte().

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

◆ butterfly_setup()

static void butterfly_setup ( PROGRAMMER pgm)
static
64{
65 if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
66 // avrdude_message(MSG_INFO, "%s: butterfly_setup(): Out of memory allocating private data\n",
67 // progname);
68 // exit(1);
69 avrdude_oom("butterfly_setup(): Out of memory allocating private data\n");
70 }
71 memset(pgm->cookie, 0, sizeof(struct pdata));
72}
void avrdude_oom(const char *context)
Definition main.c:169
Definition avr910.c:50
void * cookie
Definition libavrdude.h:689

References avrdude_oom(), programmer_t::cookie, malloc(), and pgm.

Referenced by butterfly_initpgm().

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

◆ butterfly_teardown()

static void butterfly_teardown ( PROGRAMMER pgm)
static
75{
76 free(pgm->cookie);
77}

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

Referenced by butterfly_initpgm().

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

◆ butterfly_vfy_cmd_sent()

static int butterfly_vfy_cmd_sent ( PROGRAMMER pgm,
char *  errmsg 
)
static
106{
107 char c;
108
109 butterfly_recv(pgm, &c, 1);
110 if (c != '\r') {
111 avrdude_message(MSG_INFO, "%s: error: programmer did not respond to command: %s\n",
112 progname, errmsg);
113 return -1;
114 }
115 return 0;
116}

References avrdude_message(), butterfly_recv(), MSG_INFO, pgm, and progname.

Referenced by butterfly_chip_erase(), butterfly_close(), butterfly_enter_prog_mode(), butterfly_initialize(), butterfly_leave_prog_mode(), butterfly_paged_write(), butterfly_set_addr(), butterfly_set_extaddr(), and butterfly_write_byte().

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

◆ butterfly_vfy_led()

static int butterfly_vfy_led ( PROGRAMMER pgm,
int  value 
)
static
144{
145 /* Do nothing. */
146
147 return 0;
148}

Referenced by butterfly_initpgm().

+ Here is the caller graph for this function:

◆ butterfly_write_byte()

static int butterfly_write_byte ( PROGRAMMER pgm,
AVRPART p,
AVRMEM m,
unsigned long  addr,
unsigned char  value 
)
static
456{
457 char cmd[6];
458 int size;
459 int use_ext_addr = m->op[AVR_OP_LOAD_EXT_ADDR] != NULL;
460
461 if ((strcmp(m->desc, "flash") == 0) || (strcmp(m->desc, "eeprom") == 0))
462 {
463 cmd[0] = 'B';
464 cmd[1] = 0;
465 if ((cmd[3] = toupper((int)(m->desc[0]))) == 'E') { /* write to eeprom */
466 cmd[2] = 1;
467 cmd[4] = value;
468 size = 5;
469 } else { /* write to flash */
470 /* @@@ not yet implemented */
471 cmd[2] = 2;
472 size = 6;
473 return -1;
474 }
475 if (use_ext_addr) {
477 } else {
478 butterfly_set_addr(pgm, addr);
479 }
480 }
481 else if (strcmp(m->desc, "lock") == 0)
482 {
483 cmd[0] = 'l';
484 cmd[1] = value;
485 size = 2;
486 }
487 else
488 return -1;
489
490 butterfly_send(pgm, cmd, size);
491 if (butterfly_vfy_cmd_sent(pgm, "write byte") < 0)
492 return -1;
493
494 return 0;
495}
constexpr auto size(const C &c) -> decltype(c.size())
Definition span.hpp:183

References AVR_OP_LOAD_EXT_ADDR, butterfly_send(), butterfly_set_addr(), butterfly_set_extaddr(), butterfly_vfy_cmd_sent(), cmd, avrmem::desc, avrmem::op, and pgm.

Referenced by butterfly_initpgm().

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

Variable Documentation

◆ butterfly_desc

const char butterfly_desc[] = "Atmel Butterfly evaluation board; Atmel AppNotes AVR109, AVR911"

◆ butterfly_mk_desc

const char butterfly_mk_desc[] = "Mikrokopter.de Butterfly"