OpenCores
URL https://opencores.org/ocsvn/openmsp430/openmsp430/trunk

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [tools/] [lib/] [tcl-lib/] [dbg_utils.tcl] - Blame information for rev 158

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.