1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00
myhdl/example/rs232/rs232_tx.py
2003-02-03 20:32:36 +00:00

45 lines
1010 B
Python

from __future__ import generators
import operator
from myhdl import Signal, downrange, delay, posedge
from rs232_util import reduceXor, sec, ODD, EVEN, MARK, SPACE
def rs232_tx(tx, data, cfg):
""" rs232 transmitter.
tx -- serial output data
data -- input data to be transmitted
cfg -- rs232_util.Config configuration object
"""
duration = delay(int(1*sec / cfg.baud_rate))
tx.next = 1
yield duration
for i in downrange(cfg.n_bits):
tx.next = data[i]
yield duration
if cfg.parity is not None:
if cfg.n_bits == 7:
data[7] = 0
if cfg.parity == ODD:
tx.next = not reduceXor(data[8:])
elif cfg.parity == EVEN:
tx.next = reduceXor(data[8:])
elif cfg.parity == MARK:
tx.next = 1
elif cfg.parity == SPACE:
tx.next = 0
yield duration
tx.next = 0
for i in range(cfg.n_stops):
yield duration