Skip to content

Commit 92c865a

Browse files
committed
libhackrf: add 'hackrf_radio_set_frequency_fp()' and 'hackrf_radio_set_sample_rate_fp()'
1 parent ebf8628 commit 92c865a

6 files changed

Lines changed: 167 additions & 0 deletions

File tree

firmware/hackrf_usb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ set(SRC_M4
5656
usb_endpoint.c
5757
usb_api_board_info.c
5858
usb_api_m0_state.c
59+
usb_api_radio.c
5960
usb_api_register.c
6061
usb_api_spiflash.c
6162
usb_api_transceiver.c

firmware/hackrf_usb/hackrf_usb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ static usb_request_handler_fn vendor_request_handler[] = {
185185
usb_vendor_request_get_buffer_size,
186186
usb_vendor_request_lock_radio_reg,
187187
usb_vendor_request_radio_set_config_mode,
188+
usb_vendor_request_set_radio_frequency_fp,
189+
usb_vendor_request_set_radio_sample_rate_fp,
188190
};
189191

190192
static const uint32_t vendor_request_handler_count =

firmware/hackrf_usb/usb_api_radio.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,60 @@ usb_request_status_t usb_vendor_request_radio_set_config_mode(
3939

4040
return USB_REQUEST_STATUS_OK;
4141
}
42+
43+
usb_request_status_t usb_vendor_request_set_radio_frequency_fp(
44+
usb_endpoint_t* const endpoint,
45+
const usb_transfer_stage_t stage)
46+
{
47+
static fp_40_24_t hz_param;
48+
49+
if (stage == USB_TRANSFER_STAGE_SETUP) {
50+
usb_transfer_schedule_block(
51+
endpoint->out,
52+
&hz_param,
53+
sizeof(fp_40_24_t),
54+
NULL,
55+
NULL);
56+
} else if (stage == USB_TRANSFER_STAGE_DATA) {
57+
radio_reg_write(&radio, RADIO_BANK_ACTIVE, RADIO_FREQUENCY_RF, hz_param);
58+
radio_reg_write(
59+
&radio,
60+
RADIO_BANK_ACTIVE,
61+
RADIO_FREQUENCY_IF,
62+
RADIO_UNSET);
63+
radio_reg_write(
64+
&radio,
65+
RADIO_BANK_ACTIVE,
66+
RADIO_FREQUENCY_LO,
67+
RADIO_UNSET);
68+
radio_reg_write(
69+
&radio,
70+
RADIO_BANK_ACTIVE,
71+
RADIO_IMAGE_REJECT,
72+
RADIO_UNSET);
73+
usb_transfer_schedule_ack(endpoint->in);
74+
}
75+
76+
return USB_REQUEST_STATUS_OK;
77+
}
78+
79+
usb_request_status_t usb_vendor_request_set_radio_sample_rate_fp(
80+
usb_endpoint_t* const endpoint,
81+
const usb_transfer_stage_t stage)
82+
{
83+
static fp_28_36_t sps_param;
84+
85+
if (stage == USB_TRANSFER_STAGE_SETUP) {
86+
usb_transfer_schedule_block(
87+
endpoint->out,
88+
&sps_param,
89+
sizeof(fp_28_36_t),
90+
NULL,
91+
NULL);
92+
} else if (stage == USB_TRANSFER_STAGE_DATA) {
93+
radio_reg_write(&radio, RADIO_BANK_ACTIVE, RADIO_SAMPLE_RATE, sps_param);
94+
usb_transfer_schedule_ack(endpoint->in);
95+
}
96+
97+
return USB_REQUEST_STATUS_OK;
98+
}

firmware/hackrf_usb/usb_api_radio.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@
2727
usb_request_status_t usb_vendor_request_radio_set_config_mode(
2828
usb_endpoint_t* const endpoint,
2929
const usb_transfer_stage_t stage);
30+
31+
usb_request_status_t usb_vendor_request_set_radio_frequency_fp(
32+
usb_endpoint_t* const endpoint,
33+
const usb_transfer_stage_t stage);
34+
35+
usb_request_status_t usb_vendor_request_set_radio_sample_rate_fp(
36+
usb_endpoint_t* const endpoint,
37+
const usb_transfer_stage_t stage);

host/libhackrf/src/hackrf.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ typedef enum {
122122
HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE = 61,
123123
HACKRF_VENDOR_REQUEST_RADIO_LOCK_REG = 62,
124124
HACKRF_VENDOR_REQUEST_RADIO_SET_CONFIG_MODE = 63,
125+
HACKRF_VENDOR_REQUEST_RADIO_SET_FREQUENCY_FP = 64,
126+
HACKRF_VENDOR_REQUEST_RADIO_SET_SAMPLE_RATE_FP = 65,
125127
} hackrf_vendor_request;
126128

127129
#define USB_CONFIG_STANDARD 0x1
@@ -1844,6 +1846,36 @@ int ADDCALL hackrf_set_freq_explicit(
18441846
}
18451847
}
18461848

1849+
int ADDCALL hackrf_radio_set_frequency_fp(hackrf_device* device, const fp_40_24_t freq_hz)
1850+
{
1851+
USB_API_REQUIRED(device, 0x0113);
1852+
uint64_t set_freq_fp_param;
1853+
uint8_t length;
1854+
int result;
1855+
1856+
// serialize parameters
1857+
set_freq_fp_param = TO_LE64(freq_hz);
1858+
length = sizeof(uint64_t);
1859+
1860+
result = libusb_control_transfer(
1861+
device->usb_device,
1862+
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR |
1863+
LIBUSB_RECIPIENT_DEVICE,
1864+
HACKRF_VENDOR_REQUEST_RADIO_SET_FREQUENCY_FP,
1865+
0,
1866+
0,
1867+
(unsigned char*) &set_freq_fp_param,
1868+
length,
1869+
DEFAULT_REQUEST_TIMEOUT);
1870+
1871+
if (result < length) {
1872+
last_libusb_error = result;
1873+
return HACKRF_ERROR_LIBUSB;
1874+
}
1875+
1876+
return HACKRF_SUCCESS;
1877+
}
1878+
18471879
typedef struct {
18481880
uint32_t freq_hz;
18491881
uint32_t divider;
@@ -1936,6 +1968,37 @@ int ADDCALL hackrf_set_sample_rate(hackrf_device* device, const double freq)
19361968
return hackrf_set_sample_rate_manual(device, freq_hz, divider);
19371969
}
19381970

1971+
int ADDCALL hackrf_radio_set_sample_rate_fp(
1972+
hackrf_device* device,
1973+
const fp_28_36_t freq_hz)
1974+
{
1975+
USB_API_REQUIRED(device, 0x0113);
1976+
uint64_t set_sr_fp_param;
1977+
uint8_t length;
1978+
int result;
1979+
1980+
set_sr_fp_param = TO_LE64(freq_hz);
1981+
length = sizeof(uint64_t);
1982+
1983+
result = libusb_control_transfer(
1984+
device->usb_device,
1985+
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR |
1986+
LIBUSB_RECIPIENT_DEVICE,
1987+
HACKRF_VENDOR_REQUEST_RADIO_SET_SAMPLE_RATE_FP,
1988+
0,
1989+
0,
1990+
(unsigned char*) &set_sr_fp_param,
1991+
length,
1992+
DEFAULT_REQUEST_TIMEOUT);
1993+
1994+
if (result < length) {
1995+
last_libusb_error = result;
1996+
return HACKRF_ERROR_LIBUSB;
1997+
}
1998+
1999+
return HACKRF_SUCCESS;
2000+
}
2001+
19392002
int ADDCALL hackrf_set_amp_enable(hackrf_device* device, const uint8_t value)
19402003
{
19412004
int result;

host/libhackrf/src/hackrf.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,16 @@ enum radio_config_mode {
959959
RADIO_CONFIG_HALF_PRECISION = 4,
960960
};
961961

962+
/**
963+
* 40.24 Fixed-point type.
964+
*/
965+
typedef uint64_t fp_40_24_t;
966+
967+
/**
968+
* 28.36 Fixed-point type.
969+
*/
970+
typedef uint64_t fp_28_36_t;
971+
962972
/**
963973
* Opaque struct for hackrf device info. Object can be created via @ref hackrf_open, @ref hackrf_device_list_open or @ref hackrf_open_by_serial and be destroyed via @ref hackrf_close
964974
* @ingroup device
@@ -1791,6 +1801,18 @@ extern ADDAPI int ADDCALL hackrf_set_freq_explicit(
17911801
const uint64_t lo_freq_hz,
17921802
const enum rf_path_filter path);
17931803

1804+
/**
1805+
* Set the radio center frequency to a fractional, fixed-point value.
1806+
*
1807+
* @param[in] device device to query
1808+
* @param[in] hz center frequency in Hz
1809+
* @return @ref HACKRF_SUCCESS on success or @ref hackrf_error variant
1810+
* @ingroup configuration
1811+
*/
1812+
extern ADDAPI int ADDCALL hackrf_radio_set_frequency_fp(
1813+
hackrf_device* device,
1814+
const fp_40_24_t freq_hz);
1815+
17941816
/**
17951817
* Set sample rate explicitly
17961818
*
@@ -1825,6 +1847,20 @@ extern ADDAPI int ADDCALL hackrf_set_sample_rate(
18251847
hackrf_device* device,
18261848
const double freq_hz);
18271849

1850+
/**
1851+
* Set the radio sample rate to a fractional, fixed-point value.
1852+
*
1853+
* This function does not automatically configure the baseband filter bandwidth, so any calls to this function should be followed by @ref hackrf_set_baseband_filter_bandwidth should it require adjustment.
1854+
*
1855+
* @param[in] device device to query
1856+
* @param[in] sps samples per second
1857+
* @return @ref HACKRF_SUCCESS on success or @ref hackrf_error variant
1858+
* @ingroup configuration
1859+
*/
1860+
extern ADDAPI int ADDCALL hackrf_radio_set_sample_rate_fp(
1861+
hackrf_device* device,
1862+
const fp_28_36_t freq_hz);
1863+
18281864
/**
18291865
* Enable/disable 14dB RF amplifier
18301866
*

0 commit comments

Comments
 (0)