aboutsummaryrefslogtreecommitdiff
path: root/mule/mule.sh
blob: 3badb8b4d8e1eeef6b075c61650da64215c8f463 (plain)
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
#!/bin/sh

SYSFSPATH=/sys/class/gpio

# defaults
RESET_INVERT=0
BOOT0_INVERT=0
FIRMWARE=/usr/local/share/mule/mule.dfu

die() {
	echo "$*" 1>&2
	exit 1
}

usage() {
	echo "Usage: $0 [--reset|--dfu|--disable|--flash [<firmwarea>]]"
	echo "  --reset              - resets the Cortex M0"
	echo "  --dfu                - enables DFU loader"
	echo "  --disable            - keep Cortex M0 in reset"
	echo "  --flash [<firmware>] - flash the given firmware"

	exit 1
}

gpiosetup() {
	for PIN in ${RESET_PIN},${RESET_INVERT} ${BOOT0_PIN},${BOOT0_INVERT}
	do
		PINNUM=$(echo "$PIN" | cut -d',' -f1)
		INVERT=$(echo "$PIN" | cut -d',' -f2)
		if [ ! -d "${SYSFSPATH}/gpio${PINNUM}" ] ; then
			echo "${PINNUM}" > "${SYSFSPATH}/export"
		fi
		echo out > "${SYSFSPATH}/gpio${PINNUM}/direction"
		echo "${INVERT}" > "${SYSFSPATH}/gpio${PINNUM}/active_low"
	done
}

reset_stm32() {
	echo 0 >  "${SYSFSPATH}/gpio${RESET_PIN}/value"
	echo 0 >  "${SYSFSPATH}/gpio${BOOT0_PIN}/value"
	sleep 0.1
	echo 1 >  "${SYSFSPATH}/gpio${RESET_PIN}/value"
}

enable_dfu() {
	gpiosetup
	echo 0 >  "${SYSFSPATH}/gpio${RESET_PIN}/value"
	echo 1 >  "${SYSFSPATH}/gpio${BOOT0_PIN}/value"
	sleep 0.1
	echo 1 >  "${SYSFSPATH}/gpio${RESET_PIN}/value"
}

[ -f /proc/device-tree/model ] || die "can't determine hardware version"

MODEL=$(tr -d '\000' < /proc/device-tree/model)

case "$MODEL" in
	"Theobroma Systems A31 Pangolin")
		RESET_PIN=90
		BOOT0_PIN=67
	  ;;
	"Theobroma Systems A80 Armadillo")
		RESET_PIN=128
		BOOT0_PIN=129
	  ;;
	"Theobroma Systems A64-uQ7 SoM")
		RESET_PIN=195
		BOOT0_PIN=197
	  ;;
	"Theobroma Systems RK3399-Q7 SoM"*)
		RESET_PIN=56
		BOOT0_PIN=76
		RESET_INVERT=1
		BOOT0_INVERT=1
	  ;;
	"Theobroma Systems PX30-uQ7 SoM"*|"Theobroma Systems PX30-µQ7 SoM"*)
		RESET_PIN=100
		BOOT0_PIN=101
	  ;;
	*)
		die "${MODEL} not supported"
		;;
esac

case "$1" in
--reset) echo "reseting Cortex M0"
	gpiosetup
	reset_stm32
    ;;
--dfu) echo "enabling DFU loader"
	gpiosetup
	enable_dfu
    ;;
--disable) echo "disabling Cortex M0"
	gpiosetup
	echo 0 >  ${SYSFSPATH}/gpio${RESET_PIN}/value
    ;;
--flash)
	if [ $# -eq 2 ] ; then
		[ -f "$2" ] || die "flash file ${2} does not exist"
		FIRMWARE=$2
	elif [ $# -gt 2 ] ; then
		usage
	fi
	command -v dfu-util >/dev/null 2>&1 || die "dfu-util needed but not found"
	echo "flashing Cortex M0"
	gpiosetup
	enable_dfu
	echo waiting for DFU loader
	if command -v lsusb >/dev/null 2>&1; then
		while ! lsusb -d 0483:df11 > /dev/null
		do
			sleep .1
			printf .
		done
		printf "\nfound"
	else
		sleep 5
	fi
	dfu-util -a 0 -D "${FIRMWARE}"
	reset_stm32
    ;;
--*) usage
    ;;
*) usage
    ;;
esac

exit 0