-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart-to-spice-pwl.py
101 lines (75 loc) · 2.55 KB
/
uart-to-spice-pwl.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
#!/usr/bin/python
from pathlib import WindowsPath
# Issue with second start char's start bit.
'''
Comms Definitions
'''
baudrate = 1_000_000
num_databits = 8
parity = 'n'
num_stopbits = 1
init_quiet_time = 100.0 # microseconds
inter_byte_quiet_time = 1.0 # microseconds
'''
PHY definitions
'''
lvl_1 = 0.1
lvl_0 = 0.0
idle_state = lvl_1
rise_time = 0.001 # microseconds
fall_time = rise_time # microseconds
'''
Calculated values
'''
bit_time: float = 1 / baudrate / 1e-6 # microseconds
def create_bit(bit_val: bool) -> str:
lvl = lvl_1 if bit_val else lvl_0
time_del = rise_time if bit_val else fall_time
return f'+{time_del:.4f}u {lvl}\n+{(bit_time - time_del):.4f}u {lvl}\n'
def create_byte_char(char_val: str) -> str:
pwl_str = ""
par_val = False
pwl_str += create_bit(False) # Start bit
for bit_num in range(num_databits):
bit_val = bool((ord(char_val) >> bit_num) & 1)
par_val = par_val ^ bit_val
pwl_str += create_bit(bit_val)
for ii in range(num_stopbits):
pwl_str += create_bit(True)
pwl_str += '\n' # newline for the sake of organizing the file with a blank line
return pwl_str
def create_byte_int(int_val: int) ->str:
pwl_str = ""
par_val = False
pwl_str += create_bit(False) # Start bit
for bit_num in range(num_databits):
bit_val = bool((int_val >> bit_num) & 1)
par_val = par_val ^ bit_val
pwl_str += create_bit(bit_val)
for ii in range(num_stopbits):
pwl_str += create_bit(True)
pwl_str += '\n' # newline for the sake of organizing the file with a blank line
return pwl_str
def create_string_pwl(str_val: str) -> str:
pwl_str = ""
for chars in str_val:
pwl_str += create_byte_char(chars)
return pwl_str
def create_idle_pwl(duration: int) ->str:
"""
Creates a delay at the idle state of duration length in microseconds.
:param duration: duration of idle time in microseconds
:return: pwl string to write
"""
time_del = rise_time if idle_state else fall_time
return f'+{time_del:.4f}u {idle_state}\n+{(duration - time_del):.4f}u {idle_state}\n\n'
if __name__ == '__main__':
output_file_path = WindowsPath('C:/dev_SK/jamystor/New Text Document_.txt')
with open(output_file_path, "w") as file:
for ii in range (10):
file.write(create_idle_pwl(50))
file.write(create_string_pwl("abc"))
file.write(create_idle_pwl(50))
for ii in range(255):
file.write(create_byte_int(ii))
file.write(create_idle_pwl(6))