forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswdptap.c
190 lines (167 loc) · 4.11 KB
/
swdptap.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
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
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/* This file implements the SW-DP interface. */
#include "general.h"
#include "swdptap.h"
enum {
SWDIO_STATUS_FLOAT = 0,
SWDIO_STATUS_DRIVE
};
int swdptap_init(void)
{
return 0;
}
static void swdptap_turnaround(int dir)
{
static int olddir = SWDIO_STATUS_FLOAT;
/* Don't turnaround if direction not changing */
if(dir == olddir) return;
olddir = dir;
#ifdef DEBUG_SWD_BITS
DEBUG("%s", dir ? "\n-> ":"\n<- ");
#endif
if(dir == SWDIO_STATUS_FLOAT)
SWDIO_MODE_FLOAT();
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
if(dir == SWDIO_STATUS_DRIVE)
SWDIO_MODE_DRIVE();
}
bool swdptap_bit_in(void)
{
uint16_t ret;
swdptap_turnaround(SWDIO_STATUS_FLOAT);
ret = gpio_get(SWDIO_PORT, SWDIO_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
#ifdef DEBUG_SWD_BITS
DEBUG("%d", ret?1:0);
#endif
return ret != 0;
}
uint32_t
swdptap_seq_in(int ticks)
{
uint32_t index = 1;
uint32_t ret = 0;
int len = ticks;
swdptap_turnaround(SWDIO_STATUS_FLOAT);
while (len--) {
int res;
res = gpio_get(SWDIO_PORT, SWDIO_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
if (res)
ret |= index;
index <<= 1;
gpio_clear(SWCLK_PORT, SWCLK_PIN);
}
#ifdef DEBUG_SWD_BITS
for (int i = 0; i < len; i++)
DEBUG("%d", (ret & (1 << i)) ? 1 : 0);
#endif
return ret;
}
bool
swdptap_seq_in_parity(uint32_t *ret, int ticks)
{
uint32_t index = 1;
uint8_t parity = 0;
uint32_t res = 0;
bool bit;
int len = ticks;
swdptap_turnaround(SWDIO_STATUS_FLOAT);
while (len--) {
bit = gpio_get(SWDIO_PORT, SWDIO_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
if (bit) {
res |= index;
parity ^= 1;
}
index <<= 1;
gpio_clear(SWCLK_PORT, SWCLK_PIN);
}
bit = gpio_get(SWDIO_PORT, SWDIO_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
if (bit)
parity ^= 1;
gpio_clear(SWCLK_PORT, SWCLK_PIN);
#ifdef DEBUG_SWD_BITS
for (int i = 0; i < len; i++)
DEBUG("%d", (res & (1 << i)) ? 1 : 0);
#endif
*ret = res;
return parity;
}
void swdptap_bit_out(bool val)
{
#ifdef DEBUG_SWD_BITS
DEBUG("%d", val);
#endif
swdptap_turnaround(SWDIO_STATUS_DRIVE);
gpio_set_val(SWDIO_PORT, SWDIO_PIN, val);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
}
void
swdptap_seq_out(uint32_t MS, int ticks)
{
int data = MS & 1;
#ifdef DEBUG_SWD_BITS
for (int i = 0; i < ticks; i++)
DEBUG("%d", (MS & (1 << i)) ? 1 : 0);
#endif
swdptap_turnaround(SWDIO_STATUS_DRIVE);
while (ticks--) {
gpio_set_val(SWDIO_PORT, SWDIO_PIN, data);
MS >>= 1;
data = MS & 1;
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
}
}
void
swdptap_seq_out_parity(uint32_t MS, int ticks)
{
uint8_t parity = 0;
int data = MS & 1;
#ifdef DEBUG_SWD_BITS
for (int i = 0; i < ticks; i++)
DEBUG("%d", (MS & (1 << i)) ? 1 : 0);
#endif
swdptap_turnaround(SWDIO_STATUS_DRIVE);
while (ticks--) {
gpio_set_val(SWDIO_PORT, SWDIO_PIN, data);
parity ^= MS;
MS >>= 1;
gpio_set(SWCLK_PORT, SWCLK_PIN);
data = MS & 1;
gpio_clear(SWCLK_PORT, SWCLK_PIN);
}
gpio_set_val(SWDIO_PORT, SWDIO_PIN, parity & 1);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
}