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