File: tilib_api.c

package info (click to toggle)
mspdebug 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,700 kB
  • sloc: ansic: 67,821; makefile: 201; xml: 19
file content (786 lines) | stat: -rw-r--r-- 21,388 bytes parent folder | download
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/* MSPDebug - debugging tool for MSP430 MCUs
 * Copyright (C) 2009-2015 Daniel Beer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stddef.h>
#include <string.h>
#include "util/output.h"
#include "tilib_api.h"
#include "dynload.h"

static dynload_handle_t lib_handle;
const struct tilib_api_table *tilib_api;

#if defined(__Windows__) || defined(__CYGWIN__)
static const char tilib_filename[] = "MSP430.DLL";
#define TIDLL __stdcall
#else
static const char tilib_filename[] = "libmsp430.so";
#define TIDLL
#endif

static void *get_func(const char *name)
{
	void *ret = dynload_sym(lib_handle, name);

	if (!ret) {
		printc_err("tilib_api: can't find symbol \"%s\": %s\n",
			   name, dynload_error());
		return NULL;
	}

	return ret;
}

/************************************************************************
 * New API (post-SLAC460L)
 */

struct new_messageid {
	uint32_t uiMsgIdSingleStep;
	uint32_t uiMsgIdBreakpoint;
	uint32_t uiMsgIdStorage;
	uint32_t uiMsgIdState;
	uint32_t uiMsgIdWarning;
	uint32_t uiMsgIdCPUStopped;
};

static void o2n_messageid(struct new_messageid *dst,
			  const MessageID_t *src)
{
	dst->uiMsgIdSingleStep = src->uiMsgIdSingleStep;
	dst->uiMsgIdBreakpoint = src->uiMsgIdBreakpoint;
	dst->uiMsgIdStorage = src->uiMsgIdStorage;
	dst->uiMsgIdState = src->uiMsgIdState;
	dst->uiMsgIdWarning = src->uiMsgIdWarning;
	dst->uiMsgIdCPUStopped = src->uiMsgIdCPUStopped;
}

struct new_breakpoint {
	BpMode_t          bpMode;
	int32_t           lAddrVal;
	BpType_t          bpType;
	int32_t           lReg;
	BpAccess_t        bpAccess;
	BpAction_t        bpAction;
	BpOperat_t        bpOperat;
	int32_t           lMask;
	int32_t           lRangeEndAdVa;
	BpRangeAction_t   bpRangeAction;
	BpCondition_t     bpCondition;
	uint32_t          lCondMdbVal;
	BpAccess_t        bpCondAccess;
	int32_t           lCondMask;
	BpOperat_t        bpCondOperat;
	uint16_t          wExtCombine;
};

static void o2n_breakpoint(struct new_breakpoint *dst,
			   const BpParameter_t *src)
{
	dst->bpMode = src->bpMode;
	dst->lAddrVal = src->lAddrVal;
	dst->bpType = src->bpType;
	dst->lReg = src->lReg;
	dst->bpAccess = src->bpAccess;
	dst->bpAction = src->bpAction;
	dst->bpOperat = src->bpOperat;
	dst->lMask = src->lMask;
	dst->lRangeEndAdVa = src->lRangeEndAdVa;
	dst->bpRangeAction = src->bpRangeAction;
	dst->bpCondition = src->bpCondition;
	dst->lCondMdbVal = src->lCondMdbVal;
	dst->bpCondAccess = src->bpCondAccess;
	dst->lCondMask = src->lCondMask;
	dst->bpCondOperat = src->bpCondOperat;
	dst->wExtCombine = src->wExtCombine;
}

union new_device {
	uint8_t buffer[112];
	struct {
		uint16_t  endian;
		uint16_t  id;
		uint8_t   string[32];
		uint16_t  mainStart;
		uint16_t  infoStart;
		uint16_t  ramEnd;
		uint16_t  nBreakpoints;
		uint16_t  emulation;
		uint16_t  clockControl;
		uint16_t  lcdStart;
		uint16_t  lcdEnd;
		uint16_t  vccMinOp;
		uint16_t  vccMaxOp;
		uint16_t  hasTestVpp;
		uint16_t  ramStart;
		uint16_t  ram2Start;
		uint16_t  ram2End;
		uint16_t  infoEnd;
		uint32_t  mainEnd;
		uint16_t  bslStart;
		uint16_t  bslEnd;
		uint16_t  nRegTrigger;
		uint16_t  nCombinations;
		uint8_t   cpuArch;
		uint8_t   jtagId;
		uint16_t  coreIpId;
		uint32_t  deviceIdPtr;
		uint16_t  eemVersion;
		uint16_t  nBreakpointsOptions;
		uint16_t  nBreakpointsReadWrite;
		uint16_t  nBreakpointsDma;
		uint16_t  TrigerMask;
		uint16_t  nRegTriggerOperations;
		uint16_t  nStateStorage;
		uint16_t  nCycleCounter;
		uint16_t  nCycleCounterOperations;
		uint16_t  nSequencer;
		uint16_t  HasFramMemroy;
		uint16_t  mainSegmentSize;
	} __attribute__((packed));
};

static void n2o_device(union DEVICE_T *dst, const union new_device *src)
{
	dst->endian = src->endian;
	dst->id = src->id;
	memcpy(dst->string, src->string, sizeof(dst->string));
	dst->mainStart = src->mainStart;
	dst->infoStart = src->infoStart;
	dst->ramEnd = src->ramEnd;
	dst->nBreakpoints = src->nBreakpoints;
	dst->emulation = src->emulation;
	dst->clockControl = src->clockControl;
	dst->lcdStart = src->lcdStart;
	dst->lcdEnd = src->lcdEnd;
	dst->vccMinOp = src->vccMinOp;
	dst->vccMaxOp = src->vccMaxOp;
	dst->hasTestVpp = src->hasTestVpp;
	dst->ramStart = src->ramStart;
	dst->ram2Start = src->ram2Start;
	dst->ram2End = src->ram2End;
	dst->infoEnd = src->infoEnd;
	dst->mainEnd = src->mainEnd;
	dst->bslStart = src->bslStart;
	dst->bslEnd = src->bslEnd;
	dst->nRegTrigger = src->nRegTrigger;
	dst->nCombinations = src->nCombinations;
	dst->cpuArch = src->cpuArch;
	dst->jtagId = src->jtagId;
	dst->coreIpId = src->coreIpId;
	dst->deviceIdPtr = src->deviceIdPtr;
	dst->eemVersion = src->eemVersion;
	dst->nBreakpointsOptions = src->nBreakpointsOptions;
	dst->nBreakpointsReadWrite = src->nBreakpointsReadWrite;
	dst->nBreakpointsDma = src->nBreakpointsDma;
	dst->TrigerMask = src->TrigerMask;
	dst->nRegTriggerOperations = src->nRegTriggerOperations;
	dst->nStateStorage = src->nStateStorage;
	dst->nCycleCounter = src->nCycleCounter;
	dst->nCycleCounterOperations = src->nCycleCounterOperations;
	dst->nSequencer = src->nSequencer;
	dst->HasFramMemroy = src->HasFramMemroy;
	/* dst->mainSegmentSize = src->mainSegmentSize; */
}

typedef void (*new_notify_func_t)
    (uint32_t MsgId, uint32_t wParam, uint32_t lParam, int32_t clientHandle);

struct tilib_new_api {
	/* MSP430.h */
	int32_t TIDLL (*MSP430_Initialize)(char *port, int32_t *version);
	int32_t TIDLL (*MSP430_VCC)(int32_t voltage);
	int32_t TIDLL (*MSP430_Configure)(int32_t mode, int32_t value);
	int32_t TIDLL (*MSP430_OpenDevice)(char *Device, char *Password,
					    int32_t PwLength, int32_t DeviceCode,
					    int32_t setId);
	int32_t TIDLL (*MSP430_GetFoundDevice)(uint8_t *FoundDevice,
						int32_t count);
	int32_t TIDLL (*MSP430_Close)(int32_t vccOff);
	int32_t TIDLL (*MSP430_Memory)(int32_t address, char *buffer,
					int32_t count, int32_t rw);
	int32_t TIDLL (*MSP430_Reset)(int32_t method, int32_t execute,
				       int32_t releaseJTAG);
	int32_t TIDLL (*MSP430_Erase)(int32_t type, int32_t address,
				       int32_t length);
	int32_t TIDLL (*MSP430_Secure)(void);
	int32_t TIDLL (*MSP430_Error_Number)(void);
	const char *TIDLL (*MSP430_Error_String)(int32_t errNumber);

	int32_t TIDLL (*MSP430_GetNumberOfUsbIfs)(int32_t* number);
	int32_t TIDLL (*MSP430_GetNameOfUsbIf)(int32_t idx, char **name,
						int32_t *status);
	int32_t TIDLL (*MSP430_LoadDeviceDb)(const char *f); // needed for slac460s

	/* MSP430_Debug.h */
	int32_t TIDLL (*MSP430_Registers)(int32_t *registers, int32_t mask,
					   int32_t rw);
	int32_t TIDLL (*MSP430_Run)(int32_t mode, int32_t releaseJTAG);
	int32_t TIDLL (*MSP430_State)(int32_t *state, int32_t stop,
				       int32_t *pCPUCycles);

	/* MSP430_EEM.h */
	int32_t TIDLL (*MSP430_EEM_Init)(new_notify_func_t callback,
					  int32_t clientHandle,
					  struct new_messageid *pMsgIdBuffer);
	int32_t TIDLL (*MSP430_EEM_SetBreakpoint)(uint16_t *pwBpHandle,
					   struct new_breakpoint *pBpBuffer);

	/* MSP430_FET.h */
	int32_t TIDLL (*MSP430_FET_FwUpdate)(char* lpszFileName,
					      new_notify_func_t callback,
					      int32_t clientHandle);

	/* Callback thunk data */
	DLL430_EVENTNOTIFY_FUNC		cb_event;
	DLL430_FET_NOTIFY_FUNC		cb_fw;
};

static struct tilib_new_api napi;

static STATUS_T new_Initialize(char *port, long *version)
{
	int32_t nv;
	int r;

	r = napi.MSP430_Initialize(port, &nv);
	if (r < 0)
		return r;

	if (napi.MSP430_LoadDeviceDb)
	    napi.MSP430_LoadDeviceDb(NULL);

	*version = nv;
	return 0;
}

static STATUS_T new_VCC(long voltage)
{
	return napi.MSP430_VCC(voltage);
}

static STATUS_T new_Configure(long mode, long value)
{
	return napi.MSP430_Configure(mode, value);
}

static STATUS_T new_OpenDevice(char *Device, char *Password,
			       long PwLength, long DeviceCode,
			       long setId)
{
	return napi.MSP430_OpenDevice(Device, Password, PwLength,
				     DeviceCode, setId);
}

static STATUS_T new_GetFoundDevice(char *FoundDevice, long count)
{
	union new_device ndev;
	union DEVICE_T odev;
	int r;

	r = napi.MSP430_GetFoundDevice(ndev.buffer, sizeof(ndev.buffer));
	if (r < 0)
		return r;

	memset(&odev, 0, sizeof(odev));
	n2o_device(&odev, &ndev);

	if (count > sizeof(odev.buffer))
		count = sizeof(odev.buffer);
	memcpy(FoundDevice, odev.buffer, count);
	return 0;
}

static STATUS_T new_Close(long vccOff)
{
	return napi.MSP430_Close(vccOff);
}

static STATUS_T new_Memory(long address, char *buffer, long count, long rw)
{
	return napi.MSP430_Memory(address, buffer, count, rw);
}

static STATUS_T new_Reset(long method, long execute, long releaseJTAG)
{
	return napi.MSP430_Reset(method, execute, releaseJTAG);
}

static STATUS_T new_Erase(long type, long address, long length)
{
	return napi.MSP430_Erase(type, address, length);
}

static STATUS_T new_Secure(void)
{
	return napi.MSP430_Secure();
}

static STATUS_T new_Error_Number(void)
{
	return napi.MSP430_Error_Number();
}

static const char *new_Error_String(long errNumber)
{
	return napi.MSP430_Error_String(errNumber);
}

static STATUS_T new_GetNumberOfUsbIfs(long* number)
{
	int32_t nn;
	int r;

	r = napi.MSP430_GetNumberOfUsbIfs(&nn);
	if (r < 0)
		return r;

	*number = nn;
	return 0;
}

static STATUS_T new_GetNameOfUsbIf(long idx, char **name, long *status)
{
	int32_t ns;
	int r;

	r = napi.MSP430_GetNameOfUsbIf(idx, name, &ns);
	if (r < 0)
		return r;

	*status = ns;
	return 0;
}

static STATUS_T new_Registers(long *registers, long mask, long rw)
{
	int32_t nr[16];
	int i;

	for (i = 0; i < 16; i++)
		if (mask & (1 << i))
			nr[i] = registers[i];

	i = napi.MSP430_Registers(nr, mask, rw);
	if (i < 0)
		return i;

	for (i = 0; i < 16; i++)
		if (mask & (1 << i))
			registers[i] = nr[i];

	return 0;
}

static STATUS_T new_Run(long mode, long releaseJTAG)
{
	return napi.MSP430_Run(mode, releaseJTAG);
}

static STATUS_T new_State(long *state, long stop, long *pCPUCycles)
{
	int32_t ns;
	int32_t nc;
	int r;

	r = napi.MSP430_State(&ns, stop, &nc);
	if (r < 0)
		return r;

	*state = ns;
	*pCPUCycles = nc;
	return 0;
}

static void new_event(uint32_t MsgId, uint32_t wParam,
		      uint32_t lParam, int32_t clientHandle)
{
	napi.cb_event(MsgId, wParam, lParam, clientHandle);
}

static STATUS_T new_EEM_Init(DLL430_EVENTNOTIFY_FUNC callback,
			     long clientHandle, MessageID_t *pMsgIdBuffer)
{
	struct new_messageid nm;

	napi.cb_event = callback;
	o2n_messageid(&nm, pMsgIdBuffer);

	return napi.MSP430_EEM_Init(new_event, clientHandle, &nm);
}

static STATUS_T new_EEM_SetBreakpoint(uint16_t *pwBpHandle,
				      BpParameter_t *pBpBuffer)
{
	struct new_breakpoint np;

	o2n_breakpoint(&np, pBpBuffer);
	return napi.MSP430_EEM_SetBreakpoint(pwBpHandle, &np);
}

static void new_fw(uint32_t MsgId, uint32_t wParam,
		   uint32_t lParam, int32_t clientHandle)
{
	napi.cb_fw(MsgId, wParam, lParam, clientHandle);
}

static STATUS_T new_FET_FwUpdate(char* lpszFileName,
				 DLL430_FET_NOTIFY_FUNC callback,
				 long clientHandle)
{
	napi.cb_fw = callback;
	return napi.MSP430_FET_FwUpdate(lpszFileName, new_fw, clientHandle);
}

static const struct tilib_api_table new_tab = {
	.MSP430_Initialize		= new_Initialize,
	.MSP430_VCC			= new_VCC,
	.MSP430_Configure		= new_Configure,
	.MSP430_OpenDevice		= new_OpenDevice,
	.MSP430_GetFoundDevice		= new_GetFoundDevice,
	.MSP430_Close			= new_Close,
	.MSP430_Memory			= new_Memory,
	.MSP430_Reset			= new_Reset,
	.MSP430_Erase			= new_Erase,
	.MSP430_Secure			= new_Secure,
	.MSP430_Error_Number		= new_Error_Number,
	.MSP430_Error_String		= new_Error_String,
	.MSP430_GetNumberOfUsbIfs	= new_GetNumberOfUsbIfs,
	.MSP430_GetNameOfUsbIf		= new_GetNameOfUsbIf,
	.MSP430_Registers		= new_Registers,
	.MSP430_Run			= new_Run,
	.MSP430_State			= new_State,
	.MSP430_EEM_Init		= new_EEM_Init,
	.MSP430_EEM_SetBreakpoint	= new_EEM_SetBreakpoint,
	.MSP430_FET_FwUpdate		= new_FET_FwUpdate,
};

static int init_new_api(void)
{
	if (!(napi.MSP430_Initialize = get_func("MSP430_Initialize")))
		return -1;
	if (!(napi.MSP430_VCC = get_func("MSP430_VCC")))
		return -1;
	if (!(napi.MSP430_Configure = get_func("MSP430_Configure")))
		return -1;
	if (!(napi.MSP430_OpenDevice = get_func("MSP430_OpenDevice")))
		return -1;
	if (!(napi.MSP430_GetFoundDevice = get_func("MSP430_GetFoundDevice")))
		return -1;
	if (!(napi.MSP430_Close = get_func("MSP430_Close")))
		return -1;
	if (!(napi.MSP430_Memory = get_func("MSP430_Memory")))
		return -1;
	if (!(napi.MSP430_Reset = get_func("MSP430_Reset")))
		return -1;
	if (!(napi.MSP430_Erase = get_func("MSP430_Erase")))
		return -1;
	if (!(napi.MSP430_Secure = get_func("MSP430_Secure")))
		return -1;
	if (!(napi.MSP430_Error_Number = get_func("MSP430_Error_Number")))
		return -1;
	if (!(napi.MSP430_Error_String = get_func("MSP430_Error_String")))
		return -1;
	if (!(napi.MSP430_GetNumberOfUsbIfs =
		    get_func("MSP430_GetNumberOfUsbIfs")))
		return -1;
	napi.MSP430_LoadDeviceDb = dynload_sym(lib_handle, "MSP430_LoadDeviceDb");
	if (!(napi.MSP430_GetNameOfUsbIf = get_func("MSP430_GetNameOfUsbIf")))
		return -1;
	if (!(napi.MSP430_Registers = get_func("MSP430_Registers")))
		return -1;
	if (!(napi.MSP430_Run = get_func("MSP430_Run")))
		return -1;
	if (!(napi.MSP430_State = get_func("MSP430_State")))
		return -1;
	if (!(napi.MSP430_EEM_Init = get_func("MSP430_EEM_Init")))
		return -1;
	if (!(napi.MSP430_EEM_SetBreakpoint =
			get_func("MSP430_EEM_SetBreakpoint")))
		return -1;
	if (!(napi.MSP430_FET_FwUpdate = get_func("MSP430_FET_FwUpdate")))
		return -1;

	tilib_api = &new_tab;
	return 0;
}

/************************************************************************
 * Old API (pre-SLAC460L)
 */

struct tilib_old_api {
	/* MSP430.h */
	STATUS_T TIDLL (*MSP430_Initialize)(char *port, long *version);
	STATUS_T TIDLL (*MSP430_VCC)(long voltage);
	STATUS_T TIDLL (*MSP430_Configure)(long mode, long value);
	STATUS_T TIDLL (*MSP430_OpenDevice)(char *Device, char *Password,
					     long PwLength, long DeviceCode,
					     long setId);
	STATUS_T TIDLL (*MSP430_GetFoundDevice)(char *FoundDevice,
						 long count);
	STATUS_T TIDLL (*MSP430_Close)(long vccOff);
	STATUS_T TIDLL (*MSP430_Memory)(long address, char *buffer,
					 long count, long rw);
	STATUS_T TIDLL (*MSP430_Reset)(long method, long execute,
					long releaseJTAG);
	STATUS_T TIDLL (*MSP430_Erase)(long type, long address, long length);
	STATUS_T TIDLL (*MSP430_Secure)(void);
	STATUS_T TIDLL (*MSP430_Error_Number)(void);
	const char *TIDLL (*MSP430_Error_String)(long errNumber);

	STATUS_T TIDLL (*MSP430_GetNumberOfUsbIfs)(long* number);
	STATUS_T TIDLL (*MSP430_GetNameOfUsbIf)(long idx, char **name,
						 long *status);

	/* MSP430_Debug.h */
	STATUS_T TIDLL (*MSP430_Registers)(long *registers, long mask,
					    long rw);
	STATUS_T TIDLL (*MSP430_Run)(long mode, long releaseJTAG);
	STATUS_T TIDLL (*MSP430_State)(long *state, long stop,
					long *pCPUCycles);

	/* MSP430_EEM.h */
	STATUS_T TIDLL (*MSP430_EEM_Init)(DLL430_EVENTNOTIFY_FUNC callback,
					   long clientHandle,
					   MessageID_t *pMsgIdBuffer);
	STATUS_T TIDLL (*MSP430_EEM_SetBreakpoint)(uint16_t *pwBpHandle,
						    BpParameter_t *pBpBuffer);

	/* MSP430_FET.h */
	STATUS_T TIDLL (*MSP430_FET_FwUpdate)(char* lpszFileName,
					   DLL430_FET_NOTIFY_FUNC callback,
					   long clientHandle);
};

static struct tilib_old_api old;

static STATUS_T old_Initialize(char *port, long *version)
{
	return old.MSP430_Initialize(port, version);
}

static STATUS_T old_VCC(long voltage)
{
	return old.MSP430_VCC(voltage);
}

static STATUS_T old_Configure(long mode, long value)
{
	return old.MSP430_Configure(mode, value);
}

static STATUS_T old_OpenDevice(char *Device, char *Password,
			       long PwLength, long DeviceCode,
			       long setId)
{
	return old.MSP430_OpenDevice(Device, Password, PwLength,
				     DeviceCode, setId);
}

static STATUS_T old_GetFoundDevice(char *FoundDevice, long count)
{
	return old.MSP430_GetFoundDevice(FoundDevice, count);
}

static STATUS_T old_Close(long vccOff)
{
	return old.MSP430_Close(vccOff);
}

static STATUS_T old_Memory(long address, char *buffer, long count, long rw)
{
	return old.MSP430_Memory(address, buffer, count, rw);
}

static STATUS_T old_Reset(long method, long execute, long releaseJTAG)
{
	return old.MSP430_Reset(method, execute, releaseJTAG);
}

static STATUS_T old_Erase(long type, long address, long length)
{
	return old.MSP430_Erase(type, address, length);
}

static STATUS_T old_Secure(void)
{
	return old.MSP430_Secure();
}

static STATUS_T old_Error_Number(void)
{
	return old.MSP430_Error_Number();
}

static const char *old_Error_String(long errNumber)
{
	return old.MSP430_Error_String(errNumber);
}

static STATUS_T old_GetNumberOfUsbIfs(long* number)
{
	return old.MSP430_GetNumberOfUsbIfs(number);
}

static STATUS_T old_GetNameOfUsbIf(long idx, char **name, long *status)
{
	return old.MSP430_GetNameOfUsbIf(idx, name, status);
}

static STATUS_T old_Registers(long *registers, long mask, long rw)
{
	return old.MSP430_Registers(registers, mask, rw);
}

static STATUS_T old_Run(long mode, long releaseJTAG)
{
	return old.MSP430_Run(mode, releaseJTAG);
}

static STATUS_T old_State(long *state, long stop, long *pCPUCycles)
{
	return old.MSP430_State(state, stop, pCPUCycles);
}

static STATUS_T old_EEM_Init(DLL430_EVENTNOTIFY_FUNC callback,
			     long clientHandle, MessageID_t *pMsgIdBuffer)
{
	return old.MSP430_EEM_Init(callback, clientHandle, pMsgIdBuffer);
}

static STATUS_T old_EEM_SetBreakpoint(uint16_t *pwBpHandle,
				      BpParameter_t *pBpBuffer)
{
	return old.MSP430_EEM_SetBreakpoint(pwBpHandle, pBpBuffer);
}

static STATUS_T old_FET_FwUpdate(char* lpszFileName,
				 DLL430_FET_NOTIFY_FUNC callback,
				 long clientHandle)
{
	return old.MSP430_FET_FwUpdate(lpszFileName, callback, clientHandle);
}

static const struct tilib_api_table old_tab = {
	.MSP430_Initialize		= old_Initialize,
	.MSP430_VCC			= old_VCC,
	.MSP430_Configure		= old_Configure,
	.MSP430_OpenDevice		= old_OpenDevice,
	.MSP430_GetFoundDevice		= old_GetFoundDevice,
	.MSP430_Close			= old_Close,
	.MSP430_Memory			= old_Memory,
	.MSP430_Reset			= old_Reset,
	.MSP430_Erase			= old_Erase,
	.MSP430_Secure			= old_Secure,
	.MSP430_Error_Number		= old_Error_Number,
	.MSP430_Error_String		= old_Error_String,
	.MSP430_GetNumberOfUsbIfs	= old_GetNumberOfUsbIfs,
	.MSP430_GetNameOfUsbIf		= old_GetNameOfUsbIf,
	.MSP430_Registers		= old_Registers,
	.MSP430_Run			= old_Run,
	.MSP430_State			= old_State,
	.MSP430_EEM_Init		= old_EEM_Init,
	.MSP430_EEM_SetBreakpoint	= old_EEM_SetBreakpoint,
	.MSP430_FET_FwUpdate		= old_FET_FwUpdate,
};

static int init_old_api(void)
{
	if (!(old.MSP430_Initialize = get_func("MSP430_Initialize")))
		return -1;
	if (!(old.MSP430_VCC = get_func("MSP430_VCC")))
		return -1;
	if (!(old.MSP430_Configure = get_func("MSP430_Configure")))
		return -1;
	if (!(old.MSP430_OpenDevice = get_func("MSP430_OpenDevice")))
		return -1;
	if (!(old.MSP430_GetFoundDevice = get_func("MSP430_GetFoundDevice")))
		return -1;
	if (!(old.MSP430_Close = get_func("MSP430_Close")))
		return -1;
	if (!(old.MSP430_Memory = get_func("MSP430_Memory")))
		return -1;
	if (!(old.MSP430_Reset = get_func("MSP430_Reset")))
		return -1;
	if (!(old.MSP430_Erase = get_func("MSP430_Erase")))
		return -1;
	if (!(old.MSP430_Secure = get_func("MSP430_Secure")))
		return -1;
	if (!(old.MSP430_Error_Number = get_func("MSP430_Error_Number")))
		return -1;
	if (!(old.MSP430_Error_String = get_func("MSP430_Error_String")))
		return -1;
	if (!(old.MSP430_GetNumberOfUsbIfs =
		    get_func("MSP430_GetNumberOfUsbIfs")))
		return -1;
	if (!(old.MSP430_GetNameOfUsbIf = get_func("MSP430_GetNameOfUsbIf")))
		return -1;
	if (!(old.MSP430_Registers = get_func("MSP430_Registers")))
		return -1;
	if (!(old.MSP430_Run = get_func("MSP430_Run")))
		return -1;
	if (!(old.MSP430_State = get_func("MSP430_State")))
		return -1;
	if (!(old.MSP430_EEM_Init = get_func("MSP430_EEM_Init")))
		return -1;
	if (!(old.MSP430_EEM_SetBreakpoint =
			get_func("MSP430_EEM_SetBreakpoint")))
		return -1;
	if (!(old.MSP430_FET_FwUpdate = get_func("MSP430_FET_FwUpdate")))
		return -1;

	tilib_api = &old_tab;
	return 0;
}

/************************************************************************
 * Top-level init
 */

int tilib_api_init(void)
{
	int ret;

	lib_handle = dynload_open(tilib_filename);
	if (!lib_handle) {
		printc_err("tilib_api: can't find %s: %s\n",
			   tilib_filename, dynload_error());
		return -1;
	}

	if (dynload_sym(lib_handle, "MSP430_HIL_MEMAP")) {
		printc_dbg("Using new (SLAC460L+) API\n");
		ret = init_new_api();
	} else {
		printc_dbg("Using old API\n");
		ret = init_old_api();
	}

	if (ret < 0) {
		dynload_close(lib_handle);
		return -1;
	}

	return 0;
}

void tilib_api_exit(void)
{
	dynload_close(lib_handle);
}