| 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 |
210 |
olivier.gi |
#
|
| 25 |
158 |
olivier.gi |
# File Name: dbg_utils.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 |
|
|
# Basic utility functions for UART communication.
|
| 39 |
|
|
#
|
| 40 |
|
|
# Public functions:
|
| 41 |
|
|
#
|
| 42 |
|
|
# - utils::uart_port_list ()
|
| 43 |
|
|
# - utils::uart_open (Device, Baudrate)
|
| 44 |
210 |
olivier.gi |
# - utils::uart_close ()
|
| 45 |
158 |
olivier.gi |
# - utils::uart_tx (Data)
|
| 46 |
|
|
# - utils::uart_rx (Format, Length)
|
| 47 |
210 |
olivier.gi |
#
|
| 48 |
158 |
olivier.gi |
#----------------------------------------------------------------------------------
|
| 49 |
|
|
namespace eval utils {
|
| 50 |
|
|
|
| 51 |
|
|
global serial_ch
|
| 52 |
|
|
|
| 53 |
|
|
#=============================================================================#
|
| 54 |
|
|
# utils::uart_port_list () #
|
| 55 |
|
|
#-----------------------------------------------------------------------------#
|
| 56 |
|
|
# Description: Return the available serial ports (works on both linux and #
|
| 57 |
|
|
# windows. #
|
| 58 |
|
|
# Arguments : None. #
|
| 59 |
|
|
# Result : List of the available serial ports. #
|
| 60 |
|
|
#=============================================================================#
|
| 61 |
|
|
proc uart_port_list {} {
|
| 62 |
|
|
|
| 63 |
|
|
set serial_ports ""
|
| 64 |
|
|
|
| 65 |
|
|
switch $::tcl_platform(os) {
|
| 66 |
|
|
{Linux} {
|
| 67 |
|
|
set dmesg ""
|
| 68 |
|
|
catch {exec dmesg} dmesg
|
| 69 |
|
|
while {[regexp {ttyS\d+?} $dmesg match]} {
|
| 70 |
|
|
regsub $match $dmesg {} dmesg
|
| 71 |
|
|
if { [lsearch -exact $serial_ports "/dev/$match"] == -1 } {
|
| 72 |
|
|
lappend serial_ports "/dev/$match"
|
| 73 |
|
|
}
|
| 74 |
|
|
}
|
| 75 |
|
|
while {[regexp {ttyACM\d+?} $dmesg match]} {
|
| 76 |
|
|
regsub $match $dmesg {} dmesg
|
| 77 |
|
|
if { [lsearch -exact $serial_ports "/dev/$match"] == -1 } {
|
| 78 |
|
|
lappend serial_ports "/dev/$match"
|
| 79 |
|
|
}
|
| 80 |
|
|
}
|
| 81 |
|
|
while {[regexp {ttyUSB\d+?} $dmesg match]} {
|
| 82 |
|
|
regsub $match $dmesg {} dmesg
|
| 83 |
|
|
if { [lsearch -exact $serial_ports "/dev/$match"] == -1 } {
|
| 84 |
|
|
lappend serial_ports "/dev/$match"
|
| 85 |
|
|
}
|
| 86 |
|
|
}
|
| 87 |
|
|
if {![llength $serial_ports]} {
|
| 88 |
|
|
set serial_ports [list /dev/ttyS0 /dev/ttyS1 /dev/ttyS2 /dev/ttyS3]
|
| 89 |
|
|
}
|
| 90 |
|
|
}
|
| 91 |
210 |
olivier.gi |
{Darwin} {
|
| 92 |
|
|
set serial_ports [glob -nocomplain /dev/cu.*]
|
| 93 |
|
|
}
|
| 94 |
158 |
olivier.gi |
{Windows NT} {
|
| 95 |
|
|
package require registry
|
| 96 |
|
|
set serial_base "HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM"
|
| 97 |
|
|
set values [registry values $serial_base]
|
| 98 |
|
|
foreach valueName $values {
|
| 99 |
227 |
olivier.gi |
lappend serial_ports "[registry get $serial_base $valueName]"
|
| 100 |
158 |
olivier.gi |
}
|
| 101 |
210 |
olivier.gi |
}
|
| 102 |
|
|
default {set serial_ports ""}
|
| 103 |
158 |
olivier.gi |
}
|
| 104 |
|
|
|
| 105 |
|
|
return $serial_ports
|
| 106 |
|
|
}
|
| 107 |
|
|
|
| 108 |
|
|
#=============================================================================#
|
| 109 |
210 |
olivier.gi |
# utils::uart_open (Device, Configure, Baudrate) #
|
| 110 |
158 |
olivier.gi |
#-----------------------------------------------------------------------------#
|
| 111 |
|
|
# Description: Open and configure the UART connection. #
|
| 112 |
|
|
# Arguments : Device - Serial port device (i.e. /dev/ttyS0 or COM2:) #
|
| 113 |
|
|
# Configure - Configure serial communication (1:UART/0:I2C) #
|
| 114 |
|
|
# Baudrate - UART communication speed. #
|
| 115 |
|
|
# Result : 0 if error, 1 otherwise. #
|
| 116 |
|
|
#=============================================================================#
|
| 117 |
|
|
proc uart_open {Device Configure Baudrate} {
|
| 118 |
210 |
olivier.gi |
|
| 119 |
158 |
olivier.gi |
global serial_ch
|
| 120 |
|
|
|
| 121 |
|
|
# Open device for reading and writing
|
| 122 |
227 |
olivier.gi |
if {[catch {open $Device [list RDWR]} serial_ch]} {
|
| 123 |
|
|
if {[string eq "Windows NT" $::tcl_platform(os)]} {
|
| 124 |
|
|
if {[catch {open [join [list $Device ":"] ""] [list RDWR]} serial_ch]} {
|
| 125 |
|
|
uart_close
|
| 126 |
|
|
return 0
|
| 127 |
|
|
}
|
| 128 |
|
|
} else {
|
| 129 |
|
|
uart_close
|
| 130 |
|
|
return 0
|
| 131 |
|
|
}
|
| 132 |
158 |
olivier.gi |
}
|
| 133 |
210 |
olivier.gi |
|
| 134 |
158 |
olivier.gi |
if {$Configure} {
|
| 135 |
|
|
# Setup the baud rate
|
| 136 |
|
|
fconfigure $serial_ch -mode "$Baudrate,n,8,1"
|
| 137 |
210 |
olivier.gi |
|
| 138 |
158 |
olivier.gi |
# Block on read, don't buffer output
|
| 139 |
|
|
fconfigure $serial_ch -blocking 1 -buffering none -translation binary -timeout 1000
|
| 140 |
|
|
|
| 141 |
|
|
} else {
|
| 142 |
|
|
fconfigure $serial_ch -translation binary
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
return 1
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
#=============================================================================#
|
| 149 |
210 |
olivier.gi |
# utils::uart_close (Device) #
|
| 150 |
|
|
#-----------------------------------------------------------------------------#
|
| 151 |
|
|
# Description: Closse the UART connection. #
|
| 152 |
|
|
# Arguments : None #
|
| 153 |
|
|
# Result : 0 if error, 1 otherwise. #
|
| 154 |
|
|
#=============================================================================#
|
| 155 |
|
|
proc uart_close {} {
|
| 156 |
|
|
|
| 157 |
|
|
global serial_ch
|
| 158 |
|
|
|
| 159 |
|
|
# Close the serial port
|
| 160 |
|
|
if {[info exists serial_ch]} {
|
| 161 |
|
|
|
| 162 |
|
|
if {[catch {close $serial_ch} response]} {
|
| 163 |
|
|
puts "Error while closing serial port:"
|
| 164 |
|
|
puts "$response"
|
| 165 |
|
|
after 500
|
| 166 |
|
|
return 0
|
| 167 |
|
|
}
|
| 168 |
|
|
after 500
|
| 169 |
|
|
}
|
| 170 |
|
|
return 1
|
| 171 |
|
|
}
|
| 172 |
|
|
|
| 173 |
|
|
#=============================================================================#
|
| 174 |
158 |
olivier.gi |
# utils::uart_tx (Data) #
|
| 175 |
|
|
#-----------------------------------------------------------------------------#
|
| 176 |
|
|
# Description: Transmit data over the serial debug interface. #
|
| 177 |
|
|
# Arguments : Data - Data byte list to be sent. #
|
| 178 |
|
|
# Result : 0 if error, 1 otherwise. #
|
| 179 |
|
|
#=============================================================================#
|
| 180 |
|
|
proc uart_tx {Data} {
|
| 181 |
210 |
olivier.gi |
|
| 182 |
158 |
olivier.gi |
global serial_ch
|
| 183 |
|
|
set allchar ""
|
| 184 |
|
|
# Format data
|
| 185 |
|
|
foreach char [split $Data] {
|
| 186 |
|
|
append allchar [format %02x $char]
|
| 187 |
|
|
}
|
| 188 |
|
|
# Send data
|
| 189 |
|
|
# puts "TX: $allchar"
|
| 190 |
|
|
puts -nonewline $serial_ch [binary format H* $allchar]
|
| 191 |
|
|
flush $serial_ch
|
| 192 |
|
|
|
| 193 |
|
|
return 1
|
| 194 |
|
|
}
|
| 195 |
|
|
|
| 196 |
|
|
#=============================================================================#
|
| 197 |
|
|
# utils::uart_rx (Format, Length) #
|
| 198 |
|
|
#-----------------------------------------------------------------------------#
|
| 199 |
|
|
# Description: Receive data from the serial debug interface. #
|
| 200 |
|
|
# Arguments : Format - 0 format as 16 bit word, 1 format as 8 bit word. #
|
| 201 |
|
|
# Length - Number of bytes to be received. #
|
| 202 |
|
|
# Result : List of received values, in hexadecimal. #
|
| 203 |
|
|
#=============================================================================#
|
| 204 |
|
|
proc uart_rx {Format Length} {
|
| 205 |
210 |
olivier.gi |
|
| 206 |
158 |
olivier.gi |
global serial_ch
|
| 207 |
210 |
olivier.gi |
|
| 208 |
158 |
olivier.gi |
if { [catch {read $serial_ch $Length} rx_data] } {
|
| 209 |
210 |
olivier.gi |
|
| 210 |
158 |
olivier.gi |
set hex_data "0000"
|
| 211 |
|
|
} else {
|
| 212 |
|
|
set hex_data ""
|
| 213 |
|
|
foreach char [split $rx_data {}] {
|
| 214 |
|
|
binary scan $char H* hex_char
|
| 215 |
|
|
lappend hex_data $hex_char
|
| 216 |
|
|
}
|
| 217 |
|
|
}
|
| 218 |
|
|
# puts "RX: $hex_data"
|
| 219 |
|
|
# Format data
|
| 220 |
|
|
if {$Format==0} {
|
| 221 |
|
|
set num_byte 2
|
| 222 |
|
|
} else {
|
| 223 |
|
|
set num_byte 1
|
| 224 |
|
|
}
|
| 225 |
|
|
set formated_data ""
|
| 226 |
|
|
for {set i 0} {$i<[expr $Length/$num_byte]} {incr i} {
|
| 227 |
210 |
olivier.gi |
|
| 228 |
158 |
olivier.gi |
set data ""
|
| 229 |
|
|
for {set j $num_byte} {$j>0} {set j [expr $j-1]} {
|
| 230 |
|
|
append data [lindex $hex_data [expr ($i*$num_byte)+$j-1]]
|
| 231 |
|
|
}
|
| 232 |
|
|
lappend formated_data "0x$data"
|
| 233 |
|
|
}
|
| 234 |
|
|
|
| 235 |
|
|
return $formated_data
|
| 236 |
|
|
}
|
| 237 |
|
|
|
| 238 |
|
|
}
|