double HackRFSource::set_sample_rate( double rate ) { assert(this->m_dev != nullptr); int status = HACKRF_SUCCESS; double _sample_rates[] = { 8e6, 10e6, 12.5e6, 16e6, 20e6}; bool found_supported_rate = false; for( unsigned int i = 0; i < sizeof(_sample_rates)/sizeof(double); i++ ) { if(_sample_rates[i] == rate) { found_supported_rate = true; break; } } if (!found_supported_rate) { status = HACKRF_ERROR_OTHER; HANDLE_ERROR("Unsupported samplerate: %gMsps", rate/1e6); } status = hackrf_set_sample_rate( this->m_dev, rate); HANDLE_ERROR("Error setting sample rate to %gMsps: %%s\n", rate/1e6); }
static void setup_hackrf() { int status; status = hackrf_init(); HACKRF_CHECK_STATUS(status, "hackrf_init"); status = hackrf_open(&device); HACKRF_CHECK_STATUS(status, "hackrf_open"); status = hackrf_set_freq(device, frequency); HACKRF_CHECK_STATUS(status, "hackrf_set_freq"); status = hackrf_set_sample_rate(device, SAMPLE_RATE); HACKRF_CHECK_STATUS(status, "hackrf_set_sample_rate"); status = hackrf_set_amp_enable(device, 0); HACKRF_CHECK_STATUS(status, "hackrf_set_amp_enable"); status = hackrf_set_lna_gain(device, 32); HACKRF_CHECK_STATUS(status, "hackrf_set_lna_gain"); status = hackrf_set_vga_gain(device, 40); HACKRF_CHECK_STATUS(status, "hackrf_set_lna_gain"); status = hackrf_start_rx(device, receive_sample_block, NULL); HACKRF_CHECK_STATUS(status, "hackrf_start_rx"); }
int main(int argc, char **argv) { if (argc != 3) { usage(); exit(1); } double freq_mhz = atof(argv[1]); FREQUENCY = freq_mhz * 1e6; int count = atoi(argv[2]); BUFFER_SIZE = NRF_SAMPLES_LENGTH * count; buffer = calloc(BUFFER_SIZE, 1); int status; hackrf_device *device; status = hackrf_init(); CHECK_STATUS(status, "hackrf_init"); status = hackrf_open(&device); CHECK_STATUS(status, "hackrf_open"); status = hackrf_set_freq(device, FREQUENCY); CHECK_STATUS(status, "hackrf_set_freq"); status = hackrf_set_sample_rate(device, SAMPLE_RATE); CHECK_STATUS(status, "hackrf_set_sample_rate"); status = hackrf_set_amp_enable(device, 0); CHECK_STATUS(status, "hackrf_set_amp_enable"); status = hackrf_set_lna_gain(device, 32); CHECK_STATUS(status, "hackrf_set_lna_gain"); status = hackrf_set_vga_gain(device, 34); CHECK_STATUS(status, "hackrf_set_vga_gain"); status = hackrf_start_rx(device, receive_sample_block, NULL); CHECK_STATUS(status, "hackrf_start_rx"); while (buffer_pos < BUFFER_SIZE) { sleep(1); } hackrf_stop_rx(device); hackrf_close(device); hackrf_exit(); return 0; }
int main(int argc, char **argv) { int status; if (argc != 3) { printf("Usage: rfcap FREQ N_SAMPLES\n"); exit(EXIT_FAILURE); } current_freq = atof(argv[1]); n_samples = atoi(argv[2]); status = hackrf_init(); HACKRF_CHECK_STATUS(status, "hackrf_init"); status = hackrf_open(&device); HACKRF_CHECK_STATUS(status, "hackrf_open"); status = hackrf_set_freq(device, current_freq * 1e6); HACKRF_CHECK_STATUS(status, "hackrf_set_freq"); status = hackrf_set_sample_rate(device, 10e6); HACKRF_CHECK_STATUS(status, "hackrf_set_sample_rate"); status = hackrf_set_amp_enable(device, 0); HACKRF_CHECK_STATUS(status, "hackrf_set_amp_enable"); status = hackrf_set_lna_gain(device, 32); HACKRF_CHECK_STATUS(status, "hackrf_set_lna_gain"); status = hackrf_set_vga_gain(device, 30); HACKRF_CHECK_STATUS(status, "hackrf_set_lna_gain"); status = hackrf_start_rx(device, receive_sample_block, NULL); HACKRF_CHECK_STATUS(status, "hackrf_start_rx"); while (receive_count < n_samples) { usleep(100); } hackrf_stop_rx(device); hackrf_close(device); hackrf_exit(); return 0; }
static void mdlStart(SimStruct *S) /* ======================================================================== */ { int ret = HACKRF_SUCCESS; hackrf_device_list_t *list; struct hackrf_device *device; const uint32_t device_index = (uint32_t)mxGetScalar(ssGetSFcnParam(S, DEVICE_INDEX)); const uint64_t frequency = (uint64_t)mxGetScalar(ssGetSFcnParam(S, FREQUENCY)); const double sample_rate = mxGetScalar(ssGetSFcnParam(S, SAMPLE_RATE)); const uint32_t txvga = (uint32_t)mxGetScalar(ssGetSFcnParam(S, TXVGA)); const uint32_t amp = (uint32_t)mxGetScalar(ssGetSFcnParam(S, AMP)); const uint32_t bandwidth = (uint32_t)mxGetScalar(ssGetSFcnParam(S, BANDWIDTH)); /* Set options of this Block */ ssSetOptions(S, ssGetOptions(S) | SS_OPTION_CALL_TERMINATE_ON_EXIT); /* give handle to PWork vector */ ssSetPWorkValue(S, DEVICE, NULL); /* init HackRF device */ ret = hackrf_init(); if (ret < 0) { ssSetErrorStatusf(S, "Failed to init HackRF device #%d", device_index); return; } list = hackrf_device_list(); if (list->devicecount < 1) { ssSetErrorStatusf(S, "No HackRF boards found.\n"); return; } /* open HackRF device */ ret = hackrf_device_list_open(list, device_index, &device); if (ret < 0) { ssSetErrorStatusf(S, "Failed to open HackRF device #%d", device_index); return; } /* give handle to PWork vector */ ssSetPWorkValue(S, DEVICE, (struct hackrf_device *)device); ret = hackrf_set_sample_rate(device, sample_rate); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF Sample Rate #%d", device_index); } ret = hackrf_set_freq(device, frequency); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF frequency #%d", device_index); } ret = hackrf_set_baseband_filter_bandwidth(device, bandwidth); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF bandwidth #%d", device_index); } ret = hackrf_set_txvga_gain(device, txvga); ret |= hackrf_set_amp_enable(device, amp); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF gain #%d", device_index); } /* create mutex for sample thread */ pthread_mutex_t *mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); //*mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_init(mutex, NULL); ssSetPWorkValue(S, MUTEX, mutex); /* create condition variable for sample thread */ pthread_cond_t *cond_var = (pthread_cond_t *)malloc(sizeof(pthread_cond_t)); pthread_cond_init(cond_var, NULL); ssSetPWorkValue(S, COND_VAR, cond_var); /* allocate memory for sample buffer */ SampleBuffer *sbuf = (SampleBuffer*)malloc(sizeof(SampleBuffer)); sbuf->num = BUF_NUM; sbuf->head = sbuf->offset = sbuf->count = sbuf->tail = 0; sbuf->samp_avail = BUF_SIZE / BYTES_PER_SAMPLE; sbuf->underrun = sbuf->underrun_before = false; sbuf->buf = (unsigned char **)malloc(sbuf->num * sizeof(unsigned char *)); if (sbuf->buf) { for (unsigned int i = 0; i < sbuf->num; ++i) sbuf->buf[i] = (unsigned char *)malloc(BUF_SIZE * sizeof(unsigned char)); } ssSetPWorkValue(S, SBUF, sbuf); ret = hackrf_start_tx(device, tx_callback, (void*)S); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Start HackRF #%d", device_index); } }