From d682b6c5123012c3a356e71ba802141c28e49cd9 Mon Sep 17 00:00:00 2001 From: Robert Schmidt Date: Fri, 19 Sep 2025 12:44:37 +0200 Subject: [PATCH] Add function to read max DL ToA Protect with mutex to allow reading from another thread. --- openair1/PHY/INIT/nr_init_ue.c | 1 + .../nr_dl_channel_estimation.c | 18 ++++++++++++++++++ openair1/PHY/defs_nr_common.h | 3 +++ 3 files changed, 22 insertions(+) diff --git a/openair1/PHY/INIT/nr_init_ue.c b/openair1/PHY/INIT/nr_init_ue.c index 2dce10d9dc..7ab8a8863d 100644 --- a/openair1/PHY/INIT/nr_init_ue.c +++ b/openair1/PHY/INIT/nr_init_ue.c @@ -159,6 +159,7 @@ void init_nr_prs_ue_vars(PHY_VARS_NR_UE *ue) AssertFatal((prs_vars[idx]->prs_resource[k].prs_meas[j]!=NULL), "%s: PRS measurements malloc failed for gNB_id %d, rx_ant %d\n", __FUNCTION__, idx, j); prs_meas_t *m = prs_vars[idx]->prs_resource[k].prs_meas[j]; m->next_dl_toa = m->dl_toa; + pthread_mutex_init(&m->dl_toa_mtx, NULL); } } } diff --git a/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c b/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c index 3af0937c48..de0b6d2b29 100644 --- a/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c +++ b/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c @@ -73,10 +73,28 @@ void peak_estimator(c16_t *buffer, int32_t buf_len, int32_t *peak_idx, int32_t * void set_prs_dl_toa(prs_meas_t *prs_meas, float dl_toa) { + int rc = pthread_mutex_lock(&prs_meas->dl_toa_mtx); + AssertFatal(rc == 0, "pthread_mutex_lock() failed: errno %d, %s\n", errno, strerror(errno)); *prs_meas->next_dl_toa++ = dl_toa; if (prs_meas->next_dl_toa >= prs_meas->dl_toa + sizeofArray(prs_meas->dl_toa)) prs_meas->next_dl_toa = prs_meas->dl_toa; LOG_D(NR_PHY, "next_dl_toa %p\n", prs_meas->next_dl_toa); + rc = pthread_mutex_unlock(&prs_meas->dl_toa_mtx); + AssertFatal(rc == 0, "pthread_mutex_unlock() failed: errno %d, %s\n", errno, strerror(errno)); +} + +float get_prs_max_dl_toa(prs_meas_t *prs_meas) +{ + float max = 0.0f; + int rc = pthread_mutex_lock(&prs_meas->dl_toa_mtx); + AssertFatal(rc == 0, "pthread_mutex_lock() failed: errno %d, %s\n", errno, strerror(errno)); + for (int i = 0; i < sizeofArray(prs_meas->dl_toa); ++i) { + max = cmax(max, prs_meas->dl_toa[i]); + LOG_I(NR_PHY, "i %d dl_toa %.3f max %.3f\n", i, prs_meas->dl_toa[i], max); + } + rc = pthread_mutex_unlock(&prs_meas->dl_toa_mtx); + AssertFatal(rc == 0, "pthread_mutex_unlock() failed: errno %d, %s\n", errno, strerror(errno)); + return max; } int nr_prs_channel_estimation(uint8_t gNB_id, diff --git a/openair1/PHY/defs_nr_common.h b/openair1/PHY/defs_nr_common.h index b4d61d32f9..23972fc9f2 100644 --- a/openair1/PHY/defs_nr_common.h +++ b/openair1/PHY/defs_nr_common.h @@ -38,6 +38,8 @@ #include "impl_defs_nr.h" #include "PHY/CODING/nrPolar_tools/nr_polar_defs.h" +#include + #define MAX_NUM_SUBCARRIER_SPACING 5 #define NR_MAX_OFDM_SYMBOL_SIZE 8192 @@ -312,6 +314,7 @@ typedef struct { int32_t sfn; int8_t slot; int8_t rxAnt_idx; + pthread_mutex_t dl_toa_mtx; // protect reading of max from write // circular buffer to be able to read maximum of last estimations float dl_toa[128]; // set through set_prs_dl_toa() float *next_dl_toa;