-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsusres_test.py
executable file
·128 lines (111 loc) · 3.74 KB
/
susres_test.py
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
#!/usr/bin/python3
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! Did you run as root?")
import pexpect
from pexpect.fdpexpect import fdspawn
import argparse
import time
import subprocess
import logging
import sys
import random
VBUS_PIN = 21
RESET_PIN = 24
def slow_send(console, s):
for c in s:
console.send(c)
time.sleep(0.1)
def reset_fpga():
global RESET_PIN
GPIO.output(RESET_PIN, 0)
time.sleep(0.1)
GPIO.output(RESET_PIN, 1)
def power_off():
global VBUS_PIN
GPIO.output(VBUS_PIN, 0)
def power_on():
global VBUS_PIN
GPIO.output(VBUS_PIN, 1)
def main():
global VBUS_PIN, RESET_PIN
parser = argparse.ArgumentParser(description="Suspend/resume stress test")
parser.add_argument(
"-d", "--debug", help="turn on debugging spew", default=False, action="store_true"
)
parser.add_argument(
"-t", "--touch-and-go", help="touch-and-go mode", default=False, action="store_true"
)
args = parser.parse_args()
if args.debug:
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
ps = subprocess.check_output(['ps', 'aux']).decode('utf-8')
found_screen = False
for line in ps.split('\n'):
if 'screen' in line.lower():
if '/dev/ttyS0' in line:
print(line)
found_screen = True
if found_screen:
print("Screen processes found occupying /dev/ttyS0, aborting.")
exit(0)
# ensure we can talk to /dev/ttyS0 without having to be sudo. pip doesn't install the dependencies in sudo env
perms = subprocess.check_output(['sudo', 'usermod', '-a', '-G', 'dialout', 'pi'])
# open a serial terminal
import serial
ser = serial.Serial()
ser.baudrate = 115200
ser.port="/dev/ttyS0"
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=0
try:
ser.open()
except:
print("couldn't open serial port")
exit(1)
console = fdspawn(ser)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup((VBUS_PIN, RESET_PIN), GPIO.OUT)
for i in range(100):
print("****iteration {}".format(i))
print("powering off")
power_off()
try:
print("suspending")
slow_send(console, "sleep sus\r")
print("waiting for suspend feedback")
console.expect_exact("INFO:susres: PID", 10)
except Exception as e:
log = console.before.decode('utf-8')
for line in log.split('\n'):
print(line)
print('problem putting device to sleep {}'.format(str(e)))
power_on() # leave the script with the power on.
GPIO.cleanup()
exit(0)
sleep_duration = random.randrange(15,50) / 10.0 # was 10,80
print("waiting {}s until waking up device...".format(sleep_duration))
time.sleep(sleep_duration)
print("resuming/powering on")
power_on()
if args.touch_and_go == False:
try:
console.expect_exact("Jumping to loader", 15)
except Exception as e:
log = console.before.decode('utf-8')
for line in log.split('\n'):
print(line)
print("didn't resume, {}".format(str(e)))
power_on() # leave the script with the power on.
GPIO.cleanup()
exit(0)
time.sleep(5) # wait for boot
wake_duration = random.randrange(30,80) / 10.0 # was 20,90
print("waiting {}s before issuing sleep command...".format(wake_duration))
time.sleep(wake_duration)
power_on() # leave the script with the power on.
GPIO.cleanup()
if __name__ == "__main__":
main()