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

Subversion Repositories openmsp430

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

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 15 olivier.gi
# Author(s):
29
#             - Olivier Girard,    olgirard@gmail.com
30
#
31 2 olivier.gi
#------------------------------------------------------------------------------
32 15 olivier.gi
# $Rev: 87 $
33
# $LastChangedBy: olivier.girard $
34
# $LastChangedDate: 2011-02-05 14:40:22 +0100 (Sat, 05 Feb 2011) $
35
#------------------------------------------------------------------------------
36 2 olivier.gi
 
37
global clients
38
global server
39
 
40
 
41
###############################################################################
42
#                                                                             #
43
#                           START/STOP LOCAL SERVER                           #
44
#                                                                             #
45
###############################################################################
46
 
47
proc startServer { } {
48
 
49
    global server
50
    if {![info exists server(socket)]} {
51
        putsLog "Open socket on port $server(port) ... " 1
52
        if {[catch {socket -server clientAccept $server(port)} server(socket)]} {
53
            putsLog "failed"
54
            putsLog "ERROR: $server(socket)."
55
            unset server(socket)
56
            return 0
57
        }
58
        putsLog "done"
59
        putsLog "INFO: Waiting on TCP port $server(port)"
60
    } else {
61
        putsLog "Server is already up."
62
    }
63
    return 1
64
}
65
 
66
proc stopServer { } {
67
    global serial_status
68
    global server
69
 
70
    if {[info exists server(socket)]} {
71
        set port [lindex [fconfigure $server(socket) -sockname] 2]
72
        putsLog "Stop server (port $port)"
73
        close $server(socket)
74
        unset server(socket)
75
    }
76
    if {$serial_status} {
77
        ReleaseDevice 0xfffe
78
    }
79
}
80
 
81
proc clientAccept {sock addr port} {
82
    global clients
83
 
84
    putsLog "Accept client: $addr ($port)\n"
85
 
86
    set clients(addr,$sock) [list $addr $port]
87
    fconfigure $sock -buffering none
88
    fileevent  $sock readable [list receiveRSPpacket $sock]
89
 
90
    InitBreakUnits
91
}
92
 
93
proc startServerGUI { } {
94
    global serial_device
95
    global hw_break
96
 
97
    # Connect to device
98
    if {![GetDevice]} {
99
        .serial.l3      configure -text "Connection problem" -fg red
100
        putsLog "ERROR: Could not open $serial_device"
101
        return 0
102
    }
103
    .serial.l3          configure -text "Connected" -fg green
104
 
105
    # Display info
106
    putsLog "INFO: Sucessfully connected with the openMSP430 target."
107
    set sizes [GetCPU_ID_SIZE]
108
    putsLog "INFO: ROM Size - [lindex $sizes 0] B"
109
    putsLog "INFO: RAM Size - [lindex $sizes 1] B"
110
    putsLog "INFO: $hw_break(num) Hardware Break/Watch-point unit(s) detected"
111
    putsLog " "
112
 
113 87 olivier.gi
    # Activate Load TCL script section
114
    .tclscript.ft.l          configure -state normal
115
    .tclscript.ft.file       configure -state normal
116
    .tclscript.ft.browse     configure -state normal
117
    .tclscript.fb.read       configure -state normal
118
 
119 2 olivier.gi
    # Reset & Stop CPU
120
    ExecutePOR_Halt
121
 
122
    # Start server for GDB
123
    if {![startServer]} {
124
        .server.port.l2 configure -text "Connection problem" -fg red
125
        return 0
126
    }
127
    .server.port.l2     configure -text "Running" -fg green
128
 
129
    # Disable gui entries
130
    .serial.p1               configure -state disabled
131
    .serial.p2               configure -state disabled
132
    .server.port.p           configure -state disabled
133
    .server.port.start       configure -state disabled
134
}
135
 
136
###############################################################################
137
#                                                                             #
138
#                        RECEIVE / SEND RSP PACKETS                           #
139
#                                                                             #
140
###############################################################################
141
 
142
proc receiveRSPpacket {sock} {
143
 
144
    # Get client info
145
    set ip   [lindex [fconfigure $sock -peername] 0]
146
    set port [lindex [fconfigure $sock -peername] 2]
147
 
148
    # Check if a new packet arrives
149
    set rx_packet 0
150
    set rsp_cmd [getDebugChar $sock]
151
    set rsp_sum ""
152
    if {[string eq $rsp_cmd "\$"]} {
153
        set rx_packet 1
154
        set rsp_cmd ""
155
    } else {
156
        binary scan $rsp_cmd H* rsp_cmd
157
        if {$rsp_cmd=="03"} {
158
            putsVerbose "--> BREAK"
159
            HaltCPU
160
        }
161
    }
162
    # Receive packet
163
    while {$rx_packet} {
164
        set char [getDebugChar $sock]
165
        if {$char==-1} {
166
            set    rx_packet 0
167
        } elseif {[string eq $char "\#"]} {
168
            set    rx_packet 0
169
            set    rsp_sum   [getDebugChar $sock]
170
            append rsp_sum   [getDebugChar $sock]
171
 
172
            # Re-calculate the checksum
173
            set    tmp_sum   [RSPcheckSum  $rsp_cmd]
174
 
175
            # Acknowledge and analyse the packet
176
            if {[string eq $rsp_sum $tmp_sum]} {
177
                putDebugChar $sock "+"
178
 
179
                # Remove escape characters
180
                set rsp_cmd [removeEscapeChar $rsp_cmd]
181
                putsVerbose "+ w $rsp_cmd"
182
 
183
                # Parse packet and send back the answer
184
                set rsp_answer [rspParse $sock $rsp_cmd]
185
                if {$rsp_answer != "-1"} {
186
                    sendRSPpacket $sock $rsp_answer
187
                }
188
            } else {
189
                putDebugChar $sock "-"
190
            }
191
        } else {
192
            append rsp_cmd $char
193
        }
194
    }
195
}
196
 
197
 
198
proc sendRSPpacket {sock rsp_cmd} {
199
 
200
    # Set escape characters
201
    set rsp_cmd [setEscapeChar $rsp_cmd]
202
 
203
    # Calculate checksum
204
    set rsp_sum [RSPcheckSum  $rsp_cmd]
205
 
206
    # Format the packet
207
    set rsp_packet "\$$rsp_cmd\#$rsp_sum"
208
 
209
    # Send the packet until the "+" aknowledge is received
210
    set send_ok 0
211
    while {!$send_ok} {
212
        putDebugChar $sock "$rsp_packet"
213
        set char [getDebugChar $sock]
214
 
215
        putsVerbose "$char r $rsp_cmd"
216
 
217
        if {$char==-1} {
218
            set    send_ok 1
219
        } elseif {[string eq $char "+"]} {
220
            set    send_ok 1
221
        }
222
    }
223
}
224
 
225
 
226
###############################################################################
227
#                                                                             #
228
#                   CHECKSUM / ESCAPE CHAR / RX / TX FUNCTIONS                #
229
#                                                                             #
230
###############################################################################
231
 
232
proc RSPcheckSum {rsp_cmd} {
233
 
234
    set    rsp_sum   0
235
    for {set i 0} {$i<[string length $rsp_cmd]} {incr i} {
236
        scan [string index $rsp_cmd $i] "%c" char_val
237
        set rsp_sum [expr $rsp_sum+$char_val]
238
    }
239
    set rsp_sum [format %02x [expr $rsp_sum%256]]
240
 
241
    return $rsp_sum
242
}
243
 
244
proc removeEscapeChar {rsp_cmd} {
245
 
246
    # Replace all '\}0x03' characters with '#'
247
    regsub -all "\}[binary format H* 03]" $rsp_cmd "\#" rsp_cmd
248
 
249
    # Replace all '\}0x04' characters with '$'
250
    regsub -all "\}[binary format H* 04]" $rsp_cmd "\$" rsp_cmd
251
 
252
    # Replace all '\}\]' characters with '\}'
253
    regsub -all "\}\]" $rsp_cmd "\}" rsp_cmd
254
 
255
    return "$rsp_cmd"
256
}
257
 
258
proc setEscapeChar {rsp_cmd} {
259
 
260
    # Escape all '\}' characters with '\}\]'
261
    regsub -all "\}" $rsp_cmd "\}\]" rsp_cmd
262
 
263
    # Escape all '$' characters with '\}0x04'
264
    regsub -all "\\$" $rsp_cmd "\}[binary format H* 04]" rsp_cmd
265
 
266
    # Escape all '#' characters with '\}0x03'
267
    regsub -all "\#" $rsp_cmd "\}[binary format H* 03]" rsp_cmd
268
 
269
    return "$rsp_cmd"
270
}
271
 
272
 
273
proc getDebugChar {sock} {
274
    global clients
275
 
276
    # Get client info
277
    set ip   [lindex [fconfigure $sock -peername] 0]
278
    set port [lindex [fconfigure $sock -peername] 2]
279
 
280
    if {[eof $sock] || [catch {set char [read $sock 1]}]} {
281
        # end of file or abnormal connection drop
282
        close $sock
283
        putsLog "Connection closed: $ip ($port)\n"
284
        unset clients(addr,$sock)
285
        return -1
286
    } else {
287
        return $char
288
    }
289
}
290
 
291
 
292
proc putDebugChar {sock char} {
293
    puts -nonewline $sock $char
294
}

powered by: WebSVN 2.1.0

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