Blame view

RIOT/dist/tools/openocd/openocd.sh 8.56 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  #!/bin/sh
  #
  # Unified OpenOCD script for RIOT
  #
  # This script is supposed to be called from RIOTs make system,
  # as it depends on certain environment variables. An OpenOCD
  # configuration file must be present in a the boards dist folder.
  #
  # Any extra command line arguments after the command name are passed on the
  # openocd command line after the configuration file name but before any other
  # initialization commands.
  #
  # Global environment variables used:
  # OPENOCD:             OpenOCD command name, default: "openocd"
  # OPENOCD_CONFIG:      OpenOCD configuration file name,
  #                      default: "${RIOTBOARD}/${BOARD}/dist/openocd.cfg"
  #
  # The script supports the following actions:
  #
  # flash:        flash a given ELF file to the target.
  #
  #               options:
  #               IMAGE_FILE: Filename of the file that will be flashed
  #               PRE_FLASH_CHECK_SCRIPT: a command to run before flashing to
  #               verify the integrity of the image to be flashed. ELFFILE is
  #               passed as a command line argument to this command.
  #               Even though the file name variable is named ELFFILE, flashing
  #               works with any file format recognized by OpenOCD (elf, ihex, s19, bin).
  #
  # debug:        starts OpenOCD as GDB server in the background and
  #               connects to the server with the GDB client specified by
  #               the board
  #
  #               options:
  #               GDB_PORT:       port opened for GDB connections
  #               TCL_PORT:       port opened for TCL connections
  #               TELNET_PORT:    port opened for telnet connections
  #               DBG:            debugger client command, default: 'gdb -q'
  #               TUI:            if TUI!=null, the -tui option will be used
  #               ELFFILE:        path to the file to debug, must be in a format
  #                               recognized by GDB (preferably ELF, it will not
  #                               work with .bin, .hex or .s19 because they lack
  #                               symbol information)
  #
  # debug-server: starts OpenOCD as GDB server, but does not connect to
  #               to it with any frontend. This might be useful when using
  #               IDEs.
  #
  #               options:
  #               GDB_PORT:       port opened for GDB connections
  #               TCL_PORT:       port opened for TCL connections
  #               TELNET_PORT:    port opened for telnet connections
  #
  # reset:        triggers a hardware reset of the target board
  #
  # @author       Hauke Peteresen <hauke.petersen@fu-berlin.de>
  # @author       Joakim NohlgÄrd <joakim.nohlgard@eistec.se>
  
  # Default GDB port, set to 0 to disable, required != 0 for debug and debug-server targets
  : ${GDB_PORT:=3333}
  # Default telnet port, set to 0 to disable
  : ${TELNET_PORT:=4444}
  # Default TCL port, set to 0 to disable
  : ${TCL_PORT:=6333}
  # Default path to OpenOCD configuration file
  : ${OPENOCD_CONFIG:=${RIOTBOARD}/${BOARD}/dist/openocd.cfg}
  # Default OpenOCD command
  : ${OPENOCD:=openocd}
  # The setsid command is needed so that Ctrl+C in GDB doesn't kill OpenOCD
  : ${SETSID:=setsid}
  # GDB command, usually a separate command for each platform (e.g. arm-none-eabi-gdb)
  : ${GDB:=gdb}
  # Debugger client command, can be used to wrap GDB in a front-end
  : ${DBG:=${GDB}}
  # Default debugger flags,
  : ${DBG_DEFAULT_FLAGS:=-q -ex \"tar ext :${GDB_PORT}\"}
  # Extra debugger flags, added by the user
  : ${DBG_EXTRA_FLAGS:=}
  # Debugger flags, will be passed to sh -c, remember to escape any quotation signs.
  # Use ${DBG_DEFAULT_FLAGS} to insert the default flags anywhere in the string
  : ${DBG_FLAGS:=${DBG_DEFAULT_FLAGS} ${DBG_EXTRA_FLAGS}}
  # This is an optional offset to the base address that can be used to flash an
  # image in a different location than it is linked at. This feature can be useful
  # when flashing images for firmware swapping/remapping boot loaders.
  # Default offset is 0, meaning the image will be flashed at the address that it
  # was linked at.
  : ${IMAGE_OFFSET:=0}
  # Image file used for flashing. Must be in a format that OpenOCD can handle (ELF,
  # Intel hex, S19, or raw binary)
  # Default is to use $ELFFILE
  : ${IMAGE_FILE:=${ELFFILE}}
  # Type of image, leave empty to let OpenOCD automatically detect the type from
  # the file (default).
  # Valid values: elf, hex, s19, bin (see OpenOCD manual for more information)
  : ${IMAGE_TYPE:=}
  
  #
  # Examples of alternative debugger configurations
  #
  
  # Using the GDB text UI:
  # DBG_EXTRA_FLAGS=-tui make debug
  # or to always use TUI, put in your .profile:
  # export DBG_EXTRA_FLAGS=-tui
  
  # Wrapping GDB inside DDD (https://www.gnu.org/software/ddd/)
  # DBG=ddd DBG_FLAGS='--debugger "${GDB} ${DBG_DEFAULT_FLAGS}"' make debug
  # Alternatively, to always use DDD, put the following in your .profile or similar:
  # export DBG=ddd
  # export DBG_FLAGS='--debugger "${GDB} ${DBG_DEFAULT_FLAGS}"'
  # The single quotes are important on the line above, or it will not work.
  
  #
  # a couple of tests for certain configuration options
  #
  test_config() {
      if [ ! -f "${OPENOCD_CONFIG}" ]; then
          echo "Error: Unable to locate OpenOCD configuration file"
          echo "       (${OPENOCD_CONFIG})"
          exit 1
      fi
  }
  
  test_elffile() {
      if [ ! -f "${ELFFILE}" ]; then
          echo "Error: Unable to locate ELFFILE"
          echo "       (${ELFFILE})"
          exit 1
      fi
  }
  
  test_imagefile() {
      if [ ! -f "${IMAGE_FILE}" ]; then
          echo "Error: Unable to locate IMAGE_FILE"
          echo "       (${IMAGE_FILE})"
          exit 1
      fi
  }
  
  #
  # now comes the actual actions
  #
  do_flash() {
      test_config
      test_imagefile
      if [ -n "${PRE_FLASH_CHECK_SCRIPT}" ]; then
          sh -c "${PRE_FLASH_CHECK_SCRIPT} '${IMAGE_FILE}'"
          RETVAL=$?
          if [ $RETVAL -ne 0 ]; then
              echo "pre-flash checks failed, status=$RETVAL"
              exit $RETVAL
          fi
      fi
      # flash device
      sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
              ${OPENOCD_EXTRA_INIT} \
              -c 'tcl_port 0' \
              -c 'telnet_port 0' \
              -c 'gdb_port 0' \
              -c 'init' \
              -c 'targets' \
              -c 'reset halt' \
              ${OPENOCD_PRE_FLASH_CMDS} \
              -c 'flash write_image erase \"${IMAGE_FILE}\" ${IMAGE_OFFSET} ${IMAGE_TYPE}' \
              ${OPENOCD_PRE_VERIFY_CMDS} \
              -c 'verify_image \"${IMAGE_FILE}\"' \
              -c 'reset run' \
              -c 'shutdown'" &&
      echo 'Done flashing'
  }
  
  do_debug() {
      test_config
      test_elffile
      # temporary file that saves OpenOCD pid
      OCD_PIDFILE=$(mktemp -t "openocd_pid.XXXXXXXXXX")
      # will be called by trap
      cleanup() {
          OCD_PID="$(cat $OCD_PIDFILE)"
          kill ${OCD_PID}
          rm -f "$OCD_PIDFILE"
          exit 0
      }
      # cleanup after script terminates
      trap "cleanup ${OCD_PIDFILE}" EXIT
      # don't trap on Ctrl+C, because GDB keeps running
      trap '' INT
      # start OpenOCD as GDB server
      ${SETSID} sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
              ${OPENOCD_EXTRA_INIT} \
              -c 'tcl_port ${TCL_PORT}' \
              -c 'telnet_port ${TELNET_PORT}' \
              -c 'gdb_port ${GDB_PORT}' \
              -c 'init' \
              -c 'targets' \
              -c 'halt' \
              -l /dev/null & \
              echo \$! > $OCD_PIDFILE" &
      # Export to be able to access these from the sh -c command lines, may be
      # useful when using a frontend for GDB
      export ELFFILE
      export GDB
      export GDB_PORT
      export DBG_FLAGS
      export DBG_DEFAULT_FLAGS
      export DBG_EXTRA_FLAGS
      # Start the debugger and connect to the GDB server
      sh -c "${DBG} ${DBG_FLAGS} ${ELFFILE}"
  }
  
  do_debugserver() {
      test_config
      # start OpenOCD as GDB server
      sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
              ${OPENOCD_EXTRA_INIT} \
              -c 'tcl_port ${TCL_PORT}' \
              -c 'telnet_port ${TELNET_PORT}' \
              -c 'gdb_port ${GDB_PORT}' \
              -c 'init' \
              -c 'targets' \
              -c 'halt'"
  }
  
  do_reset() {
      test_config
      # start OpenOCD and invoke board reset
      sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
              ${OPENOCD_EXTRA_INIT} \
              -c 'tcl_port 0' \
              -c 'telnet_port 0' \
              -c 'gdb_port 0' \
              -c 'init' \
              -c 'reset run' \
              -c 'shutdown'"
  }
  
  #
  # parameter dispatching
  #
  ACTION="$1"
  
  case "${ACTION}" in
    flash)
      echo "### Flashing Target ###"
      do_flash
      ;;
    debug)
      echo "### Starting Debugging ###"
      do_debug
      ;;
    debug-server)
      echo "### Starting GDB Server ###"
      do_debugserver
      ;;
    reset)
      echo "### Resetting Target ###"
      do_reset
      ;;
    *)
      echo "Usage: $0 {flash|debug|debug-server|reset}"
      exit 2
      ;;
  esac