forked from fantomgs/xvcd-jlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JlinkUtil.c
161 lines (147 loc) · 3.69 KB
/
JlinkUtil.c
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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include "JLinkARM.h"
#include "JlinkUtil.h"
static void ErrorOut(const char *str)
{
printf("\n***** Error: %s\n",str);
}
static void WarnOut(const char *str)
{
printf("\nWARNING: %s\n",str);
}
static void InfoOut(const char *str)
{
printf("Info: %s\n",str);
}
static void LogOut(const char *str)
{
puts(str);
}
static void ShowDllInfo()
{
int dllver = JLINKARM_GetDLLVersion();
printf("DLL version: %d.%d.%d\n",dllver/10000,(dllver%10000)/100,dllver%100);
}
static void ShowHwInfo()
{
char fwstr[0x100];
JLINKARM_GetFirmwareString(fwstr,0x100);
if (fwstr[0]==0)
{
puts("Can't get firmware info !");
return;
}
printf("Firmware: %s\n",fwstr);
unsigned hwver = JLINKARM_GetHardwareVersion();
if (hwver==0)
return;
printf("HW version: %d.%d.%d\n",hwver/10000,(hwver%10000)/100,hwver%100);
}
static void CheckSpeed()
{
short speed = JLINKARM_GetSpeed();
if (speed==-1)
puts("Using adaptive clocking");
else
printf("JTAG speed: %d kHz\n",speed);
}
void printTDO(const unsigned char *buf, int bitLen) {
printf(" TDO: ");
for (int i = 0; i < bitLen; i += 8 )
{
printf("%.2X", buf[(bitLen - i - 1) / 8]);
}
printf("\n");
}
static bool FindCore(unsigned int coreId)
{
// wjraw 5,1f,00
// wjraw 4,2,0
// wjraw 32,00000000,00000000
unsigned char buf[64];
int r = JLINKARM_JTAG_StoreGetRaw("\x00", 0, "\x1f", 5);
//printf("r = %d\n", r);
JLINKARM_JTAG_StoreGetRaw("\x00", 0, "\x02", 4);
JLINKARM_JTAG_StoreGetRaw("\x00\x00\x00\x00", buf, "\x00\x00\x00\x00", 32);
//printf("result %x\n", x);
//printf("IDCODE %x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
//printTDO(buf, 32);
int idcode = *(unsigned int*)buf;
//printf("IDCODE %x\n", idcode);
if(idcode == coreId) {
printf("Found core id 0x%.8X\n", idcode);
return true;
} else {
printf("Found unknown core id %08X\n", idcode);
return coreId ? false : true;
}
return false;
}
int JlinkConnect(unsigned short speed, unsigned int coreId)
{
JLINKARM_EnableLog(LogOut);
//JLINKARM_SetLogFile("c:\\Temp\\jlink\\rd.log");
JLINKARM_SetWarnOutHandler(WarnOut);
if (JLINKARM_OpenEx(LogOut,ErrorOut))
{
puts("Open failed !");
return 0;
}
puts("JLink opened successfully");
JLINKARM_SetErrorOutHandler(ErrorOut);
JLINKARM_EnableLog(InfoOut);
JLINKARM_SetInitRegsOnReset(OFF);
ShowDllInfo();
ShowHwInfo();
if (JLINKARM_HasError())
return 0;
printf("S/N: %d\n",JLINKARM_GetSN());
char oemstr[0x100];
JLINKARM_GetOEMString(oemstr);
if (strlen(oemstr)>0)
printf("OEM: %s\n",oemstr);
JLINKARM_GetFeatureString(oemstr);
if (strlen(oemstr)>0)
printf("Features: %s\n",oemstr);
printf("Capabilities: %X\n",JLINKARM_GetEmuCaps());
HWSTATUS hwsts;
if (JLINKARM_GetHWStatus(&hwsts)==JL_OK)
{
printf("VTarget = %d.%dV\n",hwsts.vtarget/1000,hwsts.vtarget%1000);
if (hwsts.tck==0)
puts("Error: TCK low !");
if (hwsts.trst==0)
puts("Error: TRST low !");
}
//JLINKARM_SetSpeed(TCKFREQ);
JLINKARM_SetSpeed(speed);
CheckSpeed();
if (hwsts.vtarget<500)
{
puts("VTarget too low !");
return 0;
}
if (!FindCore(coreId))
{
puts("Trying to connect with 100 kHz !");
JlinkResetTarget(1);
JLINKARM_SetSpeed(100);
if (!FindCore(coreId))
return 0;
}
return 1;
}
void JlinkClose()
{
JLINKARM_Close();
puts("JLink closed");
}
void JlinkResetTarget(unsigned delay)
{
JLINKARM_SetResetDelay(delay);
JLINKARM_ResetPullsRESET(ON);
JLINKARM_Reset();
}