Browse Source

Callback draft

pull/1/head
Eray Arslan 2 years ago
parent
commit
351a059961
  1. 72
      bladerf.go
  2. 66
      bladerf_test.go

72
bladerf.go

@ -4,6 +4,8 @@ package bladerf @@ -4,6 +4,8 @@ package bladerf
// #cgo darwin LDFLAGS: -L/usr/local/lib
// #cgo LDFLAGS: -lbladeRF
// #include <libbladeRF.h>
//
// extern void* cbGo(struct bladerf *dev, struct bladerf_stream *stream, struct bladerf_metadata *md, void* samples, size_t num_samples, void* user_data);
import "C"
import (
error2 "bladerf/error"
@ -11,6 +13,20 @@ import ( @@ -11,6 +13,20 @@ import (
"unsafe"
)
//export cbGo
func cbGo(dev *C.struct_bladerf,
stream *C.struct_bladerf_stream,
metadata *C.struct_bladerf_metadata,
samples unsafe.Pointer,
numSamples C.size_t,
userData unsafe.Pointer) unsafe.Pointer {
fmt.Println("HELLO")
var rv unsafe.Pointer
return rv
}
type GainMode int
type Backend int
type Direction int
@ -22,6 +38,12 @@ type RXMux int @@ -22,6 +38,12 @@ type RXMux int
type ClockSelect int
type PowerSource int
type PMICRegister int
type IOModule int
const (
IOTX IOModule = C.BLADERF_MODULE_TX
IORX IOModule = C.BLADERF_MODULE_RX
)
const (
Default GainMode = C.BLADERF_GAIN_DEFAULT
@ -137,6 +159,14 @@ type BladeRF struct { @@ -137,6 +159,14 @@ type BladeRF struct {
bladeRF *C.struct_bladerf
}
type Module struct {
module *C.struct_bladerf_module
}
type Stream struct {
stream *C.struct_bladerf_stream
}
func GetVersion() Version {
var version C.struct_bladerf_version
C.bladerf_version(&version)
@ -252,3 +282,45 @@ func Open() BladeRF { @@ -252,3 +282,45 @@ func Open() BladeRF {
func Close(bladeRF BladeRF) {
C.bladerf_close(bladeRF.bladeRF)
}
func SetLoopback(bladeRF *BladeRF, loopback Loopback) {
C.bladerf_set_loopback((*bladeRF).bladeRF, C.bladerf_loopback(loopback))
}
func SetFrequency(bladeRF *BladeRF, module IOModule, frequency int) {
err := GetError(C.bladerf_set_frequency((*bladeRF).bladeRF, C.bladerf_module(module), C.ulonglong(frequency)))
fmt.Println(err)
}
func SetSampleRate(bladeRF *BladeRF, module IOModule, sampleRate int) {
C.bladerf_set_sample_rate((*bladeRF).bladeRF, C.bladerf_module(module), C.uint(sampleRate), nil)
}
func SetBandwidth(bladeRF *BladeRF, module IOModule, bandwidth int) {
C.bladerf_set_bandwidth((*bladeRF).bladeRF, C.bladerf_module(module), C.uint(bandwidth), nil)
}
func SetGain(bladeRF *BladeRF, module IOModule, gain int) {
C.bladerf_set_gain((*bladeRF).bladeRF, C.bladerf_module(module), C.int(gain))
}
func EnableModule(bladeRF *BladeRF, module IOModule) {
C.bladerf_enable_module((*bladeRF).bladeRF, C.bladerf_module(module), true)
}
func DisableModule(bladeRF *BladeRF, module IOModule) {
C.bladerf_enable_module((*bladeRF).bladeRF, C.bladerf_module(module), false)
}
func InitStream(bladeRF *BladeRF, format Format, numBuffers int, samplesPerBuffer int, numTransfers int) *Stream {
var buffers *unsafe.Pointer
var data unsafe.Pointer
var rxStream *C.struct_bladerf_stream
stream := Stream{stream: rxStream}
C.bladerf_init_stream(&((stream).stream), (*bladeRF).bladeRF, (*[0]byte)((C.cbGo)), &buffers, C.ulong(numBuffers), C.bladerf_format(format), C.ulong(samplesPerBuffer), C.ulong(numTransfers), data)
return &stream
}
func StartStream(stream *Stream, module IOModule) {
C.bladerf_stream(stream.stream, C.bladerf_channel_layout(module))
}

66
bladerf_test.go

@ -21,4 +21,70 @@ func TestBladeRF(t *testing.T) { @@ -21,4 +21,70 @@ func TestBladeRF(t *testing.T) {
bootloaders := GetBootloaderList()
fmt.Printf("Bootloaders Len: %d\n", len(bootloaders))
rf := Open()
stream := InitStream(&rf, SC16_Q11, 2, 1024, 1)
StartStream(stream, IOTX)
Close(rf)
}
func TestStream(t *testing.T) {
log.SetVerbosity(log.Debug)
f_low := 250000000
f_high := 700000000
f_step := 1000000
freq := f_low
tx_gain := 56
rx_gain := 3
num_buffers := 24
samples_per_buffer := 8192
num_transfers := 8
sample_rate := 4000000
bandwidth := 1500000
n_steps := 1 + (f_high-f_low)/f_step
devices := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf := OpenWithDevInfo(devices[0])
defer Close(rf)
SetLoopback(&rf, Disabled)
SetFrequency(&rf, IOTX, freq)
SetFrequency(&rf, IORX, freq)
SetSampleRate(&rf, IOTX, sample_rate)
SetSampleRate(&rf, IORX, sample_rate)
SetBandwidth(&rf, IOTX, bandwidth)
SetBandwidth(&rf, IORX, bandwidth)
SetGain(&rf, IOTX, tx_gain)
SetGain(&rf, IORX, rx_gain)
EnableModule(&rf, IOTX)
EnableModule(&rf, IORX)
for i := 0; i < n_steps; i++ {
//tx_samples_left := num_buffers * samples_per_buffer
//rx_samples_left := num_buffers * samples_per_buffer
tx_stream := InitStream(&rf, SC16_Q11, num_buffers, samples_per_buffer, num_transfers)
rx_stream := InitStream(&rf, SC16_Q11, num_buffers, samples_per_buffer, num_transfers)
StartStream(tx_stream, IOTX)
StartStream(rx_stream, IORX)
}
}

Loading…
Cancel
Save