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
2005-12-27 14:45:38 +00:00

38 lines
775 B
Python

from myhdl import delay, intbv, downrange
from rs232_util import sec, parity, ParityError, StopBitError
def rs232_rx(rx, actual, cfg):
""" rs232 receiver.
rx -- serial input data
actual -- data actually received
cfg -- rs232_util.Config configuration object
"""
data = intbv(0)
period = int(1*sec / cfg.baud_rate)
yield rx.posedge
yield delay(period // 2)
data[7] = 0
for i in downrange(cfg.n_bits):
yield delay(period)
data[i] = rx
if cfg.parity is not None:
yield delay(period)
if rx != parity(data, cfg.parity):
raise ParityError
yield delay(period)
if rx != 0:
raise StopBitError
actual[8:] = data