(This is the 8-bit version; for the 32-bit version, please switch to the corresponding branch.)
CDBUS is a simple protocol designed for serial ports and serial bus communication, commonly employed in RS-485 buses. It utilizes hardware arbitration and other mechanisms to avoid data conflicts, allowing nodes to freely transmit and receive data packets, thereby overcoming the limitations of single-master polling.
CDBUS has a 3-byte header: [src_addr, dst_addr, data_len], followed by user data, and concluding with a 2-byte CRC (identical to MODBUS CRC).
The CDBUS protocol at the byte level can be directly used for traditional serial communication, such as traditional UART, RS-232, RS-485, and USB virtual serial ports.
The full CDBUS protocol at the bit level requires dedicated hardware controllers (or software emulation) to achieve conflict avoidance, higher speeds, and strong real-time performance.
- Introduces an arbitration mechanism, similar to the CAN bus, to automatically avoid conflicts.
- Supports dual baud rates for high-speed communication;
the maximum baud rate of the high-speed phase is
sysclk÷ 3 (e.g. 50 Mbps for 150 MHzsysclk). - Supports unicast, multicast and broadcast.
- The maximum user data size is 253 bytes.
- Hardware packing, unpacking, verification and filtering, reducing CPU load and development effort.
- Compatible with traditional RS-485 hardware (the arbitration function remains effective).
The protocol timing example, consisting of only one byte of user data:
(You can configure the lengths of idle time and transmission permit time.)
Tips:
- When a high-priority node needs to send unimportant data, its transmission permit time (TX_PERMIT_LEN) can be dynamically increased.
Arbitration example:
Example waveforms for TX output and TX_EN pins:
The RX data sampling point is at 1/2 bit; for TX readback, it's at 3/4 bit.
Lower-priority nodes back off during arbitration and retransmit after the bus is idle, until successful or cancelled by the user.
In CDBUS-A mode, if the low-speed portion takes too long, it can become a bottleneck for communication efficiency.
To address this, single-rate peer-to-peer bus communication can be achieved using CDBUS-BS mode:
- Different nodes have different configurations for the TX_PERMIT_LEN parameter, requiring sufficiently large differences to avoid conflicts.
- If any node has pending data frames waiting to be sent before the transmission permit point, transmission starts from that point.
- Otherwise, wait until the idle time exceeds MAX_IDLE_LEN. When there are pending data frames waiting for transmission, first send a break character to bring the bus out of the idle state.
The CDBUS-BS mode is suitable for high-speed applications with few nodes, and is also well-suited to software implementation.
For protocols other than CDBUS over a plain UART (8N1 only), e.g. MODBUS RTU,
the controller can pass raw frames through without header;
it works with the traditional half-duplex mode and the full-duplex mode, set DIV_HS equal to DIV_LS and keep FILTER at 0xff.
The CRC is left to the host, as with CRC maintained by user: the hardware neither appends nor checks it.
RX: bytes are collected into pages of up to 255 bytes. A full page is committed at once and the frame goes on in the next page; the frame ends with a shorter page when the bus is idle for IDLE_WAIT_LEN, an empty page if the frame ends with a full one. RX_LEN is the byte count minus one, 0xff for an empty page. A single byte is dropped as noise, unless it follows a full page. In half-duplex mode, the echo of the own transmission is dropped unless the loopback bit is set.
TX: the page layout is the same as a CDBUS frame, [reserved, reserved, data_len, data...], data_len + 2 bytes are sent (2 to 255):
the bytes beyond the end of the page wrap around to the reserved bytes at the start.
A page that is ready when the previous one is sent follows it in the same frame, after up to 3 extra stop bits;
to send separate frames, write the next page only after bit5 of INT_FLAG is set.
Otherwise the idle time between frames is IDLE_WAIT_LEN (half-duplex, if RX sees TX) plus TX_PERMIT_LEN plus the TX_EN pre-assert time,
which must be longer than the IDLE_WAIT_LEN of the receiver.
| Register Name | Addr | Access | Default | Description (8-bit width by default if not specified) |
|---|---|---|---|---|
| VERSION | 0x00 | RD | 0x0f | Hardware version |
| SETTING | 0x02 | RD/WR | 0x01 | Configuration |
| PIN_CFG | 0x03 | RD/WR | 0x00 | Pin configuration |
| IDLE_WAIT_LEN | 0x04 | RD/WR | 0x0a | Idle entry wait time |
| TX_PERMIT_LEN_L | 0x05 | RD/WR | 0x14 | TX permit wait time (10 bits) |
| TX_PERMIT_LEN_H | 0x06 | RD/WR | 0x00 | |
| MAX_IDLE_LEN_L | 0x07 | RD/WR | 0xc8 | Max idle wait time, BS mode (10 bits) |
| MAX_IDLE_LEN_H | 0x08 | RD/WR | 0x00 | |
| TX_CFG | 0x09 | RD/WR | 0x01 | TX_EN pre-assert time and break character length |
| FILTER | 0x0b | RD/WR | 0xff | Local address |
| DIV_LS_L | 0x0c | RD/WR | 0x5a | Low-speed rate setting (16 bits) |
| DIV_LS_H | 0x0d | RD/WR | 0x01 | |
| DIV_HS_L | 0x0e | RD/WR | 0x5a | High-speed rate setting (16 bits) |
| DIV_HS_H | 0x0f | RD/WR | 0x01 | |
| INT_MASK_L | 0x10 | RD/WR | 0x00 | Interrupt mask (16 bits) |
| INT_MASK_H | 0x11 | RD/WR | 0x00 | |
| INT_FLAG_L | 0x12 | RD/WR | n/a | Status (16 bits) |
| INT_FLAG_H | 0x13 | RD | n/a | |
| RX_LEN | 0x14 | RD | n/a | Data length of the frame to be read, or frame_len - 1 |
| DAT | 0x15 | RD/WR | n/a | Read & Write RX page |
| CTRL | 0x16 | WR | n/a | RX & TX control |
| DAT_HOLD | 0x17 | RD/WR | n/a | Same as DAT, but keeps the page open |
| RX_ADDR | 0x18 | WR | n/a | Set the read position of the RX page |
| FILTER_M0 | 0x1a | RD/WR | 0xff | Multicast filter0 |
| FILTER_M1 | 0x1b | RD/WR | 0xff | Multicast filter1 |
| FILTER_MSK0 | 0x1c | RD/WR | 0xff | Multicast mask0 |
| FILTER_MSK1 | 0x1d | RD/WR | 0xff | Multicast mask1 |
To write a register with more than 8 bits, first write the high byte followed by the low byte.
Time unit: one bit period at the low-speed baud rate.
SETTING:
| FIELD | DESCRIPTION |
|---|---|
| [5] | Save broken frame |
| [4] | CRC maintained by user |
| [3] | Transparent mode: keep own TX echo (loopback) |
| [2] | Transparent mode |
| [1:0] | Mode selection |
| [1] | [0] | DESCRIPTION |
|---|---|---|
| 0 | 0 | Traditional half-duplex mode |
| 0 | 1 | CDBUS-A mode (default) |
| 1 | 0 | CDBUS-BS mode |
| 1 | 1 | Full-duplex mode |
Transparent mode only works with mode 00 and 11.
PIN_CFG:
| FIELD | DESCRIPTION |
|---|---|
| [3] | RX pin inversion |
| [2] | TX pin inversion |
| [1] | TX stays low for arbitration and break character |
| [0] | Enable push-pull output for TX and TX_EN pin |
TX_CFG:
| FIELD | DESCRIPTION |
|---|---|
| [7:4] | Extra length of the break character: 10 + n bits |
| [2:0] | TX_EN pre-assert time, in bits |
Example waveforms for TX output and TX_EN pins (pre-assert time = 1 bit):
The pre-assert time is not used in arbitration mode, nor for break characters in BS mode: there TX_EN is asserted that time later, together with the break (with bit1 of PIN_CFG, TX is low from the start of that time).
The extra length applies to all break characters sent, the receiver accepts any break of 10 bits or longer.
FILTERS:
Addresses other than 255 may be used as multicast addresses.
DIV_xx_x:
Baud rate divider value: DIV_xx[15:0] = sysclk ÷ baud_rate − 1
The minimum value is 2.
In arbitration mode, the low-speed rate must not be too high: allow for the delays of the devices and of the transmission line.
For single-rate operation, set DIV_HS to the same value as DIV_LS.
INT_MASK:
Output of irq = ((INT_FLAG & INT_MASK) != 0).
INT_FLAG:
| FIELD | DESCRIPTION |
|---|---|
| [15] | 1: Bus not idle |
| [14] | 1: Bus idle |
| [13:8] | Number of RX pages to read |
| [7] | 1: TX error (TX low, RX sampled high) |
| [6] | 1: TX collision detected |
| [5] | 1: TX page released by hardware |
| [4] | 1: Empty TX page available |
| [3] | 1: RX error (frame broken) |
| [2] | 1: RX lost (no empty RX page) |
| [1] | 1: Break character received |
| [0] | 1: RX page ready to read |
Reading this register will automatically clear bit7, bit6, bit3, bit2 and bit1. Writing 1 to these bits of INT_FLAG_L clears them as well.
While bit4 is 0, writes to DAT and DAT_HOLD are dropped: start writing a frame only after it is 1.
If save broken frame is set, bit3 indicates whether the current page to be read is broken
(reading this register does not clear bit3).
The [13:8] field wraps around at 64: if bit0 is 1 while [13:8] reads 0, there are 64 RX pages waiting to be read.
RX_LEN:
The default value of this register corresponds to the data_len of the frame to be read.
If save broken frame is enabled, this register's value equals the size of the frame to be read minus one (including CRC).
In transparent mode, it is the size of the page minus one, 0xff for an empty page.
For interfaces like SPI, when two or three bytes are read from the INT_FLAG_L register in a single transfer, the first byte represents INT_FLAG_L, the second byte represents RX_LEN, and the third byte represents INT_FLAG_H.
CTRL:
| FIELD | DESCRIPTION |
|---|---|
| [7] | Reset RX block |
| [4] | Release current RX page |
| [3] | Abort transmission and clear current TX page |
| [2] | Clear pending TX page |
| [1] | Send break character |
| [0] | Submit TX page |
For interfaces like SPI, the RX page is automatically released after a transfer that reads the DAT register, while the TX page is automatically submitted after a transfer that writes to the DAT register.
DAT_HOLD:
An alias of DAT for interfaces like SPI: it reads and writes the same page at the same position, but the page is not released or submitted when the transfer ends, and the position is kept for the next transfer. This allows one page to be accessed across multiple transfers: access the first parts through DAT_HOLD, then either access the last part through DAT (automatic finish), or write bit4 or bit0 of the CTRL register (manual finish). Transfers to other registers in between do not affect the kept positi 923C on.
RX_ADDR:
Writing this register sets the byte offset for the next DAT or DAT_HOLD read within the current RX page, to skip the unwanted part of a frame. For interfaces like SPI, the page is kept open after the transfer that writes this register, so the following read starts from the set position.
parameter DIV_LS = 346, // default: 115200 bps for 40MHz clk
parameter DIV_HS = 346
input clk, // core clock
input reset_n, // async active-low reset
input chip_select,
output irq, // interrupt output
// only with `CD_CSR_CRC`, for host interfaces with CRC
input csr_crc_en, // CRC mode: a read has no side effect
input csr_cancel, // do not submit the TX page of this transfer
// read data is valid in the clock after csr_read, one byte per clock in a burst
input [4:0] csr_address,
input csr_read,
output [7:0] csr_readdata,
input csr_write,
input [7:0] csr_writedata,
// connect to external PHY chip, e.g. MAX3485
input rx,
output tx,
output tx_enchip_select:
Besides the transfer-based behaviour described above for interfaces like SPI (enabled by CD_CHIP_SELECT),
chip_select gates the read port of the RX RAM to reduce power consumption:
the RX RAM is only read while it is high, so it must be high in the clock of a DAT read.
For SoC integration, drive it from the bus select of this peripheral, e.g. psel of APB or hsel of AHB,
or simply tie it high at the cost of the RX RAM being read every clock.
csr_crc_en:
For host interfaces which protect every transfer by a CRC. While it is high, a read neither clears nor releases anything, so a transfer disturbed on the way can be repeated:
- Reading INT_FLAG_L does not clear any bit: write 1 to the bits to be cleared.
- Reading DAT does not release the RX page: release it by bit4 of CTRL. To read again after a read of DAT_HOLD, write RX_ADDR first.
The SPI, QSPI and I2C interfaces of the examples drive it from a chip-level register, add a length and a CRC to every transfer, and let a write take effect only if its CRC is correct, see example/Readme.md.
csr_cancel:
Sampled at the end of a transfer, in the clock after chip_select goes low.
If it is high, the TX page written through DAT in this transfer is not submitted.
Read timing:
A read is taken at the clock edge where csr_read is high, and the data is valid in the clock after it,
as on a synchronous bus with one clock of read latency; csr_address must be kept until then.
csr_read is sampled every clock: each clock it is high is one read, so holding it high for N clocks
is a burst of N reads, one byte per clock. Since reading DAT advances the address and reading INT_FLAG_L
clears the flags, a host that stretches one access over several clocks (e.g. wait states) must assert
csr_read in only one of them.
RX release:
A release of the RX page (bit4 of CTRL, or the end of a transfer that read DAT) takes the size of the page
from an index RAM, which is read in the clock after the page became the current one.
Release a page only when bit0 of INT_FLAG is set for it, and keep two releases at least 2 clocks apart
(3 with CD_SPRAM_ONLY). Otherwise the pages still waiting are read from a wrong position,
until all of them are released.
Single-port RAM:
With CD_SPRAM_ONLY the buffers use single-port RAM (cd_spram): a read that meets a write is done one clock later,
so DAT must not be read in two consecutive clocks.
The RAM has to keep its read data during a write; if it does not, define CD_SPRAM_NO_HOLD as well.
# Configuration
write(REG_PIN_CFG, [0x01]) # Enable push-pull output
# TX
while (read(REG_INT_FLAG_L) & 0x10) == 0: # Ensure free TX page is available
pass
write(REG_DAT, [0x0c, 0x0d, 0x01, 0xcd]) # Write frame without CRC
# RX
while True: # Wait until RX page is ready
val = read(REG_INT_FLAG_L, len=2)
if (val[0] & 0x01) != 0:
break
data = read(REG_DAT, len=val[1]+3)Install iverilog (>= v10) and cocotb, go to the tests/ directory, and run ./test_all.sh or ./test_all.sh test_xxx.py.
(You can view the waveform cdbus.vcd with GTKWave.)
The CDCTL controller family incorporates the CDBUS IP Core, providing peripheral interfaces such as SPI, I2C, and PCIe.
For example, the tiny CDCTL-Bx module supports SPI and I2C interfaces:
(The source code and gerber files for this module are fully open-source and located in the example/ directory.)
For a list of CDBUS-related ASIC chips, please refer to the wiki page.
Other related projects that may interest you:
- CDBUS GUI: https://github.com/dukelec/cdbus_gui
- CDNET: https://github.com/dukelec/cdnet
This Source Code Form is subject to the terms of the CERN Open
Hardware Licence Version 2 - Strongly Reciprocal (CERN-OHL-S v2):
https://ohwr.org/cern_ohl_s_v2.txt (see the LICENSE file).
Notice: The CDBUS Exception (see the LICENSE_EXCEPTION file)
grants free commercial use in FPGAs and other programmable logic
devices; it does not extend to ASIC design or manufacturing.
Copyright (c) 2017-2026 DUKELEC, All rights reserved.