diff options
-rw-r--r-- | CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/fl2k_ampliphase.c | 68 |
2 files changed, 38 insertions, 35 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bb31183..2996748 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,4 @@ # Copyright 2018 Osmocom Project -# # This file is part of osmo-fl2k # # This program is free software: you can redistribute it and/or modify @@ -51,6 +50,10 @@ include(Version) # setup version info ######################################################################## # Compiler specific setup ######################################################################## + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED TRUE) + if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32) ADD_DEFINITIONS(-Wall) ADD_DEFINITIONS(-Wextra) diff --git a/src/fl2k_ampliphase.c b/src/fl2k_ampliphase.c index 38adcf1..e40a5e8 100644 --- a/src/fl2k_ampliphase.c +++ b/src/fl2k_ampliphase.c @@ -4,6 +4,7 @@ * * fl2k-ampliphase * Copyright (C) 2022 by Felix Erckenbrecht <eligs@eligs.de> + * Copyright (C) 2022 by Matthias P. Braendli <matthias.braendli@mpb.li> * * based on fl2k-fm code: * Copyright (C) 2016-2018 by Steve Markgraf <steve@steve-m.de> @@ -71,12 +72,27 @@ pthread_cond_t iq_cond; pthread_t dbg_thread; FILE *file; -int8_t *itxbuf = NULL; -int8_t *qtxbuf = NULL; +int swap_iq = 0; + +// main thread reads from file and writes into pdbuf and pdslopebuf, if full it waits on iq_cond. +// iq_worker thread reads from pdbuf and pdslopebuf at readpos (protected by +// mutex because both threads read its value) +long int * pdbuf; +long int * pdslopebuf; +int writepos = 0; +pthread_mutex_t readpos_mutex; +int readpos = 0; + +// iq_worker runs dds_complex_buf against [iq]ambuf, DSP is done in-place in chunks of rf_to_baseband_sample_ratio int8_t *iambuf = NULL; int8_t *qambuf = NULL; -int8_t *buf1 = NULL; -int8_t *buf2 = NULL; +int8_t *itxbuf = NULL; +int8_t *qtxbuf = NULL; +// iq_worker thread swaps [iqtxbuf and [iq]ambuf, and waits for cb_cond. + +// output: +// fl2k_callback sets r_buf=itxbuf, g_buf=qtxbuf and sets cb_cond +// file_worker writes [iq]txbuf into debug file and sets cb_cond uint32_t samp_rate = 96000000; @@ -84,14 +100,7 @@ int base_freq = 1440000; int rf_to_baseband_sample_ratio; int input_freq = 48000; -complex float *ampbuf; -complex float *slopebuf; -long int * pdbuf; -long int * pdslopebuf; - -int writepos, readpos; -int swap_iq = 0; int ignore_eof = 0; int debug_to_file = 0; @@ -298,31 +307,30 @@ static inline void dds_complex_buf(dds_t *dds, int8_t *ibuf, int8_t *qbuf, int c static void *iq_worker(void *arg) { dds_t base_signal; - int8_t *tmp_ptr; uint32_t len = 0; - uint32_t readlen, remaining; int buf_prefilled = 0; /* Prepare the oscillators */ base_signal = dds_init(samp_rate, base_freq, 0, 1, WF_RECT); while (!do_exit) { - // dds_set_amp(&base_signal, ampbuf[readpos], slopebuf[readpos]); /* set phase modulation value from audio input */ dds_set_phase(&base_signal, pdbuf[readpos], pdslopebuf[readpos]); + pthread_mutex_lock(&readpos_mutex); readpos++; readpos &= BUFFER_SAMPLES_MASK; + pthread_mutex_unlock(&readpos_mutex); /* check if we reach the end of the buffer */ if ((len + rf_to_baseband_sample_ratio) > FL2K_BUF_LEN) { - readlen = FL2K_BUF_LEN - len; - remaining = rf_to_baseband_sample_ratio - readlen; + uint32_t readlen = FL2K_BUF_LEN - len; + uint32_t remaining = rf_to_baseband_sample_ratio - readlen; /* generate signal, perform phase modulation on both paths */ - dds_complex_buf(&base_signal, &iambuf[len], &qambuf[len],readlen); + dds_complex_buf(&base_signal, &iambuf[len], &qambuf[len], readlen); if (buf_prefilled) { /* swap buffers */ - tmp_ptr = iambuf; + int8_t *tmp_ptr = iambuf; iambuf = itxbuf; itxbuf = tmp_ptr; @@ -355,16 +363,16 @@ static void *iq_worker(void *arg) static inline int writelen(int maxlen) { + pthread_mutex_lock(&readpos_mutex); int rp = readpos; - int len; - int r; + pthread_mutex_unlock(&readpos_mutex); if (rp < writepos) rp += BUFFER_SAMPLES; - len = rp - writepos; + int len = rp - writepos; - r = len > maxlen ? maxlen : len; + int r = len > maxlen ? maxlen : len; return r; } @@ -426,8 +434,8 @@ void ampliphase_modulator(enum inputType_E inputType, const float modIndex) while (!do_exit) { int swap = swap_iq; len = writelen(BASEBAND_BUF_SIZE); - if (len > 1) { - if(inputType == INP_REAL){ + if (len > /* for efficiency, do not read 1 sample at a time */ BASEBAND_BUF_SIZE/4) { + if (inputType == INP_REAL) { len = fread(baseband_buf_real, 1, len, file); for(i = 0 ; i < len; i++){ /* input is -1.0 .. +1.0 (-32768 .. 32767) @@ -645,23 +653,16 @@ int main(int argc, char **argv) /* Baseband buffer */ - slopebuf = malloc(BUFFER_SAMPLES * sizeof(float complex)); - ampbuf = malloc(BUFFER_SAMPLES * sizeof(float complex)); pdbuf = malloc(BUFFER_SAMPLES * sizeof(long int)); pdslopebuf = malloc(BUFFER_SAMPLES * sizeof(long int)); - if (!slopebuf || !ampbuf || !pdbuf || !pdslopebuf) { + if (!pdbuf || !pdslopebuf) { fprintf(stderr, "malloc error!\n"); exit(1); } - memset(slopebuf, 0, BUFFER_SAMPLES * sizeof(float complex)); - memset(ampbuf, 0, BUFFER_SAMPLES * sizeof(float complex)); memset(pdbuf, 0, BUFFER_SAMPLES * sizeof(long int)); memset(pdslopebuf, 0, BUFFER_SAMPLES * sizeof(long int)); - readpos = 0; - writepos = 0; - fprintf(stdout, "Samplerate: %3.2f MHz\n", (float)samp_rate/1000000); fprintf(stdout, "Center frequency: %5.0f kHz\n", (float)base_freq/1000); if(swap_iq) @@ -669,6 +670,7 @@ int main(int argc, char **argv) if(ignore_eof) fprintf(stdout, "Ignoring EOF.\n"); + pthread_mutex_init(&readpos_mutex, NULL); pthread_mutex_init(&cb_mutex, NULL); pthread_mutex_init(&iq_mutex, NULL); pthread_cond_init(&cb_cond, NULL); @@ -741,8 +743,6 @@ int main(int argc, char **argv) if (file != stdin) fclose(file); - free(ampbuf); - free(slopebuf); free(pdbuf); free(pdslopebuf); free(iambuf); |