-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathJLinkManager.cpp
More file actions
182 lines (151 loc) · 4.05 KB
/
JLinkManager.cpp
File metadata and controls
182 lines (151 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "JLinkManager.h"
#include <QDebug>
#include <QProcess>
#include <QThread>
#include <QCoreApplication>
JLinkManager::JLinkManager(const QSharedPointer<QSettings> &settings, QObject *parent)
: QObject(parent), _settings(settings), _proc(this)
{
_proc.setProgram(_settings->value("JLink/path").toString());
QObject::connect(&_proc, SIGNAL(readyReadStandardOutput()), this, SLOT(readStandardOutput()));
QObject::connect(this, &JLinkManager::startScript, this, &JLinkManager::on_startScript);
QObject::connect(this, &JLinkManager::establishConnection, this, &JLinkManager::on_establishConnection);
clearErrorBuffer();
}
JLinkManager::~JLinkManager()
{
}
void JLinkManager::clearErrorBuffer()
{
_errorBuffer = {256, '\0'};
}
void JLinkManager::setSN(const QString &serialNumber)
{
_SN = serialNumber;
}
QString JLinkManager::getSN() const
{
return _SN;
}
bool JLinkManager::isConnected() const
{
if(_state == State::connectionTested)
return true;
return false;
}
void JLinkManager::selectByUSB()
{
if (JLINKARM_EMU_SelectByUSBSN(_SN.toUInt()) < 0)
{
_state = unknown;
_logger->logError("No connection to JLink with S/N " + _SN);
}
}
void JLinkManager::open()
{
if(JLINKARM_Open())
_logger->logError("JLINK: An error occured when opening JLink programmer.");
}
void JLinkManager::setDevice(const QString &device)
{
clearErrorBuffer();
char cmd[0x400];
strcpy_s(cmd, "device = ");
strcat_s(cmd, device.toLocal8Bit().data());
JLINKARM_ExecCommand(cmd, _errorBuffer.data(), _errorBuffer.size());
if(_errorBuffer.at(0) != 0)
{
_logger->logError("JLINK: " + _errorBuffer);
}
}
void JLinkManager::select(int interface)
{
_targetInterface = interface;
JLINKARM_TIF_Select(_targetInterface);
}
void JLinkManager::setSpeed(int speed)
{
_speed = speed;
JLINKARM_SetSpeed(_speed);
}
void JLinkManager::connect()
{
if(JLINKARM_Connect())
{
_logger->logError("JLINK: Could not connect to target.");
}
}
static void _ErrorOutHandler(const char* sError)
{
qDebug() << sError;
}
static int _fHook(const char* sTitle, const char* sMsg, U32 Flags)
{
return JLINK_DLG_BUTTON_YES;
}
int JLinkManager::erase()
{
int error = 0;
JLINK_SetHookUnsecureDialog(_fHook);
JLINKARM_SetErrorOutHandler(_ErrorOutHandler);
JLINKARM_SetWarnOutHandler(_ErrorOutHandler);
error = JLINK_EraseChip();
return error;
}
void JLinkManager::reset()
{
JLINKARM_Reset();
}
void JLinkManager::go()
{
JLINKARM_Go();
}
int JLinkManager::downloadFile(const QString &fileName, int adress)
{
int error = 0;
JLINKARM_BeginDownload(0); // Indicates start of flash download
error = JLINK_DownloadFile(QString(_settings->value("workDirectory").toString() + "/" + fileName).toLocal8Bit().data(), adress); // Load the application binary to address 0
JLINKARM_EndDownload();
return error;
}
void JLinkManager::close()
{
JLINKARM_Close();
}
void JLinkManager::on_establishConnection()
{
if(_SN.isEmpty())
{
_logger->logError("No serial number for the JLink device provided.");
return;
}
_state = waitingTestResponse;
if (JLINKARM_EMU_SelectByUSBSN(_SN.toUInt()) < 0)
{
_state = unknown;
_logger->logError("No connection to JLink with S/N " + _SN);
_logger->logDebug("No connection to JLink with S/N " + _SN);
}
else
{
_state = connectionTested;
_logger->logSuccess("JLink with S/N: " + _SN + " connected");
}
}
void JLinkManager::on_startScript(const QString &scriptFile)
{
_proc.setArguments({"-USB", _SN, "-CommanderScript", QString(_settings->value("workDirectory").toString() + "/" + scriptFile)});
_proc.start();
_proc.waitForStarted(2000);
}
void JLinkManager::readStandardOutput()
{
QByteArray data = _proc.readAllStandardOutput();
data.replace('\0', ' ');
QStringList lines = QString::fromLocal8Bit(data).split("\r\n");
for(auto & line : lines)
{
if(line.size())
qDebug() << line;
}
}