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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [tools/] [openmsp430-gdbproxy/] [server.tcl] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 olivier.gi
#!/usr/bin/wish
2
#------------------------------------------------------------------------------
3
# Copyright (C) 2001 Authors
4
#
5
# This source file may be used and distributed without restriction provided
6
# that this copyright statement is not removed from the file and that any
7
# derivative work contains the original copyright notice and the associated
8
# disclaimer.
9
#
10
# This source file is free software; you can redistribute it and/or modify
11
# it under the terms of the GNU Lesser General Public License as published
12
# by the Free Software Foundation; either version 2.1 of the License, or
13
# (at your option) any later version.
14
#
15
# This source is distributed in the hope that it will be useful, but WITHOUT
16
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18
# License for more details.
19
#
20
# You should have received a copy of the GNU Lesser General Public License
21
# along with this source; if not, write to the Free Software Foundation,
22
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
#
24
#------------------------------------------------------------------------------
25
# 
26
# File Name: server.tcl
27
# 
28
#------------------------------------------------------------------------------
29
 
30
global clients
31
global server
32
 
33
 
34
###############################################################################
35
#                                                                             #
36
#                           START/STOP LOCAL SERVER                           #
37
#                                                                             #
38
###############################################################################
39
 
40
proc startServer { } {
41
 
42
    global server
43
    if {![info exists server(socket)]} {
44
        putsLog "Open socket on port $server(port) ... " 1
45
        if {[catch {socket -server clientAccept $server(port)} server(socket)]} {
46
            putsLog "failed"
47
            putsLog "ERROR: $server(socket)."
48
            unset server(socket)
49
            return 0
50
        }
51
        putsLog "done"
52
        putsLog "INFO: Waiting on TCP port $server(port)"
53
    } else {
54
        putsLog "Server is already up."
55
    }
56
    return 1
57
}
58
 
59
proc stopServer { } {
60
    global serial_status
61
    global server
62
 
63
    if {[info exists server(socket)]} {
64
        set port [lindex [fconfigure $server(socket) -sockname] 2]
65
        putsLog "Stop server (port $port)"
66
        close $server(socket)
67
        unset server(socket)
68
    }
69
    if {$serial_status} {
70
        ReleaseDevice 0xfffe
71
    }
72
}
73
 
74
proc clientAccept {sock addr port} {
75
    global clients
76
 
77
    putsLog "Accept client: $addr ($port)\n"
78
 
79
    set clients(addr,$sock) [list $addr $port]
80
    fconfigure $sock -buffering none
81
    fileevent  $sock readable [list receiveRSPpacket $sock]
82
 
83
    InitBreakUnits
84
}
85
 
86
proc startServerGUI { } {
87
    global serial_device
88
    global hw_break
89
 
90
    # Connect to device
91
    if {![GetDevice]} {
92
        .serial.l3      configure -text "Connection problem" -fg red
93
        putsLog "ERROR: Could not open $serial_device"
94
        return 0
95
    }
96
    .serial.l3          configure -text "Connected" -fg green
97
 
98
    # Display info
99
    putsLog "INFO: Sucessfully connected with the openMSP430 target."
100
    set sizes [GetCPU_ID_SIZE]
101
    putsLog "INFO: ROM Size - [lindex $sizes 0] B"
102
    putsLog "INFO: RAM Size - [lindex $sizes 1] B"
103
    putsLog "INFO: $hw_break(num) Hardware Break/Watch-point unit(s) detected"
104
    putsLog " "
105
 
106
    # Reset & Stop CPU
107
    ExecutePOR_Halt
108
 
109
    # Start server for GDB
110
    if {![startServer]} {
111
        .server.port.l2 configure -text "Connection problem" -fg red
112
        return 0
113
    }
114
    .server.port.l2     configure -text "Running" -fg green
115
 
116
    # Disable gui entries
117
    .serial.p1               configure -state disabled
118
    .serial.p2               configure -state disabled
119
    .server.port.p           configure -state disabled
120
    .server.port.start       configure -state disabled
121
}
122
 
123
###############################################################################
124
#                                                                             #
125
#                        RECEIVE / SEND RSP PACKETS                           #
126
#                                                                             #
127
###############################################################################
128
 
129
proc receiveRSPpacket {sock} {
130
 
131
    # Get client info
132
    set ip   [lindex [fconfigure $sock -peername] 0]
133
    set port [lindex [fconfigure $sock -peername] 2]
134
 
135
    # Check if a new packet arrives
136
    set rx_packet 0
137
    set rsp_cmd [getDebugChar $sock]
138
    set rsp_sum ""
139
    if {[string eq $rsp_cmd "\$"]} {
140
        set rx_packet 1
141
        set rsp_cmd ""
142
    } else {
143
        binary scan $rsp_cmd H* rsp_cmd
144
        if {$rsp_cmd=="03"} {
145
            putsVerbose "--> BREAK"
146
            HaltCPU
147
        }
148
    }
149
    # Receive packet
150
    while {$rx_packet} {
151
        set char [getDebugChar $sock]
152
        if {$char==-1} {
153
            set    rx_packet 0
154
        } elseif {[string eq $char "\#"]} {
155
            set    rx_packet 0
156
            set    rsp_sum   [getDebugChar $sock]
157
            append rsp_sum   [getDebugChar $sock]
158
 
159
            # Re-calculate the checksum
160
            set    tmp_sum   [RSPcheckSum  $rsp_cmd]
161
 
162
            # Acknowledge and analyse the packet
163
            if {[string eq $rsp_sum $tmp_sum]} {
164
                putDebugChar $sock "+"
165
 
166
                # Remove escape characters
167
                set rsp_cmd [removeEscapeChar $rsp_cmd]
168
                putsVerbose "+ w $rsp_cmd"
169
 
170
                # Parse packet and send back the answer
171
                set rsp_answer [rspParse $sock $rsp_cmd]
172
                if {$rsp_answer != "-1"} {
173
                    sendRSPpacket $sock $rsp_answer
174
                }
175
            } else {
176
                putDebugChar $sock "-"
177
            }
178
        } else {
179
            append rsp_cmd $char
180
        }
181
    }
182
}
183
 
184
 
185
proc sendRSPpacket {sock rsp_cmd} {
186
 
187
    # Set escape characters
188
    set rsp_cmd [setEscapeChar $rsp_cmd]
189
 
190
    # Calculate checksum
191
    set rsp_sum [RSPcheckSum  $rsp_cmd]
192
 
193
    # Format the packet
194
    set rsp_packet "\$$rsp_cmd\#$rsp_sum"
195
 
196
    # Send the packet until the "+" aknowledge is received
197
    set send_ok 0
198
    while {!$send_ok} {
199
        putDebugChar $sock "$rsp_packet"
200
        set char [getDebugChar $sock]
201
 
202
        putsVerbose "$char r $rsp_cmd"
203
 
204
        if {$char==-1} {
205
            set    send_ok 1
206
        } elseif {[string eq $char "+"]} {
207
            set    send_ok 1
208
        }
209
    }
210
}
211
 
212
 
213
###############################################################################
214
#                                                                             #
215
#                   CHECKSUM / ESCAPE CHAR / RX / TX FUNCTIONS                #
216
#                                                                             #
217
###############################################################################
218
 
219
proc RSPcheckSum {rsp_cmd} {
220
 
221
    set    rsp_sum   0
222
    for {set i 0} {$i<[string length $rsp_cmd]} {incr i} {
223
        scan [string index $rsp_cmd $i] "%c" char_val
224
        set rsp_sum [expr $rsp_sum+$char_val]
225
    }
226
    set rsp_sum [format %02x [expr $rsp_sum%256]]
227
 
228
    return $rsp_sum
229
}
230
 
231
proc removeEscapeChar {rsp_cmd} {
232
 
233
    # Replace all '\}0x03' characters with '#'
234
    regsub -all "\}[binary format H* 03]" $rsp_cmd "\#" rsp_cmd
235
 
236
    # Replace all '\}0x04' characters with '$'
237
    regsub -all "\}[binary format H* 04]" $rsp_cmd "\$" rsp_cmd
238
 
239
    # Replace all '\}\]' characters with '\}'
240
    regsub -all "\}\]" $rsp_cmd "\}" rsp_cmd
241
 
242
    return "$rsp_cmd"
243
}
244
 
245
proc setEscapeChar {rsp_cmd} {
246
 
247
    # Escape all '\}' characters with '\}\]'
248
    regsub -all "\}" $rsp_cmd "\}\]" rsp_cmd
249
 
250
    # Escape all '$' characters with '\}0x04'
251
    regsub -all "\\$" $rsp_cmd "\}[binary format H* 04]" rsp_cmd
252
 
253
    # Escape all '#' characters with '\}0x03'
254
    regsub -all "\#" $rsp_cmd "\}[binary format H* 03]" rsp_cmd
255
 
256
    return "$rsp_cmd"
257
}
258
 
259
 
260
proc getDebugChar {sock} {
261
    global clients
262
 
263
    # Get client info
264
    set ip   [lindex [fconfigure $sock -peername] 0]
265
    set port [lindex [fconfigure $sock -peername] 2]
266
 
267
    if {[eof $sock] || [catch {set char [read $sock 1]}]} {
268
        # end of file or abnormal connection drop
269
        close $sock
270
        putsLog "Connection closed: $ip ($port)\n"
271
        unset clients(addr,$sock)
272
        return -1
273
    } else {
274
        return $char
275
    }
276
}
277
 
278
 
279
proc putDebugChar {sock char} {
280
    puts -nonewline $sock $char
281
}

powered by: WebSVN 2.1.0

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