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

Subversion Repositories openmsp430

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

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: 15 $
33
# $LastChangedBy: olivier.girard $
34
# $LastChangedDate: 2009-08-04 22:41:47 +0200 (Tue, 04 Aug 2009) $
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
    # Reset & Stop CPU
114
    ExecutePOR_Halt
115
 
116
    # Start server for GDB
117
    if {![startServer]} {
118
        .server.port.l2 configure -text "Connection problem" -fg red
119
        return 0
120
    }
121
    .server.port.l2     configure -text "Running" -fg green
122
 
123
    # Disable gui entries
124
    .serial.p1               configure -state disabled
125
    .serial.p2               configure -state disabled
126
    .server.port.p           configure -state disabled
127
    .server.port.start       configure -state disabled
128
}
129
 
130
###############################################################################
131
#                                                                             #
132
#                        RECEIVE / SEND RSP PACKETS                           #
133
#                                                                             #
134
###############################################################################
135
 
136
proc receiveRSPpacket {sock} {
137
 
138
    # Get client info
139
    set ip   [lindex [fconfigure $sock -peername] 0]
140
    set port [lindex [fconfigure $sock -peername] 2]
141
 
142
    # Check if a new packet arrives
143
    set rx_packet 0
144
    set rsp_cmd [getDebugChar $sock]
145
    set rsp_sum ""
146
    if {[string eq $rsp_cmd "\$"]} {
147
        set rx_packet 1
148
        set rsp_cmd ""
149
    } else {
150
        binary scan $rsp_cmd H* rsp_cmd
151
        if {$rsp_cmd=="03"} {
152
            putsVerbose "--> BREAK"
153
            HaltCPU
154
        }
155
    }
156
    # Receive packet
157
    while {$rx_packet} {
158
        set char [getDebugChar $sock]
159
        if {$char==-1} {
160
            set    rx_packet 0
161
        } elseif {[string eq $char "\#"]} {
162
            set    rx_packet 0
163
            set    rsp_sum   [getDebugChar $sock]
164
            append rsp_sum   [getDebugChar $sock]
165
 
166
            # Re-calculate the checksum
167
            set    tmp_sum   [RSPcheckSum  $rsp_cmd]
168
 
169
            # Acknowledge and analyse the packet
170
            if {[string eq $rsp_sum $tmp_sum]} {
171
                putDebugChar $sock "+"
172
 
173
                # Remove escape characters
174
                set rsp_cmd [removeEscapeChar $rsp_cmd]
175
                putsVerbose "+ w $rsp_cmd"
176
 
177
                # Parse packet and send back the answer
178
                set rsp_answer [rspParse $sock $rsp_cmd]
179
                if {$rsp_answer != "-1"} {
180
                    sendRSPpacket $sock $rsp_answer
181
                }
182
            } else {
183
                putDebugChar $sock "-"
184
            }
185
        } else {
186
            append rsp_cmd $char
187
        }
188
    }
189
}
190
 
191
 
192
proc sendRSPpacket {sock rsp_cmd} {
193
 
194
    # Set escape characters
195
    set rsp_cmd [setEscapeChar $rsp_cmd]
196
 
197
    # Calculate checksum
198
    set rsp_sum [RSPcheckSum  $rsp_cmd]
199
 
200
    # Format the packet
201
    set rsp_packet "\$$rsp_cmd\#$rsp_sum"
202
 
203
    # Send the packet until the "+" aknowledge is received
204
    set send_ok 0
205
    while {!$send_ok} {
206
        putDebugChar $sock "$rsp_packet"
207
        set char [getDebugChar $sock]
208
 
209
        putsVerbose "$char r $rsp_cmd"
210
 
211
        if {$char==-1} {
212
            set    send_ok 1
213
        } elseif {[string eq $char "+"]} {
214
            set    send_ok 1
215
        }
216
    }
217
}
218
 
219
 
220
###############################################################################
221
#                                                                             #
222
#                   CHECKSUM / ESCAPE CHAR / RX / TX FUNCTIONS                #
223
#                                                                             #
224
###############################################################################
225
 
226
proc RSPcheckSum {rsp_cmd} {
227
 
228
    set    rsp_sum   0
229
    for {set i 0} {$i<[string length $rsp_cmd]} {incr i} {
230
        scan [string index $rsp_cmd $i] "%c" char_val
231
        set rsp_sum [expr $rsp_sum+$char_val]
232
    }
233
    set rsp_sum [format %02x [expr $rsp_sum%256]]
234
 
235
    return $rsp_sum
236
}
237
 
238
proc removeEscapeChar {rsp_cmd} {
239
 
240
    # Replace all '\}0x03' characters with '#'
241
    regsub -all "\}[binary format H* 03]" $rsp_cmd "\#" rsp_cmd
242
 
243
    # Replace all '\}0x04' characters with '$'
244
    regsub -all "\}[binary format H* 04]" $rsp_cmd "\$" rsp_cmd
245
 
246
    # Replace all '\}\]' characters with '\}'
247
    regsub -all "\}\]" $rsp_cmd "\}" rsp_cmd
248
 
249
    return "$rsp_cmd"
250
}
251
 
252
proc setEscapeChar {rsp_cmd} {
253
 
254
    # Escape all '\}' characters with '\}\]'
255
    regsub -all "\}" $rsp_cmd "\}\]" rsp_cmd
256
 
257
    # Escape all '$' characters with '\}0x04'
258
    regsub -all "\\$" $rsp_cmd "\}[binary format H* 04]" rsp_cmd
259
 
260
    # Escape all '#' characters with '\}0x03'
261
    regsub -all "\#" $rsp_cmd "\}[binary format H* 03]" rsp_cmd
262
 
263
    return "$rsp_cmd"
264
}
265
 
266
 
267
proc getDebugChar {sock} {
268
    global clients
269
 
270
    # Get client info
271
    set ip   [lindex [fconfigure $sock -peername] 0]
272
    set port [lindex [fconfigure $sock -peername] 2]
273
 
274
    if {[eof $sock] || [catch {set char [read $sock 1]}]} {
275
        # end of file or abnormal connection drop
276
        close $sock
277
        putsLog "Connection closed: $ip ($port)\n"
278
        unset clients(addr,$sock)
279
        return -1
280
    } else {
281
        return $char
282
    }
283
}
284
 
285
 
286
proc putDebugChar {sock char} {
287
    puts -nonewline $sock $char
288
}

powered by: WebSVN 2.1.0

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