Debian Codegraph @

src/jtag3.h

raw symbols SCIP
1 /*
2 * avarice - The "avarice" program.
3 * Copyright (C) 2012 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 Version 2
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
17 *
18 * This file extends the generic "jtag" class for the JTAGICE3 protocol.
19 *
20 * $Id: jtag3.h 391 2021-08-23 20:57:14Z joerg_wunsch $
21 */
22
23 #ifndef JTAG3_H
24 #define JTAG3_H
25
26 #include "jtag.h"
27
28 enum jtag3consts
29 {
30 SCOPE_INFO = 0x00,
31 SCOPE_GENERAL = 0x01,
32 SCOPE_AVR_ISP = 0x11,
33 SCOPE_AVR = 0x12,
34
35 /* Info scope */
36 CMD3_GET_INFO = 0x00,
37
38 /* byte after GET_INFO is always 0, next is: */
39 CMD3_INFO_NAME = 0x80, /* JTAGICE3 */
40 CMD3_INFO_SERIAL = 0x81, /* J3xxxxxxxxxx */
41
42 /* Generic scope */
43 CMD3_SET_PARAMETER = 0x01,
44 CMD3_GET_PARAMETER = 0x02,
45 CMD3_SIGN_ON = 0x10,
46 CMD3_SIGN_OFF = 0x11, /* takes one parameter? */
47
48 /* AVR ISP scope: no commands of its own */
49
50 /* AVR scope */
51 //CMD3_SET_PARAMETER = 0x01,
52 //CMD3_GET_PARAMETER = 0x02,
53 //CMD3_SIGN_ON = 0x10, /* an additional signon/-off pair */
54 //CMD3_SIGN_OFF = 0x11,
55 CMD3_DEVICE_ID = 0x12,
56 CMD3_START_DEBUG = 0x13,
57 CMD3_STOP_DEBUG = 0x14,
58 CMD3_ENTER_PROGMODE = 0x15,
59 CMD3_LEAVE_PROGMODE = 0x16,
60 CMD3_MONCON_DISABLE = 0x17,
61 CMD3_ERASE_MEMORY = 0x20,
62 CMD3_READ_MEMORY = 0x21,
63 CMD3_WRITE_MEMORY = 0x23,
64 CMD3_RESET = 0x30,
65 CMD3_STOP = 0x31,
66 CMD3_GO = 0x32,
67 CMD3_RUN_TO = 0x33, /* followed by 0x00, word address to stop at*/
68 CMD3_STEP = 0x34,
69 CMD3_READ_PC = 0x35,
70 CMD3_WRITE_PC = 0x36,
71 CMD3_SET_BP = 0x40,
72 CMD3_CLEAR_BP = 0x41,
73 CMD3_SET_BP_XMEGA = 0x42, /* very similar to mkII CMND_SET_BREAK_XMEGA */
74 CMD3_SET_SOFT_BP = 0x43,
75 CMD3_CLEAR_SOFT_BP = 0x44,
76 CMD3_CLEANUP = 0x45,
77
78 /* ICE responses */
79 RSP3_OK = 0x80,
80 RSP3_INFO = 0x81,
81 RSP3_PC = 0x83,
82 RSP3_DATA = 0x84,
83 RSP3_FAILED = 0xA0,
84
85 RSP3_STATUS_MASK = 0xE0,
86
87 /* possible failure codes that could be appended to RSP3_FAILED: */
88 RSP3_FAIL_DEBUGWIRE = 0x10,
89 RSP3_FAIL_PDI = 0x1B,
90 RSP3_FAIL_NO_ANSWER = 0x20,
91 RSP3_FAIL_NO_TARGET_POWER = 0x22,
92 RSP3_FAIL_WRONG_MODE = 0x32, /* CPU running vs. stopped */
93 RSP3_FAIL_UNSUPP_MEMORY = 0x34, /* unsupported memory type */
94 RSP3_FAIL_WRONG_LENGTH = 0x35, /* wrong lenth for mem access */
95 RSP3_FAIL_NOT_UNDERSTOOD = 0x91,
96
97 /* ICE events */
98 EVT3_BREAK = 0x40, /* AVR scope */
99 EVT3_IDR = 0x41, /* AVR scope */
100 EVT3_SLEEP = 0x11, /* General scope, also wakeup */
101 EVT3_POWER = 0x10, /* General scope */
102
103 /* memory types */
104 /* not defined here, identical to generic definitions in jtag.h */
105 // MTYPE_SRAM = 0x20, /* target's SRAM or [ext.] IO registers */
106 // MTYPE_EEPROM = 0x22, /* EEPROM, what way? */
107 // MTYPE_SPM = 0xA0, /* flash through LPM/SPM */
108 // MTYPE_FLASH_PAGE = 0xB0, /* flash in programming mode */
109 // MTYPE_EEPROM_PAGE = 0xB1, /* EEPROM in programming mode */
110 // MTYPE_FUSE_BITS = 0xB2, /* fuse bits in programming mode */
111 // MTYPE_LOCK_BITS = 0xB3, /* lock bits in programming mode */
112 // MTYPE_SIGN_JTAG = 0xB4, /* signature in programming mode */
113 // MTYPE_OSCCAL_BYTE = 0xB5, /* osccal cells in programming mode */
114 // MTYPE_FLASH = 0xc0, /* xmega (app.) flash - undocumented in AVR067 */
115 // MTYPE_BOOT_FLASH = 0xc1, /* xmega boot flash - undocumented in AVR067 */
116 // MTYPE_USERSIG = 0xc5, /* xmega user signature - undocumented in AVR067 */
117 // MTYPE_PRODSIG = 0xc6, /* xmega production signature - undocumented in AVR067 */
118
119 /*
120 * Parameters are divided into sections, where the section number
121 * precedes each parameter address. There are distinct parameter
122 * sets for generic and AVR scope.
123 */
124 PARM3_HW_VER = 0x00, /* section 0, generic scope, 1 byte */
125 PARM3_FW_MAJOR = 0x01, /* section 0, generic scope, 1 byte */
126 PARM3_FW_MINOR = 0x02, /* section 0, generic scope, 1 byte */
127 PARM3_FW_RELEASE = 0x03, /* section 0, generic scope, 1 byte;
128 * always asked for by Atmel Studio,
129 * but never displayed there */
130 PARM3_VTARGET = 0x00, /* section 1, generic scope, 2 bytes,
131 * in millivolts */
132 PARM3_DEVICEDESC = 0x00, /* section 2, memory etc. configuration,
133 * 31 bytes for tiny/mega AVR, 47 bytes
134 * for Xmega; is also used in command
135 * 0x36 in JTAGICEmkII, starting with
136 * firmware 7.x */
137
138 PARM3_ARCH = 0x00, /* section 0, AVR scope, 1 byte */
139 PARM3_ARCH_TINY = 1, /* also small megaAVR with ISP/DW only */
140 PARM3_ARCH_MEGA = 2,
141 PARM3_ARCH_XMEGA = 3,
142
143 PARM3_SESS_PURPOSE = 0x01, /* section 0, AVR scope, 1 byte */
144 PARM3_SESS_PROGRAMMING = 1,
145 PARM3_SESS_DEBUGGING = 2,
146
147 PARM3_CONNECTION = 0x00, /* section 1, AVR scope, 1 byte */
148 PARM3_CONN_ISP = 1,
149 PARM3_CONN_JTAG = 4,
150 PARM3_CONN_DW = 5,
151 PARM3_CONN_PDI = 6,
152
153 PARM3_JTAGCHAIN = 0x01, /* JTAG chain info, AVR scope (units
154 * before/after, bits before/after),
155 * 4 bytes */
156
157 PARM3_CLK_MEGA_PROG = 0x20, /* section 1, AVR scope, 2 bytes (kHz) */
158 PARM3_CLK_MEGA_DEBUG = 0x21, /* section 1, AVR scope, 2 bytes (kHz) */
159 PARM3_CLK_XMEGA_JTAG = 0x30, /* section 1, AVR scope, 2 bytes (kHz) */
160 PARM3_CLK_XMEGA_PDI = 0x31, /* section 1, AVR scope, 2 bytes (kHz) */
161
162 PARM3_TIMERS_RUNNING = 0x00, /* section 3, AVR scope, 1 byte */
163
164
165 /* Xmega erase memory types, for CMND_XMEGA_ERASE */
166 XMEGA_ERASE_CHIP = 0x00,
167 XMEGA_ERASE_APP = 0x01,
168 XMEGA_ERASE_BOOT = 0x02,
169 XMEGA_ERASE_EEPROM = 0x03,
170 XMEGA_ERASE_APP_PAGE = 0x04,
171 XMEGA_ERASE_BOOT_PAGE = 0x05,
172 XMEGA_ERASE_EEPROM_PAGE = 0x06,
173 XMEGA_ERASE_USERSIG = 0x07,
174
175
176 MAX_MESSAGE_SIZE_JTAGICE3 = 512,
177 };
178
179
180 class jtag3: public jtag
181 {
182 private:
183 unsigned short command_sequence;
184 bool signedIn;
185 bool debug_active;
186 enum debugproto proto;
187 unsigned long cached_pc;
188 bool cached_pc_is_valid;
189 bool is_edbg;
190
191 unsigned char flashCache[MAX_FLASH_PAGE_SIZE];
192 unsigned int flashCachePageAddr;
193 unsigned char eepromCache[MAX_EEPROM_PAGE_SIZE];
194 unsigned int eepromCachePageAddr;
195
196 unsigned long appsize;
197 unsigned int device_id;
198
199 unsigned char *cached_event;
200
201 public:
202 jtag3(const char *dev, char *name, enum debugproto prot = PROTO_JTAG,
203 bool nsrst = false,
204 bool xmega = false,
205 bool edbg = false):
206 jtag(dev, name, edbg? EMULATOR_EDBG: EMULATOR_JTAGICE3) {
207 signedIn = debug_active = false;
208 command_sequence = 0;
209 proto = prot;
210 apply_nSRST = nsrst;
211 is_xmega = xmega;
212 xmega_n_bps = 0;
213 flashCachePageAddr = (unsigned int)-1;
214 eepromCachePageAddr = (unsigned short)-1;
215
216 for (int j = 0; j < MAX_TOTAL_BREAKPOINTS2; j++)
217 bp[j] = default_bp;
218 cached_pc_is_valid = false;
219 appsize = 0;
220 device_id = 0;
221 cached_event = NULL;
222 is_edbg = edbg;
223 };
224 virtual ~jtag3(void);
225
226 virtual void initJtagBox(void);
227 virtual void initJtagOnChipDebugging(unsigned long bitrate);
228
229 virtual void deleteAllBreakpoints(void);
230 virtual void updateBreakpoints(void);
231 virtual bool codeBreakpointAt(unsigned int address);
232 virtual void parseEvents(const char *);
233
234 virtual void enableProgramming(void);
235 virtual void disableProgramming(void);
236 virtual void eraseProgramMemory(void);
237 virtual void eraseProgramPage(unsigned long address);
238 virtual void downloadToTarget(const char* filename, bool program, bool verify);
239
240 virtual unsigned long getProgramCounter(void);
241 virtual void setProgramCounter(unsigned long pc);
242 virtual void resetProgram(bool ignored = false);
243 virtual void interruptProgram(void);
244 virtual void resumeProgram(void);
245 virtual void jtagSingleStep(void);
246 virtual bool jtagContinue(void);
247
248 virtual uchar *jtagRead(unsigned long addr, unsigned int numBytes);
249 virtual void jtagWrite(unsigned long addr, unsigned int numBytes, uchar buffer[]);
250 virtual unsigned int statusAreaAddress(void) const {
251 return (is_xmega? 0x3D: 0x5D) + DATA_SPACE_ADDR_OFFSET;
252 };
253 virtual unsigned int cpuRegisterAreaAddress(void) const {
254 return is_xmega? REGISTER_SPACE_ADDR_OFFSET: DATA_SPACE_ADDR_OFFSET;
255 }
256
257 private:
258 virtual void changeBitRate(int newBitRate);
259 virtual bool synchroniseAt(int bitrate);
260 virtual void setDeviceDescriptor(jtag_device_def_type *dev);
261 virtual void startJtagLink(void);
262 virtual void deviceAutoConfig(void);
263 virtual void configDaisyChain(void);
264
265 void sendFrame(uchar *command, int commandSize);
266 int recvFrame(unsigned char *&msg, unsigned short &seqno);
267 int recv(unsigned char *&msg);
268
269 unsigned long b4_to_u32(unsigned char *b) {
270 unsigned long l;
271 l = (unsigned)b[0];
272 l += (unsigned)b[1] << 8;
273 l += (unsigned)(unsigned)b[2] << 16;
274 l += (unsigned)b[3] << 24;
275
276 return l;
277 };
278
279 void u32_to_b4(unsigned char *b, unsigned long l) {
280 b[0] = l & 0xff;
281 b[1] = (l >> 8) & 0xff;
282 b[2] = (l >> 16) & 0xff;
283 b[3] = (l >> 24) & 0xff;
284 };
285
286 unsigned short b2_to_u16(unsigned char *b) {
287 unsigned short l;
288 l = (unsigned)b[0];
289 l += (unsigned)b[1] << 8;
290
291 return l;
292 };
293
294 void u16_to_b2(unsigned char *b, unsigned short l) {
295 b[0] = l & 0xff;
296 b[1] = (l >> 8) & 0xff;
297 };
298
299
300 bool sendJtagCommand(uchar *command, int commandSize,
301 const char *name,
302 uchar *&msg, int &msgsize);
303
304 /** Send a command to the jtag, and return the
305 'responseSize' byte &response, response size in
306 &responseSize.
307
308 If a negative response arrived, throw an exception.
309
310 Caller must delete [] the response.
311 **/
312 void doJtagCommand(uchar *command, int commandSize,
313 const char *name,
314 uchar *&response, int &responseSize);
315
316 /** Simplified form of doJtagCommand:
317 Send 1-byte command 'cmd' to JTAG ICE, expecting a
318 response that consists only of the status byte which must be
319 RSP_OK.
320 **/
321 void doSimpleJtagCommand(uchar cmd, const char *name,
322 uchar scope = SCOPE_AVR);
323
324 // Miscellaneous
325 // -------------
326
327 /** Set JTAG ICE parameter 'item' to 'newValue' **/
328 void setJtagParameter(uchar scope, uchar section, uchar item,
329 uchar *newValue, int valSize);
330
331 /** Return value of JTAG ICE parameter 'item'; caller must delete
332 [] resp
333 **/
334 void getJtagParameter(uchar scope, uchar section, uchar item, int length,
335 uchar *&resp);
336
337 uchar memorySpace(unsigned long &addr);
338
339 /** debugWire version of the breakpoint updater.
340 **/
341 void updateBreakpintsDW(void);
342
343 /** Wait until either the ICE or GDB issued an event. As this is
344 the heart of jtagContinue for the mkII, it returns true when a
345 breakpoint was reached, and false for GDB input.
346 **/
347 bool eventLoop(void);
348
349 /** Expect an ICE event in the input stream.
350 **/
351 void expectEvent(bool &breakpoint, bool &gdbInterrupt);
352
353 /** Update Xmega breakpoints on target
354 **/
355 void xmegaSendBPs(void);
356 };
357
358 class jtag3_io_exception: public jtag_io_exception
359 {
360 public:
361 jtag3_io_exception(): jtag_io_exception(0) {}
362 jtag3_io_exception(unsigned int code);
363
364 unsigned int get_response(void) { return response_code; }
365 };
366
367 #endif