Skip to content

cameronweston/cocotbext-syncserial

Repository files navigation

HDLC encoded Synchronous Serial interface modules for Cocotb

Build Status codecov PyPI version

Introduction

Synchronous HDLC simulation models for cocotb.

Installation

Installation from respository:

$ git clone https://github.com/cameronweston/cocotbext-syncserial.git
$ pip install cocotbext-syncserial

Documentation and usage examples

See the tests directory for a testbench using this module.

Synchronous Serial

The SyncSerialSource and SyncSerialSink classes can be used to drive, receive, and monitor HDLC encoded synchronous serial data.

To use these modules, import the module you need and connect it to the DUT.

from cocotbext.syncserial import SyncSerialSource, SyncSerialSink

sync_serial_source = SyncSerialSource(dut.data, dut.clk)

sync_serial_sink = SyncSerialSink(dut.data, dut.clk)

To send data with SyncSerialSource, call write() or write_nowait(). Accepted data types are iterables of 8-bit ints, including lists, bytes, bytearrays, etc.

To receive data with SyncSerialSink, call read() or read_nowait(). read() will block until at least 1 packet is available.

Constructor parameters:

  • data: data signal
  • clock: clock signal
  • clock_rate_mhz: clock rate in megahertz (optional, default = 10)
  • append_crc: appends crc to packet (optional, default = True)
  • crc_polynomial: crc polynomial to use (optional, default = 0x1021)
  • crc_init: crc initial value to use (optional, default = 0xFFFF)
  • crc_final_xor: crc final xor to use (optional, default = 0x0000)
  • validate_crc: validates incoming packet's crc (optional, default = True)
  • strip_crc: Strips off crc after validation on incoming packet (optional, default = True)

Methods

  • write(): send packet of data (blocking) (source)
  • write_nowait(): send packet of data (non-blocking) (source)
  • read(): read one packet of data (blocking) (sink)
  • read_nowait(): read one packet of data (non-blocking) (sink)
  • count(): returns the number of packets in the queue (all)
  • empty(): returns True if the queue is empty (all)
  • clear(): drop all data from the queue (all)
  • wait(timeout=0, timeout_unit='ns'): wait for packet received (sink)

CRC-16

The Crc_16 classes can be used to calculate or validate a 16 bit CRC. To use this module: from cocotbext.syncserial import Crc_16

crc = Crc_16(crc_polynomial, crc_init, crc_final_xor)

To calculate a CRC, call calculate_crc(). Accepted data types are iterables of 8-bit ints, including lists, bytes, bytearrays, etc.

To validate a CRC, call validate_crc(). Accepted data types are iterables of 8-bit ints, including lists, bytes, bytearrays, etc.

Constructor parameters:

  • crc_polynomial: CRC polynomial to use (optional, default = 0x1021)
  • crc_init: CRC initial value to use (optional, default = 0xFFFF)
  • crc_final_xor: CRC final XOR value to use (optional, default = 0x0000)

Methods

  • validate_crc(data, crc): Calculates a CRC for data and compared to crc
  • calculate_crc(data): Calucalates and returns a CRC for data