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

147 lines
4.0 KiB
Python
Raw Normal View History

2003-01-30 12:20:43 +00:00
import sys
from random import randrange
import unittest
from unittest import TestCase
from rs232_rx import rs232_rx
from myhdl import Simulation, Signal, intbv, join
from rs232_tx import rs232_tx
from rs232_util import Config, EVEN, ODD, ParityError, Error
class rs232Test(TestCase):
2003-02-03 19:38:37 +00:00
""" rs232 functional unit test """
2003-01-30 12:20:43 +00:00
def default(self):
tx = Signal(intbv(0))
rx = tx
actual = intbv(0)
cfg = Config()
for i in range(256):
data = intbv(i)
yield join(rs232_tx(tx, data, cfg), rs232_rx(rx, actual, cfg))
self.assertEqual(data, actual)
def testDefault(self):
2003-01-30 22:17:15 +00:00
""" Check default case """
2003-01-30 13:57:30 +00:00
Simulation(self.default()).run(quiet=1)
2003-01-30 12:20:43 +00:00
def oddParity(self):
tx = Signal(intbv(0))
rx = tx
actual = intbv(0)
cfg = Config(parity=ODD)
for i in range(256):
data = intbv(i)
yield join(rs232_tx(tx, data, cfg), rs232_rx(rx, actual, cfg))
self.assertEqual(data, actual)
def testOddParity(self):
2003-01-30 22:17:15 +00:00
""" Check odd parity """
2003-01-30 13:57:30 +00:00
Simulation(self.oddParity()).run(quiet=1)
2003-01-30 12:20:43 +00:00
2003-01-30 13:57:30 +00:00
def sevenBitsEvenParity(self):
tx = Signal(intbv(0))
rx = tx
actual = intbv(0)
cfg = Config(parity=EVEN, n_bits=7)
cfg_rx = Config(parity=EVEN, n_bits=7)
for i in range(256):
data = intbv(i)
yield join(rs232_tx(tx, data, cfg), rs232_rx(rx, actual, cfg_rx))
self.assertEqual(data, actual)
def testSevenBitsEvenParity(self):
2003-01-30 22:17:15 +00:00
""" Check 7 bits with even parity """
2003-01-30 13:57:30 +00:00
Simulation(self.sevenBitsEvenParity()).run(quiet=1)
2003-01-30 12:20:43 +00:00
def ParityError(self):
tx = Signal(intbv(0))
rx = tx
actual = intbv(0)
cfg_rx = Config(parity=ODD)
cfg_tx = Config(parity=EVEN)
data = intbv(randrange(256))
yield join(rs232_tx(tx, data, cfg_tx), rs232_rx(rx, actual, cfg_rx))
def testParityError(self):
2003-01-30 22:17:15 +00:00
""" Expect a parity error """
2003-01-30 12:20:43 +00:00
try:
2003-01-30 13:57:30 +00:00
Simulation(self.ParityError()).run(quiet=1)
2003-01-30 12:20:43 +00:00
except ParityError:
pass
else:
self.fail("Expected parity error")
class rs232Characterize(TestCase):
2003-02-03 19:38:37 +00:00
""" rs232 baud rate characterization test """
2003-01-30 12:20:43 +00:00
def bench(self, tx_baud_rate):
tx = Signal(intbv(0))
rx = tx
actual = intbv(0)
cfg_tx = Config(baud_rate=tx_baud_rate)
cfg_rx = Config()
for i in range(256):
data = intbv(i)
yield join(rs232_tx(tx, data, cfg_tx), rs232_rx(rx, actual, cfg_rx))
if not data == actual:
raise Error
def testCharacterize(self):
2003-02-03 19:38:37 +00:00
""" Find min/max tx baud rate tolerance by simulation """
2003-01-30 13:57:30 +00:00
coarseOffset = 100
fineOffset = 5
tx_baud_rate = 9600
2003-01-30 12:20:43 +00:00
try:
while 1:
2003-01-30 13:57:30 +00:00
tx_baud_rate += coarseOffset
2003-01-30 12:20:43 +00:00
Simulation(self.bench(tx_baud_rate)).run(quiet=1)
except Error:
2003-01-30 13:57:30 +00:00
pass
while 1:
try:
tx_baud_rate -= fineOffset
Simulation(self.bench(tx_baud_rate)).run(quiet=1)
except Error:
continue
else:
print "Max tx baudrate: %s" % tx_baud_rate
break
tx_baud_rate = 9600
2003-01-30 12:20:43 +00:00
try:
while 1:
2003-01-30 13:57:30 +00:00
tx_baud_rate -= coarseOffset
2003-01-30 12:20:43 +00:00
Simulation(self.bench(tx_baud_rate)).run(quiet=1)
except Error:
2003-01-30 13:57:30 +00:00
pass
while 1:
try:
tx_baud_rate += fineOffset
Simulation(self.bench(tx_baud_rate)).run(quiet=1)
except Error:
continue
else:
print "Min tx baudrate: %s" % tx_baud_rate
break
2003-01-30 12:20:43 +00:00
if __name__ == "__main__":
2003-01-30 13:57:30 +00:00
testRunner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=testRunner)
2003-01-30 12:20:43 +00:00