1 |
158 |
olivier.gi |
#----------------------------------------------------------------------------------
|
2 |
|
|
# Copyright (C) 2001 Authors
|
3 |
|
|
#
|
4 |
|
|
# This source file may be used and distributed without restriction provided
|
5 |
|
|
# that this copyright statement is not removed from the file and that any
|
6 |
|
|
# derivative work contains the original copyright notice and the associated
|
7 |
|
|
# disclaimer.
|
8 |
|
|
#
|
9 |
|
|
# This source file is free software; you can redistribute it and/or modify
|
10 |
|
|
# it under the terms of the GNU Lesser General Public License as published
|
11 |
|
|
# by the Free Software Foundation; either version 2.1 of the License, or
|
12 |
|
|
# (at your option) any later version.
|
13 |
|
|
#
|
14 |
|
|
# This source is distributed in the hope that it will be useful, but WITHOUT
|
15 |
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
16 |
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
17 |
|
|
# License for more details.
|
18 |
|
|
#
|
19 |
|
|
# You should have received a copy of the GNU Lesser General Public License
|
20 |
|
|
# along with this source; if not, write to the Free Software Foundation,
|
21 |
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
22 |
|
|
#
|
23 |
|
|
#----------------------------------------------------------------------------------
|
24 |
|
|
#
|
25 |
|
|
# File Name: dbg_uart_i2c-iss.tcl
|
26 |
|
|
#
|
27 |
|
|
# Author(s):
|
28 |
|
|
# - Olivier Girard, olgirard@gmail.com
|
29 |
|
|
#
|
30 |
|
|
#----------------------------------------------------------------------------------
|
31 |
|
|
# $Rev: 133 $
|
32 |
|
|
# $LastChangedBy: olivier.girard $
|
33 |
|
|
# $LastChangedDate: 2012-03-22 21:28:26 +0100 (Thu, 22 Mar 2012) $
|
34 |
|
|
#----------------------------------------------------------------------------------
|
35 |
|
|
#
|
36 |
|
|
# Description:
|
37 |
|
|
#
|
38 |
|
|
# Some I2C utility functions for the openMSP430 serial debug interface
|
39 |
|
|
# using the USB-ISS adapter.
|
40 |
|
|
#
|
41 |
|
|
# Mandatory Public functions:
|
42 |
|
|
#
|
43 |
|
|
# - i2c_usb-iss::dbg_open (Device, OperatingMode)
|
44 |
|
|
# - i2c_usb-iss::dbg_connect (CpuAddr)
|
45 |
|
|
# - i2c_usb-iss::dbg_rd (CpuAddr, RegisterName)
|
46 |
|
|
# - i2c_usb-iss::dbg_wr (CpuAddr, RegisterName, Data)
|
47 |
|
|
# - i2c_usb-iss::dbg_burst_rx (CpuAddr, Format, Length)
|
48 |
|
|
# - i2c_usb-iss::dbg_burst_tx (CpuAddr, Format, DataList)
|
49 |
|
|
# - i2c_usb-iss::get_allowed_speeds ()
|
50 |
|
|
#
|
51 |
|
|
#
|
52 |
|
|
# Private functions:
|
53 |
|
|
#
|
54 |
|
|
# - i2c_usb-iss::i2c_mode (OperatingMode)
|
55 |
|
|
# - i2c_usb-iss::dbg_format_cmd (RegisterName, Action)
|
56 |
|
|
# - i2c_usb-iss::ISS_VERSION ()
|
57 |
|
|
# - i2c_usb-iss::GET_SER_NUM ()
|
58 |
|
|
# - i2c_usb-iss::ISS_MODE (OperatingMode)
|
59 |
|
|
# - i2c_usb-iss::SETPINS (IO1, IO2, IO3, IO4)
|
60 |
|
|
# - i2c_usb-iss::GETPINS ()
|
61 |
|
|
#
|
62 |
|
|
#----------------------------------------------------------------------------------
|
63 |
|
|
namespace eval i2c_usb-iss {
|
64 |
|
|
|
65 |
|
|
#=============================================================================#
|
66 |
|
|
# Source required libraries #
|
67 |
|
|
#=============================================================================#
|
68 |
|
|
|
69 |
|
|
set scriptDir [file dirname [info script]]
|
70 |
|
|
source $scriptDir/dbg_utils.tcl
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
#=============================================================================#
|
74 |
|
|
# i2c_usb-iss::dbg_open (Device, Baudrate) #
|
75 |
|
|
#-----------------------------------------------------------------------------#
|
76 |
|
|
# Description: Open and configure the UART connection. #
|
77 |
|
|
# Attach and configure the USB-ISS adapter. #
|
78 |
|
|
# Arguments : Device - Serial port device (i.e. /dev/ttyS0 or COM2:) #
|
79 |
|
|
# OperatingMode - Name of the I2C operating mode. #
|
80 |
|
|
# Result : 0 if error, 1 otherwise. #
|
81 |
|
|
#=============================================================================#
|
82 |
|
|
proc dbg_open {Device OperatingMode} {
|
83 |
|
|
|
84 |
|
|
# Open UART interface
|
85 |
|
|
if {![utils::uart_open $Device 0 115200]} {
|
86 |
|
|
return 0
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
# Check the ISS-VERSION command on the adapter
|
90 |
|
|
if {[lindex [ISS_VERSION] 0]=="0"} {
|
91 |
|
|
return 0
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
# Get operating mode value
|
95 |
|
|
set op_mode [i2c_mode $OperatingMode]
|
96 |
|
|
|
97 |
|
|
# Configure the USB-ISS adaptor for I2C communication
|
98 |
|
|
# IO_MODE+I2C IO_TYPE
|
99 |
|
|
if {![ISS_MODE [list $op_mode 0x00]]} {
|
100 |
|
|
return 0
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
# Clear IO pins to make sure the Serial debug interface is under reset
|
104 |
|
|
SETPINS 0 0 0 0
|
105 |
|
|
after 100
|
106 |
|
|
|
107 |
|
|
return 1
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
#=============================================================================#
|
112 |
|
|
# i2c_usb-iss::dbg_connect (CpuAddr) #
|
113 |
|
|
#-----------------------------------------------------------------------------#
|
114 |
|
|
# Description: In case the serial debug interface enable signal is connected #
|
115 |
|
|
# to the I/O 1 or I/O 2 port, this function will enable it. #
|
116 |
|
|
# Arguments : CpuAddr - Unused argument. #
|
117 |
|
|
# Result : 0 if error, 1 otherwise. #
|
118 |
|
|
#=============================================================================#
|
119 |
|
|
proc dbg_connect {CpuAddr} {
|
120 |
|
|
|
121 |
|
|
# Make sure the Serial debug interface is still under reset
|
122 |
|
|
SETPINS 0 0 0 0
|
123 |
|
|
after 100
|
124 |
|
|
|
125 |
|
|
# Enable the Serial debug interface
|
126 |
|
|
SETPINS 1 1 1 1
|
127 |
|
|
after 100
|
128 |
|
|
|
129 |
|
|
return 1
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
#=============================================================================#
|
134 |
|
|
# i2c_usb-iss::dbg_rd (CpuAddr, RegisterName) #
|
135 |
|
|
#-----------------------------------------------------------------------------#
|
136 |
|
|
# Description: Read the specified debug register. #
|
137 |
|
|
# Arguments : CpuAddr - I2C address of the target CPU. #
|
138 |
|
|
# RegisterName - Name of the register to be read. #
|
139 |
|
|
# Result : Register content, in hexadecimal. #
|
140 |
|
|
#=============================================================================#
|
141 |
|
|
proc dbg_rd {CpuAddr RegisterName} {
|
142 |
|
|
|
143 |
|
|
# Send command frame
|
144 |
|
|
set cmd [dbg_format_cmd $RegisterName RD]
|
145 |
|
|
|
146 |
|
|
# Word or Byte register
|
147 |
|
|
set isByte [string eq [expr 0x40 & $cmd] 64]
|
148 |
|
|
|
149 |
|
|
# Compute device frame (address+WR/RD)
|
150 |
|
|
set DeviceFrameWR [format "0x%02x" [expr $CpuAddr*2+0]]
|
151 |
|
|
set DeviceFrameRD [format "0x%02x" [expr $CpuAddr*2+1]]
|
152 |
|
|
|
153 |
|
|
# Send command frame: I2C_DIRECT I2C_START I2C_WRITE2 DEVICE_ADDRESS+WR DATA STOP
|
154 |
|
|
utils::uart_tx [concat 0x57 0x01 0x31 $DeviceFrameWR $cmd 0x03]
|
155 |
|
|
|
156 |
|
|
set response [utils::uart_rx 1 2]
|
157 |
|
|
if {[lindex $response 0] == 0x00} {
|
158 |
|
|
puts "I2C ERROR: $response"
|
159 |
|
|
return "0x"
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
if {$isByte} {
|
163 |
|
|
# Read data: I2C_DIRECT I2C_START I2C_WRITE1 DEVICE_ADDRESS+RD NACK READ1 STOP
|
164 |
|
|
utils::uart_tx [concat 0x57 0x01 0x30 $DeviceFrameRD 0x04 0x20 0x03]
|
165 |
|
|
} else {
|
166 |
|
|
# Read data: I2C_DIRECT I2C_START I2C_WRITE1 DEVICE_ADDRESS+RD READ1 NACK READ1 STOP
|
167 |
|
|
utils::uart_tx [concat 0x57 0x01 0x30 $DeviceFrameRD 0x20 0x04 0x20 0x03]
|
168 |
|
|
}
|
169 |
|
|
set response [utils::uart_rx 1 2]
|
170 |
|
|
if {[lindex $response 0] == 0x00} {
|
171 |
|
|
puts "I2C ERROR: $response"
|
172 |
|
|
return "0x"
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
# Compute size of data to be received
|
176 |
|
|
if {$isByte} {
|
177 |
|
|
set format 1
|
178 |
|
|
set length 1
|
179 |
|
|
} else {
|
180 |
|
|
set format 0
|
181 |
|
|
set length 2
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
# Receive data
|
185 |
|
|
set rx_data [utils::uart_rx $format [expr $length]]
|
186 |
|
|
|
187 |
|
|
return $rx_data
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
#=============================================================================#
|
191 |
|
|
# i2c_usb-iss::dbg_wr (CpuAddr, RegisterName, Data) #
|
192 |
|
|
#-----------------------------------------------------------------------------#
|
193 |
|
|
# Description: Write to the specified debug register. #
|
194 |
|
|
# Arguments : CpuAddr - I2C address of the target CPU. #
|
195 |
|
|
# RegisterName - Name of the register to be written. #
|
196 |
|
|
# Data - Data to be written. #
|
197 |
|
|
# Result : 0 if error, 1 otherwise. #
|
198 |
|
|
#=============================================================================#
|
199 |
|
|
proc dbg_wr {CpuAddr RegisterName Data} {
|
200 |
|
|
|
201 |
|
|
# Send command frame
|
202 |
|
|
set cmd [dbg_format_cmd $RegisterName WR]
|
203 |
|
|
|
204 |
|
|
# Word or Byte register
|
205 |
|
|
set isByte [string eq [expr 0x40 & $cmd] 64]
|
206 |
|
|
|
207 |
|
|
# Compute device frame (address+WR/RD)
|
208 |
|
|
set DeviceFrameWR [format "0x%02x" [expr $CpuAddr*2+0]]
|
209 |
|
|
set DeviceFrameRD [format "0x%02x" [expr $CpuAddr*2+1]]
|
210 |
|
|
|
211 |
|
|
# Send command frame: I2C_DIRECT I2C_START I2C_WRITE2 DEVICE_ADDRESS+WR DATA STOP
|
212 |
|
|
utils::uart_tx [concat 0x57 0x01 0x31 $DeviceFrameWR $cmd 0x03]
|
213 |
|
|
|
214 |
|
|
set response [utils::uart_rx 1 2]
|
215 |
|
|
if {[lindex $response 0] == 0x00} {
|
216 |
|
|
puts "I2C ERROR: $response"
|
217 |
|
|
return 0
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
# Format input data
|
221 |
|
|
if {![regexp {0x} $Data match]} {
|
222 |
|
|
set Data [format "0x%x" $Data]
|
223 |
|
|
}
|
224 |
|
|
set hex_val [format %04x $Data]
|
225 |
|
|
regexp {(..)(..)} $hex_val match hex_msb hex_lsb
|
226 |
|
|
|
227 |
|
|
if {$isByte} {
|
228 |
|
|
# Read data: I2C_DIRECT I2C_START I2C_WRITE2 DEVICE_ADDRESS+WR DATA STOP
|
229 |
|
|
utils::uart_tx [concat 0x57 0x01 0x31 $DeviceFrameWR "0x$hex_lsb" 0x03]
|
230 |
|
|
} else {
|
231 |
|
|
# Read data: I2C_DIRECT I2C_START I2C_WRITE1 DEVICE_ADDRESS+WR DATA_LSB DATA_MSB STOP
|
232 |
|
|
utils::uart_tx [concat 0x57 0x01 0x32 $DeviceFrameWR "0x$hex_lsb" "0x$hex_msb" 0x03]
|
233 |
|
|
}
|
234 |
|
|
set response [utils::uart_rx 1 2]
|
235 |
|
|
if {[lindex $response 0] == 0x00} {
|
236 |
|
|
puts "I2C ERROR: $response"
|
237 |
|
|
return 0
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
return 1
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
#=============================================================================#
|
244 |
|
|
# i2c_usb-iss::dbg_burst_rx (CpuAddr, Format, Length) #
|
245 |
|
|
#-----------------------------------------------------------------------------#
|
246 |
|
|
# Description: Receive data list as burst from the serial debug interface. #
|
247 |
|
|
# Arguments : CpuAddr - I2C address of the target CPU. #
|
248 |
|
|
# Format - 0 format as 16 bit word, 1 format as 8 bit word. #
|
249 |
|
|
# Length - Number of byte to be received. #
|
250 |
|
|
# Result : 0 if error, 1 otherwise. #
|
251 |
|
|
#=============================================================================#
|
252 |
|
|
proc dbg_burst_rx {CpuAddr Format Length} {
|
253 |
|
|
|
254 |
|
|
# Compute device frame (address+WR/RD)
|
255 |
|
|
set DeviceFrameWR [format "0x%02x" [expr $CpuAddr*2+0]]
|
256 |
|
|
set DeviceFrameRD [format "0x%02x" [expr $CpuAddr*2+1]]
|
257 |
|
|
|
258 |
|
|
set rx_data ""
|
259 |
|
|
while { $Length > 0 } {
|
260 |
|
|
|
261 |
|
|
# Maximum frame length is 16 bytes
|
262 |
|
|
if {$Length >= 16} {set rxLength 16
|
263 |
|
|
} else {set rxLength $Length}
|
264 |
|
|
|
265 |
|
|
# Compute I2C read command
|
266 |
|
|
set readCmd [format "0x%x" [expr $rxLength + 0x1f - 1]]
|
267 |
|
|
|
268 |
|
|
if {$readCmd == "0x1f"} {
|
269 |
|
|
# Read data: I2C_DIRECT I2C_START I2C_WRITE1 DEVICE_ADDRESS+RD NACK READ1 STOP
|
270 |
|
|
utils::uart_tx [concat 0x57 0x01 0x30 $DeviceFrameRD 0x04 0x20 0x03]
|
271 |
|
|
} else {
|
272 |
|
|
# Read data: I2C_DIRECT I2C_START I2C_WRITE1 DEVICE_ADDRESS+RD READxx NACK READ1 STOP
|
273 |
|
|
utils::uart_tx [concat 0x57 0x01 0x30 $DeviceFrameRD $readCmd 0x04 0x20 0x03]
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
set response [utils::uart_rx 1 2]
|
277 |
|
|
if {[lindex $response 0] == 0x00} {
|
278 |
|
|
puts "I2C ERROR: $response"
|
279 |
|
|
return 0
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
# Receive data
|
283 |
|
|
set rx_data [concat $rx_data [utils::uart_rx $Format [expr $rxLength]]]
|
284 |
|
|
|
285 |
|
|
# Remaining bytes to be received
|
286 |
|
|
set Length [expr $Length - $rxLength]
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
return $rx_data
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
#=============================================================================#
|
294 |
|
|
# i2c_usb-iss::dbg_burst_tx (CpuAddr, Format, DataList) #
|
295 |
|
|
#-----------------------------------------------------------------------------#
|
296 |
|
|
# Description: Transmit data list as burst to the serial debug interface. #
|
297 |
|
|
# Arguments : CpuAddr - I2C address of the target CPU. #
|
298 |
|
|
# Format - 0 format as 16 bit word, 1 format as 8 bit word. #
|
299 |
|
|
# DataList - List of data to be written (in hexadecimal). #
|
300 |
|
|
# Result : 0 if error, 1 otherwise. #
|
301 |
|
|
#=============================================================================#
|
302 |
|
|
proc dbg_burst_tx {CpuAddr Format DataList} {
|
303 |
|
|
|
304 |
|
|
# Compute device frame (address+WR/RD)
|
305 |
|
|
set DeviceFrameWR [format "0x%02x" [expr $CpuAddr*2+0]]
|
306 |
|
|
set DeviceFrameRD [format "0x%02x" [expr $CpuAddr*2+1]]
|
307 |
|
|
|
308 |
|
|
#----------------------------------------
|
309 |
|
|
# Format the list of bytes to be sent
|
310 |
|
|
#----------------------------------------
|
311 |
|
|
set tx_data ""
|
312 |
|
|
foreach data [split $DataList] {
|
313 |
|
|
|
314 |
|
|
if {$Format==1} { #### 8-bit data format ####
|
315 |
|
|
# Format data
|
316 |
|
|
set data [format %02x $data]
|
317 |
|
|
|
318 |
|
|
# Add data to list
|
319 |
|
|
set tx_data [concat $tx_data "0x$data"]
|
320 |
|
|
|
321 |
|
|
} else { #### 16-bit data format ####
|
322 |
|
|
# Format data
|
323 |
|
|
set data [format %04x $data]
|
324 |
|
|
regexp {(..)(..)} $data match data_msb data_lsb
|
325 |
|
|
|
326 |
|
|
# Add data to list
|
327 |
|
|
set tx_data [concat $tx_data "0x$data_lsb 0x$data_msb"]
|
328 |
|
|
}
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
#----------------------------------------
|
332 |
|
|
# Send the list of bytes over I2C
|
333 |
|
|
#----------------------------------------
|
334 |
|
|
set Length [llength $tx_data]
|
335 |
|
|
|
336 |
|
|
while { $Length > 0 } {
|
337 |
|
|
|
338 |
|
|
# Maximum frame length is 16 bytes
|
339 |
|
|
if {$Length >= 16} {set txLength 16
|
340 |
|
|
} else {set txLength $Length}
|
341 |
|
|
|
342 |
|
|
# Compute I2C write command
|
343 |
|
|
set writeCmd [format "0x%x" [expr $txLength + 0x2f]]
|
344 |
|
|
|
345 |
|
|
# Get the data from the list and remove it from there
|
346 |
|
|
set hex_data [lrange $tx_data 0 [expr $txLength-1]]
|
347 |
|
|
set tx_data [lreplace $tx_data 0 [expr $txLength-1]]
|
348 |
|
|
|
349 |
|
|
# Read data: I2C_DIRECT I2C_START I2C_WRITE1 DEVICE_ADDRESS+WR WRITExx DATA STOP
|
350 |
|
|
utils::uart_tx [concat 0x57 0x01 0x30 $DeviceFrameWR $writeCmd $hex_data 0x03]
|
351 |
|
|
|
352 |
|
|
set response [utils::uart_rx 1 2]
|
353 |
|
|
if {[lindex $response 0] == 0x00} {
|
354 |
|
|
puts "I2C ERROR: $response"
|
355 |
|
|
return 0
|
356 |
|
|
}
|
357 |
|
|
|
358 |
|
|
# Remaining bytes to be received
|
359 |
|
|
set Length [expr $Length - $txLength]
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
#=============================================================================#
|
365 |
|
|
# i2c_usb-iss::get_allowed_speeds () #
|
366 |
|
|
#-----------------------------------------------------------------------------#
|
367 |
|
|
# Description: Return the list of allowed I2C configuration modes. #
|
368 |
|
|
#=============================================================================#
|
369 |
|
|
proc get_allowed_speeds {} {
|
370 |
|
|
|
371 |
|
|
# Not-Editable Default I2C-Operating-Modes
|
372 |
|
|
return [list 0 I2C_S_100KHZ [list I2C_S_20KHZ \
|
373 |
|
|
I2C_S_50KHZ \
|
374 |
|
|
I2C_S_100KHZ \
|
375 |
|
|
I2C_S_400KHZ \
|
376 |
|
|
I2C_H_100KHZ \
|
377 |
|
|
I2C_H_400KHZ \
|
378 |
|
|
I2C_H_1000KHZ] ]
|
379 |
|
|
}
|
380 |
|
|
|
381 |
|
|
###################################################################################################
|
382 |
|
|
###################################################################################################
|
383 |
|
|
###################################################################################################
|
384 |
|
|
###################################################################################################
|
385 |
|
|
####### #######
|
386 |
|
|
####### PRIVATE FUNCTIONS #######
|
387 |
|
|
####### #######
|
388 |
|
|
###################################################################################################
|
389 |
|
|
###################################################################################################
|
390 |
|
|
###################################################################################################
|
391 |
|
|
###################################################################################################
|
392 |
|
|
|
393 |
|
|
#=============================================================================#
|
394 |
|
|
# i2c_usb-iss::i2c_mode (OperatingMode) #
|
395 |
|
|
#-----------------------------------------------------------------------------#
|
396 |
|
|
# Description: Get the correcponding hexadecimal value for a given I2C mode. #
|
397 |
|
|
# Arguments : OperatingMode - Name of the I2C operating mode. #
|
398 |
|
|
# Result : Command to be sent via UART. #
|
399 |
|
|
#=============================================================================#
|
400 |
|
|
proc i2c_mode {OperatingMode} {
|
401 |
|
|
|
402 |
|
|
switch -exact $OperatingMode {
|
403 |
|
|
I2C_S_20KHZ {set op_mode "0x20"}
|
404 |
|
|
I2C_S_50KHZ {set op_mode "0x30"}
|
405 |
|
|
I2C_S_100KHZ {set op_mode "0x40"}
|
406 |
|
|
I2C_S_400KHZ {set op_mode "0x50"}
|
407 |
|
|
I2C_H_100KHZ {set op_mode "0x60"}
|
408 |
|
|
I2C_H_400KHZ {set op_mode "0x70"}
|
409 |
|
|
I2C_H_1000KHZ {set op_mode "0x80"}
|
410 |
|
|
default {set op_mode "0x40"}
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
return $op_mode
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
#=============================================================================#
|
417 |
|
|
# i2c_usb-iss::dbg_format_cmd (RegisterName, Action) #
|
418 |
|
|
#-----------------------------------------------------------------------------#
|
419 |
|
|
# Description: Get the correcponding UART command to a given debug register #
|
420 |
|
|
# access. #
|
421 |
|
|
# Arguments : RegisterName - Name of the register to be accessed. #
|
422 |
|
|
# Action - RD for read / WR for write. #
|
423 |
|
|
# Result : Command to be sent via UART. #
|
424 |
|
|
#=============================================================================#
|
425 |
|
|
proc dbg_format_cmd {RegisterName Action} {
|
426 |
|
|
|
427 |
|
|
switch -exact $Action {
|
428 |
|
|
RD {set rd_wr "0x00"}
|
429 |
|
|
WR {set rd_wr "0x080"}
|
430 |
|
|
default {set rd_wr "0x00"}
|
431 |
|
|
}
|
432 |
|
|
|
433 |
|
|
switch -exact $RegisterName {
|
434 |
|
|
CPU_ID_LO {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x00]]}
|
435 |
|
|
CPU_ID_HI {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x01]]}
|
436 |
|
|
CPU_CTL {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x02]]}
|
437 |
|
|
CPU_STAT {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x03]]}
|
438 |
|
|
MEM_CTL {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x04]]}
|
439 |
|
|
MEM_ADDR {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x05]]}
|
440 |
|
|
MEM_DATA {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x06]]}
|
441 |
|
|
MEM_CNT {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x07]]}
|
442 |
|
|
BRK0_CTL {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x08]]}
|
443 |
|
|
BRK0_STAT {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x09]]}
|
444 |
|
|
BRK0_ADDR0 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x0A]]}
|
445 |
|
|
BRK0_ADDR1 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x0B]]}
|
446 |
|
|
BRK1_CTL {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x0C]]}
|
447 |
|
|
BRK1_STAT {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x0D]]}
|
448 |
|
|
BRK1_ADDR0 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x0E]]}
|
449 |
|
|
BRK1_ADDR1 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x0F]]}
|
450 |
|
|
BRK2_CTL {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x10]]}
|
451 |
|
|
BRK2_STAT {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x11]]}
|
452 |
|
|
BRK2_ADDR0 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x12]]}
|
453 |
|
|
BRK2_ADDR1 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x13]]}
|
454 |
|
|
BRK3_CTL {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x14]]}
|
455 |
|
|
BRK3_STAT {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x40 | 0x15]]}
|
456 |
|
|
BRK3_ADDR0 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x16]]}
|
457 |
|
|
BRK3_ADDR1 {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x17]]}
|
458 |
|
|
CPU_NR {set uart_cmd [format "0x%02x" [expr $rd_wr | 0x00 | 0x18]]}
|
459 |
|
|
default {set uart_cmd "0x00"}
|
460 |
|
|
}
|
461 |
|
|
|
462 |
|
|
return $uart_cmd
|
463 |
|
|
}
|
464 |
|
|
|
465 |
|
|
|
466 |
|
|
#=============================================================================#
|
467 |
|
|
# i2c_usb-iss::ISS_VERSION () #
|
468 |
|
|
#-----------------------------------------------------------------------------#
|
469 |
|
|
# Description: Will return three bytes. #
|
470 |
|
|
# The first is the Module ID, this will always be 7. #
|
471 |
|
|
# The second byte is the firmware revision number. #
|
472 |
|
|
# The third byte is the current operating mode. #
|
473 |
|
|
#=============================================================================#
|
474 |
|
|
proc ISS_VERSION {} {
|
475 |
|
|
|
476 |
|
|
# Send ISS-VERSION command to the adapter
|
477 |
|
|
utils::uart_tx [list 0x5A 0x01]
|
478 |
|
|
|
479 |
|
|
# Make sure 3 bytes are then received (the first one must be 0x07)
|
480 |
|
|
set iss_version [utils::uart_rx 1 3]
|
481 |
|
|
if {([lindex $iss_version 0] != "0x07") |
|
482 |
|
|
([lindex $iss_version 1] == "0x") |
|
483 |
|
|
([lindex $iss_version 2] == "0x")} {
|
484 |
|
|
return [list 0 0 0]
|
485 |
|
|
}
|
486 |
|
|
return $iss_version
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
#=============================================================================#
|
490 |
|
|
# i2c_usb-iss::GET_SER_NUM () #
|
491 |
|
|
#-----------------------------------------------------------------------------#
|
492 |
|
|
# Description: Will return the modules unique 8 byte USB serial number. #
|
493 |
|
|
#=============================================================================#
|
494 |
|
|
proc GET_SER_NUM {} {
|
495 |
|
|
|
496 |
|
|
# Send GET_SER_NUM command to the adapter
|
497 |
|
|
utils::uart_tx [list 0x5A 0x03]
|
498 |
|
|
|
499 |
|
|
# Get the 8 bytes
|
500 |
|
|
set serial_number [utils::uart_rx 1 8]
|
501 |
|
|
|
502 |
|
|
return $serial_number
|
503 |
|
|
}
|
504 |
|
|
|
505 |
|
|
#=============================================================================#
|
506 |
|
|
# i2c_usb-iss::ISS_MODE (OperatingMode) #
|
507 |
|
|
#-----------------------------------------------------------------------------#
|
508 |
|
|
# Description: Sets the operating mode. #
|
509 |
|
|
# This sets up the modules I/O pins and hardware for the #
|
510 |
|
|
# required mode. #
|
511 |
|
|
# There are 4 operating modes (I2C, SPI, Serial and I/O) some #
|
512 |
|
|
# which can be combined. #
|
513 |
|
|
# See online documentation for more info: #
|
514 |
|
|
# http://www.robot-electronics.co.uk/htm/usb_iss_tech.htm #
|
515 |
|
|
#=============================================================================#
|
516 |
|
|
proc ISS_MODE {OperatingMode} {
|
517 |
|
|
|
518 |
|
|
|
519 |
|
|
# Send the ISS_MODE command:
|
520 |
|
|
# ISS_CMD ISS_MODE OPERATION_MODE+REMAINING
|
521 |
|
|
utils::uart_tx [concat [list 0x5A 0x02 ] $OperatingMode ]
|
522 |
|
|
|
523 |
|
|
# Get the 2 byte response
|
524 |
|
|
set config_response [utils::uart_rx 1 2]
|
525 |
|
|
if {$config_response != [list 0xff 0x00]} {
|
526 |
|
|
return 0
|
527 |
|
|
}
|
528 |
|
|
return 1
|
529 |
|
|
}
|
530 |
|
|
|
531 |
|
|
#=============================================================================#
|
532 |
|
|
# i2c_usb-iss::SETPINS (IO1, IO2, IO3, IO4) #
|
533 |
|
|
#-----------------------------------------------------------------------------#
|
534 |
|
|
# Description: The SETPINS command only operates on pins that have been set #
|
535 |
|
|
# as output pins. #
|
536 |
|
|
# Digital or analogue input pins, or pins that are used for I2C #
|
537 |
|
|
# or serial are not affected. #
|
538 |
|
|
#=============================================================================#
|
539 |
|
|
proc SETPINS {IO1 IO2 IO3 IO4} {
|
540 |
|
|
|
541 |
|
|
# Get the hex value for IO configuration
|
542 |
|
|
set io_config [format "0x0%x" [expr $IO1+($IO2*2)+($IO3*4)+($IO4*8)]]
|
543 |
|
|
|
544 |
|
|
# Send the SETPINS command to the adaptor
|
545 |
|
|
utils::uart_tx [list 0x63 $io_config]
|
546 |
|
|
|
547 |
|
|
# Get the 1 byte response
|
548 |
|
|
set config_response [utils::uart_rx 1 1]
|
549 |
|
|
if {$config_response != "0xff"} {
|
550 |
|
|
return 0
|
551 |
|
|
}
|
552 |
|
|
return 1
|
553 |
|
|
}
|
554 |
|
|
|
555 |
|
|
#=============================================================================#
|
556 |
|
|
# i2c_usb-iss::GETPINS () #
|
557 |
|
|
#-----------------------------------------------------------------------------#
|
558 |
|
|
# Description: This is used to get the current state of all digital I/O pins #
|
559 |
|
|
#=============================================================================#
|
560 |
|
|
proc GETPINS {} {
|
561 |
|
|
|
562 |
|
|
# Send the GETPINS command to the adaptor
|
563 |
|
|
utils::uart_tx 0x64
|
564 |
|
|
|
565 |
|
|
# Get the 1 byte response
|
566 |
|
|
set config_response [utils::uart_rx 1 1]
|
567 |
|
|
if {$config_response == "0x"} {
|
568 |
|
|
return 0
|
569 |
|
|
}
|
570 |
|
|
|
571 |
|
|
# Get the hex value for IO configuration
|
572 |
|
|
set IO4 [expr ($config_response & 0x08)/8]
|
573 |
|
|
set IO3 [expr ($config_response & 0x04)/4]
|
574 |
|
|
set IO2 [expr ($config_response & 0x02)/2]
|
575 |
|
|
set IO1 [expr ($config_response & 0x01)/1]
|
576 |
|
|
|
577 |
|
|
return [list $IO1 $IO2 $IO3 $IO4]
|
578 |
|
|
}
|
579 |
|
|
|
580 |
|
|
}
|