| 1 |
/* |
| 2 |
* avrjtag - The "avrjtag" program. |
| 3 |
* Copyright (C) 2001 Scott Finneran & Peter Jansen |
| 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, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 18 |
* |
| 19 |
* This file contains the AVR JTAG ICE protocol documentation. |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
AVR JTAG-ICE serial protocol commands |
| 24 |
|
| 25 |
Background: This document outlines the results of a reverse engineering |
| 26 |
effort of the protocol which exists between AVRStudio and the AVR |
| 27 |
JTAG-Ice. |
| 28 |
|
| 29 |
Something to note is that the protocol supports dynamic bitrate |
| 30 |
changes. AVRStudio does indeed change the bitrate "mid-stream". |
| 31 |
Specifically, it seems to speed it up to 115200bps just prior to |
| 32 |
downloading (and possibly uploading) blocks of date to the |
| 33 |
JTAG-Ice. After the download, the bitrate is restored to 19200bps. |
| 34 |
|
| 35 |
This can make Analysing the protocol somewhat painful using a serial line |
| 36 |
sniffer. |
| 37 |
|
| 38 |
Something that the document does lack is detail on the "state" or |
| 39 |
procedural aspect of the protocol. ie "This command must be proceeded |
| 40 |
by blah blah...." |
| 41 |
|
| 42 |
Most commands are of the format <letter> [operands....]. An assumption |
| 43 |
was made that the letters actually mean something (ie W for write, R for |
| 44 |
read). |
| 45 |
|
| 46 |
*************************************************************** |
| 47 |
|
| 48 |
Hints for those wishing to help with understanding the protocol: |
| 49 |
|
| 50 |
The thing that made understanding the protocol difficult, is that it |
| 51 |
switches bitrates when writing to memory. As such, a traditional |
| 52 |
protocol analyser or line monitor (which connects to the serial line |
| 53 |
itself) will lose sync when this happens!. |
| 54 |
|
| 55 |
One work-around for this is to use a program like HHD Serial Monitor |
| 56 |
(http://www.hhdsoftware.com). Rather than involve serial hardware, this |
| 57 |
program captures the MS-Windows serial driver requests. As such, you can |
| 58 |
see the data sent and received, without worrying about bitrate changes |
| 59 |
(although you can see driver config requests also, so you know when it |
| 60 |
happens). |
| 61 |
|
| 62 |
*************************************************************** |
| 63 |
Formatting: |
| 64 |
|
| 65 |
All commands from the Host end in a Space, Space (shown below as '__') |
| 66 |
|
| 67 |
All commands from the JTAG box start with a 'A' and end in a 'A', except |
| 68 |
when writing to memory, a 'A''A' is sent only after the data is |
| 69 |
written. |
| 70 |
|
| 71 |
Things in [] are hex characters. eg [42] [FA] |
| 72 |
|
| 73 |
Underscores '_' indicate a space. Actual spaces in the text below are |
| 74 |
for the purposes of formatting only. |
| 75 |
|
| 76 |
Serial port works at 19200, N 8 1 |
| 77 |
(until the bitrate change command is issued) |
| 78 |
|
| 79 |
**************************************************************** |
| 80 |
This command is either sync (ie serial line sync) or status or |
| 81 |
something. The first command also appears to have a few spaces sent down |
| 82 |
first. Haven't quite worked it out yet. |
| 83 |
|
| 84 |
S__ : Sync |
| 85 |
|
| 86 |
A AVRNOCD A - the reply |
| 87 |
|
| 88 |
**************************************************************** |
| 89 |
Query: |
| 90 |
These commands start with a ASCII 'q' (ie [71]) |
| 91 |
|
| 92 |
qz__ : query HW version |
| 93 |
|
| 94 |
A [c0] A - the reply |
| 95 |
|
| 96 |
|
| 97 |
q [7B] __ : query SW version |
| 98 |
|
| 99 |
A [66] A |
| 100 |
|
| 101 |
|
| 102 |
q [84] __ : query supply voltage |
| 103 |
|
| 104 |
A [xx] A - the reply voltage = xx/41 |
| 105 |
|
| 106 |
|
| 107 |
q [A7] __ : ???? |
| 108 |
|
| 109 |
A [3f] A |
| 110 |
|
| 111 |
|
| 112 |
q [A8] __ : ???? |
| 113 |
|
| 114 |
A [20] A |
| 115 |
|
| 116 |
|
| 117 |
q [A9] __ : ???? |
| 118 |
|
| 119 |
A [70] A |
| 120 |
|
| 121 |
|
| 122 |
q [AA] __ : ???? |
| 123 |
|
| 124 |
A [29] A |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
**************************************************************** |
| 129 |
|
| 130 |
Appears to somehow configure the JTAG box. The data probably changes for |
| 131 |
different processors. |
| 132 |
|
| 133 |
[A0] [.(123 bytes).....] __ : ???? |
| 134 |
|
| 135 |
A A |
| 136 |
|
| 137 |
**************************************************************** |
| 138 |
|
| 139 |
[A3] __ : Enable program mode. |
| 140 |
|
| 141 |
A A |
| 142 |
|
| 143 |
**************************************************************** |
| 144 |
|
| 145 |
[A4] __ : Disable program mode. |
| 146 |
|
| 147 |
A A |
| 148 |
|
| 149 |
**************************************************************** |
| 150 |
|
| 151 |
[A5] __ : Erase program memory. Program mode must be enabled. |
| 152 |
|
| 153 |
A A |
| 154 |
|
| 155 |
**************************************************************** |
| 156 |
|
| 157 |
|
| 158 |
R [cc] [x][x][y][y] __ : Read memory |
| 159 |
|
| 160 |
where cc is command |
| 161 |
|
| 162 |
[B2] - Fuse Bits |
| 163 |
[B3] - Lock Bits |
| 164 |
[B4] - Signature Bits |
| 165 |
[A0] - Program memory if Prog Mode enabled (read words) |
| 166 |
[B0] - Program memory if Prog Mode disabled (read words) |
| 167 |
[20] - Data Memory |
| 168 |
[22] - EEPROM |
| 169 |
|
| 170 |
x x is the number of locations - 1 (little endian) *1 *2 |
| 171 |
y y is the start address (big endian) |
| 172 |
|
| 173 |
A [....(data)....] [00] A *1 |
| 174 |
|
| 175 |
For data memory, the data returned is a series of bytes (endian-ness |
| 176 |
does not apply). |
| 177 |
|
| 178 |
For prog memory, the data returned is a 16-bit big endian. |
| 179 |
|
| 180 |
*1 note: For program memory each location is 16 bits. As such, it will |
| 181 |
return N words, rather than bytes. These words are little endian. For |
| 182 |
data space, locations are 8 bits wide, so N bytes are returned. |
| 183 |
*2 AVRStudio only requests 0xff bytes at a time. The reliability of the |
| 184 |
system to respond to larger numbers is unknown. |
| 185 |
|
| 186 |
**************************************************************** |
| 187 |
|
| 188 |
W [cc] [x][x][y][y] __ : Write memory |
| 189 |
|
| 190 |
where cc is command |
| 191 |
|
| 192 |
[A0] - Program memory if Prog Mode enabled (write words) |
| 193 |
[B0] - Program memory if Prog Mode disabled (write words) |
| 194 |
[B2] - Fuse Bits |
| 195 |
[B3] - Lock Bits |
| 196 |
[B4] - Signature Bits |
| 197 |
[20] - Data memory (write bytes) |
| 198 |
[22] - EEPROM |
| 199 |
[60] - first program break point at location y y |
| 200 |
this needs the data [01] for the following h command |
| 201 |
|
| 202 |
x x is the numer of locations - 1 (little endian) |
| 203 |
y y is the start address (big endian) |
| 204 |
|
| 205 |
The reply to this command is a single A |
| 206 |
|
| 207 |
Then the Host sends h [....(data)....] __ which is the data |
| 208 |
|
| 209 |
The reply is A A (success) or E E (failure). |
| 210 |
|
| 211 |
ONLY IN THE CASE OF PROG SPACE WRITE: |
| 212 |
The data is written into the flash when the flash programming buffer |
| 213 |
(2048 bits/256 bytes/128 words) is filled. As such, in the event of a |
| 214 |
prog space page being smaller than an entire page (ie < 0x7F words), |
| 215 |
AVRStudio sends down a single write to the last location in the page. |
| 216 |
|
| 217 |
notes: |
| 218 |
- For program memory each location is 16 bits. As such, N words (rather |
| 219 |
than bytes) need to be sent. These words are little endian. |
| 220 |
- The object files output from both AVRStudio & GCC are already |
| 221 |
word-wise little endian. |
| 222 |
|
| 223 |
**************************************************************** |
| 224 |
|
| 225 |
|
| 226 |
3 [x][x][x] __ : set PC |
| 227 |
|
| 228 |
A A : where xxx is the new value for the PC. |
| 229 |
|
| 230 |
**************************************************************** |
| 231 |
|
| 232 |
|
| 233 |
2 __ : get next PC |
| 234 |
|
| 235 |
A [x][x][x] A : where xxx is the next location to execute (?) |
| 236 |
NOTE: APPEARS TO BE INVALID WHEN THE LAST INSTRUCTION |
| 237 |
WAS A BRANCH. |
| 238 |
|
| 239 |
**************************************************************** |
| 240 |
|
| 241 |
|
| 242 |
1 __ : single step command |
| 243 |
|
| 244 |
A A |
| 245 |
|
| 246 |
**************************************************************** |
| 247 |
|
| 248 |
|
| 249 |
G __ : go command |
| 250 |
|
| 251 |
The JTAG box replies immediately with a single |
| 252 |
|
| 253 |
A |
| 254 |
|
| 255 |
|
| 256 |
If breakpoints have been configured, and a breakpoint "hits", the JTAG |
| 257 |
box will send |
| 258 |
|
| 259 |
B [00] [BP] |
| 260 |
|
| 261 |
[BP] - which breakpoint was "hit" (refer breakpoint below). |
| 262 |
[04] - Breakpoint X |
| 263 |
[08] - Breakpoint Y |
| 264 |
[10] - Breakpoint Z |
| 265 |
|
| 266 |
**************************************************************** |
| 267 |
|
| 268 |
|
| 269 |
F __ : finish (halt) |
| 270 |
|
| 271 |
A [00][PCL][PCH] A |
| 272 |
|
| 273 |
[PCL] - PC Low byte |
| 274 |
[PCH] - PC High byte |
| 275 |
|
| 276 |
**************************************************************** |
| 277 |
|
| 278 |
|
| 279 |
x __ : ??? |
| 280 |
|
| 281 |
A A |
| 282 |
|
| 283 |
**************************************************************** |
| 284 |
|
| 285 |
|
| 286 |
"Box Config" commands |
| 287 |
|
| 288 |
These appear to be set actions performed on various configuration |
| 289 |
registers withing the box itself. The generic command appears to be: |
| 290 |
|
| 291 |
B [rn] [val] __ : Box Config |
| 292 |
|
| 293 |
where rn is the register number, val is the value written. |
| 294 |
|
| 295 |
[FD] - ??? |
| 296 |
[FF] - ??? |
| 297 |
[FA] - ??? |
| 298 |
[00] - ??? |
| 299 |
|
| 300 |
[62] set bit rate. |
| 301 |
val = (FA: 19200, FF: 115200) |
| 302 |
|
| 303 |
[86] set JTAG port bitrate (AVRStudio calls this the debug freq.) |
| 304 |
val = (FA: 100kHz, F3: 50kHz, FD: 200kHz) |
| 305 |
|
| 306 |
[81] [00] Written after modifying the PC. |
| 307 |
|
| 308 |
[A1] set break on branch/skip (will run until the next |
| 309 |
branch/skip instruction) |
| 310 |
[A2] val = Breakpoint X Address -High |
| 311 |
[A3] val = Breakpoint X Address -Low |
| 312 |
[A4] val = Breakpoint Y Address -High |
| 313 |
[A5] val = Breakpoint Y Address -Low |
| 314 |
[A6] val = BP mode (see Breakpoints below) |
| 315 |
|
| 316 |
[88] val = ?? (related to config bits. refer below). |
| 317 |
[89] val = ?? (related to config bits. refer below). |
| 318 |
[8A] val = ?? (related to config bits. refer below). |
| 319 |
|
| 320 |
|
| 321 |
Programming Mode. |
| 322 |
|
| 323 |
The following sequence (according to AVRStudio), prepares the chip for |
| 324 |
JTAG programming. It appears to only happen once per session (when |
| 325 |
programming mode is enabled). |
| 326 |
|
| 327 |
Send: [42] [88] [00] [20] [20] |
| 328 |
Reply: [41] [41] |
| 329 |
|
| 330 |
Send: [42] [89] [01] [20] [20] |
| 331 |
Reply: [41] [41] |
| 332 |
|
| 333 |
Send: [42] [8A] [08] [20] [20] |
| 334 |
Reply: [41] [41] |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
Chip Config (Fuse, Signature and Lock) Bits: |
| 340 |
|
| 341 |
These are access using the normal read & write commands using |
| 342 |
"special" address space identifiers.Fuse Bits: |
| 343 |
|
| 344 |
Fuse bits are written/read by accessing address zero of the [B2] |
| 345 |
address space. |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
Signature Bits: |
| 350 |
|
| 351 |
Signature bits are written/read by accessing address zero of the [B4] |
| 352 |
address space. |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
Lock Bits: |
| 357 |
|
| 358 |
Lock bits are written/read by accessing address zero of the [B3] |
| 359 |
address space. |
| 360 |
|
| 361 |
|
| 362 |
Breakpoints: |
| 363 |
|
| 364 |
note: AVRStudio's first breakpoint is set by writing a 1 to the |
| 365 |
desired break address using address space 60 (refer Write |
| 366 |
command). I believe a corresponding write of zero will clear this |
| 367 |
breakpoint.This is a code space breakpoint only. |
| 368 |
|
| 369 |
Where BP mode is a bitmask formatted thusly (don't you think the word |
| 370 |
thusly doesn't get used enough in doco?): |
| 371 |
|
| 372 |
------------------------------------------------- |
| 373 |
| | | | | | | |
| 374 |
| 0 | Mask| BP Active | BP Mode | BP Mode | |
| 375 |
| | Mode| | | (BP X) | (BP Y) | |
| 376 |
------------------------------------------------- |
| 377 |
|
| 378 |
Mask Mode: |
| 379 |
[0] = X & Y are seperate Breakpoints (non-masked mode). |
| 380 |
[1] = X is masked with Y to create a single range of |
| 381 |
breakpoint addresses. |
| 382 |
|
| 383 |
BP Active: |
| 384 |
[00] = No breakpoints enabled |
| 385 |
[01] = Masked Mode (must be set to this value if Mask Mode = 1). |
| 386 |
[10] = 1 Breakpoint (Breakpoint X active only) |
| 387 |
[11] = 2 Breakpoints (Breakpoints X & Y active) |
| 388 |
|
| 389 |
BP Mode: |
| 390 |
[00] = Data Space Read |
| 391 |
[01] = Data Space Write |
| 392 |
[10] = Data Space Read/Write (ie Data Access) |
| 393 |
[11] = Program Space Read |
| 394 |
|
| 395 |
Examples: |
| 396 |
[2C] - Single program BP at [x][x] |
| 397 |
[3F] - two break points at [x][x] and [y][y] |
| 398 |
[28] - single data R/W BP at [x][x] |
| 399 |
[20] - single data R BP at [x][x] |
| 400 |
[24] - single data W BP at [x][x] |
| 401 |
[34] - two data BP's one R at [x][x] and one W at [y][y] |
| 402 |
[35] - two data BP's one R at [x][x] and one R at [y][y] |
| 403 |
[53] - Masked program read BP at [x][x] mask [y][y] |
| 404 |
[50] - Masked data read BP at [x][x] mask [y][y] |
| 405 |
[51] - masked data write BP at [x][x] mask [y][y] |
| 406 |
[52] - masked data RW BP at [x][x] mask [y][y] |
| 407 |
|
| 408 |
The reply is A A |