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

32 lines
677 B
Python
Raw Normal View History

2003-01-30 12:20:43 +00:00
from __future__ import generators
from myhdl import delay, posedge, intbv, downrange
from rs232_util import sec, parity, ParityError, StopBitError
def rs232_rx(rx, actual, cfg):
2003-01-30 13:57:30 +00:00
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
2003-01-30 12:20:43 +00:00
yield posedge(rx)
2003-01-30 13:57:30 +00:00
2003-01-30 12:20:43 +00:00
yield delay(period / 2)
data[7] = 0
for i in downrange(cfg.n_bits):
yield delay(period)
data[i] = rx.val
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)
if rx.val != parity(data, cfg.parity):
raise ParityError
2003-01-30 13:57:30 +00:00
2003-01-30 12:20:43 +00:00
yield delay(period)
if rx.val != 0:
raise StopBitError
2003-01-30 13:57:30 +00:00
2003-01-30 12:20:43 +00:00
actual[8:] = data