1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00
myhdl/example/rs232/rs232_rx.py

38 lines
775 B
Python
Raw Normal View History

2005-12-27 14:45:38 +00:00
from myhdl import delay, intbv, downrange
2003-01-30 12:20:43 +00:00
from rs232_util import sec, parity, ParityError, StopBitError
def rs232_rx(rx, actual, cfg):
2003-01-30 13:57:30 +00:00
2003-02-03 20:21:46 +00:00
""" rs232 receiver.
rx -- serial input data
actual -- data actually received
cfg -- rs232_util.Config configuration object
"""
2003-01-30 12:20:43 +00:00
data = intbv(0)
period = int(1*sec / cfg.baud_rate)
2003-01-30 13:57:30 +00:00
2005-12-27 14:45:38 +00:00
yield rx.posedge
2003-02-25 22:55:46 +00:00
yield delay(period // 2)
2003-01-30 22:17:15 +00:00
2003-01-30 12:20:43 +00:00
data[7] = 0
for i in downrange(cfg.n_bits):
yield delay(period)
data[i] = rx
2003-01-30 13:57:30 +00:00
2003-01-30 12:20:43 +00:00
if cfg.parity is not None:
yield delay(period)
2003-02-15 14:54:40 +00:00
if rx != parity(data, cfg.parity):
2003-01-30 12:20:43 +00:00
raise ParityError
2003-01-30 13:57:30 +00:00
2003-01-30 12:20:43 +00:00
yield delay(period)
2003-02-15 14:54:40 +00:00
if rx != 0:
2003-01-30 12:20:43 +00:00
raise StopBitError
2003-01-30 13:57:30 +00:00
2003-01-30 12:20:43 +00:00
actual[8:] = data