Skip to content

Commit

Permalink
hackrf_clock: Allow CLKOUT to be enabled / disabled
Browse files Browse the repository at this point in the history
  hackrf_clock -o 1 / hackrf_clock -o 0
  • Loading branch information
dominicgs committed Nov 7, 2017
1 parent 1f9c2c8 commit 3f569a8
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 57 deletions.
2 changes: 0 additions & 2 deletions firmware/common/hackrf_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ void cpu_clock_init(void)

si5351c_set_clock_source(&clock_gen, PLL_SOURCE_XTAL);
// soft reset
// uint8_t resetdata[] = { 177, 0xac };
// si5351c_write(&clock_gen, resetdata, sizeof(resetdata));
si5351c_reset_pll(&clock_gen);
si5351c_enable_clock_outputs(&clock_gen);

Expand Down
57 changes: 56 additions & 1 deletion firmware/common/si5351c.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,27 @@ void si5351c_configure_clock_control(si5351c_driver_t* const drv, const enum pll
si5351c_write(drv, data, sizeof(data));
}

#define SI5351C_CLK_ENABLE(x) (0<<x)
#define SI5351C_CLK_DISABLE(x) (1<<x)
#define SI5351C_REG_OUTPUT_EN (3)
#define SI5351C_REG_CLK3_CTRL (19)

void si5351c_enable_clock_outputs(si5351c_driver_t* const drv)
{
/* Enable CLK outputs 0, 1, 2, 4, 5 only. */
/* 7: Clock to CPU is deactivated as it is not used and creates noise */
/* 3: External clock output is deactivated by default */
uint8_t data[] = { 3, ~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 4) | (1 << 5))};
// uint8_t data[] = { 3, ~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 4) | (1 << 5))};
uint8_t data[] = { SI5351C_REG_OUTPUT_EN,
SI5351C_CLK_ENABLE(0) |
SI5351C_CLK_ENABLE(1) |
SI5351C_CLK_ENABLE(2) |
SI5351C_CLK_DISABLE(3) |
SI5351C_CLK_ENABLE(4) |
SI5351C_CLK_ENABLE(5) |
SI5351C_CLK_DISABLE(6) |
SI5351C_CLK_DISABLE(7)
};
si5351c_write(drv, data, sizeof(data));
}

Expand Down Expand Up @@ -244,3 +259,43 @@ void si5351c_activate_best_clock_source(si5351c_driver_t* const drv)
}
}
}

void si5351c_clkout_enable(si5351c_driver_t* const drv, uint8_t enable)
{
/* Set optput in output enable register */
uint8_t output_enable = si5351c_read_single(drv, 3);
output_enable = output_enable & !SI5351C_CLK_DISABLE(3);
if(enable)
output_enable = output_enable | SI5351C_CLK_ENABLE(3);
else
output_enable = output_enable | SI5351C_CLK_DISABLE(3);
uint8_t oe_data[] = {SI5351C_REG_OUTPUT_EN, output_enable};
si5351c_write(drv, oe_data, 2);

/* Configure clock to 10MHz (TODO customisable?) */
si5351c_configure_multisynth(drv, 3, 80*128-512, 0, 1, 0);

/* Set power up/doen in CLK3 control register*/
uint8_t pll;
#ifdef RAD1O
/* PLLA on XTAL */
pll = SI5351C_CLK_PLL_SRC_A;
#endif

#if (defined JAWBREAKER || defined HACKRF_ONE)
if (active_clock_source == PLL_SOURCE_CLKIN) {
/* PLLB on CLKIN */
pll = SI5351C_CLK_PLL_SRC_B;
} else {
/* PLLA on XTAL */
pll = SI5351C_CLK_PLL_SRC_A;
}
#endif
uint8_t clk3_ctrl;
if(enable)
clk3_ctrl = SI5351C_CLK_INT_MODE | SI5351C_CLK_PLL_SRC(pll) | SI5351C_CLK_SRC(SI5351C_CLK_SRC_MULTISYNTH_SELF) | SI5351C_CLK_IDRV(SI5351C_CLK_IDRV_8MA);
else
clk3_ctrl = SI5351C_CLK_POWERDOWN | SI5351C_CLK_INT_MODE;
uint8_t clk3_data[] = {SI5351C_REG_CLK3_CTRL, clk3_ctrl};
si5351c_write(drv, clk3_data, 2);
}
1 change: 1 addition & 0 deletions firmware/common/si5351c.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void si5351c_activate_best_clock_source(si5351c_driver_t* const drv);
void si5351c_write_single(si5351c_driver_t* const drv, uint8_t reg, uint8_t val);
uint8_t si5351c_read_single(si5351c_driver_t* const drv, uint8_t reg);
void si5351c_write(si5351c_driver_t* const drv, const uint8_t* const data, const size_t data_count);
void si5351c_clkout_enable(si5351c_driver_t* const drv, uint8_t enable);

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion firmware/hackrf_usb/hackrf_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ static const usb_request_handler_fn vendor_request_handler[] = {
usb_vendor_request_operacake_set_ports,
usb_vendor_request_set_hw_sync_mode,
usb_vendor_request_reset,
usb_vendor_request_operacake_set_ranges
usb_vendor_request_operacake_set_ranges,
usb_vendor_request_set_clkout_enable
};

static const uint32_t vendor_request_handler_count =
Expand Down
11 changes: 11 additions & 0 deletions firmware/hackrf_usb/usb_api_register.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,14 @@ usb_request_status_t usb_vendor_request_read_rffc5071(
}
}
#endif

usb_request_status_t usb_vendor_request_set_clkout_enable(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage
) {
if (stage == USB_TRANSFER_STAGE_SETUP) {
si5351c_clkout_enable(&clock_gen, endpoint->setup.value);
usb_transfer_schedule_ack(endpoint->in);
}
return USB_REQUEST_STATUS_OK;
}
4 changes: 4 additions & 0 deletions firmware/hackrf_usb/usb_api_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ usb_request_status_t usb_vendor_request_read_rffc5071(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage
);
usb_request_status_t usb_vendor_request_set_clkout_enable(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage
);

#endif /* end of include guard: __USB_API_REGISTER_H__ */
2 changes: 2 additions & 0 deletions host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
cmake_minimum_required(VERSION 2.8)
project (HackRF)

set(CMAKE_C_FLAGS "$ENV{CFLAGS}" CACHE STRING "C Flags")

add_subdirectory(libhackrf)
add_subdirectory(hackrf-tools)

Expand Down
80 changes: 27 additions & 53 deletions host/hackrf-tools/src/hackrf_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ typedef int bool;
#define false 0
#endif

#define CLOCK_INPUT 0xFE
#define CLOCK_UNDEFINED 0xFF
#define REGISTER_INVALID 32767

Expand Down Expand Up @@ -226,38 +225,33 @@ int si5351c_read_configuration(hackrf_device* device) {
}

static void usage() {
printf("\nhackrf_clock - HackRF clock configuration utility\n");
printf("hackrf_clock - HackRF clock configuration utility\n");
printf("Usage:\n");
printf("\t-h, --help: this help\n");
printf("\t-r, --read: read clock settings\n");
printf("\t-w, --write <f>: set clock frequnecy to <f>\n");
printf("\t-c, --clock <n>: select multisynth output clock to configure\n");
printf("\t-i, --clkin: configure clock in\n");
printf("\t-o, --clkout: configure clock out\n");
printf("\t-d, --device <s>: specify a particular device by serial number\n");
printf("\t-r, --read <clock_num>: read settings for clock_num\n");
printf("\t-a, --all: read settings for all clocks\n");
printf("\t-o, --clkout <clkout_enable>: enable/disable CLKOUT\n");
printf("\t-d, --device <serial_number>: Serial number of desired HackRF.\n");
printf("\nExamples:\n");
printf("\thackrf_clock -c 3 -r : prints settings for CLKOUT\n");
printf("\thackrf_clock -c # \n");
printf("\thackrf_clock -r 3 : prints settings for CLKOUT\n");
}

static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "read", no_argument, 0, 'r' },
{ "write", required_argument, 0, 'w' },
{ "clock", required_argument, 0, 'c' },
{ "clkin", no_argument, 0, 'i' },
{ "clkout", no_argument, 0, 'o' },
{ "read", required_argument, 0, 'r' },
{ "all", no_argument, 0, 'a' },
{ "clkout", required_argument, 0, 'o' },
{ "device", required_argument, 0, 'd' },
{ 0, 0, 0, 0 },
};

int main(int argc, char** argv) {
int opt;
hackrf_device* device = NULL;
int option_index = 0;
int opt, option_index = 0;
bool read = false;
bool write = false;
uint16_t clock = CLOCK_UNDEFINED;
bool clkout = false;
uint16_t clkout_enable;
const char* serial_number = NULL;

int result = hackrf_init();
Expand All @@ -266,27 +260,20 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

while( (opt = getopt_long(argc, argv, "riod:w:c:h?", long_options, &option_index)) != EOF ) {
while( (opt = getopt_long(argc, argv, "r:ao:d:h?", long_options, &option_index)) != EOF ) {
switch( opt ) {
case 'r':
read = true;
break;

case 'w':
write = true;
result = parse_int(optarg, &clock);
break;

case 'c':
result = parse_int(optarg, &clock);
break;

case 'i':
clock = CLOCK_INPUT;
case 'a':
read = true;
break;

case 'o':
clock = 3;
clkout = true;
result = parse_int(optarg, &clkout_enable);
break;

case 'd':
Expand All @@ -310,20 +297,8 @@ int main(int argc, char** argv) {
}
}

if(write && read) {
fprintf(stderr, "Read and write options are mutually exclusive.\n");
usage();
return EXIT_FAILURE;
}

if(!write && !read) {
fprintf(stderr, "Either read or write option must be specified.\n");
usage();
return EXIT_FAILURE;
}

if(write && clock==CLOCK_UNDEFINED) {
fprintf(stderr, "A clock must be specified whe using the write option.\n");
if(!clkout && !read) {
fprintf(stderr, "Either read or enable CLKOUT option must be specified.\n");
usage();
return EXIT_FAILURE;
}
Expand All @@ -334,6 +309,14 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

if(clkout) {
result = hackrf_set_clkout_enable(device, clkout_enable);
if(result) {
printf("hackrf_set_clkout_enable() failed: %s (%d)\n", hackrf_error_name(result), result);
return EXIT_FAILURE;
}
}

if(read) {
if(clock == CLOCK_UNDEFINED)
si5351c_read_configuration(device);
Expand All @@ -343,15 +326,6 @@ int main(int argc, char** argv) {
}
}

if(write) {
// result = si5351c_write_register(device, register_number, register_value);
if(result) {
printf("si5351c_write_register() failed: %s (%d)\n", hackrf_error_name(result), result);
return EXIT_FAILURE;
}
}


result = hackrf_close(device);
if(result) {
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
Expand Down
26 changes: 26 additions & 0 deletions host/libhackrf/src/hackrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef enum {
HACKRF_VENDOR_REQUEST_SET_HW_SYNC_MODE = 29,
HACKRF_VENDOR_REQUEST_RESET = 30,
HACKRF_VENDOR_REQUEST_OPERACAKE_SET_RANGES = 31,
HACKRF_VENDOR_REQUEST_CLKOUT_ENABLE = 32,
} hackrf_vendor_request;

#define USB_CONFIG_STANDARD 0x1
Expand Down Expand Up @@ -1991,6 +1992,31 @@ int ADDCALL hackrf_set_operacake_ranges(hackrf_device* device, uint8_t* ranges,
return HACKRF_SUCCESS;
}
}

int ADDCALL hackrf_set_clkout_enable(hackrf_device* device, const uint8_t value)
{
USB_API_REQUIRED(device, 0x0103)
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
HACKRF_VENDOR_REQUEST_CLKOUT_ENABLE,
value,
0,
NULL,
0,
0
);

if (result != 0)
{
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
} else {
return HACKRF_SUCCESS;
}
}

#ifdef __cplusplus
} // __cplusplus defined.
#endif
Expand Down
2 changes: 2 additions & 0 deletions host/libhackrf/src/hackrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ extern ADDAPI int ADDCALL hackrf_set_operacake_ranges(hackrf_device* device,
uint8_t* ranges,
uint8_t num_ranges);

extern ADDAPI int ADDCALL hackrf_set_clkout_enable(hackrf_device* device, const uint8_t value);

#ifdef __cplusplus
} // __cplusplus defined.
#endif
Expand Down

0 comments on commit 3f569a8

Please sign in to comment.