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

40 lines
750 B
Python
Raw Normal View History

2003-01-30 12:20:43 +00:00
import operator
sec = 1e9
ODD, EVEN, MARK, SPACE = range(4)
class Error(Exception):
pass
class ParityError(Error):
pass
class StopBitError(Error):
pass
class Config(object):
__slots__ = ("baud_rate", "n_bits", "n_stops", "parity")
def __init__(self, baud_rate=9600, n_bits=8, n_stops=1, parity=None):
self.baud_rate = baud_rate
self.n_bits = n_bits
self.n_stops = n_stops
self.parity = parity
def parity(data, cfg):
if cfg == ODD:
2003-01-30 22:17:15 +00:00
return not reduceXor(data[8:])
2003-01-30 12:20:43 +00:00
elif cfg== EVEN:
2003-01-30 22:17:15 +00:00
return reduceXor(data[8:])
2003-01-30 12:20:43 +00:00
elif cfg == MARK:
return 1
elif cfg == SPACE:
return 0
2003-01-30 22:17:15 +00:00
def reduceXor(bv):
return reduce(operator.xor, [b for b in bv])
2003-01-30 12:20:43 +00:00