| 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 contains miscellaneous routines for JTAGICE3 protocol. |
| 19 | * |
| 20 | * $Id: jtag3misc.cc 380 2020-08-31 21:01:26Z joerg_wunsch $ |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <termios.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include "avarice.h" |
| 34 | #include "jtag.h" |
| 35 | #include "jtag3.h" |
| 36 | |
| 37 | void jtag3::setJtagParameter(uchar scope, uchar section, uchar item, |
| 38 | uchar *newValue, int valSize) |
| 39 | { |
| 40 | uchar buf[6 + valSize], *resp; |
| 41 | |
| 42 | buf[0] = scope; |
| 43 | buf[1] = CMD3_SET_PARAMETER; |
| 44 | buf[2] = 0; |
| 45 | buf[3] = section; |
| 46 | buf[4] = item; |
| 47 | buf[5] = valSize; |
| 48 | memcpy(buf + 6, newValue, valSize); |
| 49 | |
| 50 | int respsize; |
| 51 | |
| 52 | try |
| 53 | { |
| 54 | doJtagCommand(buf, valSize + 6, "set parameter", resp, respsize); |
| 55 | } |
| 56 | catch (jtag_exception& e) |
| 57 | { |
| 58 | fprintf(stderr, "set parameter command failed: %s\n", |
| 59 | e.what()); |
| 60 | throw; |
| 61 | } |
| 62 | |
| 63 | delete [] resp; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Get a JTAG ICE parameter. Caller must delete [] the response. Note |
| 68 | * that the actual response data returned starts at offset 2. |
| 69 | */ |
| 70 | void jtag3::getJtagParameter(uchar scope, uchar section, uchar item, int length, |
| 71 | uchar *&resp) |
| 72 | { |
| 73 | unsigned char buf[6]; |
| 74 | int respsize; |
| 75 | |
| 76 | buf[0] = scope; |
| 77 | buf[1] = CMD3_GET_PARAMETER; |
| 78 | buf[2] = 0; |
| 79 | buf[3] = section; |
| 80 | buf[4] = item; |
| 81 | buf[5] = length; |
| 82 | |
| 83 | try |
| 84 | { |
| 85 | doJtagCommand(buf, 6, "get parameter", resp, respsize); |
| 86 | } |
| 87 | catch (jtag_exception& e) |
| 88 | { |
| 89 | fprintf(stderr, "get parameter command failed: %s\n", |
| 90 | e.what()); |
| 91 | throw; |
| 92 | } |
| 93 | if (resp[1] != RSP3_DATA || respsize < 3 + length) |
| 94 | { |
| 95 | debugOut("unexpected response to get parameter command: 0x%02x\n", |
| 96 | resp[1]); |
| 97 | delete [] resp; |
| 98 | throw jtag_exception("unexpected response to get parameter command"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 |