Last active
August 29, 2015 14:27
-
-
Save adamgreig/3ef5e8b0a71938ff34c2 to your computer and use it in GitHub Desktop.
BladeRF 10MHz Clock Output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <libbladeRF.h> | |
#define KNRM "\x1B[0m" | |
#define KRED "\x1B[31m" | |
#define KGRN "\x1B[32m" | |
int main(int argc, char* argv[]) | |
{ | |
int i, result; | |
struct bladerf* dev = NULL; | |
struct bladerf_devinfo* devices = NULL; | |
/* f_vco = 38.4MHz * 66 */ | |
/* 10MHz = f_vco / 253.44 */ | |
/* 253.44 = 254 + 44/100 */ | |
uint32_t a = 253, b = 44, c = 100; | |
uint32_t p1, p2, p3; | |
uint8_t regs[10]; | |
uint64_t temp; | |
(void)argc; | |
(void)argv; | |
temp = (uint64_t)a * c + b; | |
temp *= 128; | |
temp /= c; | |
temp -= 512; | |
p1 = temp; | |
temp = (uint64_t)b * 128; | |
temp %= c; | |
p2 = temp; | |
p3 = c; | |
regs[0] = ((p1 >> 0) & 0xff); | |
regs[1] = ((p1 >> 8) & 0xff); | |
regs[2] = ((p1 >> 16) & 0x03) | (p2 & 0x3f) << 2; | |
regs[3] = ((p2 >> 6) & 0xff); | |
regs[4] = ((p2 >> 14) & 0xff); | |
regs[5] = ((p2 >> 22) & 0xff); | |
regs[6] = ((p3 >> 0) & 0xff); | |
regs[7] = ((p3 >> 8) & 0xff); | |
regs[8] = ((p3 >> 16) & 0xff); | |
regs[9] = ((p3 >> 24) & 0x3f); | |
printf("%-50s", "Searching for BladeRFs..."); | |
fflush(stdout); | |
result = bladerf_get_device_list(&devices); | |
if(result < 0) goto error; | |
else printf(KGRN "OK" KNRM "\n"); | |
if(result == 0) { | |
printf("No BladeRFs found, exiting.\n"); | |
return 1; | |
} else for(i=0; i<result; i++) { | |
printf("Found BladeRF[%d]:\n", i); | |
printf(" Backend: %s\n", bladerf_backend_str(devices[i].backend)); | |
printf(" Serial: %s\n", devices[i].serial); | |
printf(" USB: %X:%X\n", devices[i].usb_bus, devices[i].usb_addr); | |
printf(" Instance ID: %d\n", devices[i].instance); | |
} | |
printf("%-50s", "Opening device 0..."); | |
fflush(stdout); | |
result = bladerf_open_with_devinfo(&dev, &devices[0]); | |
if(result < 0) goto error; | |
printf(KGRN "OK" KNRM "\n"); | |
printf("%-50s", "Setting MS3 to 10MHz output..."); | |
fflush(stdout); | |
for(i=0; i<10; i++) { | |
result = bladerf_si5338_write(dev, 86+i, regs[i]); | |
if(result < 0) goto error; | |
} | |
printf(KGRN "OK" KNRM "\n"); | |
printf("%-50s", "Enabling CLK3A..."); | |
fflush(stdout); | |
result = bladerf_si5338_write(dev, 39, 0x01); | |
if(result < 0) goto error; | |
printf(KGRN "OK" KNRM "\n"); | |
printf("%-50s", "Enabling MS3 on CLK3A..."); | |
fflush(stdout); | |
result = bladerf_si5338_write(dev, 34, 0xC0); | |
if(result < 0) goto error; | |
printf(KGRN "OK" KNRM "\n"); | |
printf("Done, exiting.\n"); | |
result = 0; | |
goto cleanup; | |
error: | |
printf(KRED "ERR" KNRM "\nBladeRF Error: %d %s\n", | |
result, bladerf_strerror(result)); | |
result = 1; | |
cleanup: | |
bladerf_close(dev); | |
bladerf_free_device_list(devices); | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
gcc -g -c main.c -Wall -Werror -Wpedantic | |
gcc -g main.o -lpthread -lm -lbladeRF -Wall -Werror -Wpedantic -o bladerf-10mhz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment