-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathswd-ftdi.rb
258 lines (219 loc) · 5.69 KB
/
swd-ftdi.rb
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
require 'ftdi'
require 'log'
require 'swd-bitbang'
# Documentation:
# <http://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf>
module Ftdi
# This data comes from ftdi.h
MPSSE_WRITE_NEG = 0x01 # Write TDI/DO on negative TCK/SK edge
MPSSE_BITMODE = 0x02 # Write bits, not bytes
MPSSE_READ_NEG = 0x04 # Sample TDO/DI on negative TCK/SK edge
MPSSE_LSB = 0x08 # LSB first
MPSSE_DO_WRITE = 0x10 # Write TDI/DO
MPSSE_DO_READ = 0x20 # Read TDO/DI
MPSSE_WRITE_TMS = 0x40 # Write TMS/CS
# FTDI MPSSE commands
SET_BITS_LOW = 0x80
#BYTE DATA
#BYTE Direction
SET_BITS_HIGH = 0x82
#BYTE DATA
#BYTE Direction
GET_BITS_LOW = 0x81
GET_BITS_HIGH = 0x83
LOOPBACK_START = 0x84
LOOPBACK_END = 0x85
TCK_DIVISOR = 0x86
# H Type specific commands
DIS_DIV_5 = 0x8a
EN_DIV_5 = 0x8b
EN_3_PHASE = 0x8c
DIS_3_PHASE = 0x8d
CLK_BITS = 0x8e
CLK_BYTES = 0x8f
CLK_WAIT_HIGH = 0x94
CLK_WAIT_LOW = 0x95
EN_ADAPTIVE = 0x96
DIS_ADAPTIVE = 0x97
CLK_BYTES_OR_HIGH = 0x9c
CLK_BYTES_OR_LOW = 0x0d
#FT232H specific commands
DRIVE_OPEN_COLLECTOR = 0x9e
# Value Low
# Value HIGH #rate is 12000000/((1+value)*2)
def self.div_value(rate)
if rate > 6000000
0
else
if 6000000/rate - 1 > 0xffff
0xffff
else
6000000/rate - 1
end
end
end
# Commands in MPSSE and Host Emulation Mode
SEND_IMMEDIATE = 0x87
#define WAIT_ON_HIGH 0x88
WAIT_ON_LOW = 0x89
# Commands in Host Emulation Mode
READ_SHORT = 0x90
# Address_Low
READ_EXTENDED = 0x91
# Address High
# Address Low
WRITE_SHORT = 0x92
# Address_Low
WRITE_EXTENDED = 0x93
# Address High
# Address Low
class Context
def set_bitmode(mask, mode)
check_result(Ftdi.ftdi_set_bitmode(ctx, mask, mode))
end
def latency_timer=(new_latency)
check_result(Ftdi.ftdi_set_latency_timer(ctx, new_latency))
new_latency
end
def purge_buffers!
check_result(Ftdi.ftdi_usb_purge_buffers(ctx))
nil
end
end
attach_function :ftdi_set_bitmode, [ :pointer, :int, :int ], :int
attach_function :ftdi_set_latency_timer, [ :pointer, :int ], :int
attach_function :ftdi_usb_purge_buffers, [ :pointer ], :int
end
class FtdiSwd < BitbangSwd
XFER_MODE = Ftdi::MPSSE_LSB | Ftdi::MPSSE_WRITE_NEG
ACK_OK = 1
ACK_WAIT = 2
ACK_FAULT = 4
def initialize(opt = {})
super
@outbuf = String.new
@inbuf = String.new
self.speed = opt[:speed] || 10000000
@bits = 0xbc02
@dirs = 0xff2b
@swdoe = opt[:swdoe_data] || 0x1000
@dev = Ftdi::Context.new
@dev.interface = opt[:iface] || :interface_any
@dev.usb_open(opt[:vid] || 0, opt[:pid] || 0)
@dev.usb_reset
@dev.set_bitmode(0, 0) # reset
@dev.set_bitmode(0, 2) # mpsse
set_line_mode(:out)
@cur_dir = :out
end
def flush!
return if @outbuf.empty?
Log(:phys, 2){ ['flush', hexify(@outbuf)] }
@dev.write_data(@outbuf)
@outbuf = String.new
end
def set_line_mode(dir)
case dir
when :in
bits = @bits | @swdoe
when :out
bits = @bits & ~@swdoe
end
@outbuf << Ftdi::SET_BITS_LOW
@outbuf << (bits & 0xff)
@outbuf << (@dirs & 0xff)
@outbuf << Ftdi::SET_BITS_HIGH
@outbuf << (bits >> 8)
@outbuf << (@dirs >> 8)
end
def turn(dir)
return if @cur_dir == dir
if dir == :in
set_line_mode(dir)
@cur_dir = dir
end
write_bits(0, 1)
if dir == :out
set_line_mode(dir)
@cur_dir = dir
end
end
def write_bytes(bytes)
return if bytes.empty?
turn(:out)
len = bytes.length - 1
@outbuf << (XFER_MODE | Ftdi::MPSSE_DO_WRITE)
@outbuf << len % 256
@outbuf << len / 256
@outbuf << bytes
end
def write_bits(byte, len)
return if len == 0
@outbuf << (XFER_MODE | Ftdi::MPSSE_DO_WRITE | Ftdi::MPSSE_BITMODE)
@outbuf << len - 1
@outbuf << byte
end
def write_cmd(cmd)
write_bytes(cmd)
turn(:in)
ack = read_bits(3)
end
def write_word_and_parity(word, parity)
write_bytes([word].pack('V'))
write_bits(parity, 1)
end
def read_word_and_parity
raise RuntimeError, "wrong line direction" if @cur_dir != :in
len = 4
@outbuf << (XFER_MODE | Ftdi::MPSSE_DO_READ)
@outbuf << (len - 1) % 256
@outbuf << (len - 1) / 256
@outbuf << (XFER_MODE | Ftdi::MPSSE_DO_READ | Ftdi::MPSSE_BITMODE)
@outbuf << 0 # 1 bit
flush!
data = expect_bytes(len + 1)
ret, par = data.unpack('VC')
par >>= 7
Log(:phys, 2){ 'read_bytes: %08x, parity %d' % [ret, par] }
[ret, par]
end
def read_bits(len)
return if len == 0
@outbuf << (XFER_MODE | Ftdi::MPSSE_DO_READ | Ftdi::MPSSE_BITMODE)
@outbuf << len - 1
flush!
data = expect_bytes(1)
ret = data.unpack('C').first >> (8 - len)
Log(:phys, 2){ 'read_bits: %0*b' % [len, ret] }
ret
end
def speed=(hz)
opspeed = 12000000
div = opspeed / hz - 2
div = 0 if div < 0
div = 65535 if div > 65535
@speed = opspeed / (div + 2)
@outbuf << Ftdi::EN_DIV_5
@outbuf << Ftdi::TCK_DIVISOR
@outbuf << (div & 0xff)
@outbuf << (div >> 8)
end
def expect_bytes(num)
while @inbuf.length < num
@inbuf << @dev.read_data
end
Log(:phys, 3){ ['inbuf:', hexify(@inbuf)] }
ret = @inbuf.byteslice(0, num)
@inbuf = @inbuf.byteslice(num..-1)
Log(:phys, 3){ ['read:', hexify(ret)] }
ret
end
end
if $0 == __FILE__
s = FtdiSwd.new(:vid => Integer(ARGV[0]), :pid => Integer(ARGV[1]))
s.raw_out(255.chr * 7)
s.raw_out([0xe79e].pack('v'))
s.raw_out(255.chr * 7)
s.raw_out(0.chr)
puts "IDCODE: %08x" % s.transact(0xa5)
end