Debian Codegraph @

src/jtag.h

raw symbols SCIP
1 /*
2 * avarice - The "avarice" program.
3 * Copyright (C) 2001 Scott Finneran
4 * Copyright (C) 2002, 2003, 2004 Intel Corporation
5 * Copyright (C) 2005, 2006, 2007 Joerg Wunsch
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
19 *
20 * This file contains functions for interfacing with the GDB remote protocol.
21 *
22 * $Id: jtag.h 392 2021-09-14 18:16:05Z jputcu $
23 */
24
25 #ifndef JTAG_H
26 #define JTAG_H
27
28 #include <sys/types.h>
29 #include <termios.h>
30
31 #include <exception>
32
33 #include "pragma.h"
34 #include "ioreg.h"
35
36 using namespace std;
37
38 /* The data in this structure will be sent directorly to the jtagice box. */
39
40 typedef struct {
41 unsigned char cmd; // The jtag command to prefix the desc.
42
43 /* The following arrays are bitmaps where each bit is a flag denoting
44 wether the register can be read or written. Bit 0 of byte 0 represents
45 the IO register at sram addres 0x20, while bit 7 of byte 7 represents
46 the register at 0x5f. */
47
48 unsigned char rd[8]; // IO read access.
49 unsigned char wr[8]; // IO write access.
50 unsigned char sh_rd[8]; // IO shadow read access.
51 unsigned char sh_wr[8]; // IO shadow write access.
52
53 /* Same as above, except that first bit is register at sram address 0x60
54 and last bit is register at 0xff. */
55
56 unsigned char ext_rd[20]; // Extended IO read access.
57 unsigned char ext_wr[20]; // Extended IO write access.
58 unsigned char ext_sh_rd[20]; // Extended IO shadow read access.
59 unsigned char ext_sh_wr[20]; // Extended IO shadow write access.
60
61 /* Register locations. */
62
63 unsigned char idr_addr; // IDR address in IO space.
64 unsigned char spmcr_addr; // SPMCR address in SRAM space.
65 unsigned char rampz_addr; // RAMPZ address in IO space.
66
67 /* Memory programming page sizes (in bytes). */
68
69 unsigned char flash_pg_sz[2]; // [0]->little end; [1]->big end
70 unsigned char eeprom_pg_sz;
71
72 unsigned char boot_addr[4]; // Boot loader start address.
73 // This is a WORD address.
74 // [0]->little end; [3]->big end
75
76 unsigned char last_ext_io_addr; // Last extended IO location, 0 if no
77 // extended IO.
78
79 unsigned char eom[2]; // JTAG command terminator.
80 } jtag1_device_desc_type;
81
82 // In appnote AVR067, struct device_descriptor is written with
83 // int/long field types. We cannot use them directly, as they were
84 // neither properly aligned for portability, nor did they care for
85 // endianess issues. We thus use arrays of unsigned chars, plus
86 // conversion macros.
87
88 typedef struct {
89 unsigned char cmd; // The jtag command to prefix the desc.
90
91 unsigned char ucReadIO[8]; //LSB = IOloc 0, MSB = IOloc63
92 unsigned char ucReadIOShadow[8]; //LSB = IOloc 0, MSB = IOloc63
93 unsigned char ucWriteIO[8]; //LSB = IOloc 0, MSB = IOloc63
94 unsigned char ucWriteIOShadow[8]; //LSB = IOloc 0, MSB = IOloc63
95 unsigned char ucReadExtIO[52]; //LSB = IOloc 96, MSB = IOloc511
96 unsigned char ucReadIOExtShadow[52]; //LSB = IOloc 96, MSB = IOloc511
97 unsigned char ucWriteExtIO[52]; //LSB = IOloc 96, MSB = IOloc511
98 unsigned char ucWriteIOExtShadow[52];//LSB = IOloc 96, MSB = IOloc511
99 unsigned char ucIDRAddress; //IDR address
100 unsigned char ucSPMCRAddress; //SPMCR Register address and dW BasePC
101 unsigned char ucRAMPZAddress; //RAMPZ Register address in SRAM I/O
102 //space
103 unsigned char uiFlashPageSize[2]; //Device Flash Page Size
104 unsigned char ucEepromPageSize; //Device Eeprom Page Size in bytes
105 unsigned char ulBootAddress[4]; //Device Boot Loader Start Address
106 unsigned char uiUpperExtIOLoc[2]; //Topmost (last) extended I/O
107 //location, 0 if no external I/O
108 unsigned char ulFlashSize[4]; //Device Flash Size
109 unsigned char ucEepromInst[20]; //Instructions for W/R EEPROM
110 unsigned char ucFlashInst[3]; //Instructions for W/R FLASH
111 unsigned char ucSPHaddr; // stack pointer high
112 unsigned char ucSPLaddr; // stack pointer low
113 // new as of 16-02-2004
114 unsigned char uiFlashpages[2]; // number of pages in flash
115 unsigned char ucDWDRAddress; // DWDR register address
116 unsigned char ucDWBasePC; // base/mask value of the PC
117 // new as of 30-04-2004
118 unsigned char ucAllowFullPageBitstream; // FALSE on ALL new
119 //parts
120 unsigned char uiStartSmallestBootLoaderSection[2]; //
121 // new as of 18-10-2004
122 unsigned char EnablePageProgramming; // For JTAG parts only,
123 // default TRUE
124 unsigned char ucCacheType; // CacheType_Normal 0x00,
125 // CacheType_CAN 0x01,
126 // CacheType_HEIMDALL 0x02
127 // new as of 27-10-2004
128 unsigned char uiSramStartAddr[2]; // Start of SRAM
129 unsigned char ucResetType; // Selects reset type. ResetNormal = 0x00
130 // ResetAT76CXXX = 0x01
131 unsigned char ucPCMaskExtended; // For parts with extended PC
132 unsigned char ucPCMaskHigh; // PC high mask
133 unsigned char ucEindAddress; // Selects reset type. [EIND address...]
134 // new as of early 2005, firmware 4.x
135 unsigned char EECRAddress[2]; // EECR IO address
136 } jtag2_device_desc_type;
137
138 // New Xmega device descriptor, for firmware version 7 and above
139 typedef struct {
140 unsigned char cmd; // CMND_SET_XMEGA_PARAMS
141 unsigned char whatever[2]; // cannot guess; must be 0x0002
142 unsigned char datalen; // length of the following data, = 47
143 unsigned char nvm_app_offset[4]; // NVM offset for application flash
144 unsigned char nvm_boot_offset[4]; // NVM offset for boot flash
145 unsigned char nvm_eeprom_offset[4]; // NVM offset for EEPROM
146 unsigned char nvm_fuse_offset[4]; // NVM offset for fuses
147 unsigned char nvm_lock_offset[4]; // NVM offset for lock bits
148 unsigned char nvm_user_sig_offset[4]; // NVM offset for user signature row
149 unsigned char nvm_prod_sig_offset[4]; // NVM offset for production sign. row
150 unsigned char nvm_data_offset[4]; // NVM offset for data memory (SRAM + IO)
151 unsigned char app_size[4]; // size of application flash
152 unsigned char boot_size[2]; // size of boot flash
153 unsigned char flash_page_size[2]; // flash page size
154 unsigned char eeprom_size[2]; // size of EEPROM
155 unsigned char eeprom_page_size; // EEPROM page size
156 unsigned char nvm_base_addr[2]; // IO space base address of NVM controller
157 unsigned char mcu_base_addr[2]; // IO space base address of MCU control
158 } xmega_device_desc_type;
159
160 // JTAGICE3 megaAVR parameter structure
161 typedef struct {
162 unsigned char flash_page_size[2]; // in bytes
163 unsigned char flash_size[4]; // in bytes
164 unsigned char dummy1[4]; // always 0
165 unsigned char boot_address[4]; // maximal (BOOTSZ = 3) bootloader
166 // address, in 16-bit words (!)
167 unsigned char sram_offset[2]; // pointing behind IO registers
168 unsigned char eeprom_size[2];
169 unsigned char eeprom_page_size;
170 unsigned char ocd_revision; // see XML; basically:
171 // t13*, t2313*, t4313: 0
172 // all other DW devices: 1
173 // ATmega128(A): 1 (!)
174 // ATmega16*,162,169*,32*,64*: 2
175 // ATmega2560/2561: 4
176 // all other megaAVR devices: 3
177 unsigned char always_one; // always = 1
178 unsigned char allow_full_page_bitstream; // old AVRs, see XML
179 unsigned char dummy2[2]; // always 0
180 // all IO addresses below are given
181 // in IO number space (without
182 // offset 0x20), even though e.g.
183 // OSCCAL always resides outside
184 unsigned char idr_address; // IDR, aka. OCDR
185 unsigned char eearh_address; // EEPROM access
186 unsigned char eearl_address;
187 unsigned char eecr_address;
188 unsigned char eedr_address;
189 unsigned char spmcr_address;
190 unsigned char osccal_address;
191 } jtag3_device_desc_type;
192
193 #define fill_b4(u) \
194 { ((u) & 0xffUL), (((u) & 0xff00UL) >> 8), \
195 (((u) & 0xff0000UL) >> 16), (((u) & 0xff000000UL) >> 24) }
196 #define fill_b2(u) \
197 { ((u) & 0xff), (((u) & 0xff00) >> 8) }
198
199 enum dev_flags {
200 DEVFL_NONE = 0x000000,
201 DEVFL_NO_SOFTBP = 0x000001, // Device cannot use software BPs (no BREAK insn)
202 DEVFL_MKII_ONLY = 0x000002, // Device is only supported in JTAG ICE mkII
203 };
204
205 typedef struct {
206 const char* name;
207 const unsigned int device_id; // Part Number from JTAG Device
208 // Identification Register
209 unsigned int flash_page_size; // Flash memory page size in bytes
210 unsigned int flash_page_count; // Flash memory page count
211 unsigned char eeprom_page_size; // EEPROM page size in bytes
212 unsigned int eeprom_page_count; // EEPROM page count
213 unsigned int vectors_end; // End of interrupt vector table
214 enum dev_flags device_flags; // See above.
215
216 gdb_io_reg_def_type *io_reg_defs;
217
218 bool is_xmega; // Device is an ATxmega part
219 unsigned int fusemap; // Bitmap of fuses supported (0b00000111 = 0x07 for
220 // classic megaAVR devices, 0b00110111 = 0x37 for
221 // Xmega devices
222 unsigned int ocden_fuse; // bitmask of the OCDEN fuse, bit [15:0] => high
223 // fuse byte, so 0x00008000 for megaAVRs (Xmega
224 // devices don't have OCDEN at all)
225
226 unsigned char osccal; // OSCCAL offset (XML param)
227 unsigned char ocdrev; // OCD revision (Studio 6 XML param)
228
229 jtag1_device_desc_type dev_desc1; // Device descriptor to download to
230 // mkI device
231 jtag2_device_desc_type dev_desc2; // Device descriptor to download to
232 // mkII device
233 xmega_device_desc_type dev_desc3; // Device descriptor to download for
234 // Xmega devices in new (7+) firmware
235 // JTAGICE mkII and AVR Dragon
236 } jtag_device_def_type;
237
238 extern jtag_device_def_type deviceDefinitions[];
239
240 // various enums
241 enum
242 {
243 // Constants common to both mkI and mkII
244
245 // Address space offsets
246 FLASH_SPACE_ADDR_OFFSET = 0x000000,
247 DATA_SPACE_ADDR_OFFSET = 0x800000,
248
249 EEPROM_SPACE_ADDR_OFFSET = 0x810000,
250
251 FUSE_SPACE_ADDR_OFFSET = 0x820000,
252
253 LOCK_SPACE_ADDR_OFFSET = 0x830000,
254
255 SIG_SPACE_ADDR_OFFSET = 0x840000,
256
257 REGISTER_SPACE_ADDR_OFFSET = 0x850000, // Xmega only
258
259 BREAKPOINT_SPACE_ADDR_OFFSET = 0x900000,
260
261 ADDR_SPACE_MASK = (DATA_SPACE_ADDR_OFFSET |
262 EEPROM_SPACE_ADDR_OFFSET |
263 FUSE_SPACE_ADDR_OFFSET |
264 LOCK_SPACE_ADDR_OFFSET |
265 SIG_SPACE_ADDR_OFFSET |
266 REGISTER_SPACE_ADDR_OFFSET |
267 BREAKPOINT_SPACE_ADDR_OFFSET),
268
269 // JTAG communication timeouts, in microseconds
270 // RESPONSE is for the first response byte
271 // COMM is for subsequent response bytes
272 MAX_JTAG_COMM_ATTEMPS = 10,
273 MAX_JTAG_SYNC_ATTEMPS = 3,
274
275 JTAG_RESPONSE_TIMEOUT = 1000000,
276 JTAG_COMM_TIMEOUT = 100000,
277 JTAG3_PIPE_TIMEOUT = 100,
278
279 MAX_FLASH_PAGE_SIZE = 512,
280 MAX_EEPROM_PAGE_SIZE = 32,
281
282 // JTAG ICE mkI protocol constants
283
284 // Address space selector values
285 ADDR_PROG_SPACE_PROG_ENABLED = 0xB0,
286 ADDR_PROG_SPACE_PROG_DISABLED = 0xA0,
287 ADDR_DATA_SPACE = 0x20,
288 ADDR_EEPROM_SPACE = 0xB1,
289 ADDR_FUSE_SPACE = 0xB2,
290 ADDR_LOCK_SPACE = 0xB3,
291 ADDR_SIG_SPACE = 0xB4,
292 ADDR_BREAKPOINT_SPACE = 0x60,
293
294 // Comms link bit rates
295 BIT_RATE_9600 = 0xf4,
296 BIT_RATE_14400 = 0xf8,
297 BIT_RATE_19200 = 0xfa,
298 BIT_RATE_38400 = 0xfd,
299 BIT_RATE_57600 = 0xfe,
300 BIT_RATE_115200 = 0xff,
301
302 // Breakpoints (match values returned by JTAG box).
303 BREAKPOINT_NONE = 0x00,
304 BREAKPOINT_X = 0x04,
305 BREAKPOINT_Y = 0x08,
306 BREAKPOINT_Z = 0x10,
307
308 // Responses from JTAG ICE
309 JTAG_R_OK = 'A',
310 JTAG_R_BREAK = 'B',
311 JTAG_R_INFO = 'G',
312 JTAG_R_FAILED = 'F',
313 JTAG_R_SYNC_ERROR = 'E',
314 JTAG_R_SLEEP = 'H',
315 JTAG_R_POWER = 'I',
316
317 // JTAG parameters
318 JTAG_P_BITRATE = 'b',
319 JTAG_P_SW_VERSION = 0x7b,
320 JTAG_P_HW_VERSION = 0x7a,
321 JTAG_P_IREG_HIGH = 0x81,
322 JTAG_P_IREG_LOW = 0x82,
323 JTAG_P_OCD_VTARGET = 0x84,
324 JTAG_P_OCD_BREAK_CAUSE = 0x85,
325 JTAG_P_CLOCK = 0x86,
326 JTAG_P_EXTERNAL_RESET = 0x8b, /* W */
327 JTAG_P_FLASH_PAGESIZE_LOW = 0x88, /* W */
328 JTAG_P_FLASH_PAGESIZE_HIGH = 0x89, /* W */
329 JTAG_P_EEPROM_PAGESIZE = 0x8a, /* W */
330 JTAG_P_TIMERS_RUNNING = 0xa0,
331 JTAG_P_BP_FLOW = 0xa1,
332 JTAG_P_BP_X_HIGH = 0xa2,
333 JTAG_P_BP_X_LOW = 0xa3,
334 JTAG_P_BP_Y_HIGH = 0xa4,
335 JTAG_P_BP_Y_LOW = 0xa5,
336 JTAG_P_BP_MODE = 0xa6,
337 JTAG_P_JTAGID_BYTE0 = 0xa7, /* R */
338 JTAG_P_JTAGID_BYTE1 = 0xa8, /* R */
339 JTAG_P_JTAGID_BYTE2 = 0xa9, /* R */
340 JTAG_P_JTAGID_BYTE3 = 0xaa, /* R */
341 JTAG_P_UNITS_BEFORE = 0xab, /* W */
342 JTAG_P_UNITS_AFTER = 0xac, /* W */
343 JTAG_P_BIT_BEFORE = 0xad, /* W */
344 JTAG_P_BIT_AFTER = 0xae, /* W */
345 JTAG_P_PSB0_LOW = 0xaf, /* W */
346 JTAG_P_PSBO_HIGH = 0xb0, /* W */
347 JTAG_P_PSB1_LOW = 0xb1, /* W */
348 JTAG_P_PSB1_HIGH = 0xb2, /* W */
349 JTAG_P_MCU_MODE = 0xb3, /* R */
350
351 // JTAG commands
352 JTAG_C_SET_DEVICE_DESCRIPTOR = 0xA0,
353
354 // Set JTAG bitrate to 1MHz
355 // ff: 1MHz, fe: 500kHz, fd: 250khz, fb: 125Khz
356 // JTAG bitrates
357 JTAG_BITRATE_1_MHz = 0xff,
358 JTAG_BITRATE_500_KHz = 0xfe,
359 JTAG_BITRATE_250_KHz = 0xfd,
360 JTAG_BITRATE_125_KHz = 0xfb,
361
362 // JTAG ICE mkII stuff goes here. Most of this is straight from
363 // AppNote AVR067.
364
365 // Communication with the JTAG ICE works in frames. The protocol
366 // somewhat resembles the STK500v2 protocol, yet it is sufficiently
367 // different to prevent a direct code reuse. :-(
368 //
369 // Frame format:
370 //
371 // +---------------------------------------------------------------+
372 // | 0 | 1 . 2 | 3 . 4 . 5 . 6 | 7 | ... | N-1 . N |
373 // | | | | | | |
374 // | start | LSB MSB | LSB ....... MSB | token | msg | LSB MSB |
375 // | 0x1B | sequence# | message size | 0x0E | | CRC16 |
376 // +---------------------------------------------------------------+
377 //
378 // Each request message will be returned by a response with a matching
379 // sequence #. Sequence # 0xffff is reserved for asynchronous event
380 // notifications that will be sent by the ICE without a request
381 // message (e.g. when the target hit a breakpoint).
382 //
383 // The message size excludes the framing overhead (10 bytes).
384 //
385 // The first byte of the message is always the request or response
386 // code, which is roughly classified as:
387 //
388 // . Messages (commands) use 0x00 through 0x3f. (The documentation
389 // claims that messages start at 0x01, but actually CMND_SIGN_OFF is
390 // 0x00.)
391 // . Internal commands use 0x40 through 0x7f (not documented).
392 // . Success responses use 0x80 through 0x9f.
393 // . Failure responses use 0xa0 through 0xbf.
394 // . Events use 0xe0 through 0xff.
395
396 MESSAGE_START = 0x1b,
397 TOKEN = 0x0e,
398
399 // On the JTAGICE3, the USB reader framework modifies the token of
400 // a received event into this so the upper layers can tell an event
401 // message apart from a normal response message.
402 TOKEN_EVT3 = 'T',
403
404 // Max message size we are willing to accept. Prevents us from trying
405 // to allocate too much VM in case we received a nonsensical packet
406 // length. We have to allocate the buffer as soon as we've got the
407 // length information (and thus have to trust that information by that
408 // time at first), as the final CRC check can only be done once the
409 // entire packet came it.
410 MAX_MESSAGE = 100000,
411
412 // ICE command codes
413 CMND_CHIP_ERASE = 0x13,
414 CMND_CLEAR_EVENTS = 0x22,
415 CMND_CLR_BREAK = 0x1A,
416 CMND_ENTER_PROGMODE = 0x14,
417 CMND_ERASEPAGE_SPM = 0x0D,
418 CMND_FORCED_STOP = 0x0A,
419 CMND_GET_BREAK = 0x12,
420 CMND_GET_PARAMETER = 0x03,
421 CMND_GET_SIGN_ON = 0x01,
422 CMND_GET_SYNC = 0x0f,
423 CMND_GO = 0x08,
424 CMND_LEAVE_PROGMODE = 0x15,
425 CMND_READ_MEMORY = 0x05,
426 CMND_READ_PC = 0x07,
427 CMND_RESET = 0x0B,
428 CMND_RESTORE_TARGET = 0x23,
429 CMND_RUN_TO_ADDR = 0x1C,
430 CMND_SELFTEST = 0x10,
431 CMND_SET_BREAK = 0x11,
432 CMND_SET_BREAK_XMEGA = 0x37, // undocumented in AVR067
433 CMND_SET_DEVICE_DESCRIPTOR = 0x0C,
434 CMND_SET_N_PARAMETERS = 0x16,
435 CMND_SET_XMEGA_PARAMS = 0x36, // undocumented in AVR067
436 CMND_SET_PARAMETER = 0x02,
437 CMND_SIGN_OFF = 0x00,
438 CMND_SINGLE_STEP = 0x09,
439 CMND_SPI_CMD = 0x1D,
440 CMND_WRITE_MEMORY = 0x04,
441 CMND_WRITE_PC = 0x06,
442 CMND_XMEGA_ERASE = 0x34,
443
444 // ICE responses
445 RSP_DEBUGWIRE_SYNC_FAILED = 0xAC,
446 RSP_FAILED = 0xA0,
447 RSP_GET_BREAK = 0x83,
448 RSP_ILLEGAL_BREAKPOINT = 0xA8,
449 RSP_ILLEGAL_COMMAND = 0xAA,
450 RSP_ILLEGAL_EMULATOR_MODE = 0xA4,
451 RSP_ILLEGAL_JTAG_ID = 0xA9,
452 RSP_ILLEGAL_MCU_STATE = 0xA5,
453 RSP_ILLEGAL_MEMORY_TYPE = 0xA2,
454 RSP_ILLEGAL_MEMORY_RANGE = 0xA3,
455 RSP_ILLEGAL_PARAMETER = 0xA1,
456 RSP_ILLEGAL_POWER_STATE = 0xAD,
457 RSP_ILLEGAL_VALUE = 0xA6,
458 RSP_MEMORY = 0x82,
459 RSP_NO_TARGET_POWER = 0xAB,
460 RSP_OK = 0x80,
461 RSP_PARAMETER = 0x81,
462 RSP_PC = 0x84,
463 RSP_SELFTEST = 0x85,
464 RSP_SET_N_PARAMETERS = 0xA7,
465 RSP_SIGN_ON = 0x86,
466 RSP_SPI_DATA = 0x88,
467
468 // ICE events
469 EVT_BREAK = 0xE0,
470 EVT_DEBUG = 0xE6,
471 EVT_ERROR_PHY_FORCE_BREAK_TIMEOUT = 0xE2,
472 EVT_ERROR_PHY_MAX_BIT_LENGTH_DIFF = 0xED,
473 EVT_ERROR_PHY_OPT_RECEIVE_TIMEOUT = 0xF9,
474 EVT_ERROR_PHY_OPT_RECEIVED_BREAK = 0xFA,
475 EVT_ERROR_PHY_RECEIVED_BREAK = 0xF8,
476 EVT_ERROR_PHY_RECEIVE_TIMEOUT = 0xF7,
477 EVT_ERROR_PHY_RELEASE_BREAK_TIMEOUT = 0xE3,
478 EVT_ERROR_PHY_SYNC_OUT_OF_RANGE = 0xF5,
479 EVT_ERROR_PHY_SYNC_TIMEOUT = 0xF0,
480 EVT_ERROR_PHY_SYNC_TIMEOUT_BAUD = 0xF4,
481 EVT_ERROR_PHY_SYNC_WAIT_TIMEOUT = 0xF6,
482 EVT_RESULT_PHY_NO_ACTIVITY = 0xFB,
483 EVT_EXT_RESET = 0xE7,
484 EVT_ICE_POWER_ERROR_STATE = 0xEA,
485 EVT_ICE_POWER_OK = 0xEB,
486 EVT_IDR_DIRTY = 0xEC,
487 EVT_NONE = 0xEF,
488 EVT_PDSB_BREAK = 0xF2,
489 EVT_PDSMB_BREAK = 0xF3,
490 EVT_PROGRAM_BREAK = 0xF1,
491 EVT_RUN = 0xE1,
492 EVT_TARGET_POWER_OFF = 0xE5,
493 EVT_TARGET_POWER_ON = 0xE4,
494 EVT_TARGET_SLEEP = 0xE8,
495 EVT_TARGET_WAKEUP = 0xE9,
496 // trailer
497 EVT_MAX = 0xFF,
498
499 // memory types for CMND_{READ,WRITE}_MEMORY
500 MTYPE_IO_SHADOW = 0x30, // cached IO registers?
501 MTYPE_SRAM = 0x20, // target's SRAM or [ext.] IO registers
502 MTYPE_EEPROM = 0x22, // EEPROM, what way?
503 MTYPE_EVENT = 0x60, // ICE event memory
504 MTYPE_EVENT_COMPRESSED = 0x61, // ICE event memory, bit-mapped
505 MTYPE_SPM = 0xA0, // flash through LPM/SPM
506 MTYPE_FLASH_PAGE = 0xB0, // flash in programming mode
507 MTYPE_EEPROM_PAGE = 0xB1, // EEPROM in programming mode
508 MTYPE_FUSE_BITS = 0xB2, // fuse bits in programming mode
509 MTYPE_LOCK_BITS = 0xB3, // lock bits in programming mode
510 MTYPE_SIGN_JTAG = 0xB4, // signature in programming mode
511 MTYPE_OSCCAL_BYTE = 0xB5, // osccal cells in programming mode
512 MTYPE_CAN = 0xB6, // CAN mailbox
513 MTYPE_XMEGA_REG = 0xB8, // Xmega CPU registers
514 MTYPE_XMEGA_APP_FLASH = 0xC0, // Xmega application flash
515
516 // (some) ICE parameters, for CMND_{GET,SET}_PARAMETER
517 PAR_HW_VERSION = 0x01,
518 PAR_FW_VERSION = 0x02,
519
520 PAR_EMULATOR_MODE = 0x03,
521 EMULATOR_MODE_DEBUGWIRE = 0x00,
522 EMULATOR_MODE_JTAG = 0x01,
523 EMULATOR_MODE_UNKNOWN = 0x02,
524 EMULATOR_MODE_SPI = 0x03,
525 EMULATOR_MODE_JTAG_XMEGA = 0x05,
526 EMULATOR_MODE_PDI = 0x06,
527
528 PAR_IREG = 0x04,
529
530 PAR_BAUD_RATE = 0x05,
531 PAR_BAUD_2400 = 0x01,
532 PAR_BAUD_4800 = 0x02,
533 PAR_BAUD_9600 = 0x03,
534 PAR_BAUD_19200 = 0x04, // default
535 PAR_BAUD_38400 = 0x05,
536 PAR_BAUD_57600 = 0x06,
537 PAR_BAUD_115200 = 0x07,
538 PAR_BAUD_14400 = 0x08,
539
540 PAR_OCD_VTARGET = 0x06,
541 PAR_OCD_JTAG_CLK = 0x07,
542 PAR_OCD_BREAK_CAUSE = 0x08,
543 PAR_TIMERS_RUNNING = 0x09,
544 PAR_BREAK_ON_CHANGE_FLOW = 0x0A,
545 PAR_BREAK_ADDR1 = 0x0B,
546 PAR_BREAK_ADDR2 = 0x0C,
547 PAR_COMBBREAKCTRL = 0x0D,
548 PAR_JTAGID = 0x0E,
549 PAR_UNITS_BEFORE = 0x0F,
550 PAR_UNITS_AFTER = 0x10,
551 PAR_BIT_BEFORE = 0x11,
552 PAR_BIT_ATER = 0x12,
553 PAR_EXTERNAL_RESET = 0x13,
554 PAR_FLASH_PAGE_SIZE = 0x14,
555 PAR_EEPROM_PAGE_SIZE = 0x15,
556 PAR_UNUSED1 = 0x16,
557 PAR_PSB0 = 0x17,
558 PAR_PSB1 = 0x18,
559 PAR_PROTOCOL_DEBUG_EVENT = 0x19,
560
561 PAR_MCU_STATE = 0x1A,
562 STOPPED = 0x00,
563 RUNNING = 0x01,
564 PROGRAMMING = 0x02,
565
566 PAR_DAISY_CHAIN_INFO = 0x1B,
567 PAR_BOOT_ADDRESS = 0x1C,
568 PAR_TARGET_SIGNATURE = 0x1D,
569 PAR_DEBUGWIRE_BAUDRATE = 0x1E,
570 PAR_PROGRAM_ENTRY_POINT = 0x1F,
571 PAR_PACKET_PARSING_ERRORS = 0x40,
572 PAR_VALID_PACKETS_RECEIVED = 0x41,
573 PAR_INTERCOMMUNICATION_TX_FAILURES = 0x42,
574 PAR_INTERCOMMUNICATION_RX_FAILURES = 0x43,
575 PAR_CRC_ERRORS = 0x44,
576
577 PAR_POWER_SOURCE = 0x45,
578 POWER_EXTERNAL = 0x00,
579 POWER_USB = 0x01,
580
581 PAR_CAN_FLAG = 0x22,
582 DONT_READ_CAN_MAILBOX = 0x00,
583 READ_CAN_MAILBOX = 0x01,
584
585 PAR_ENABLE_IDR_IN_RUN_MODE = 0x23,
586 ACCESS_OSCCAL = 0x00,
587 ACCESS_IDR = 0x01,
588
589 PAR_ALLOW_PAGEPROGRAMMING_IN_SCANCHAIN = 0x24,
590 PAGEPROG_NOT_ALLOWED = 0x00,
591 PAGEPROG_ALLOWED = 0x01,
592
593 /* EDBG related constants */
594 EDBG_VENDOR_AVR_CMD = 0x80,
595 EDBG_VENDOR_AVR_RSP = 0x81,
596 EDBG_VENDOR_AVR_EVT = 0x82,
597
598 };
599
600 enum {
601 PC_INVALID = 0xffffffff
602 };
603
604 /*
605 * JTAG ICE mkII breakpoints are quite tricky.
606 *
607 * There are four possible breakpoint slots in JTAG. The first one is
608 * always reserved for single-stepping, and cannot be accessed
609 * directly. The second slot (ID #1) can be freely used as a code
610 * breakpoint (only). The third and fourth slot can either be used as
611 * a code breakpoint, as an independent (byte-wide) data breakpoint
612 * (i.e. a watchpoint in GDB terms), or together as a data breakpoint
613 * consisting of an address and a mask. The latter does not match
614 * directly to GDB watchpoints (of a certain lenght > 1) but imposes
615 * the additional requirement that the base address be aligned
616 * properly wrt. the mask.
617 *
618 * The single-step breakpoint can indirectly be used by filling the
619 * respective high-level language information into the event memory,
620 * and issuing a high-level "step over" single-step. As GDB does not
621 * install the breakpoints for "stepi"-style single-steps (which would
622 * require the very same JTAG breakpoint register), this ought to
623 * work.
624 *
625 * Finally, there are software breakpoints where the respective
626 * instruction will be replaced by a BREAK instruction in flash ROM by
627 * means of an SPM call. Some devices do not allow for this (as they
628 * are said to be broken), and in general as this method contributes
629 * to flash wear, we rather do not uninstall and reinstall these
630 * breakpoints each time GDB asks for it, but instead "cache" them
631 * locally, and only delete and add them as they actually change.
632 *
633 * To add to the mess, GDB's remote protocol isn't very smart about
634 * telling us whether the next list of breakpoints + resume are
635 * actually meant to be a high-level language (HLL) single-step
636 * command. Thus, always assume the last breakpoint that comes in to
637 * be a single-step breakpoint, and replace the "resume" operation by
638 * a "step" one in that case. At least, GDB issues the breakpoint
639 * list in the order of breakpoint numbers, so any possible HLL
640 * single-step breakoint must be the last one in the list.
641 *
642 * Finally, GDB has explicit commands for setting hardware-assisted
643 * breakpoints, but the default "break" command uses a software
644 * breakpoint. We try to replace as many software breakpoints as
645 * possible by hardware breakpoints though, for the sake of
646 * efficiency, yet would want to respect the user's choice for setting
647 * a hardware breakpoint ("hbreak" or "thbreak")...
648 *
649 * XXX This is not done yet.
650 */
651
652 enum bpType
653 {
654 NONE, // disabled.
655 CODE, // normal code space breakpoint.
656 WRITE_DATA, // write data space breakpoint (ie "watch").
657 READ_DATA, // read data space breakpoint (ie "watch").
658 ACCESS_DATA, // read/write data space breakpoint (ie "watch").
659 DATA_MASK, // mask for data space breakpoint.
660 };
661
662 enum {
663 // We distinguish the total possible breakpoints and those for each type
664 // (code or data) - see above
665 MAX_BREAKPOINTS2_CODE = 4,
666 MAX_BREAKPOINTS2_DATA = 2,
667 MAX_BREAKPOINTS2 = 4,
668 // various slot #s
669 BREAKPOINT2_XMEGA_UNAVAIL = 1,
670 BREAKPOINT2_FIRST_DATA = 2,
671 BREAKPOINT2_DATA_MASK = 3,
672
673 MAX_TOTAL_BREAKPOINTS2 = 255
674 };
675
676 struct breakpoint2
677 {
678 // High-level information on breakpoint
679 unsigned int address;
680 unsigned int mask_pointer;
681 bpType type;
682 bool enabled;
683
684 // Used to flag end of list
685 bool last;
686
687 // Low-level information on breakpoint
688 bool icestatus; // Status of breakpoint in ICE itself: 'true'
689 // when is enabled in ACTUAL device
690 bool toremove; // Delete this guy in ICE
691 bool toadd; // Add this guy in ICE
692 uchar bpnum; // ICE's breakpoint number (0x00 for software)
693 bool has_mask; // data watchpoint has a mask associated
694 };
695
696 const struct breakpoint2 default_bp =
697 {
698 0, /* address */
699 0, /* mask_pointer */
700 NONE, /* type */
701 false, /* enabled */
702 true, /* last */
703 false, /* icestatus */
704 false, /* toremove */
705 false, /* toadd */
706 0, /* bpnum*/
707 false, /* has_mask */
708 };
709
710 // Enumerations for target memory type.
711 typedef enum {
712 MEM_FLASH = 0,
713 MEM_EEPROM = 1,
714 MEM_RAM = 2,
715 } BFDmemoryType;
716
717 extern const char *BFDmemoryTypeString[];
718 extern const int BFDmemorySpaceOffset[];
719
720 enum emulator {
721 EMULATOR_JTAGICE,
722 EMULATOR_DRAGON,
723 EMULATOR_JTAGICE3,
724 EMULATOR_EDBG
725 };
726
727
728 // Allocate 1 meg for image buffer. This is where the file data is
729 // stored before writing occurs.
730 #define MAX_IMAGE_SIZE 1000000
731
732
733 typedef struct {
734 uchar val;
735 bool used;
736 } AVRMemoryByte;
737
738
739 // Struct that holds the memory image. We read from file using BFD
740 // into this struct, then pass the entire struct to the target writer.
741 typedef struct {
742 AVRMemoryByte image[MAX_IMAGE_SIZE];
743 unsigned int last_address;
744 unsigned int first_address;
745 bool first_address_ok;
746 bool has_data;
747 const char *name;
748 } BFDimage;
749
750
751 // The Sync_CRC/EOP message terminator (no real CRC in sight...)
752 #define JTAG_EOM 0x20, 0x20
753
754 enum debugproto {
755 PROTO_JTAG, PROTO_DW, PROTO_PDI,
756 };
757
758 class jtag
759 {
760 protected:
761 // The initial serial port parameters. We restore them on exit.
762 struct termios oldtio;
763 bool oldtioValid;
764
765 // The file descriptor used while talking to the JTAG ICE
766 int jtagBox;
767
768 // For the mkII device, is the box attached via USB?
769 bool is_usb;
770
771 // The type of our emulator: JTAG ICE, or AVR Dragon.
772 emulator emu_type;
773
774 // Whether nSRST is to be applied when connecting (override JTD bit).
775 bool apply_nSRST;
776
777 // Total breakpoints including software
778 breakpoint2 bp[MAX_TOTAL_BREAKPOINTS2];
779
780 // Xmega hard breakpoing break handling
781 unsigned int xmega_n_bps;
782 unsigned long xmega_bps[2];
783
784 // This device or connection cannot handle hard BPs
785 bool softbp_only;
786
787 // Target device is an ATxmega one
788 bool is_xmega;
789
790 public:
791 // Whether we are in "programming mode" (changes how program memory
792 // is written, apparently)
793 bool programmingEnabled;
794
795 // Name of the device controlled by the JTAG ICE
796 char *device_name;
797
798 // Pointer to device definition
799 jtag_device_def_type *deviceDef;
800
801 // Daisy chain info
802 struct {
803 unsigned char units_before;
804 unsigned char units_after;
805 unsigned char bits_before;
806 unsigned char bits_after;
807 } dchain;
808
809 protected:
810 void openUSB(const char *jtagDeviceName);
811 void resetUSB(void);
812 int safewrite(const void *b, int count);
813 void changeLocalBitRate(int newBitRate);
814 void restoreSerialPort(void);
815
816 virtual void changeBitRate(int newBitRate) = 0;
817 virtual void setDeviceDescriptor(jtag_device_def_type *dev) = 0;
818 virtual bool synchroniseAt(int bitrate) = 0;
819 virtual void startJtagLink(void) = 0;
820 virtual void deviceAutoConfig(void) = 0;
821 void jtag_flash_image(BFDimage *image, BFDmemoryType memtype,
822 bool program, bool verify);
823 // Return page address of
824 unsigned int page_addr(unsigned int addr, BFDmemoryType memtype)
825 {
826 unsigned int page_size = get_page_size( memtype );
827 return (unsigned int)(addr & (~(page_size - 1)));
828 };
829
830 unsigned int get_page_size(BFDmemoryType memtype);
831
832 public:
833 jtag(void);
834 jtag(const char *dev, char *name, emulator type = EMULATOR_JTAGICE);
835 virtual ~jtag(void);
836
837 // Basic JTAG I/O
838 // -------------
839
840 /** Send initial configuration to setup the JTAG box itself.
841 **/
842 virtual void initJtagBox(void) = 0;
843
844 /**
845 Send initial configuration to the JTAG box when starting a new
846 debug session. (Note: when attaching to a running target, fuse
847 bits cannot be set so debugging must have been enabled earlier)
848
849 The bitrate sets the JTAG bitrate. The bitrate must be less than
850 1/4 that of the target avr frequency or the jtagice will have
851 problems reading from the target. The problems are usually
852 manifested as failed calls to jtagRead().
853 **/
854 virtual void initJtagOnChipDebugging(unsigned long bitrate) = 0;
855
856
857 /** A timed-out read from file descriptor 'fd'.
858
859 'timeout' is in microseconds, it is the maximum interval within which
860 the read must make progress (i.e., it's a per-byte timeout)
861
862 Returns the number of bytes read or -1 for errors other than timeout.
863
864 Note: EOF and timeout cannot be distinguished
865 **/
866 int timeout_read(void *buf, size_t count, unsigned long timeout);
867
868 // Breakpoints
869 // -----------
870
871 /** Clear out the breakpoints. */
872 virtual void deleteAllBreakpoints(void) = 0;
873
874 /** Delete breakpoint at the specified address. */
875 virtual bool deleteBreakpoint(unsigned int address, bpType type, unsigned int length);
876
877 /** Add a code breakpoint at the specified address. */
878 virtual bool addBreakpoint(unsigned int address, bpType type, unsigned int length);
879
880 bool layoutBreakpoints(void);
881
882 /** Send the breakpoint details down to the JTAG box. */
883 virtual void updateBreakpoints(void) = 0;
884
885 /** True if there is a breakpoint at address */
886 virtual bool codeBreakpointAt(unsigned int address) = 0;
887
888 /** Parse a list of event names to *not* cause a break. */
889 virtual void parseEvents(const char *) = 0;
890
891 // Writing to program memory
892 // -------------------------
893
894 /** Switch to faster programming mode, allows chip erase */
895 virtual void enableProgramming(void) = 0;
896
897 /** Switch back to normal programming mode **/
898 virtual void disableProgramming(void) = 0;
899
900 /** Erase all chip memory **/
901 virtual void eraseProgramMemory(void) = 0;
902
903 virtual void eraseProgramPage(unsigned long address) = 0;
904
905 /** Download an image contained in the specified file. */
906 virtual void downloadToTarget(const char* filename, bool program, bool verify) = 0;
907
908 // Running, single stepping, etc
909 // -----------------------------
910
911 /** Retrieve the current Program Counter value, or PC_INVALID if fails */
912 virtual unsigned long getProgramCounter(void) = 0;
913
914 /** Set program counter to 'pc' **/
915 virtual void setProgramCounter(unsigned long pc) = 0;
916
917 /** Reset AVR. **/
918 virtual void resetProgram(bool possible_nSRST) = 0;
919
920 /** Interrupt AVR. **/
921 virtual void interruptProgram(void) = 0;
922
923 /** Resume program execution.
924 Note: the gdb 'continue' command is handled by jtagContinue,
925 this is just the low level command to resume after interruptProgram
926 **/
927 virtual void resumeProgram(void) = 0;
928
929 /** Issue a "single step" command to the JTAG box.
930 **/
931 virtual void jtagSingleStep(void) = 0;
932
933 /** Send the program on it's merry way, and wait for a breakpoint or
934 input from gdb.
935 Return true for a breakpoint, false for gdb input. **/
936 virtual bool jtagContinue(void) = 0;
937
938 // R/W memory
939 // ----------
940
941 /** Read 'numBytes' from target memory address 'addr'.
942
943 The memory space is selected by the high order bits of 'addr' (see
944 above).
945
946 Returns a dynamically allocated buffer with the requested bytes if
947 successful (caller must free), or NULL if the read failed.
948 **/
949 virtual uchar *jtagRead(unsigned long addr, unsigned int numBytes) = 0;
950
951 /** Write 'numBytes' bytes from 'buffer' to target memory address 'addr'
952
953 The memory space is selected by the high order bits of 'addr' (see
954 above).
955
956 Returns true for a successful write, false for a failed one.
957
958 Note: The current behaviour for program-space writes is pretty
959 weird (does not match jtagRead). See comments in jtagrw.cc.
960 **/
961 virtual void jtagWrite(unsigned long addr, unsigned int numBytes, uchar buffer[]) = 0;
962
963
964 /** Write fuses to target.
965
966 The input parameter is a string from command-line, as produced by
967 printf("%x", 0xaabbcc );
968 **/
969 void jtagWriteFuses(char *fuses);
970
971
972 /** Read fuses from target.
973
974 Shows extended, high and low fuse byte.
975 */
976 void jtagReadFuses(void);
977
978
979 /** Display fuses.
980
981 Shows extended, high and low fuse byte.
982 */
983 void jtagDisplayFuses(uchar *fuseBits);
984
985
986 /** Activate OCDEN.
987 */
988 void jtagActivateOcdenFuse(void);
989
990
991 /** Write lockbits to target.
992
993 The input parameter is a string from command-line, as produced by
994 printf("%x", 0xaa);
995 **/
996 void jtagWriteLockBits(char *lock);
997
998
999 /** Read the lock bits from the target and display them.
1000 **/
1001 void jtagReadLockBits(void);
1002
1003
1004 /** Display lockbits.
1005
1006 Shows raw value and individual bits.
1007 **/
1008 void jtagDisplayLockBits(uchar *lockBits);
1009
1010 /** Return read address for CPU status area.
1011
1012 Returns the address to read the CPU status area (SPL, SPH, SREG)
1013 from. 0x5D + DATA_SPACE_ADDR_OFFSET for megaAVR, 0x3D +
1014 DATA_SPACE_ADDR_OFFSET for Xmega devices.
1015 **/
1016 virtual unsigned int statusAreaAddress(void) const = 0;
1017
1018 /** Return read address for CPU registers.
1019
1020 Returns the address to read the CPU registers (r0 through r31).
1021 DATA_SPACE_ADDR_OFFSET for megaAVR, REGISTER_SPACE_ADDR_OFFSET
1022 for Xmega devices.
1023
1024 **/
1025 virtual unsigned int cpuRegisterAreaAddress(void) const = 0;
1026
1027 };
1028
1029 class jtag_exception: public exception
1030 {
1031 protected:
1032 const char *reason;
1033 public:
1034 jtag_exception()
1035 {
1036 reason = "JTAG exception";
1037 }
1038 jtag_exception(const char *r)
1039 {
1040 reason = r;
1041 }
1042 virtual const char * what() const throw()
1043 {
1044 return reason;
1045 }
1046 };
1047
1048 class jtag_timeout_exception: public jtag_exception
1049 {
1050 public:
1051 jtag_timeout_exception(): jtag_exception("JTAG ICE timeout exception") {}
1052 };
1053
1054 class jtag_io_exception: public jtag_exception
1055 {
1056 protected:
1057 unsigned int response_code;
1058
1059 public:
1060 jtag_io_exception(): jtag_exception("Unknown JTAG response exception")
1061 {
1062 response_code = 0;
1063 }
1064 jtag_io_exception(unsigned int code);
1065
1066 unsigned int get_response(void) { return response_code; }
1067 };
1068
1069
1070 extern class jtag *theJtagICE;
1071
1072 #endif