forked from deanmao/avrdude-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoreset
executable file
·57 lines (47 loc) · 1.44 KB
/
autoreset
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
#!/usr/bin/python
"""
# Python script to check ioctl_tty for DTR send signal and toggle GPIO for avrdude.
#/usr/bin/autoreset
#
# #References:
# https://github.com/deanmao/avrdude-rpi
# https://github.com/mharizanov/avrdude-rpi
# https://github.com/SpellFoundry/avrdude-rpi
# https://github.com/openenergymonitor/avrdude-rpi
"""
import sys, os, time, fcntl
import RPi.GPIO as GPIO
## Configure ##
GPIO.setmode(GPIO.BOARD)
_RESET_PIN = 13 # BOARD: 13 = BCM: 27
_PULSE_TIME = 0.12 # Seconds
_TIMEOUT = 60 # Seconds
def set_stdin_to_nonblocking():
""" Sets STDIN to be non-blocking """
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
def send_reset_pulse(pin, pulse_time):
""" Send reset pulse to MCU on reset line over GPIO pin """
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
time.sleep(pulse_time)
GPIO.output(pin, GPIO.LOW)
def process():
""" Detects TIOCM_DTR call from ioctl_tty from STDIN """
_DTR = "TIOCM_DTR"
_IOCTL_CMD = "TIOCMGET"
start = time.time()
while True:
duration = time.time() - start
stdin_text = sys.stdin.readline()
if _DTR in stdin_text:
send_reset_pulse(_RESET_PIN, _PULSE_TIME)
return
elif duration > _TIMEOUT:
return
if __name__ == "__main__":
set_stdin_to_nonblocking()
process()
GPIO.cleanup()
print("Done with autoreset")