forked from sysprogs/msp430-gdbproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSP430Target.cpp
390 lines (329 loc) · 12.5 KB
/
MSP430Target.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "stdafx.h"
#include "MSP430Target.h"
#include "MSP430Util.h"
using namespace GDBServerFoundation;
using namespace MSP430Proxy;
#define REPORT_AND_RETURN(msg, result) { ReportLastMSP430Error(msg); return result; }
#define MAIN_SEGMENT_SIZE 512
bool MSP430Proxy::MSP430GDBTarget::Initialize(const GlobalSettings &settings)
{
if (m_bClosePending)
return m_bValid;
m_bVerbose = settings.Verbose;
LONG version = 0;
if (MSP430_Initialize((char *)settings.PortName, &version) != STATUS_OK)
REPORT_AND_RETURN("Cannot initialize MSP430.DLL", false);
m_bClosePending = true;
switch(settings.Interface)
{
case Jtag:
if (m_bVerbose)
printf("Selecting JTAG interface...\n");
if (MSP430_Configure(INTERFACE_MODE, JTAG_IF) != STATUS_OK)
REPORT_AND_RETURN("Cannot select JTAG interface", false);
break;
case SpyBiWare:
if (m_bVerbose)
printf("Selecting Spy-bi-Wire interface...\n");
if (MSP430_Configure(INTERFACE_MODE, SPYBIWIRE_IF) != STATUS_OK)
REPORT_AND_RETURN("Cannot select Spy-bi-Wire interface", false);
break;
case JtagOverSpyBiWare:
if (m_bVerbose)
printf("Selecting JTAG-over-Spy-bi-Wire interface...\n");
if (MSP430_Configure(INTERFACE_MODE, SPYBIWIREJTAG_IF) != STATUS_OK)
REPORT_AND_RETURN("Cannot select JTAG-over-Spy-bi-Wire interface", false);
break;
case AutomaticInterface:
if (m_bVerbose)
printf("Selecting auto interface...\n");
if (MSP430_Configure(INTERFACE_MODE, AUTOMATIC_IF) != STATUS_OK)
REPORT_AND_RETURN("Cannot select auto interface", false);
break;
}
switch(settings.InterfaceSpeed)
{
case Slow:
if (m_bVerbose)
printf("Setting interface speed to slow...\n");
if (MSP430_Configure(SET_INTERFACE_SPEED, SLOW) != STATUS_OK)
REPORT_AND_RETURN("Cannot set interface speed", false);
break;
case Medium:
if (m_bVerbose)
printf("Setting interface speed to medium...\n");
if (MSP430_Configure(SET_INTERFACE_SPEED, MEDIUM) != STATUS_OK)
REPORT_AND_RETURN("Cannot set interface speed", false);
break;
case Fast:
if (m_bVerbose)
printf("Setting interface speed to fast...\n");
if (MSP430_Configure(SET_INTERFACE_SPEED, FAST) != STATUS_OK)
REPORT_AND_RETURN("Cannot set interface speed", false);
break;
}
if (settings.Voltage != 0)
{
if (MSP430_VCC(settings.Voltage) != STATUS_OK)
REPORT_AND_RETURN("Cannot enable Vcc", false);
}
if (MSP430_OpenDevice("DEVICE_UNKNOWN", "", 0, 0, DEVICE_UNKNOWN) != STATUS_OK)
REPORT_AND_RETURN("Cannot identify the MSP430 device", false);
if (MSP430_GetFoundDevice((char *)&m_DeviceInfo, sizeof(m_DeviceInfo)) != STATUS_OK)
REPORT_AND_RETURN("Cannot identify the MSP430 device", false);
if (MSP430_Reset(ALL_RESETS, FALSE, FALSE) != STATUS_OK)
REPORT_AND_RETURN("Cannot reset the MSP430 device", false);
m_bEraseInfoMem = settings.EraseInfoMem;
if (settings.AutoErase)
{
printf("Erasing FLASH...\n");
if (MSP430_Erase(m_bEraseInfoMem ? ERASE_ALL : ERASE_MAIN, m_DeviceInfo.mainStart, m_DeviceInfo.mainEnd - m_DeviceInfo.mainStart) != STATUS_OK)
printf("Warning: cannot erase FLASH: %s\n", GetLastMSP430Error());
else
m_bFLASHErased = true;
}
m_b32BitRegisterMode = settings.Emulate32BitRegisters;
m_DeviceInfo.string[__countof(m_DeviceInfo.string) - 1] = 0;
printf("Found a device: %s\n", m_DeviceInfo.string);
printf("Number of hardware breakpoints: %d\n", m_DeviceInfo.nBreakpoints);
printf("%d bytes of FLASH memory (0x%04x-0x%04x)\n", m_DeviceInfo.mainEnd - m_DeviceInfo.mainStart + 1, m_DeviceInfo.mainStart, m_DeviceInfo.mainEnd);
printf("%d bytes of RAM (0x%04x-0x%04x)\n", m_DeviceInfo.ramEnd - m_DeviceInfo.ramStart + 1, m_DeviceInfo.ramStart, m_DeviceInfo.ramEnd);
if (m_DeviceInfo.ram2End || m_DeviceInfo.ram2Start)
printf("%d bytes of RAM2 (0x%04x-0x%04x)\n", m_DeviceInfo.ram2End - m_DeviceInfo.ram2Start + 1, m_DeviceInfo.ram2Start, m_DeviceInfo.ram2End);
printf("%d bytes of INFO memory (0x%04x-0x%04x)\n", m_DeviceInfo.infoEnd - m_DeviceInfo.infoStart + 1, m_DeviceInfo.infoStart, m_DeviceInfo.infoEnd);
m_UsedBreakpoints.resize(m_DeviceInfo.nBreakpoints);
m_bValid = true;
return true;
}
#include "GlobalSessionMonitor.h"
MSP430Proxy::MSP430GDBTarget::~MSP430GDBTarget()
{
if (m_bClosePending)
{
printf("GDB Disconnected. Releasing MSP430 interface.\n");
MSP430_Close(FALSE);
}
g_SessionMonitor.UnregisterSession(this);
}
bool MSP430Proxy::MSP430GDBTarget::WaitForJTAGEvent()
{
LONGLONG lastReportTime = {0,};
for (;;)
{
LONG state = 0;
if (MSP430_State(&state, m_BreakInPending, NULL) != STATUS_OK)
REPORT_AND_RETURN("Cannot query device state", false);
if (m_bVerbose)
{
LONGLONG currentTime = {0,};
GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(¤tTime));
if ((currentTime - lastReportTime) > (3000000)) //300ms
{
printf("MSP430_State() => %d, break-in %s\n", state, m_BreakInPending ? "requested" : "not requested");
}
}
if (state != RUNNING)
{
m_BreakInPending = false;
return true;
}
}
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430GDBTarget::ExecuteRemoteCommand( const std::string &command, std::string &output )
{
if (command == "help")
{
output = "Supported stub commands:\n\
\tmon help - Display this message\n\
\tmon erase - Erase the FLASH memory\n\
\tmon detach - Disconnect the target, but keep it running\n";
return kGDBSuccess;
}
else if (command == "erase")
{
if (MSP430_Erase(m_bEraseInfoMem ? ERASE_ALL : ERASE_MAIN, m_DeviceInfo.mainStart, m_DeviceInfo.mainEnd - m_DeviceInfo.mainStart) != STATUS_OK)
{
output = "Cannot erase FLASH: ";
output += GetLastMSP430Error();
output += "\n";
}
else
{
output = "Flash memory erased. Run \"load\" to program your binary.\n";
m_bFLASHErased = true;
}
return kGDBSuccess;
}
else if (command == "detach")
{
STATUS_T status = MSP430_Run(FREE_RUN, TRUE);
if (status == STATUS_OK)
{
output = "Successfully detached from target\n";
m_bDetached = true;
}
else
output = "Failed to resume target\n";
return kGDBSuccess;
}
else
return kGDBNotSupported;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430GDBTarget::GetEmbeddedMemoryRegions( std::vector<EmbeddedMemoryRegion> ®ions )
{
if (m_DeviceInfo.mainStart || m_DeviceInfo.mainEnd)
regions.push_back(EmbeddedMemoryRegion(mtFLASH, m_DeviceInfo.mainStart, m_DeviceInfo.mainEnd - m_DeviceInfo.mainStart + 1, MAIN_SEGMENT_SIZE));
if (m_DeviceInfo.ramStart || m_DeviceInfo.ramEnd)
regions.push_back(EmbeddedMemoryRegion(mtRAM, m_DeviceInfo.ramStart, m_DeviceInfo.ramEnd- m_DeviceInfo.ramStart + 1));
if (m_DeviceInfo.ram2Start || m_DeviceInfo.ram2End)
regions.push_back(EmbeddedMemoryRegion(mtRAM, m_DeviceInfo.ram2Start, m_DeviceInfo.ram2End- m_DeviceInfo.ram2Start + 1));
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::GetLastStopRecord( TargetStopRecord *pRec )
{
pRec->Reason = kSignalReceived;
pRec->Extension.SignalNumber = SIGTRAP;
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::ResumeAndWait( int threadID )
{
if (!DoResumeTarget(RUN_TO_BREAKPOINT))
return kGDBUnknownError;
if (!WaitForJTAGEvent())
return kGDBUnknownError;
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::Step( int threadID )
{
if (!DoResumeTarget(SINGLE_STEP))
return kGDBUnknownError;
if (!WaitForJTAGEvent())
return kGDBUnknownError;
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::SendBreakInRequestAsync()
{
m_BreakInPending = true;
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::ReadFrameRelatedRegisters( int threadID, RegisterSetContainer ®isters )
{
LONG rawRegs[16] = {0,};
if (MSP430_Read_Registers(rawRegs, MASKREG(PC) | MASKREG(SP)) != STATUS_OK)
REPORT_AND_RETURN("Cannot read frame-related registers", kGDBUnknownError);
registers[PC] = RegisterValue(rawRegs[PC], m_b32BitRegisterMode ? 4 : 2);
registers[SP] = RegisterValue(rawRegs[SP], m_b32BitRegisterMode ? 4 : 2);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::ReadTargetRegisters( int threadID, RegisterSetContainer ®isters )
{
LONG rawRegs[16] = {0,};
if (MSP430_Read_Registers(rawRegs, ALL_REGS) != STATUS_OK)
REPORT_AND_RETURN("Cannot read device registers", kGDBUnknownError);
for (size_t i = 0; i < __countof(rawRegs); i++)
registers[i] = RegisterValue(rawRegs[i], m_b32BitRegisterMode ? 4 : 2);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::WriteTargetRegisters( int threadID, const RegisterSetContainer ®isters )
{
LONG rawRegs[16] = {0,};
int mask = 0;
for (size_t i = 0; i < 16; i++)
if (registers[i].Valid)
{
mask |= MASKREG(i);
rawRegs[i] = registers[i].ToUInt16();
}
if (MSP430_Write_Registers(rawRegs, mask) != STATUS_OK)
REPORT_AND_RETURN("Cannot write device registers", kGDBUnknownError);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::ReadTargetMemory( ULONGLONG Address, void *pBuffer, size_t *pSizeInBytes )
{
size_t readSize = *pSizeInBytes;
if (MSP430_Read_Memory((LONG)Address, (char *)pBuffer, *pSizeInBytes) != STATUS_OK)
{
char szMsg[256];
_snprintf(szMsg, _TRUNCATE, "Cannot read %d memory bytes at 0x%I64X", readSize, Address);
REPORT_AND_RETURN(szMsg, kGDBUnknownError);
}
if (m_bVerbose)
printf("MSP430_Read_Memory(0x%x, %d) => success\n", (LONG)Address, *pSizeInBytes);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::WriteTargetMemory( ULONGLONG Address, const void *pBuffer, size_t sizeInBytes )
{
if (Address >= m_DeviceInfo.mainStart && Address <= m_DeviceInfo.mainEnd)
{
if (!m_bFLASHErased)
{
printf("Error: FLASH needs to be erased before programming.\nPlease either use gdb with XML support, or execute \"mon erase\" command in GDB.");
return kGDBUnknownError;
}
}
if (MSP430_Write_Memory((LONG)Address, (char *)pBuffer, sizeInBytes) != STATUS_OK)
REPORT_AND_RETURN("Cannot write device memory", kGDBUnknownError);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::GetDynamicLibraryList( std::vector<DynamicLibraryRecord> &libraries )
{
return kGDBNotSupported;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::GetThreadList( std::vector<ThreadRecord> &threads )
{
return kGDBNotSupported;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::SetThreadModeForNextCont( int threadID, DebugThreadMode mode, OUT bool *pNeedRestoreCall, IN OUT INT_PTR *pRestoreCookie )
{
return kGDBNotSupported;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::Terminate()
{
return kGDBNotSupported;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::CreateBreakpoint( BreakpointType type, ULONGLONG Address, unsigned kind, OUT INT_PTR *pCookie )
{
printf("Warning! Breakpoints are no longer supported in non-EEM mode.\n");
return kGDBUnknownError;
}
GDBServerFoundation::GDBStatus MSP430GDBTarget::RemoveBreakpoint( BreakpointType type, ULONGLONG Address, INT_PTR Cookie )
{
printf("Warning! Breakpoints are no longer supported in non-EEM mode.\n");
return kGDBUnknownError;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430GDBTarget::EraseFLASH( ULONGLONG addr, size_t length )
{
m_bFLASHCommandsUsed = true;
if (MSP430_Erase(ERASE_SEGMENT, (LONG)addr, length) != STATUS_OK)
REPORT_AND_RETURN("Cannot erase FLASH memory", kGDBUnknownError);
m_bFLASHErased = true;
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430GDBTarget::WriteFLASH( ULONGLONG addr, const void *pBuffer, size_t length )
{
m_bFLASHCommandsUsed = true;
if (MSP430_Write_Memory((LONG)addr, (char *)pBuffer, length) != STATUS_OK)
REPORT_AND_RETURN("Cannot program FLASH memory", kGDBUnknownError);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430GDBTarget::CommitFLASHWrite()
{
m_bFLASHCommandsUsed = true;
return kGDBSuccess;
}
void MSP430Proxy::MSP430GDBTarget::ReportLastMSP430Error( const char *pHint )
{
if (pHint)
printf("%s: %s\n", pHint, GetLastMSP430Error());
else
printf("%s\n", GetLastMSP430Error());
}
bool MSP430Proxy::MSP430GDBTarget::DoResumeTarget( RUN_MODES_t mode )
{
STATUS_T status = MSP430_Run(mode, FALSE);
if (m_bVerbose)
printf("MSP430_Run(%d) => %d\n", mode, status);
if (status != STATUS_OK)
REPORT_AND_RETURN("Cannot resume device", false);
return true;
}