diff --git a/src/target/firmware/apps/l1test/main.c b/src/target/firmware/apps/l1test/main.c index 97bc5287e..fc51acdc4 100644 --- a/src/target/firmware/apps/l1test/main.c +++ b/src/target/firmware/apps/l1test/main.c @@ -210,7 +210,7 @@ static int afcout = 0; static void update_vga_gain(void) { printf("VGA Gain: %u %s\n", vga_gain, high_gain ? "HIGH" : "LOW"); - trf6151_set_gain(vga_gain, high_gain); + trf6151_compute_gain(vga_gain, high_gain); tpu_enq_sleep(); tpu_enable(1); tpu_wait_idle(); diff --git a/src/target/firmware/apps/layer1/main.c b/src/target/firmware/apps/layer1/main.c index 385278f7e..db7609828 100644 --- a/src/target/firmware/apps/layer1/main.c +++ b/src/target/firmware/apps/layer1/main.c @@ -94,7 +94,7 @@ static int afcout = 0; static void update_vga_gain(void) { printf("VGA Gain: %u %s\n", vga_gain, high_gain ? "HIGH" : "LOW"); - trf6151_set_gain(vga_gain, high_gain); + trf6151_compute_gain(vga_gain, high_gain); tpu_enq_sleep(); tpu_enable(1); tpu_wait_idle(); diff --git a/src/target/firmware/board/compal/rffe_dualband.c b/src/target/firmware/board/compal/rffe_dualband.c index 152f31bad..f4b736189 100644 --- a/src/target/firmware/board/compal/rffe_dualband.c +++ b/src/target/firmware/board/compal/rffe_dualband.c @@ -82,11 +82,16 @@ uint8_t rffe_get_gain(void) return trf6151_get_gain(); } +void rffe_set_gain(uint8_t dbm) +{ + trf6151_set_gain(dbm); +} + const uint8_t system_inherent_gain = SYSTEM_INHERENT_GAIN; /* Given the expected input level of exp_inp dBm/8 and the target of target_bb * dBm8, configure the RF Frontend with the respective gain */ -void rffe_set_gain(int16_t exp_inp, int16_t target_bb) +void rffe_compute_gain(int16_t exp_inp, int16_t target_bb) { trf6151_compute_gain(exp_inp, target_bb); } diff --git a/src/target/firmware/board/gta0x/rffe_gta0x_triband.c b/src/target/firmware/board/gta0x/rffe_gta0x_triband.c index 63f151376..f118d2914 100644 --- a/src/target/firmware/board/gta0x/rffe_gta0x_triband.c +++ b/src/target/firmware/board/gta0x/rffe_gta0x_triband.c @@ -111,11 +111,16 @@ uint8_t rffe_get_gain(void) return trf6151_get_gain(); } +void rffe_set_gain(uint8_t dbm) +{ + trf6151_set_gain(dbm); +} + const uint8_t system_inherent_gain = SYSTEM_INHERENT_GAIN; /* Given the expected input level of exp_inp dBm/8 and the target of target_bb * dBm8, configure the RF Frontend with the respective gain */ -void rffe_set_gain(int16_t exp_inp, int16_t target_bb) +void rffe_compute_gain(int16_t exp_inp, int16_t target_bb) { trf6151_compute_gain(exp_inp, target_bb); } diff --git a/src/target/firmware/board/pirelli_dpl10/rffe_dpl10_triband.c b/src/target/firmware/board/pirelli_dpl10/rffe_dpl10_triband.c index 00f108929..d4d13424b 100644 --- a/src/target/firmware/board/pirelli_dpl10/rffe_dpl10_triband.c +++ b/src/target/firmware/board/pirelli_dpl10/rffe_dpl10_triband.c @@ -116,11 +116,16 @@ uint8_t rffe_get_gain(void) return trf6151_get_gain(); } +void rffe_set_gain(uint8_t dbm) +{ + trf6151_set_gain(dbm); +} + const uint8_t system_inherent_gain = SYSTEM_INHERENT_GAIN; /* Given the expected input level of exp_inp dBm/8 and the target of target_bb * dBm8, configure the RF Frontend with the respective gain */ -void rffe_set_gain(int16_t exp_inp, int16_t target_bb) +void rffe_compute_gain(int16_t exp_inp, int16_t target_bb) { trf6151_compute_gain(exp_inp, target_bb); } diff --git a/src/target/firmware/include/rf/trf6151.h b/src/target/firmware/include/rf/trf6151.h index 73ff8c09b..f0891b6bc 100644 --- a/src/target/firmware/include/rf/trf6151.h +++ b/src/target/firmware/include/rf/trf6151.h @@ -9,10 +9,16 @@ void trf6151_init(uint8_t tsp_uid, uint16_t tsp_reset_id); /* switch power off or on */ void trf6151_power(int on); -/* set the VGA and RF gain */ -int trf6151_set_gain(uint8_t dbm, int high); - /* obtain the current total gain of the TRF6151 */ +uint8_t trf6151_get_gain_reg(void); + +/* put current set (or computed) gain to register */ +int trf6151_set_gain_reg(uint8_t dbm, int high); + +/* set the global gain to use */ +int trf6151_set_gain(uint8_t dbm); + +/* obtain the global gain set */ uint8_t trf6151_get_gain(void); /* Request the PLL to be tuned to the given frequency */ diff --git a/src/target/firmware/include/rffe.h b/src/target/firmware/include/rffe.h index c28c696fe..63a3a4b51 100644 --- a/src/target/firmware/include/rffe.h +++ b/src/target/firmware/include/rffe.h @@ -28,6 +28,8 @@ uint32_t rffe_get_tx_ports(void); /* get current gain of RF frontend (anything between antenna and baseband in dBm */ uint8_t rffe_get_gain(void); -void rffe_set_gain(int16_t exp_inp, int16_t target_bb); +void rffe_set_gain(uint8_t dbm); + +void rffe_compute_gain(int16_t exp_inp, int16_t target_bb); #endif diff --git a/src/target/firmware/layer1/prim_fbsb.c b/src/target/firmware/layer1/prim_fbsb.c index 124a3162a..53e30c820 100644 --- a/src/target/firmware/layer1/prim_fbsb.c +++ b/src/target/firmware/layer1/prim_fbsb.c @@ -373,7 +373,7 @@ static int l1s_fbdet_cmd(__unused uint8_t p1, __unused uint8_t p2, l1s.fb.mode = fb_mode; /* Tell the RF frontend to set the gain appropriately */ - rffe_set_gain(-85, CAL_DSP_TGT_BB_LVL); + rffe_compute_gain(-85, CAL_DSP_TGT_BB_LVL); /* Program DSP */ dsp_api.db_w->d_task_md = FB_DSP_TASK; /* maybe with I/Q swap? */ diff --git a/src/target/firmware/layer1/prim_pm.c b/src/target/firmware/layer1/prim_pm.c index 5d2f413c1..e6a79553c 100644 --- a/src/target/firmware/layer1/prim_pm.c +++ b/src/target/firmware/layer1/prim_pm.c @@ -46,6 +46,7 @@ #include #include #include +#include #include @@ -68,7 +69,7 @@ static int l1s_pm_cmd(uint8_t num_meas, dsp_api.ndb->d_fb_mode = 0; /* wideband search */ /* Tell the RF frontend to set the gain appropriately */ - rffe_set_gain(-85, CAL_DSP_TGT_BB_LVL); + rffe_compute_gain(-85, CAL_DSP_TGT_BB_LVL); /* Program TPU */ /* FIXME: RXWIN_PW needs to set up multiple times in case diff --git a/src/target/firmware/layer1/prim_rx_nb.c b/src/target/firmware/layer1/prim_rx_nb.c index ea9026773..7eb45483a 100644 --- a/src/target/firmware/layer1/prim_rx_nb.c +++ b/src/target/firmware/layer1/prim_rx_nb.c @@ -105,7 +105,7 @@ static int l1s_nb_resp(__unused uint8_t p1, uint8_t burst_id, uint16_t p3) toa_input(rxnb.meas[burst_id].toa_qbit << 2, rxnb.meas[burst_id].snr); /* Tell the RF frontend to set the gain appropriately */ - rffe_set_gain(rxnb.meas[burst_id].pm_dbm8/8, CAL_DSP_TGT_BB_LVL); + rffe_compute_gain(rxnb.meas[burst_id].pm_dbm8/8, CAL_DSP_TGT_BB_LVL); /* 4th burst, get frame data */ if (dsp_api.db_r->d_burst_d == 3) { diff --git a/src/target/firmware/layer1/prim_tch.c b/src/target/firmware/layer1/prim_tch.c index 013318d46..cb0da71c6 100644 --- a/src/target/firmware/layer1/prim_tch.c +++ b/src/target/firmware/layer1/prim_tch.c @@ -178,7 +178,8 @@ static int l1s_tch_resp(__unused uint8_t p1, __unused uint8_t p2, uint16_t p3) toa_input(rx_tch.meas[meas_id].toa_qbit << 2, rx_tch.meas[meas_id].snr); /* Tell the RF frontend to set the gain appropriately */ - rffe_set_gain(rx_tch.meas[meas_id].pm_dbm8 / 8, CAL_DSP_TGT_BB_LVL); + rffe_compute_gain(rx_tch.meas[meas_id].pm_dbm8 / 8, + CAL_DSP_TGT_BB_LVL); /* FACCH Block end ? */ if (tch_f_hn) { @@ -510,7 +511,8 @@ static int l1s_tch_a_resp(__unused uint8_t p1, __unused uint8_t p2, uint16_t p3) toa_input(rx_tch_a.meas[burst_id].toa_qbit << 2, rx_tch_a.meas[burst_id].snr); /* Tell the RF frontend to set the gain appropriately */ - rffe_set_gain(rx_tch_a.meas[burst_id].pm_dbm8 / 8, CAL_DSP_TGT_BB_LVL); + rffe_compute_gain(rx_tch_a.meas[burst_id].pm_dbm8 / 8, + CAL_DSP_TGT_BB_LVL); /* Last burst, read data & send to the up layer */ if ((burst_id == 3) && (dsp_api.ndb->a_cd[0] & (1< 40) + if (dbm < TRF6151_VGA_GAIN_MIN || dbm > TRF6151_VGA_GAIN_MAX) return -1; /* clear the gain bits first */ reg &= ~((0x1F) << RX_VGA_GAIN_SHIFT); /* OR-in the new gain value */ - reg |= (6 + (dbm-14)/2) << RX_VGA_GAIN_SHIFT; + reg |= (6 + (dbm-TRF6151_VGA_GAIN_MIN)/2) << RX_VGA_GAIN_SHIFT; if (high) reg |= RX_RF_GAIN_HIGH; @@ -157,6 +166,35 @@ int trf6151_set_gain(uint8_t dbm, int high) return 0; } +int trf6151_set_gain(uint8_t dbm) +{ + int high = 0; + + printd("trf6151_set_gain(%u, %d)\n", dbm); + /* If this is negative or less than TRF6151_GAIN_MIN, we are pretty + * much lost as we cannot reduce the system inherent gain. If it is + * positive, it corresponds to the gain that we need to configure */ + if (dbm < TRF6151_FE_GAIN_LOW + TRF6151_VGA_GAIN_MIN) { + printd("AGC Input level overflow\n"); + trf6151_vga_dbm = TRF6151_VGA_GAIN_MIN; + trf6151_gain_high = 0; + return 0; + } else if (dbm >= TRF6151_FE_GAIN_HIGH + TRF6151_VGA_GAIN_MIN) { + high = 1; + dbm -= TRF6151_FE_GAIN_HIGH; + } else + dbm -= TRF6151_FE_GAIN_LOW; + if (dbm > TRF6151_VGA_GAIN_MAX) + dbm = TRF6151_VGA_GAIN_MAX; + + /* update the static global variables which are used when programming + * the window */ + trf6151_vga_dbm = dbm; + trf6151_gain_high = high; + + return 0; +} + #define SCALE_100KHZ 100 /* Compute TRF6151 PLL valuese */ @@ -436,15 +474,7 @@ void trf6151_calib_dc_offs(void) tpu_enq_wait(55); } -/* Frontend gain can be switched high or low (dB) */ -#define TRF6151_FE_GAIN_LOW 7 -#define TRF6151_FE_GAIN_HIGH 27 - -/* VGA at baseband can be adjusted in this range (dB) */ -#define TRF6151_VGA_GAIN_MIN 14 -#define TRF6151_VGA_GAIN_MAX 40 - -uint8_t trf6151_get_gain(void) +uint8_t trf6151_get_gain_reg(void) { uint16_t vga, reg_rx = trf6151_reg_cache[REG_RX]; uint8_t gain = 0; @@ -467,6 +497,19 @@ uint8_t trf6151_get_gain(void) return gain; } +uint8_t trf6151_get_gain(void) +{ + uint8_t gain; + + gain = trf6151_vga_dbm; + if (trf6151_gain_high) + gain += TRF6151_FE_GAIN_HIGH; + else + gain += TRF6151_FE_GAIN_LOW; + + return gain; +} + void trf6151_test(uint16_t arfcn) { /* Select ARFCN 871 downlink */ @@ -500,7 +543,7 @@ void trf6151_tx_test(uint16_t arfcn) } #define TRF6151_REGWR_QBITS 8 /* 4 GSM qbits + 4 TPU instructions */ -#define TRF6151_RX_TPU_INSTR 4 /* set_gain(1), set_arfcn(2), set_mode(1) */ +#define TRF6151_RX_TPU_INSTR 4 /* set_gain_reg(1), set_arfcn(2), set_mode(1) */ /* delay caused by this driver programming the TPU for RX mode */ #define TRF6151_RX_TPU_DELAY (TRF6151_RX_TPU_INSTR * TRF6151_REGWR_QBITS) @@ -516,7 +559,7 @@ void trf6151_rx_window(int16_t start_qbits, uint16_t arfcn) /* Set the AGC and PLL registers */ trf6151_set_arfcn(arfcn, 0); - trf6151_set_gain(trf6151_vga_dbm, trf6151_gain_high); + trf6151_set_gain_reg(trf6151_vga_dbm, trf6151_gain_high); trf6151_set_mode(TRF6151_RX); /* FIXME: power down at the right time again */ @@ -544,36 +587,15 @@ void trf6151_tx_window(int16_t start_qbits, uint16_t arfcn) void trf6151_compute_gain(int16_t exp_inp, int16_t target_bb) { /* TRF6151 VGA gain between 14 to 40 dB, plus 20db high/low */ - int16_t exp_bb_dbm8, delta_dbm8; - int16_t exp_inp_dbm8 = to_dbm8(exp_inp); - int16_t target_bb_dbm8 = to_dbm8(target_bb); - int16_t vga_gain = TRF6151_VGA_GAIN_MIN; - int high = 0; + int16_t exp_bb, delta; /* calculate the dBm8 that we expect at the baseband */ - exp_bb_dbm8 = exp_inp_dbm8 + to_dbm8(system_inherent_gain); + exp_bb = exp_inp + system_inherent_gain; /* calculate the error that we expect. */ - delta_dbm8 = target_bb_dbm8 - exp_bb_dbm8; + delta = target_bb - exp_bb; - /* If this is negative or less than TRF6151_GAIN_MIN, we are pretty - * much lost as we cannot reduce the system inherent gain. If it is - * positive, it corresponds to the gain that we need to configure */ - if (delta_dbm8 < to_dbm8(TRF6151_FE_GAIN_LOW + TRF6151_VGA_GAIN_MIN)) { - printd("AGC Input level overflow\n"); - high = 0; - vga_gain = TRF6151_VGA_GAIN_MIN; - } else if (delta_dbm8 > to_dbm8(TRF6151_FE_GAIN_HIGH + - TRF6151_VGA_GAIN_MIN)) { - high = 1; - delta_dbm8 -= to_dbm8(TRF6151_FE_GAIN_HIGH); - } - vga_gain = delta_dbm8/8; - if (vga_gain > TRF6151_VGA_GAIN_MAX) - vga_gain = TRF6151_VGA_GAIN_MAX; - - /* update the static global variables which are used when programming - * the window */ - trf6151_vga_dbm = vga_gain; - trf6151_gain_high = high; + printd("computed gain %d\n", delta); + trf6151_set_gain(delta); } +