Skip to content

Instantly share code, notes, and snippets.

@hwhw
Created November 27, 2015 21:55
Show Gist options
  • Save hwhw/7e19ee8e6043a108170a to your computer and use it in GitHub Desktop.
Save hwhw/7e19ee8e6043a108170a to your computer and use it in GitHub Desktop.
/* compile with gcc -lhackrf */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <libhackrf/hackrf.h>
/* table of cos/sin pairs for x=0..1023:
* cos(x*pi*2/1024) * 127, sin(x*pi*2/1024) * 127
*/
static const int8_t cos_sin_tbl[] = {
127, 0, 126, 0, 126, 1, 126, 2, 126, 3, 126, 3, 126, 4, 126, 5, 126, 6,
126, 7, 126, 7, 126, 8, 126, 9, 126, 10, 126, 10, 126, 11, 126, 12, 126,
13, 126, 13, 126, 14, 126, 15, 125, 16, 125, 17, 125, 17, 125, 18, 125, 19,
125, 20, 125, 20, 125, 21, 124, 22, 124, 23, 124, 24, 124, 24, 124, 25,
124, 26, 124, 27, 123, 27, 123, 28, 123, 29, 123, 30, 123, 30, 123, 31,
122, 32, 122, 33, 122, 33, 122, 34, 121, 35, 121, 36, 121, 36, 121, 37,
121, 38, 120, 39, 120, 39, 120, 40, 120, 41, 119, 42, 119, 42, 119, 43,
119, 44, 118, 44, 118, 45, 118, 46, 117, 47, 117, 47, 117, 48, 117, 49,
116, 50, 116, 50, 116, 51, 115, 52, 115, 52, 115, 53, 114, 54, 114, 55,
114, 55, 113, 56, 113, 57, 113, 57, 112, 58, 112, 59, 112, 59, 111, 60,
111, 61, 110, 61, 110, 62, 110, 63, 109, 63, 109, 64, 108, 65, 108, 65,
108, 66, 107, 67, 107, 67, 106, 68, 106, 69, 106, 69, 105, 70, 105, 71,
104, 71, 104, 72, 103, 73, 103, 73, 102, 74, 102, 75, 102, 75, 101, 76,
101, 76, 100, 77, 100, 78, 99, 78, 99, 79, 98, 79, 98, 80, 97, 81, 97, 81,
96, 82, 96, 82, 95, 83, 95, 84, 94, 84, 94, 85, 93, 85, 93, 86, 92, 87, 91,
87, 91, 88, 90, 88, 90, 89, 89, 89, 89, 90, 88, 90, 88, 91, 87, 91, 87, 92,
86, 93, 85, 93, 85, 94, 84, 94, 84, 95, 83, 95, 82, 96, 82, 96, 81, 97, 81,
97, 80, 98, 79, 98, 79, 99, 78, 99, 78, 100, 77, 100, 76, 101, 76, 101, 75,
102, 75, 102, 74, 102, 73, 103, 73, 103, 72, 104, 71, 104, 71, 105, 70,
105, 69, 106, 69, 106, 68, 106, 67, 107, 67, 107, 66, 108, 65, 108, 65,
108, 64, 109, 63, 109, 63, 110, 62, 110, 61, 110, 61, 111, 60, 111, 59,
112, 59, 112, 58, 112, 57, 113, 57, 113, 56, 113, 55, 114, 55, 114, 54,
114, 53, 115, 52, 115, 52, 115, 51, 116, 50, 116, 50, 116, 49, 117, 48,
117, 47, 117, 47, 117, 46, 118, 45, 118, 44, 118, 44, 119, 43, 119, 42,
119, 42, 119, 41, 120, 40, 120, 39, 120, 39, 120, 38, 121, 37, 121, 36,
121, 36, 121, 35, 121, 34, 122, 33, 122, 33, 122, 32, 122, 31, 123, 30,
123, 30, 123, 29, 123, 28, 123, 27, 123, 27, 124, 26, 124, 25, 124, 24,
124, 24, 124, 23, 124, 22, 124, 21, 125, 20, 125, 20, 125, 19, 125, 18,
125, 17, 125, 17, 125, 16, 125, 15, 126, 14, 126, 13, 126, 13, 126, 12,
126, 11, 126, 10, 126, 10, 126, 9, 126, 8, 126, 7, 126, 7, 126, 6, 126, 5,
126, 4, 126, 3, 126, 3, 126, 2, 126, 1, 126, 0, 126, 0, 127, -1, 126, -2,
126, -3, 126, -4, 126, -4, 126, -5, 126, -6, 126, -7, 126, -8, 126, -8,
126, -9, 126, -10, 126, -11, 126, -11, 126, -12, 126, -13, 126, -14, 126,
-14, 126, -15, 126, -16, 126, -17, 125, -18, 125, -18, 125, -19, 125, -20,
125, -21, 125, -21, 125, -22, 125, -23, 124, -24, 124, -25, 124, -25, 124,
-26, 124, -27, 124, -28, 124, -28, 123, -29, 123, -30, 123, -31, 123, -31,
123, -32, 123, -33, 122, -34, 122, -34, 122, -35, 122, -36, 121, -37, 121,
-37, 121, -38, 121, -39, 121, -40, 120, -40, 120, -41, 120, -42, 120, -43,
119, -43, 119, -44, 119, -45, 119, -45, 118, -46, 118, -47, 118, -48, 117,
-48, 117, -49, 117, -50, 117, -51, 116, -51, 116, -52, 116, -53, 115, -53,
115, -54, 115, -55, 114, -56, 114, -56, 114, -57, 113, -58, 113, -58, 113,
-59, 112, -60, 112, -60, 112, -61, 111, -62, 111, -62, 110, -63, 110, -64,
110, -64, 109, -65, 109, -66, 108, -66, 108, -67, 108, -68, 107, -68, 107,
-69, 106, -70, 106, -70, 106, -71, 105, -72, 105, -72, 104, -73, 104, -74,
103, -74, 103, -75, 102, -76, 102, -76, 102, -77, 101, -77, 101, -78, 100,
-79, 100, -79, 99, -80, 99, -80, 98, -81, 98, -82, 97, -82, 97, -83, 96,
-83, 96, -84, 95, -85, 95, -85, 94, -86, 94, -86, 93, -87, 93, -88, 92,
-88, 91, -89, 91, -89, 90, -90, 90, -90, 89, -91, 89, -91, 88, -92, 88,
-92, 87, -93, 87, -94, 86, -94, 85, -95, 85, -95, 84, -96, 84, -96, 83,
-97, 82, -97, 82, -98, 81, -98, 81, -99, 80, -99, 79, -100, 79, -100, 78,
-101, 78, -101, 77, -102, 76, -102, 76, -103, 75, -103, 75, -103, 74, -104,
73, -104, 73, -105, 72, -105, 71, -106, 71, -106, 70, -107, 69, -107, 69,
-107, 68, -108, 67, -108, 67, -109, 66, -109, 65, -109, 65, -110, 64, -110,
63, -111, 63, -111, 62, -111, 61, -112, 61, -112, 60, -113, 59, -113, 59,
-113, 58, -114, 57, -114, 57, -114, 56, -115, 55, -115, 55, -115, 54, -116,
53, -116, 52, -116, 52, -117, 51, -117, 50, -117, 50, -118, 49, -118, 48,
-118, 47, -118, 47, -119, 46, -119, 45, -119, 44, -120, 44, -120, 43, -120,
42, -120, 42, -121, 41, -121, 40, -121, 39, -121, 39, -122, 38, -122, 37,
-122, 36, -122, 36, -122, 35, -123, 34, -123, 33, -123, 33, -123, 32, -124,
31, -124, 30, -124, 30, -124, 29, -124, 28, -124, 27, -125, 27, -125, 26,
-125, 25, -125, 24, -125, 24, -125, 23, -125, 22, -126, 21, -126, 20, -126,
20, -126, 19, -126, 18, -126, 17, -126, 17, -126, 16, -127, 15, -127, 14,
-127, 13, -127, 13, -127, 12, -127, 11, -127, 10, -127, 10, -127, 9, -127,
8, -127, 7, -127, 7, -127, 6, -127, 5, -127, 4, -127, 3, -127, 3, -127, 2,
-127, 1, -127, 0, -127, 0, -127, -1, -127, -2, -127, -3, -127, -4, -127,
-4, -127, -5, -127, -6, -127, -7, -127, -8, -127, -8, -127, -9, -127, -10,
-127, -11, -127, -11, -127, -12, -127, -13, -127, -14, -127, -14, -127,
-15, -127, -16, -126, -17, -126, -18, -126, -18, -126, -19, -126, -20,
-126, -21, -126, -21, -126, -22, -125, -23, -125, -24, -125, -25, -125,
-25, -125, -26, -125, -27, -125, -28, -124, -28, -124, -29, -124, -30,
-124, -31, -124, -31, -124, -32, -123, -33, -123, -34, -123, -34, -123,
-35, -122, -36, -122, -37, -122, -37, -122, -38, -122, -39, -121, -40,
-121, -40, -121, -41, -121, -42, -120, -43, -120, -43, -120, -44, -120,
-45, -119, -45, -119, -46, -119, -47, -118, -48, -118, -48, -118, -49,
-118, -50, -117, -51, -117, -51, -117, -52, -116, -53, -116, -53, -116,
-54, -115, -55, -115, -56, -115, -56, -114, -57, -114, -58, -114, -58,
-113, -59, -113, -60, -113, -60, -112, -61, -112, -62, -111, -62, -111,
-63, -111, -64, -110, -64, -110, -65, -109, -66, -109, -66, -109, -67,
-108, -68, -108, -68, -107, -69, -107, -70, -107, -70, -106, -71, -106,
-72, -105, -72, -105, -73, -104, -74, -104, -74, -103, -75, -103, -76,
-103, -76, -102, -77, -102, -77, -101, -78, -101, -79, -100, -79, -100,
-80, -99, -80, -99, -81, -98, -82, -98, -82, -97, -83, -97, -83, -96, -84,
-96, -85, -95, -85, -95, -86, -94, -86, -94, -87, -93, -88, -92, -88, -92,
-89, -91, -89, -91, -90, -90, -90, -90, -91, -89, -91, -89, -92, -88, -92,
-88, -93, -87, -94, -86, -94, -86, -95, -85, -95, -85, -96, -84, -96, -83,
-97, -83, -97, -82, -98, -82, -98, -81, -99, -80, -99, -80, -100, -79,
-100, -79, -101, -78, -101, -77, -102, -77, -102, -76, -103, -76, -103,
-75, -103, -74, -104, -74, -104, -73, -105, -72, -105, -72, -106, -71,
-106, -70, -107, -70, -107, -69, -107, -68, -108, -68, -108, -67, -109,
-66, -109, -66, -109, -65, -110, -64, -110, -64, -111, -63, -111, -62,
-111, -62, -112, -61, -112, -60, -113, -60, -113, -59, -113, -58, -114,
-58, -114, -57, -114, -56, -115, -56, -115, -55, -115, -54, -116, -53,
-116, -53, -116, -52, -117, -51, -117, -51, -117, -50, -118, -49, -118,
-48, -118, -48, -118, -47, -119, -46, -119, -45, -119, -45, -120, -44,
-120, -43, -120, -43, -120, -42, -121, -41, -121, -40, -121, -40, -121,
-39, -122, -38, -122, -37, -122, -37, -122, -36, -122, -35, -123, -34,
-123, -34, -123, -33, -123, -32, -124, -31, -124, -31, -124, -30, -124,
-29, -124, -28, -124, -28, -125, -27, -125, -26, -125, -25, -125, -25,
-125, -24, -125, -23, -125, -22, -126, -21, -126, -21, -126, -20, -126,
-19, -126, -18, -126, -18, -126, -17, -126, -16, -127, -15, -127, -14,
-127, -14, -127, -13, -127, -12, -127, -11, -127, -11, -127, -10, -127, -9,
-127, -8, -127, -8, -127, -7, -127, -6, -127, -5, -127, -4, -127, -4, -127,
-3, -127, -2, -127, -1, -127, -1, -127, 0, -127, 1, -127, 2, -127, 3, -127,
3, -127, 4, -127, 5, -127, 6, -127, 7, -127, 7, -127, 8, -127, 9, -127, 10,
-127, 10, -127, 11, -127, 12, -127, 13, -127, 13, -127, 14, -127, 15, -127,
16, -126, 17, -126, 17, -126, 18, -126, 19, -126, 20, -126, 20, -126, 21,
-126, 22, -125, 23, -125, 24, -125, 24, -125, 25, -125, 26, -125, 27, -125,
27, -124, 28, -124, 29, -124, 30, -124, 30, -124, 31, -124, 32, -123, 33,
-123, 33, -123, 34, -123, 35, -122, 36, -122, 36, -122, 37, -122, 38, -122,
39, -121, 39, -121, 40, -121, 41, -121, 42, -120, 42, -120, 43, -120, 44,
-120, 44, -119, 45, -119, 46, -119, 47, -118, 47, -118, 48, -118, 49, -118,
50, -117, 50, -117, 51, -117, 52, -116, 52, -116, 53, -116, 54, -115, 55,
-115, 55, -115, 56, -114, 57, -114, 57, -114, 58, -113, 59, -113, 59, -113,
60, -112, 61, -112, 61, -111, 62, -111, 63, -111, 63, -110, 64, -110, 65,
-109, 65, -109, 66, -109, 67, -108, 67, -108, 68, -107, 69, -107, 69, -107,
70, -106, 71, -106, 71, -105, 72, -105, 73, -104, 73, -104, 74, -103, 75,
-103, 75, -103, 76, -102, 76, -102, 77, -101, 78, -101, 78, -100, 79, -100,
79, -99, 80, -99, 81, -98, 81, -98, 82, -97, 82, -97, 83, -96, 84, -96, 84,
-95, 85, -95, 85, -94, 86, -94, 87, -93, 87, -92, 88, -92, 88, -91, 89,
-91, 89, -90, 90, -90, 90, -89, 91, -89, 91, -88, 92, -88, 93, -87, 93,
-86, 94, -86, 94, -85, 95, -85, 95, -84, 96, -83, 96, -83, 97, -82, 97,
-82, 98, -81, 98, -80, 99, -80, 99, -79, 100, -79, 100, -78, 101, -77, 101,
-77, 102, -76, 102, -76, 102, -75, 103, -74, 103, -74, 104, -73, 104, -72,
105, -72, 105, -71, 106, -70, 106, -70, 106, -69, 107, -68, 107, -68, 108,
-67, 108, -66, 108, -66, 109, -65, 109, -64, 110, -64, 110, -63, 110, -62,
111, -62, 111, -61, 112, -60, 112, -60, 112, -59, 113, -58, 113, -58, 113,
-57, 114, -56, 114, -56, 114, -55, 115, -54, 115, -53, 115, -53, 116, -52,
116, -51, 116, -51, 117, -50, 117, -49, 117, -48, 117, -48, 118, -47, 118,
-46, 118, -45, 119, -45, 119, -44, 119, -43, 119, -43, 120, -42, 120, -41,
120, -40, 120, -40, 121, -39, 121, -38, 121, -37, 121, -37, 121, -36, 122,
-35, 122, -34, 122, -34, 122, -33, 123, -32, 123, -31, 123, -31, 123, -30,
123, -29, 123, -28, 124, -28, 124, -27, 124, -26, 124, -25, 124, -25, 124,
-24, 124, -23, 125, -22, 125, -21, 125, -21, 125, -20, 125, -19, 125, -18,
125, -18, 125, -17, 126, -16, 126, -15, 126, -14, 126, -14, 126, -13, 126,
-12, 126, -11, 126, -11, 126, -10, 126, -9, 126, -8, 126, -8, 126, -7, 126,
-6, 126, -5, 126, -4, 126, -4, 126, -3, 126, -2, 126, -1, 127, -1
};
/* easier handling: */
static const uint16_t *cos_sin = (uint16_t*) cos_sin_tbl;
uint16_t my_adc_get_single() {
uint8_t sample[1];
if(feof(stdin)) exit(0);
fread(sample, 1, 1, stdin);
uint16_t v = sample[0] << 2;
return v;
}
/*
* the following follows the example of hackrf's sgpio_isr
* which triggers write to SGPIO on 8 planes
* will be triggered with FREQ/16
**/
void tx(uint8_t *samples) {
uint16_t *samplebuf = (uint16_t*) samples;
static uint32_t audiosamplebuf = 0; // we use this to rotate 3x 10bit
static uint32_t audiosample = 0;
static int16_t audiosample_diff = 0;
static uint8_t audioctr = 0;
/* ADC 10 bit conversion time is 2.45 us minimum (see UM10503, ch. 47)
* so we fetch a new audio sample only every 4th run.
* This means we get 1 new audio sample per 64 generated I/Q samples.
**/
if(0 == (audioctr++ & 3)) {
// f = 125kHz, -> 8us
// moving average over 4 samples:
uint16_t sample = my_adc_get_single();
uint32_t audiosample_new =
sample +
(audiosamplebuf & 0x3FF) +
((audiosamplebuf & (0x3FF<<11)) >> 11) +
(audiosamplebuf >> 22);
audiosamplebuf = (audiosamplebuf << 11) + sample;
/* we will linearly go from previous sample to this over 4x 16 steps.
* we start with 64x the previous sample (already accumulated in the
* variable), 0x the current one, and will on each step add the
* difference, which we need to calculate here:
**/
audiosample_diff = audiosample_new - (audiosample >> 6);
}
/* prepare 16 I/Q samples.
* "audiosample" contains the sum of 64 audio samples and thus
* needs to be shifted/divided.
**/
static uint32_t j = 0;
for(int i=0; i<16; i++) {
audiosample += audiosample_diff;
j = j + audiosample + ((2048 - 128) << 11);
samplebuf[i] = cos_sin[(j >> 16) % 1024];
}
}
static hackrf_device* dev = NULL;
int transmit(hackrf_transfer* transfer) {
//fprintf(stderr, "run %d\n", count);
//fwrite(transfer->buffer, transfer->buffer_length, 1, stdout);
for(int i=0; i <= (transfer->buffer_length - 32); i = i+32) {
tx(transfer->buffer + i);
}
return HACKRF_SUCCESS;
}
int main() {
hackrf_init();
hackrf_device_list_t* list = hackrf_device_list();
if(list->devicecount != 1) {
fprintf(stderr, "hackrf device count must be 1\n");
exit(-1);
}
hackrf_device_list_free(list);
if(hackrf_open(&dev) != HACKRF_SUCCESS) {
fprintf(stderr, "cannot open device\n");
exit(-1);
}
if(hackrf_set_freq(dev, 100500000 - 500000) != HACKRF_SUCCESS) {
fprintf(stderr, "error setting freq\n");
hackrf_close(dev);
exit(-1);
}
if(hackrf_set_sample_rate(dev, 8000000) != HACKRF_SUCCESS) {
fprintf(stderr, "error setting sample rate\n");
hackrf_close(dev);
exit(-1);
}
if(hackrf_set_baseband_filter_bandwidth(dev, 1750000) != HACKRF_SUCCESS) {
fprintf(stderr, "error setting bb filter bw\n");
hackrf_close(dev);
exit(-1);
}
if(hackrf_set_txvga_gain(dev, 35) != HACKRF_SUCCESS) {
fprintf(stderr, "error setting txvga gain\n");
hackrf_close(dev);
exit(-1);
}
if(hackrf_start_tx(dev, transmit, NULL) != HACKRF_SUCCESS) {
fprintf(stderr, "error starting transmit thread\n");
hackrf_close(dev);
exit(-1);
}
while(1) {};
hackrf_stop_rx(dev);
hackrf_close(dev);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment