@@ -153,25 +153,44 @@ void PGASensorManager::setupSensor(int sensorId)
153153 spiRegWrite (sensorId, PGA_REG_CURR_LIM_P1, PGA_DIS_CL (0 ) | PGA_CURR_LIM1 (0 )); // CURR_LIM1*7mA + 50mA
154154 spiRegWrite (sensorId, PGA_REG_CURR_LIM_P2, PGA_LPF_CO (0 ) | PGA_CURR_LIM2 (0 )); // CURR_LIM1*7mA + 50mA
155155 spiRegWrite (sensorId, PGA_REG_REC_LENGTH, PGA_P1_REC (8 ) | PGA_P2_REC (0 )); // Record time = 4.096 × (Px_REC + 1) [ms], 8 = 36,9ms = 6m range
156- spiRegWrite (sensorId, PGA_REG_DECPL_TEMP, PGA_AFE_GAIN_RNG (1 ) | PGA_LPM_EN (0 ) | PGA_DECPL_TEMP_SEL (0 ) | PGA_DECPL_T (0 )); // Time = 4096 × (DECPL_T + 1) [μs], 0 = 4ms = 0,66m?!?
156+ spiRegWrite (sensorId, PGA_REG_DECPL_TEMP, PGA_AFE_GAIN_RNG (3 ) | PGA_LPM_EN (0 ) | PGA_DECPL_TEMP_SEL (0 ) | PGA_DECPL_T (0 )); // Time = 4096 × (DECPL_T + 1) [μs], 0 = 4ms = 0,66m?!?
157157 spiRegWrite (sensorId, PGA_REG_EE_CNTRL, PGA_DATADUMP_EN (0 )); // Disable data dump
158158
159+ // Gain map
160+ // Gain = 0.5 × (TVGAIN+1) + value(AFE_GAIN_RNG) [dB]
161+ // TVGAIN is 0..63, time is any TH_TIME_DELTA_*
162+ // The gain map is applied for both profiles.
163+ // Times are given in deltas to previous time, except the first one
164+ // So there is a constant gain of g0 from 0 to t0, then a linear interpolation from g0 to g1 between t0 and t1,
165+ // then another linear interpolation from g1 to g2 between t1 and t2, and so on. After t6, the gain is constant at g6.
166+ // I think the INIT_GAIN value is ignored, if TVGAIN is used, but AFE_GAIN_RNG still applies.
167+ PGATVGain gains (
168+ TH_TIME_DELTA_2000US, 0 ,
169+ TH_TIME_DELTA_5200US, 40 ,
170+ TH_TIME_DELTA_5200US, 45 ,
171+ TH_TIME_DELTA_5200US, 50 ,
172+ TH_TIME_DELTA_5200US, 55 ,
173+ TH_TIME_DELTA_5200US, 60
174+ );
175+ spiRegWriteGains (sensorId, gains);
176+
159177 // Threshold map, then check DEV_STAT0.THR_CRC_ERR
160- // th_t = np.array([2000, 5200, 2400, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000])
161- // th_l = np.array([200, 80, 16, 16, 16, 24, 24, 24, 24, 24, 24, 24])
178+ // The first 8 values are only 5 bit, so the 0..255 value has to be devided by 8.
179+ // th_t = np.array([1000, 2000, 2400, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000])
180+ // th_l = np.array([255, 80, 30, 30, 30, 25, 25, 20, 20, 20, 20, 20])
162181 PGAThresholds thresholds (
163- TH_TIME_DELTA_2000US, 255 /8 ,
164- TH_TIME_DELTA_4000US , 80 /8 ,
165- TH_TIME_DELTA_2400US, 24 /8 ,
166- TH_TIME_DELTA_8000US, 24 /8 ,
167- TH_TIME_DELTA_8000US, 24 /8 ,
168- TH_TIME_DELTA_8000US, 32 /8 ,
169- TH_TIME_DELTA_8000US, 32 /8 ,
170- TH_TIME_DELTA_8000US, 32 /8 ,
171- TH_TIME_DELTA_8000US, 32 ,
172- TH_TIME_DELTA_8000US, 32 ,
173- TH_TIME_DELTA_8000US, 32 ,
174- TH_TIME_DELTA_8000US, 32
182+ TH_TIME_DELTA_1000US, 240 /8 ,
183+ TH_TIME_DELTA_2000US , 80 /8 ,
184+ TH_TIME_DELTA_2400US, 30 /8 ,
185+ TH_TIME_DELTA_8000US, 30 /8 ,
186+ TH_TIME_DELTA_8000US, 30 /8 ,
187+ TH_TIME_DELTA_8000US, 25 /8 ,
188+ TH_TIME_DELTA_8000US, 25 /8 ,
189+ TH_TIME_DELTA_8000US, 20 /8 ,
190+ TH_TIME_DELTA_8000US, 20 ,
191+ TH_TIME_DELTA_8000US, 20 ,
192+ TH_TIME_DELTA_8000US, 20 ,
193+ TH_TIME_DELTA_8000US, 20
175194 );
176195 spiRegWriteThesholds (sensorId, 1 , thresholds);
177196 spiRegWriteThesholds (sensorId, 2 , thresholds);
@@ -267,6 +286,20 @@ void PGASensorManager::spiRegWrite(uint8_t sensorId, uint8_t reg_addr, uint8_t v
267286 safe_usleep (5 );
268287}
269288
289+ void PGASensorManager::spiRegWriteGains (uint8_t sensorId, PGATVGain &gains)
290+ {
291+ assert (sensorId >= 0 && sensorId <= 1 );
292+
293+ // The order of the time/gain values is a bit messy, so we have to do some bit shifting to write them to the registers
294+ spiRegWrite (sensorId, PGA_REG_TVGAIN0, gains.t0 <<4 | gains.t1 );
295+ spiRegWrite (sensorId, PGA_REG_TVGAIN1, gains.t2 <<4 | gains.t3 );
296+ spiRegWrite (sensorId, PGA_REG_TVGAIN2, gains.t4 <<4 | gains.t5 );
297+ spiRegWrite (sensorId, PGA_REG_TVGAIN3, gains.g1 <<2 | gains.g2 >>4 );
298+ spiRegWrite (sensorId, PGA_REG_TVGAIN4, gains.g2 <<4 | gains.g3 >>2 );
299+ spiRegWrite (sensorId, PGA_REG_TVGAIN5, gains.g3 <<6 | gains.g4 );
300+ spiRegWrite (sensorId, PGA_REG_TVGAIN6, gains.g5 <<2 );
301+ }
302+
270303void PGASensorManager::spiRegWriteThesholds (uint8_t sensorId, uint8_t preset, PGAThresholds &thresholds)
271304{
272305 assert (sensorId >= 0 && sensorId <= 1 );
0 commit comments