Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python tx and rx with metadata and get_timestamp #932

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions host/libraries/libbladeRF_bindings/python/bladerf/_bladerf.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,38 @@ def from_struct(serial):
def __str__(self):
return self.serial

class MetadataFlags(enum.IntEnum):
BLADERF_META_STATUS_OVERRUN = (1 << 0)
BLADERF_META_STATUS_UNDERRUN = (1 << 1)
BLADERF_META_FLAG_TX_BURST_START = (1 << 0)
BLADERF_META_FLAG_TX_BURST_END = (1 << 1)
BLADERF_META_FLAG_TX_NOW = (1 << 2)
BLADERF_META_FLAG_TX_UPDATE_TIMESTAMP = (1 << 3)
BLADERF_META_FLAG_RX_NOW = (1 << 31)
BLADERF_META_FLAG_RX_HW_UNDERFLOW = (1 << 0)
BLADERF_META_FLAG_RX_HW_MINIEXP1 = (1 << 16)
BLADERF_META_FLAG_RX_HW_MINIEXP2 = (1 << 17)


class Metadata(collections.namedtuple("Metadata", [
"timestamp", "flags", "status", "actual_count"])):
@staticmethod
def from_struct(metadata):
return Metadata(metadata.timestamp,
metadata.flags,
metadata.status,
metadata.actual_count)

def to_struct(self):
return ffi.new("struct bladerf_metadata *", [
ffi.cast("uint64_t", self.timestamp),
ffi.cast("uint32_t", self.flags),
ffi.cast("uint32_t", self.status),
ffi.cast("unsigned int", self.actual_count)
])

struct = property(to_struct)


###############################################################################

Expand Down Expand Up @@ -607,6 +639,12 @@ def enable_module(self, ch, enable):
ret = libbladeRF.bladerf_enable_module(self.dev[0], ch, bool(enable))
_check_error(ret)

def get_timestamp(self, dir: Direction):
timestamp = ffi.new("uint64_t *")
ret = libbladeRF.bladerf_get_timestamp(self.dev[0], dir.value, timestamp)
_check_error(ret)
return timestamp[0]

# Gain

def set_gain(self, ch, gain):
Expand Down Expand Up @@ -862,20 +900,29 @@ def sync_config(self, layout, fmt, num_buffers, buffer_size, num_transfers,
_check_error(ret)

def sync_tx(self, buf, num_samples, timeout_ms=None, meta=ffi.NULL):

if type(meta) == Metadata:
meta_struct = meta.struct
else:
meta_struct = ffi.cast("struct bladerf_metadata *", meta)

ret = libbladeRF.bladerf_sync_tx(self.dev[0],
ffi.from_buffer(buf),
num_samples,
ffi.cast("struct bladerf_metadata *", meta),
meta_struct,
timeout_ms or 0)
_check_error(ret)

def sync_rx(self, buf, num_samples, timeout_ms=None, meta=ffi.NULL):
meta_struct = meta.struct if type(meta) == Metadata else meta
ret = libbladeRF.bladerf_sync_rx(self.dev[0],
ffi.from_buffer(buf),
num_samples,
meta,
meta_struct,
timeout_ms or 0)
_check_error(ret)
meta = Metadata.from_struct(meta_struct[0]) if type(meta) == Metadata else meta_struct
return meta

# FPGA/Firmware Loading/Flashing

Expand Down