1 /*
2  * avrdude - A Downloader/Uploader for AVR device programmers
3  * Copyright (C) Joerg Wunsch
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /* $Id: libavrdude.h 1332 2014-08-18 21:43:08Z rliebscher $ */
20 
21 #ifndef libavrdude_h
22 #define libavrdude_h
23 
24 /* XXX should go away */
25 #include "ac_cfg.h"
26 
27 #include <stdio.h>
28 #include <limits.h>
29 #include <stdbool.h>
30 
31 /* lets try to select at least 32 bits */
32 #ifdef HAVE_STDINT_H
33 #include <stdint.h>
34 typedef uint32_t pinmask_t;
35 #else
36 #if UINT_MAX >= 0xFFFFFFFF
37 typedef unsigned int pinmask_t;
38 #else
39 typedef unsigned long pinmask_t;
40 #endif
41 #endif
42 
43 
44 /* formerly lists.h */
45 
46 /*----------------------------------------------------------------------
47   General purpose linked list routines - header file declarations.
48 
49   Author : Brian Dean
50   Date   : 10 January, 1990
51   ----------------------------------------------------------------------*/
52 
53 typedef void * LISTID;
54 typedef void * LNODEID;
55 
56 
57 /*----------------------------------------------------------------------
58   several defines to access the LIST structure as as stack or a queue
59   --- use for program readability
60   ----------------------------------------------------------------------*/
61 #define STACKID LISTID
62 #define SNODEID LNODEID
63 #define QUEUEID LISTID
64 #define QNODEID LNODEID
65 
66 
67 #define PUSH(s,d)    lins_n(s,d,1)   /* push 'd' onto the stack */
68 #define POP(s)       lrmv_n(s,1)     /* pop the stack */
69 #define LOOKSTACK(s) lget_n(s,1)     /* look at the top of the stack,
70 					but don't pop */
71 
72 
73 #define ENQUEUE(q,d) lins_n(q,d,1)   /* put 'd' on the end of the queue */
74 #define DEQUEUE(q)   lrmv(q)         /* remove next item from the front of
75 					the queue */
76 #define REQUEUE(q,d) ladd(q,d)       /* re-insert (push) item back on the
77 					front of the queue */
78 #define LOOKQUEUE(q) lget(q)         /* return next item on the queue,
79 					but don't dequeue */
80 #define QUEUELEN(q)  lsize(q)       /* length of the queue */
81 
82 
83 #define LISTADD(l,d) ladd(l,d)       /* add to end of the list */
84 #define LISTRMV(l,d) lrmv_d(l,d)     /* remove from end of the list */
85 
86 
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
91 /* .................... Function Prototypes .................... */
92 
93 LISTID     lcreat      ( void * liststruct, int poolsize );
94 void       ldestroy    ( LISTID lid );
95 void       ldestroy_cb ( LISTID lid, void (*ucleanup)(void * data_ptr) );
96 
97 LNODEID    lfirst ( LISTID  ); /* head of the list */
98 LNODEID    llast  ( LISTID  ); /* tail of the list */
99 LNODEID    lnext  ( LNODEID ); /* next item in the list */
100 LNODEID    lprev  ( LNODEID ); /* previous item in the list */
101 void     * ldata  ( LNODEID ); /* data at the current position */
102 int        lsize  ( LISTID  ); /* number of elements in the list */
103 
104 int        ladd     ( LISTID lid, void * p );
105 int        laddo    ( LISTID lid, void *p,
106 		      int (*compare)(const void *p1,const void *p2),
107 		      LNODEID * firstdup );
108 int        laddu    ( LISTID lid, void * p,
109 		      int (*compare)(const void *p1,const void *p2));
110 int        lins_n   ( LISTID lid, void * d, unsigned int n );
111 int        lins_ln  ( LISTID lid, LNODEID lnid, void * data_ptr );
112 
113 void     * lget    ( LISTID lid );
114 void     * lget_n  ( LISTID lid, unsigned int n );
115 LNODEID    lget_ln ( LISTID lid, unsigned int n );
116 
117 void     * lrmv    ( LISTID lid );
118 void     * lrmv_n  ( LISTID lid, unsigned int n );
119 void     * lrmv_ln ( LISTID lid, LNODEID lnid );
120 void     * lrmv_d  ( LISTID lid, void * data_ptr );
121 
122 LISTID     lcat    ( LISTID lid1, LISTID lid2 );
123 
124 void       lsort   ( LISTID lid, int (*compare)(void * p1, void * p2));
125 
126 void     * lsrch   ( LISTID lid, void * p, int (*compare)(void *p1,void *p2));
127 
128 int        lprint  ( FILE * f, LISTID lid );
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 /* formerly avrpart.h */
135 
136 /*
137  * AVR serial programming instructions
138  */
139 enum {
140   AVR_OP_READ,
141   AVR_OP_WRITE,
142   AVR_OP_READ_LO,
143   AVR_OP_READ_HI,
144   AVR_OP_WRITE_LO,
145   AVR_OP_WRITE_HI,
146   AVR_OP_LOADPAGE_LO,
147   AVR_OP_LOADPAGE_HI,
148   AVR_OP_LOAD_EXT_ADDR,
149   AVR_OP_WRITEPAGE,
150   AVR_OP_CHIP_ERASE,
151   AVR_OP_PGM_ENABLE,
152   AVR_OP_MAX
153 };
154 
155 
156 enum {
157   AVR_CMDBIT_IGNORE,  /* bit is ignored on input and output */
158   AVR_CMDBIT_VALUE,   /* bit is set to 0 or 1 for input or output */
159   AVR_CMDBIT_ADDRESS, /* this bit represents an input address bit */
160   AVR_CMDBIT_INPUT,   /* this bit is an input bit */
161   AVR_CMDBIT_OUTPUT   /* this bit is an output bit */
162 };
163 
164 enum { /* these are assigned to reset_disposition of AVRPART */
165   RESET_DEDICATED,    /* reset pin is dedicated */
166   RESET_IO            /* reset pin might be configured as an I/O pin */
167 };
168 
169 enum ctl_stack_t {
170   CTL_STACK_NONE,     /* no control stack defined */
171   CTL_STACK_PP,	      /* parallel programming control stack */
172   CTL_STACK_HVSP      /* high voltage serial programming control stack */
173 };
174 
175 /*
176  * serial programming instruction bit specifications
177  */
178 typedef struct cmdbit {
179   int          type;  /* AVR_CMDBIT_* */
180   int          bitno; /* which input bit to use for this command bit */
181   int          value; /* bit value if type == AVR_CMDBIT_VALUD */
182 } CMDBIT;
183 
184 typedef struct opcode {
185   CMDBIT        bit[32]; /* opcode bit specs */
186 } OPCODE;
187 
188 
189 #define AVRPART_SERIALOK       0x0001  /* part supports serial programming */
190 #define AVRPART_PARALLELOK     0x0002  /* part supports parallel programming */
191 #define AVRPART_PSEUDOPARALLEL 0x0004  /* part has pseudo parallel support */
192 #define AVRPART_HAS_JTAG       0x0008  /* part has a JTAG i/f */
193 #define AVRPART_ALLOWFULLPAGEBITSTREAM 0x0010 /* JTAG ICE mkII param. */
194 #define AVRPART_ENABLEPAGEPROGRAMMING 0x0020 /* JTAG ICE mkII param. */
195 #define AVRPART_HAS_DW         0x0040  /* part has a debugWire i/f */
196 #define AVRPART_HAS_PDI        0x0080  /* part has PDI i/f rather than ISP (ATxmega) */
197 #define AVRPART_AVR32          0x0100  /* part is in AVR32 family */
198 #define AVRPART_INIT_SMC       0x0200  /* part will undergo chip erase */
199 #define AVRPART_WRITE          0x0400  /* at least one write operation specified */
200 #define AVRPART_HAS_TPI        0x0800  /* part has TPI i/f rather than ISP (ATtiny4/5/9/10) */
201 #define AVRPART_IS_AT90S1200   0x1000  /* part is an AT90S1200 (needs special treatment) */
202 
203 #define AVR_DESCLEN 64
204 #define AVR_IDLEN   32
205 #define CTL_STACK_SIZE 32
206 #define FLASH_INSTR_SIZE 3
207 #define EEPROM_INSTR_SIZE 20
208 
209 #define TAG_ALLOCATED          1    /* memory byte is allocated */
210 
211 typedef struct avrpart {
212   char          desc[AVR_DESCLEN];  /* long part name */
213   char          id[AVR_IDLEN];      /* short part name */
214   int           stk500_devcode;     /* stk500 device code */
215   int           avr910_devcode;     /* avr910 device code */
216   int           chip_erase_delay;   /* microseconds */
217   unsigned char pagel;              /* for parallel programming */
218   unsigned char bs2;                /* for parallel programming */
219   unsigned char signature[3];       /* expected value of signature bytes */
220   unsigned short usbpid;            /* USB DFU product ID (0 = none) */
221   int           reset_disposition;  /* see RESET_ enums */
222   int           retry_pulse;        /* retry program enable by pulsing
223                                        this pin (PIN_AVR_*) */
224   unsigned      flags;              /* see AVRPART_ masks */
225 
226   int           timeout;            /* stk500 v2 xml file parameter */
227   int           stabdelay;          /* stk500 v2 xml file parameter */
228   int           cmdexedelay;        /* stk500 v2 xml file parameter */
229   int           synchloops;         /* stk500 v2 xml file parameter */
230   int           bytedelay;          /* stk500 v2 xml file parameter */
231   int           pollindex;          /* stk500 v2 xml file parameter */
232   unsigned char pollvalue;          /* stk500 v2 xml file parameter */
233   int           predelay;           /* stk500 v2 xml file parameter */
234   int           postdelay;          /* stk500 v2 xml file parameter */
235   int           pollmethod;         /* stk500 v2 xml file parameter */
236 
237   enum ctl_stack_t ctl_stack_type;  /* what to use the ctl stack for */
238   unsigned char controlstack[CTL_STACK_SIZE]; /* stk500v2 PP/HVSP ctl stack */
239   unsigned char flash_instr[FLASH_INSTR_SIZE]; /* flash instructions (debugWire, JTAG) */
240   unsigned char eeprom_instr[EEPROM_INSTR_SIZE]; /* EEPROM instructions (debugWire, JTAG) */
241 
242   int           hventerstabdelay;   /* stk500 v2 hv mode parameter */
243   int           progmodedelay;      /* stk500 v2 hv mode parameter */
244   int           latchcycles;        /* stk500 v2 hv mode parameter */
245   int           togglevtg;          /* stk500 v2 hv mode parameter */
246   int           poweroffdelay;      /* stk500 v2 hv mode parameter */
247   int           resetdelayms;       /* stk500 v2 hv mode parameter */
248   int           resetdelayus;       /* stk500 v2 hv mode parameter */
249   int           hvleavestabdelay;   /* stk500 v2 hv mode parameter */
250   int           resetdelay;         /* stk500 v2 hv mode parameter */
251   int           chiperasepulsewidth; /* stk500 v2 hv mode parameter */
252   int           chiperasepolltimeout; /* stk500 v2 hv mode parameter */
253   int           chiperasetime;      /* stk500 v2 hv mode parameter */
254   int           programfusepulsewidth; /* stk500 v2 hv mode parameter */
255   int           programfusepolltimeout; /* stk500 v2 hv mode parameter */
256   int           programlockpulsewidth; /* stk500 v2 hv mode parameter */
257   int           programlockpolltimeout; /* stk500 v2 hv mode parameter */
258   int           synchcycles;        /* stk500 v2 hv mode parameter */
259   int           hvspcmdexedelay;    /* stk500 v2 xml file parameter */
260 
261   unsigned char idr;                /* JTAG ICE mkII XML file parameter */
262   unsigned char rampz;              /* JTAG ICE mkII XML file parameter */
263   unsigned char spmcr;              /* JTAG ICE mkII XML file parameter */
264   unsigned short eecr;              /* JTAC ICE mkII XML file parameter */
265   unsigned int mcu_base;            /* Base address of MCU control block in ATxmega devices */
266   unsigned int nvm_base;            /* Base address of NVM controller in ATxmega devices */
267   int           ocdrev;             /* OCD revision (JTAGICE3 parameter, from AS6 XML files) */
268 
269   OPCODE      * op[AVR_OP_MAX];     /* opcodes */
270 
271   LISTID        mem;                /* avr memory definitions */
272   char          config_file[PATH_MAX]; /* config file where defined */
273   int           lineno;                /* config file line number */
274 } AVRPART;
275 
276 #define AVR_MEMDESCLEN 64
277 typedef struct avrmem {
278   char desc[AVR_MEMDESCLEN];  /* memory description ("flash", "eeprom", etc) */
279   int paged;                  /* page addressed (e.g. ATmega flash) */
280   int size;                   /* total memory size in bytes */
281   int page_size;              /* size of memory page (if page addressed) */
282   int num_pages;              /* number of pages (if page addressed) */
283   unsigned int offset;        /* offset in IO memory (ATxmega) */
284   int min_write_delay;        /* microseconds */
285   int max_write_delay;        /* microseconds */
286   int pwroff_after_write;     /* after this memory type is written to,
287                                  the device must be powered off and
288                                  back on, see errata
289                                  http://www.atmel.com/dyn/resources/prod_documents/doc1280.pdf */
290   unsigned char readback[2];  /* polled read-back values */
291 
292   int mode;                   /* stk500 v2 xml file parameter */
293   int delay;                  /* stk500 v2 xml file parameter */
294   int blocksize;              /* stk500 v2 xml file parameter */
295   int readsize;               /* stk500 v2 xml file parameter */
296   int pollindex;              /* stk500 v2 xml file parameter */
297 
298   unsigned char * buf;        /* pointer to memory buffer */
299   unsigned char * tags;       /* allocation tags */
300   OPCODE * op[AVR_OP_MAX];    /* opcodes */
301 } AVRMEM;
302 
303 #ifdef __cplusplus
304 extern "C" {
305 #endif
306 
307 /* Functions for OPCODE structures */
308 OPCODE * avr_new_opcode(void);
309 void     avr_free_opcode(OPCODE * op);
310 int avr_set_bits(OPCODE * op, unsigned char * cmd);
311 int avr_set_addr(OPCODE * op, unsigned char * cmd, unsigned long addr);
312 int avr_set_input(OPCODE * op, unsigned char * cmd, unsigned char data);
313 int avr_get_output(OPCODE * op, unsigned char * res, unsigned char * data);
314 int avr_get_output_index(OPCODE * op);
315 
316 /* Functions for AVRMEM structures */
317 AVRMEM * avr_new_memtype(void);
318 int avr_initmem(AVRPART * p);
319 AVRMEM * avr_dup_mem(AVRMEM * m);
320 void     avr_free_mem(AVRMEM * m);
321 AVRMEM * avr_locate_mem(AVRPART * p, char * desc);
322 void avr_mem_display(const char * prefix, FILE * f, AVRMEM * m, int type,
323                      int verbose);
324 
325 /* Functions for AVRPART structures */
326 AVRPART * avr_new_part(void);
327 AVRPART * avr_dup_part(AVRPART * d);
328 void      avr_free_part(AVRPART * d);
329 AVRPART * locate_part(LISTID parts, char * partdesc);
330 AVRPART * locate_part_by_avr910_devcode(LISTID parts, int devcode);
331 AVRPART * locate_part_by_signature(LISTID parts, unsigned char * sig,
332                                    int sigsize);
333 void avr_display(FILE * f, AVRPART * p, const char * prefix, int verbose);
334 
335 typedef void (*walk_avrparts_cb)(const char *name, const char *desc,
336                                  const char *cfgname, int cfglineno,
337                                  void *cookie);
338 void walk_avrparts(LISTID avrparts, walk_avrparts_cb cb, void *cookie);
339 void sort_avrparts(LISTID avrparts);
340 uint8_t get_fuse_bitmask(AVRMEM * m);
341 int compare_memory_masked(AVRMEM * m, unsigned char buf1, unsigned char buf2);
342 #ifdef __cplusplus
343 }
344 #endif
345 
346 /* formerly pindefs.h */
347 
348 enum {
349   PPI_AVR_VCC = 1,
350   PPI_AVR_BUFF,
351   PIN_AVR_RESET,
352   PIN_AVR_SCK,
353   PIN_AVR_MOSI,
354   PIN_AVR_MISO,
355   PIN_LED_ERR,
356   PIN_LED_RDY,
357   PIN_LED_PGM,
358   PIN_LED_VFY,
359   N_PINS
360 };
361 
362 #define PIN_MASK    (UINT_MAX>>1)
363 #define PIN_INVERSE (~(PIN_MASK))	/* flag for inverted pin in serbb */
364 #define PIN_MIN     0   /* smallest allowed pin number */
365 #define PIN_MAX     31  /* largest allowed pin number */
366 
367 #ifdef HAVE_LINUXGPIO
368 /* Embedded systems might have a lot more gpio than only 0-31 */
369 #undef PIN_MAX
370 #define PIN_MAX     255 /* largest allowed pin number */
371 #endif
372 
373 /** Number of pins in each element of the bitfield */
374 #define PIN_FIELD_ELEMENT_SIZE (sizeof(pinmask_t) * 8)
375 /** Numer of elements to store the complete bitfield of all pins */
376 #define PIN_FIELD_SIZE ((PIN_MAX + PIN_FIELD_ELEMENT_SIZE)/PIN_FIELD_ELEMENT_SIZE)
377 
378 /**
379  * This sets the corresponding bits to 1 or 0, the inverse mask is used to invert the value in necessary.
380  * It uses only the lowest element (index=0) of the bitfield, which should be enough for most
381  * programmers.
382  *
383  * @param[in] x       input value
384  * @param[in] pgm     the programmer whose pin definitions to use
385  * @param[in] pinname the logical name of the pin (PIN_AVR_*, ...)
386  * @param[in] level   the logical level (level != 0 => 1, level == 0 => 0),
387  *                      if the pin is defined as inverted the resulting bit is also inverted
388  * @returns           the input value with the relevant bits modified
389  */
390 #define SET_BITS_0(x,pgm,pinname,level) (((x) & ~(pgm)->pin[pinname].mask[0]) \
391     | (\
392         (pgm)->pin[pinname].mask[0] & ( \
393              (level) \
394              ?~((pgm)->pin[pinname].inverse[0]) \
395              : ((pgm)->pin[pinname].inverse[0]) \
396         ) \
397     ) \
398 )
399 
400 /**
401  * Check if the corresponding bit is set (returns != 0) or cleared.
402  * The inverse mask is used, to invert the relevant bits.
403  * If the pin definition contains multiple pins, then a single set pin leads to return value != 0.
404  * Then you have to check the relevant bits of the returned value, if you need more information.
405  * It uses only the lowest element (index=0) of the bitfield, which should be enough for most
406  * programmers.
407  *
408  * @param[in] x       input value
409  * @param[in] pgm     the programmer whose pin definitions to use
410  * @param[in] pinname the logical name of the pin (PIN_AVR_*, ...)
411  * @returns           the input value with only the relevant bits (which are already inverted,
412  *                      so you get always the logical level)
413  */
414 #define GET_BITS_0(x,pgm,pinname)       (((x) ^ (pgm)->pin[pinname].inverse[0]) & (pgm)->pin[pinname].mask[0])
415 
416 /**
417  * Data structure to hold used pins by logical function (PIN_AVR_*, ...)
418  */
419 struct pindef_t {
420   pinmask_t mask[PIN_FIELD_SIZE]; ///< bitfield of used pins
421   pinmask_t inverse[PIN_FIELD_SIZE]; ///< bitfield of inverse/normal usage of used pins
422 };
423 
424 /**
425  * Data structure to define a checklist of valid pins for each function.
426  */
427 struct pin_checklist_t {
428   int pinname; ///< logical pinname eg. PIN_AVR_SCK
429   int mandatory; ///< is this a mandatory pin
430   const struct pindef_t* valid_pins; ///< mask defines allowed pins, inverse define is they might be used inverted
431 };
432 
433 /**
434  * Adds a pin in the pin definition as normal or inverse pin.
435  *
436  * @param[out] pindef pin definition to update
437  * @param[in] pin number of pin [0..PIN_MAX]
438  * @param[in] inverse inverse (true) or normal (false) pin
439  */
440 void pin_set_value(struct pindef_t * const pindef, const int pin, const bool inverse);
441 
442 /**
443  * Clear all defined pins in pindef.
444  *
445  * @param[out] pindef pin definition to clear
446  */
447 void pin_clear_all(struct pindef_t * const pindef);
448 
449 struct programmer_t; /* forward declaration */
450 
451 /**
452  * Convert for given programmer new pin definitions to old pin definitions.
453  *
454  * @param[inout] pgm programmer whose pins shall be converted.
455  */
456 int pgm_fill_old_pins(struct programmer_t * const pgm);
457 
458 /**
459  * This function checks all pin of pgm against the constraints given in the checklist.
460  * It checks if
461  * @li any invalid pins are used
462  * @li valid pins are used inverted when not allowed
463  * @li any pins are used by more than one function
464  * @li any mandatory pin is not set all.
465  *
466  * In case of any error it report the wrong function and the pin numbers.
467  * For verbose >= 2 it also reports the possible correct values.
468  * For verbose >=3 it shows also which pins were ok.
469  *
470  * @param[in] pgm the programmer to check
471  * @param[in] checklist the constraint for the pins
472  * @param[in] size the number of entries in checklist
473  * @param[in] output false suppresses error messages to the user
474  * @returns 0 if all pin definitions are valid, -1 otherwise
475  */
476 int pins_check(const struct programmer_t * const pgm, const struct pin_checklist_t * const checklist, const int size, const bool output);
477 
478 /**
479  * Returns the name of the pin as string.
480  *
481  * @param pinname the pinname which we want as string.
482  * @returns a string with the pinname, or <unknown> if pinname is invalid.
483  */
484 const char * avr_pin_name(int pinname);
485 
486 /**
487  * This function returns a string representation of defined pins eg. ~1,2,~4,~5,7
488  * Another execution of this function will overwrite the previous result in the static buffer.
489  *
490  * @param[in] pindef the pin definition for which we want the string representation
491  * @returns pointer to a static string.
492  */
493 const char * pins_to_str(const struct pindef_t * const pindef);
494 
495 /**
496  * This function returns a string representation of pins in the mask eg. 1,3,5-7,9,12
497  * Another execution of this function will overwrite the previous result in the static buffer.
498  * Consecutive pin number are representated as start-end.
499  *
500  * @param[in] pinmask the pin mask for which we want the string representation
501  * @returns pointer to a static string.
502  */
503 const char * pinmask_to_str(const pinmask_t * const pinmask);
504 
505 /* formerly serial.h */
506 
507 /* This is the API for the generic serial interface. The implementations are
508    actually provided by the target dependant files:
509 
510    ser_posix.c : posix serial interface.
511    ser_win32.c : native win32 serial interface.
512 
513    The target file will be selected at configure time. */
514 
515 extern long serial_recv_timeout;
516 union filedescriptor
517 {
518   int ifd;
519   void *pfd;
520   struct
521   {
522     void *handle;
523     int rep;                    /* bulk read endpoint */
524     int wep;                    /* bulk write endpoint */
525     int eep;                    /* event read endpoint */
526     int max_xfer;               /* max transfer size */
527     int use_interrupt_xfer;     /* device uses interrupt transfers */
528   } usb;
529 };
530 
531 union pinfo
532 {
533   long baud;
534   struct
535   {
536     unsigned short vid;
537     unsigned short pid;
538     unsigned short flags;
539 #define PINFO_FL_USEHID         0x0001
540 #define PINFO_FL_SILENT         0x0002  /* don't complain if not found */
541   } usbinfo;
542 };
543 
544 
545 struct serial_device
546 {
547   // open should return -1 on error, other values on success
548   int (*open)(char * port, union pinfo pinfo, union filedescriptor *fd);
549   int (*setspeed)(union filedescriptor *fd, long baud);
550   void (*close)(union filedescriptor *fd);
551 
552   int (*send)(union filedescriptor *fd, const unsigned char * buf, size_t buflen);
553   int (*recv)(union filedescriptor *fd, unsigned char * buf, size_t buflen);
554   int (*drain)(union filedescriptor *fd, int display);
555 
556   int (*set_dtr_rts)(union filedescriptor *fd, int is_on);
557 
558   int flags;
559 #define SERDEV_FL_NONE         0x0000 /* no flags */
560 #define SERDEV_FL_CANSETSPEED  0x0001 /* device can change speed */
561 };
562 
563 extern struct serial_device *serdev;
564 extern struct serial_device serial_serdev;
565 extern struct serial_device usb_serdev;
566 extern struct serial_device usb_serdev_frame;
567 extern struct serial_device avrdoper_serdev;
568 
569 #define serial_open (serdev->open)
570 #define serial_setspeed (serdev->setspeed)
571 #define serial_close (serdev->close)
572 #define serial_send (serdev->send)
573 #define serial_recv (serdev->recv)
574 #define serial_drain (serdev->drain)
575 #define serial_set_dtr_rts (serdev->set_dtr_rts)
576 
577 /* formerly pgm.h */
578 
579 #define ON  1
580 #define OFF 0
581 
582 #define PGM_DESCLEN 80
583 #define PGM_PORTLEN PATH_MAX
584 #define PGM_TYPELEN 32
585 #define PGM_USBSTRINGLEN 256
586 
587 typedef enum {
588   EXIT_VCC_UNSPEC,
589   EXIT_VCC_ENABLED,
590   EXIT_VCC_DISABLED
591 } exit_vcc_t;
592 
593 typedef enum {
594   EXIT_RESET_UNSPEC,
595   EXIT_RESET_ENABLED,
596   EXIT_RESET_DISABLED
597 } exit_reset_t;
598 
599 typedef enum {
600   EXIT_DATAHIGH_UNSPEC,
601   EXIT_DATAHIGH_ENABLED,
602   EXIT_DATAHIGH_DISABLED
603 } exit_datahigh_t;
604 
605 typedef enum {
606   CONNTYPE_PARALLEL,
607   CONNTYPE_SERIAL,
608   CONNTYPE_USB
609 } conntype_t;
610 
611 typedef struct programmer_t {
612   LISTID id;
613   char desc[PGM_DESCLEN];
614   char type[PGM_TYPELEN];
615   char port[PGM_PORTLEN];
616   void (*initpgm)(struct programmer_t * pgm);
617   unsigned int pinno[N_PINS];
618   struct pindef_t pin[N_PINS];
619   exit_vcc_t exit_vcc;
620   exit_reset_t exit_reset;
621   exit_datahigh_t exit_datahigh;
622   conntype_t conntype;
623   int ppidata;
624   int ppictrl;
625   int baudrate;
626   int usbvid;
627   LISTID usbpid;
628   char usbdev[PGM_USBSTRINGLEN], usbsn[PGM_USBSTRINGLEN];
629   char usbvendor[PGM_USBSTRINGLEN], usbproduct[PGM_USBSTRINGLEN];
630   double bitclock;    /* JTAG ICE clock period in microseconds */
631   int ispdelay;    /* ISP clock delay */
632   union filedescriptor fd;
633   int  page_size;  /* page size if the programmer supports paged write/load */
634   int  (*rdy_led)        (struct programmer_t * pgm, int value);
635   int  (*err_led)        (struct programmer_t * pgm, int value);
636   int  (*pgm_led)        (struct programmer_t * pgm, int value);
637   int  (*vfy_led)        (struct programmer_t * pgm, int value);
638   int  (*initialize)     (struct programmer_t * pgm, AVRPART * p);
639   void (*display)        (struct programmer_t * pgm, const char * p);
640   void (*enable)         (struct programmer_t * pgm);
641   void (*disable)        (struct programmer_t * pgm);
642   void (*powerup)        (struct programmer_t * pgm);
643   void (*powerdown)      (struct programmer_t * pgm);
644   int  (*program_enable) (struct programmer_t * pgm, AVRPART * p);
645   int  (*chip_erase)     (struct programmer_t * pgm, AVRPART * p);
646   int  (*cmd)            (struct programmer_t * pgm, const unsigned char *cmd,
647                           unsigned char *res);
648   int  (*cmd_tpi)        (struct programmer_t * pgm, const unsigned char *cmd,
649                           int cmd_len, unsigned char res[], int res_len);
650   int  (*spi)            (struct programmer_t * pgm, const unsigned char *cmd,
651                           unsigned char *res, int count);
652   int  (*open)           (struct programmer_t * pgm, char * port);
653   void (*close)          (struct programmer_t * pgm);
654   int  (*paged_write)    (struct programmer_t * pgm, AVRPART * p, AVRMEM * m,
655                           unsigned int page_size, unsigned int baseaddr,
656                           unsigned int n_bytes);
657   int  (*paged_load)     (struct programmer_t * pgm, AVRPART * p, AVRMEM * m,
658                           unsigned int page_size, unsigned int baseaddr,
659                           unsigned int n_bytes);
660   int  (*page_erase)     (struct programmer_t * pgm, AVRPART * p, AVRMEM * m,
661                           unsigned int baseaddr);
662   void (*write_setup)    (struct programmer_t * pgm, AVRPART * p, AVRMEM * m);
663   int  (*write_byte)     (struct programmer_t * pgm, AVRPART * p, AVRMEM * m,
664                           unsigned long addr, unsigned char value);
665   int  (*read_byte)      (struct programmer_t * pgm, AVRPART * p, AVRMEM * m,
666                           unsigned long addr, unsigned char * value);
667   int  (*read_sig_bytes) (struct programmer_t * pgm, AVRPART * p, AVRMEM * m);
668   void (*print_parms)    (struct programmer_t * pgm);
669   int  (*set_vtarget)    (struct programmer_t * pgm, double v);
670   int  (*set_varef)      (struct programmer_t * pgm, unsigned int chan, double v);
671   int  (*set_fosc)       (struct programmer_t * pgm, double v);
672   int  (*set_sck_period) (struct programmer_t * pgm, double v);
673   int  (*setpin)         (struct programmer_t * pgm, int pinfunc, int value);
674   int  (*getpin)         (struct programmer_t * pgm, int pinfunc);
675   int  (*highpulsepin)   (struct programmer_t * pgm, int pinfunc);
676   int  (*parseexitspecs) (struct programmer_t * pgm, char *s);
677   int  (*perform_osccal) (struct programmer_t * pgm);
678   int  (*parseextparams) (struct programmer_t * pgm, LISTID xparams);
679   void (*setup)          (struct programmer_t * pgm);
680   void (*teardown)       (struct programmer_t * pgm);
681   char config_file[PATH_MAX]; /* config file where defined */
682   int  lineno;                /* config file line number */
683   void *cookie;		      /* for private use by the programmer */
684   char flag;		      /* for private use of the programmer */
685 } PROGRAMMER;
686 
687 #ifdef __cplusplus
688 extern "C" {
689 #endif
690 
691 PROGRAMMER * pgm_new(void);
692 PROGRAMMER * pgm_dup(const PROGRAMMER * const src);
693 void         pgm_free(PROGRAMMER * const p);
694 
695 void programmer_display(PROGRAMMER * pgm, const char * p);
696 
697 /* show is a mask like this (1<<PIN_AVR_SCK)|(1<<PIN_AVR_MOSI)| ... */
698 #define SHOW_ALL_PINS (~0u)
699 #define SHOW_PPI_PINS ((1<<PPI_AVR_VCC)|(1<<PPI_AVR_BUFF))
700 #define SHOW_AVR_PINS ((1<<PIN_AVR_RESET)|(1<<PIN_AVR_SCK)|(1<<PIN_AVR_MOSI)|(1<<PIN_AVR_MISO))
701 #define SHOW_LED_PINS ((1<<PIN_LED_ERR)|(1<<PIN_LED_RDY)|(1<<PIN_LED_PGM)|(1<<PIN_LED_VFY))
702 void pgm_display_generic_mask(PROGRAMMER * pgm, const char * p, unsigned int show);
703 void pgm_display_generic(PROGRAMMER * pgm, const char * p);
704 
705 PROGRAMMER * locate_programmer(LISTID programmers, const char * configid);
706 
707 typedef void (*walk_programmers_cb)(const char *name, const char *desc,
708                                     const char *cfgname, int cfglineno,
709                                     void *cookie);
710 void walk_programmers(LISTID programmers, walk_programmers_cb cb, void *cookie);
711 
712 void sort_programmers(LISTID programmers);
713 
714 #ifdef __cplusplus
715 }
716 #endif
717 
718 /* formerly avr.h */
719 
720 typedef void (*FP_UpdateProgress)(int percent, double etime, char *hdr);
721 
722 extern struct avrpart parts[];
723 
724 extern FP_UpdateProgress update_progress;
725 
726 #ifdef __cplusplus
727 extern "C" {
728 #endif
729 
730 int avr_tpi_poll_nvmbsy(PROGRAMMER *pgm);
731 int avr_tpi_chip_erase(PROGRAMMER * pgm, AVRPART * p);
732 int avr_tpi_program_enable(PROGRAMMER * pgm, AVRPART * p, unsigned char guard_time);
733 int avr_read_byte_default(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
734 			  unsigned long addr, unsigned char * value);
735 
736 int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype, AVRPART * v);
737 
738 int avr_write_page(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
739                    unsigned long addr);
740 
741 int avr_write_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
742                    unsigned long addr, unsigned char data);
743 
744 int avr_write_byte_default(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
745 			   unsigned long addr, unsigned char data);
746 
747 int avr_write(PROGRAMMER * pgm, AVRPART * p, char * memtype, int size,
748               int auto_erase);
749 
750 int avr_signature(PROGRAMMER * pgm, AVRPART * p);
751 
752 int avr_verify(AVRPART * p, AVRPART * v, char * memtype, int size);
753 
754 int avr_get_cycle_count(PROGRAMMER * pgm, AVRPART * p, int * cycles);
755 
756 int avr_put_cycle_count(PROGRAMMER * pgm, AVRPART * p, int cycles);
757 
758 int avr_mem_hiaddr(AVRMEM * mem);
759 
760 int avr_chip_erase(PROGRAMMER * pgm, AVRPART * p);
761 
762 void report_progress (int completed, int total, char *hdr);
763 
764 #ifdef __cplusplus
765 }
766 #endif
767 
768 /* formerly fileio.h */
769 
770 typedef enum {
771   FMT_AUTO,
772   FMT_SREC,
773   FMT_IHEX,
774   FMT_RBIN,
775   FMT_IMM,
776   FMT_HEX,
777   FMT_DEC,
778   FMT_OCT,
779   FMT_BIN,
780   FMT_ELF
781 } FILEFMT;
782 
783 struct fioparms {
784   int    op;
785   char * mode;
786   char * iodesc;
787   char * dir;
788   char * rw;
789   unsigned int fileoffset;
790 };
791 
792 enum {
793   FIO_READ,
794   FIO_WRITE
795 };
796 
797 #ifdef __cplusplus
798 extern "C" {
799 #endif
800 
801 char * fmtstr(FILEFMT format);
802 
803 int fileio(int op, char * filename, FILEFMT format,
804            struct avrpart * p, char * memtype, int size);
805 
806 #ifdef __cplusplus
807 }
808 #endif
809 
810 
811 /* formerly safemode.h */
812 
813 #ifdef __cplusplus
814 extern "C" {
815 #endif
816 
817 /* Writes the specified fuse in fusename (can be "lfuse", "hfuse", or "efuse") and verifies it. Will try up to tries
818 amount of times before giving up */
819 int safemode_writefuse (unsigned char fuse, char * fusename, PROGRAMMER * pgm, AVRPART * p, int tries);
820 
821 /* Reads the fuses three times, checking that all readings are the same. This will ensure that the before values aren't in error! */
822 int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, unsigned char * efuse, unsigned char * fuse, PROGRAMMER * pgm, AVRPART * p);
823 
824 /* This routine will store the current values pointed to by lfuse, hfuse, and efuse into an internal buffer in this routine
825 when save is set to 1. When save is 0 (or not 1 really) it will copy the values from the internal buffer into the locations
826 pointed to be lfuse, hfuse, and efuse. This allows you to change the fuse bits if needed from another routine (ie: have it so
827 if user requests fuse bits are changed, the requested value is now verified */
828 int safemode_memfuses (int save, unsigned char * lfuse, unsigned char * hfuse, unsigned char * efuse, unsigned char * fuse);
829 
830 #ifdef __cplusplus
831 }
832 #endif
833 
834 
835 /* formerly update.h */
836 
837 enum {
838   DEVICE_READ,
839   DEVICE_WRITE,
840   DEVICE_VERIFY
841 };
842 
843 enum updateflags {
844   UF_NONE = 0,
845   UF_NOWRITE = 1,
846   UF_AUTO_ERASE = 2,
847 };
848 
849 
850 typedef struct update_t {
851   char * memtype;
852   int    op;
853   char * filename;
854   int    format;
855 } UPDATE;
856 
857 #ifdef __cplusplus
858 extern "C" {
859 #endif
860 
861 extern UPDATE * parse_op(char * s);
862 extern UPDATE * dup_update(UPDATE * upd);
863 extern UPDATE * new_update(int op, char * memtype, int filefmt,
864 			   char * filename);
865 extern void free_update(UPDATE * upd);
866 extern int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd,
867 		 enum updateflags flags);
868 
869 #ifdef __cplusplus
870 }
871 #endif
872 
873 
874 /* formerly pgm_type.h */
875 
876 /*LISTID programmer_types;*/
877 
878 typedef struct programmer_type_t {
879   const char * const id;
880   void (*initpgm)(struct programmer_t * pgm);
881   const char * const desc;
882 } PROGRAMMER_TYPE;
883 
884 #ifdef __cplusplus
885 extern "C" {
886 #endif
887 
888 const PROGRAMMER_TYPE * locate_programmer_type(/*LISTID programmer_types, */const char * id);
889 
890 typedef void (*walk_programmer_types_cb)(const char *id, const char *desc,
891                                     void *cookie);
892 void walk_programmer_types(/*LISTID programmer_types,*/ walk_programmer_types_cb cb, void *cookie);
893 
894 #ifdef __cplusplus
895 }
896 #endif
897 
898 /* formerly config.h */
899 
900 extern LISTID       part_list;
901 extern LISTID       programmers;
902 extern char         default_programmer[];
903 extern char         default_parallel[];
904 extern char         default_serial[];
905 extern double       default_bitclock;
906 extern int          default_safemode;
907 
908 /* This name is fixed, it's only here for symmetry with
909  * default_parallel and default_serial. */
910 #define DEFAULT_USB "usb"
911 
912 #ifdef __cplusplus
913 extern "C" {
914 #endif
915 
916 int init_config(void);
917 
918 void cleanup_config(void);
919 
920 int read_config(const char * file);
921 
922 #ifdef __cplusplus
923 }
924 #endif
925 
926 
927 /* formerly confwin.h */
928 
929 #if defined(WIN32NATIVE)
930 
931 #ifdef __cplusplus
932 extern "C" {
933 #endif
934 
935 void win_sys_config_set(char sys_config[PATH_MAX]);
936 void win_usr_config_set(char usr_config[PATH_MAX]);
937 
938 #ifdef __cplusplus
939 }
940 #endif
941 
942 #endif  /* WIN32NATIVE */
943 
944 
945 #endif  /* libavrdude_h */
946