-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcutool
executable file
·153 lines (115 loc) · 4.44 KB
/
mcutool
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
#!/usr/bin/env python3.7
import time
import argparse
import sys
import os
import subprocess
DELAY = 0.01
GPIO_ROOT = "/sys/class/gpio"
NX_GPIOS = [
dict(num=393, name="MCU_ENABLE", desc="MCU chip enable", direction="out", default=1),
dict(num=422, name="MCU_SIGNAL", desc="MCU signal / bootloader selection", direction="out", default=1),
dict(num=421, name="MCU_BUFFER", desc="MCU buffer enable", direction="out", default=1),
]
GPIOS = dict(NX=NX_GPIOS)
PLATFORM = 'NX'
__version__ = "0.1"
PORT = "/dev/ttyTHS0"
if (not os.access(GPIO_ROOT + '/export', os.W_OK) or not os.access(GPIO_ROOT + '/unexport', os.W_OK)):
raise RuntimeError("The current user does not have permissions set to access the library functionalites."
"Please configure permissions or use the root user to run this")
def _run(args):
out = subprocess.Popen(args.split(" "), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
return stdout,stderr
def _setup_gpios():
for dev in GPIOS[PLATFORM]:
path = "{}/gpio{}".format(GPIO_ROOT, dev['num'])
if os.path.exists(path):
print("{} : Already created".format(dev['name']))
else:
print("{} : Creating {} pin".format(dev['name'], dev['direction']))
with open("{}/export".format(GPIO_ROOT), 'w') as outfile:
outfile.write(str(dev['num']))
while not os.access("{}/gpio{}/value".format(GPIO_ROOT, dev['num']), os.R_OK | os.W_OK):
time.sleep(0.01)
with open("{}/gpio{}/direction".format(GPIO_ROOT, dev['num']), 'w') as outfile:
outfile.write(dev['direction'])
with open("{}/gpio{}/value".format(GPIO_ROOT, dev['num']), 'w') as outfile:
outfile.write(str(dev['default']))
def _sys_gpio(dev, value):
path = "{}/gpio{}".format(GPIO_ROOT, dev['num'])
if not os.path.exists(path):
_setup_gpios()
if value == None:
stdout,_ = _run("cat {}/gpio{}/value".format(GPIO_ROOT, dev['num']))
print(int(stdout.strip()))
else:
with open("{}/gpio{}/value".format(GPIO_ROOT, dev['num']), 'w') as outfile:
outfile.write(str(value))
def _gpio_cmd(name, value):
if value not in ['1', '0', 1, 0, None]:
print("ERROR: Value {} is invalid".format(value))
return
if name == None:
print("ERROR: Please specify a GPIO name:")
for dev in GPIOS[PLATFORM]:
print(" {} : {}".format(dev['name'], dev['desc']))
return
for dev in GPIOS[PLATFORM]:
if dev['name'] == name.upper():
_sys_gpio(dev, value)
return
print("ERROR: GPIO named {} is unknown".format(name))
class Control:
def __init__(self):
parser = argparse.ArgumentParser(
description="mcutool v%s" % __version__, usage="mcutool <command> [<args>]"
)
parser.add_argument('command', help='Can be one of: [bootloader reboot halt start flash signalon signaloff]')
args = parser.parse_args(sys.argv[1:])
if args.command == 'signaloff':
self.bufferisolate()
else:
self.bufferconnect()
getattr(self, args.command)()
def signalon(self):
_gpio_cmd("MCU_SIGNAL", 1)
def signaloff(self):
_gpio_cmd("MCU_SIGNAL", 0)
def bootloader(self):
_gpio_cmd("MCU_SIGNAL", 0)
time.sleep(DELAY)
self.reboot()
_gpio_cmd("MCU_SIGNAL", 1)
time.sleep(DELAY*10)
def reboot(self):
_gpio_cmd("MCU_ENABLE", 0)
time.sleep(DELAY)
_gpio_cmd("MCU_ENABLE", 1)
time.sleep(DELAY*10)
def halt(self):
_gpio_cmd("MCU_ENABLE", 0)
time.sleep(DELAY)
def start(self):
_gpio_cmd("MCU_ENABLE", 1)
time.sleep(DELAY)
def bufferconnect(self):
_gpio_cmd("MCU_BUFFER", 1)
time.sleep(DELAY)
def bufferisolate(self):
_gpio_cmd("MCU_BUFFER", 0)
time.sleep(DELAY)
def flash(self):
print("... entering bootloader ...")
self.bootloader()
print("... flashing microcontroller ...")
stdout,_ = _run("espflash {} firmware.elf".format(PORT))
for line in stdout.decode("utf-8").splitlines():
line = line.strip()
if len(line) > 0:
print(" " + line)
print("... rebooting microcontroller ...")
self.reboot()
if __name__ == '__main__':
Control()