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 210

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 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
                              lappend serial_ports "[registry get $serial_base $valueName]:"
100
                          }
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 210 olivier.gi
        if {[catch {open $Device [list RDWR]} serial_ch]} {
123
            uart_close
124 158 olivier.gi
            return 0
125
        }
126 210 olivier.gi
 
127 158 olivier.gi
        if {$Configure} {
128
            # Setup the baud rate
129
            fconfigure $serial_ch -mode "$Baudrate,n,8,1"
130 210 olivier.gi
 
131 158 olivier.gi
            # Block on read, don't buffer output
132
            fconfigure $serial_ch -blocking 1 -buffering none -translation binary -timeout 1000
133
 
134
        } else {
135
            fconfigure $serial_ch                             -translation binary
136
        }
137
 
138
        return 1
139
    }
140
 
141
    #=============================================================================#
142 210 olivier.gi
    # utils::uart_close (Device)                                                  #
143
    #-----------------------------------------------------------------------------#
144
    # Description: Closse the UART connection.                                    #
145
    # Arguments  : None                                                           #
146
    # Result     : 0 if error, 1 otherwise.                                       #
147
    #=============================================================================#
148
    proc uart_close {} {
149
 
150
        global serial_ch
151
 
152
        # Close the serial port
153
        if {[info exists serial_ch]} {
154
 
155
            if {[catch {close $serial_ch} response]} {
156
                puts "Error while closing serial port:"
157
                puts "$response"
158
                after 500
159
                return 0
160
            }
161
            after 500
162
        }
163
        return 1
164
    }
165
 
166
    #=============================================================================#
167 158 olivier.gi
    # utils::uart_tx (Data)                                                       #
168
    #-----------------------------------------------------------------------------#
169
    # Description: Transmit data over the serial debug interface.                 #
170
    # Arguments  : Data    - Data byte list to be sent.                           #
171
    # Result     : 0 if error, 1 otherwise.                                       #
172
    #=============================================================================#
173
    proc uart_tx {Data} {
174 210 olivier.gi
 
175 158 olivier.gi
        global serial_ch
176
        set allchar ""
177
        # Format data
178
        foreach char [split $Data] {
179
            append allchar [format %02x $char]
180
        }
181
        # Send data
182
#        puts "TX: $allchar"
183
        puts -nonewline $serial_ch [binary format H* $allchar]
184
        flush $serial_ch
185
 
186
        return 1
187
    }
188
 
189
    #=============================================================================#
190
    # utils::uart_rx (Format, Length)                                             #
191
    #-----------------------------------------------------------------------------#
192
    # Description: Receive data from the serial debug interface.                  #
193
    # Arguments  : Format   - 0 format as 16 bit word, 1 format as 8 bit word.    #
194
    #              Length   - Number of bytes to be received.                     #
195
    # Result     : List of received values, in hexadecimal.                       #
196
    #=============================================================================#
197
    proc uart_rx {Format Length} {
198 210 olivier.gi
 
199 158 olivier.gi
        global serial_ch
200 210 olivier.gi
 
201 158 olivier.gi
        if { [catch {read $serial_ch $Length} rx_data] } {
202 210 olivier.gi
 
203 158 olivier.gi
            set hex_data "0000"
204
        } else {
205
            set hex_data ""
206
            foreach char [split $rx_data {}] {
207
                binary scan $char H* hex_char
208
                lappend hex_data $hex_char
209
            }
210
        }
211
#        puts "RX: $hex_data"
212
        # Format data
213
        if {$Format==0} {
214
            set num_byte 2
215
        } else {
216
            set num_byte 1
217
        }
218
        set formated_data ""
219
        for {set i 0} {$i<[expr $Length/$num_byte]} {incr i} {
220 210 olivier.gi
 
221 158 olivier.gi
            set data ""
222
            for {set j $num_byte} {$j>0} {set j [expr $j-1]} {
223
                append data [lindex $hex_data [expr ($i*$num_byte)+$j-1]]
224
            }
225
            lappend formated_data "0x$data"
226
        }
227
 
228
        return $formated_data
229
    }
230
 
231
}

powered by: WebSVN 2.1.0

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