-
Notifications
You must be signed in to change notification settings - Fork 5
/
jlink.bash
executable file
·136 lines (116 loc) · 3.04 KB
/
jlink.bash
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
#!/bin/bash
# Segger JLink setup script
# Used for flashing and erasing Kinetis (SWD) devices
# NOTE: Only tested with a Segger JLink on Linux
# Uses TMPSCRIPT to generate/run batch script
# Expected exported variables
# TOOLCHAIN - Path to Segger JLink (all)
# FIRMWARE - Path to firmware image to flash (flash)
# ADDRESS - Starting address to flash firmware from (flash)
# DEVICE - Name of the chip series
# Modes
# flash - Flashes given firmware
# erase - Erases chip
TMPSCRIPT=/tmp/jlinkscript
# Find actual directory of this script and enter it
ORIG_PATH=$(pwd)
FIRMWARE=${ORIG_PATH}/${FIRMWARE}
cd "$(dirname $(realpath "$0"))"
# First check for toolchain
if [ ! -d "${TOOLCHAIN}" ]; then
echo "ERROR: Could not find toolchain directory '${TOOLCHAIN}'"
exit 1
fi
# Make sure ADDRESS is in hex
ADDRESS=$(printf "0x%x" $((${ADDRESS})))
# Check mode
case "$1" in
# Flash Mode
"flash")
# Generate commander script
echo "
power on
Sleep 10
rx 10
loadbin ${FIRMWARE} ${ADDRESS}
rx 10
power off
q
" > "${TMPSCRIPT}"
cat ${TMPSCRIPT}
# Attempt to flash
# Udev rules are required to run commands without root priviledges (see 99-jink.rules)
"${TOOLCHAIN}"/JLinkExe -Device "${DEVICE}" -CommanderScript "${TMPSCRIPT}"
RETVAL=$?
sleep 0.5 # Wait for Device/USB to initialize before continuing
;;
# Erase Mode
"erase")
# Generate commander script
echo "
power on
Sleep 10
rx 10
unlock kinetis
erase
unlock kinetis
power off
q
" > "${TMPSCRIPT}"
cat ${TMPSCRIPT}
# Attempt to erase
# Udev rules are required to run commands without root priviledges (see 99-jink.rules)
"${TOOLCHAIN}"/JLinkExe -Device "${DEVICE}" -CommanderScript "${TMPSCRIPT}"
RETVAL=$?
;;
# Reset to bootloader mode
"bootloader")
# Generate commander script
echo "
rx 10
r0
r1
q
" > "${TMPSCRIPT}"
cat ${TMPSCRIPT}
# Attempt to reset chip to bootloader
# Udev rules are required to run commands without root priviledges (see 99-jink.rules)
"${TOOLCHAIN}"/JLinkExe -CommanderScript "${TMPSCRIPT}"
RETVAL=$?
;;
# Reset Mode
"reset")
# Generate commander script
echo "
r
q
" > "${TMPSCRIPT}"
cat ${TMPSCRIPT}
# Attempt to reset chip to bootloader
# Udev rules are required to run commands without root priviledges (see 99-jink.rules)
"${TOOLCHAIN}"/JLinkExe -CommanderScript "${TMPSCRIPT}"
RETVAL=$?
;;
# Invalid Mode
*)
echo "ERROR: '$1' is an invalid mode"
RETVAL=1
esac
echo "NOTE: jlink does not indicate cabling failures, here are some indicators"
echo "USB Cable Problem -> 'Can not connect to J-Link via USB.'"
echo "Flashing Cable Problem -> 'VTarget = 0.000V'"
echo " 'WARNING: RESET (pin 15) high, but should be low. Please check target hardware.'"
echo " 'Downloading file [kiibohd_bootloader.bin]...Writing target memory failed.'"
echo ""
# Error occurred, show debug
if [ "$RETVAL" -ne "0" ]; then
echo ""
# Debug info
echo "TOOLCHAIN: '$TOOLCHAIN'"
echo "FIRMWARE: '$FIRMWARE'"
echo "ADDRESS: '$ADDRESS'"
echo "DEVICE: '$DEVICE'"
echo "TMPSCRIPT: '$TMPSCRIPT'"
echo "CWD: '$(pwd)'"
fi
exit $RETVAL