2003-01-30 12:20:43 +00:00
|
|
|
from __future__ import generators
|
|
|
|
import operator
|
|
|
|
|
|
|
|
from myhdl import Signal, downrange, delay, posedge
|
|
|
|
|
2003-01-30 22:17:15 +00:00
|
|
|
from rs232_util import reduceXor, sec, ODD, EVEN, MARK, SPACE
|
2003-01-30 12:20:43 +00:00
|
|
|
|
|
|
|
def rs232_tx(tx, data, cfg):
|
2003-02-03 20:21:46 +00:00
|
|
|
|
|
|
|
""" rs232 transmitter.
|
|
|
|
|
|
|
|
tx -- serial output data
|
2003-02-25 22:55:46 +00:00
|
|
|
data -- input data byte to be transmitted
|
2003-02-03 20:32:36 +00:00
|
|
|
cfg -- rs232_util.Config configuration object
|
2003-02-03 20:21:46 +00:00
|
|
|
|
|
|
|
"""
|
2003-01-30 13:57:30 +00:00
|
|
|
|
2003-01-30 12:20:43 +00:00
|
|
|
duration = delay(int(1*sec / cfg.baud_rate))
|
2003-01-30 13:57:30 +00:00
|
|
|
|
2003-01-30 12:20:43 +00:00
|
|
|
tx.next = 1
|
|
|
|
yield duration
|
2003-01-30 13:57:30 +00:00
|
|
|
|
2003-01-30 12:20:43 +00:00
|
|
|
for i in downrange(cfg.n_bits):
|
|
|
|
tx.next = data[i]
|
|
|
|
yield duration
|
2003-01-30 13:57:30 +00:00
|
|
|
|
2003-01-30 12:20:43 +00:00
|
|
|
if cfg.parity is not None:
|
|
|
|
if cfg.n_bits == 7:
|
|
|
|
data[7] = 0
|
|
|
|
if cfg.parity == ODD:
|
2003-01-30 22:17:15 +00:00
|
|
|
tx.next = not reduceXor(data[8:])
|
2003-01-30 12:20:43 +00:00
|
|
|
elif cfg.parity == EVEN:
|
2003-01-30 22:17:15 +00:00
|
|
|
tx.next = reduceXor(data[8:])
|
2003-01-30 12:20:43 +00:00
|
|
|
elif cfg.parity == MARK:
|
|
|
|
tx.next = 1
|
|
|
|
elif cfg.parity == SPACE:
|
|
|
|
tx.next = 0
|
|
|
|
yield duration
|
2003-01-30 13:57:30 +00:00
|
|
|
|
2003-01-30 12:20:43 +00:00
|
|
|
tx.next = 0
|
|
|
|
for i in range(cfg.n_stops):
|
|
|
|
yield duration
|
|
|
|
|
|
|
|
|