Commit 85ec062a authored by Matthieu Cattin's avatar Matthieu Cattin

Add gain and offset correction functions.

parent 97bc7288
......@@ -82,7 +82,7 @@ class CFmcAdc100Ms:
R_CH3_SSR = 0x4C
R_CH3_VALUE = 0x50
R_CH3_GAIN = 0x54
R_CH1_OFFSET = 0x58
R_CH3_OFFSET = 0x58
R_CH4_SSR = 0x5C
R_CH4_VALUE = 0x60
R_CH4_GAIN = 0x64
......@@ -122,6 +122,9 @@ class CFmcAdc100Ms:
IN_TERM_MASK = 0x08
IN_RANGES = {'100mV': 0x23, '1V': 0x11, '10V': 0x45, 'CAL': 0x40, 'OPEN': 0x00, 'CAL_100mV': 0x42, 'CAL_1V': 0x40, 'CAL_10V': 0x44}
dac_offset_corr = [0.0] * 4
dac_gain_corr = [1.0] * 4
def channel_addr(self, channel, reg):
if(channel < 1 or channel > 4):
......@@ -282,6 +285,43 @@ class CFmcAdc100Ms:
else:
raise Exception('Unsupported parameter, channel number from 1 to 4')
def set_dac_corr(self, gain, offset):
self.dac_offset_corr = offset
self.dac_gain_corr = gain
def dac_apply_corr(self, value, gain_corr, offset_corr):
return int((float(value) + offset_corr) * gain_corr)
# Set DC offset with gain and offset correction
# value = DAC unsigned integer value
def set_dc_offset_corrected(self, channel, value):
if(1 == channel):
#print('CH%d DAC uncorrected value: 0x%.4X')%(channel, value)
value = self.dac_apply_corr(value, self.dac_gain_corr[channel-1],self.dac_offset_corr[channel-1])
#print('gain corr: %1.9f offset corr: %1.9f')%(self.dac_gain_corr[channel-1], self.dac_offset_corr[channel-1])
#print('CH%d DAC corrected value : 0x%.4X')%(channel, value)
self.dac_ch1.set_offset(value)
elif(2 == channel):
#print('CH%d DAC uncorrected value: 0x%.4X')%(channel, value)
value = self.dac_apply_corr(value, self.dac_gain_corr[channel-1],self.dac_offset_corr[channel-1])
#print('gain corr: %1.9f offset corr: %1.9f')%(self.dac_gain_corr[channel-1], self.dac_offset_corr[channel-1])
#print('CH%d DAC corrected value : 0x%.4X')%(channel, value)
self.dac_ch2.set_offset(value)
elif(3 == channel):
#print('CH%d DAC uncorrected value: 0x%.4X')%(channel, value)
value = self.dac_apply_corr(value, self.dac_gain_corr[channel-1],self.dac_offset_corr[channel-1])
#print('gain corr: %1.9f offset corr: %1.9f')%(self.dac_gain_corr[channel-1], self.dac_offset_corr[channel-1])
#print('CH%d DAC corrected value : 0x%.4X')%(channel, value)
self.dac_ch3.set_offset(value)
elif(4 == channel):
#print('CH%d DAC uncorrected value: 0x%.4X')%(channel, value)
value = self.dac_apply_corr(value, self.dac_gain_corr[channel-1],self.dac_offset_corr[channel-1])
#print('gain corr: %1.9f offset corr: %1.9f')%(self.dac_gain_corr[channel-1], self.dac_offset_corr[channel-1])
#print('CH%d DAC corrected value : 0x%.4X')%(channel, value)
self.dac_ch4.set_offset(value)
else:
raise Exception('Unsupported parameter, channel number from 1 to 4')
# Reset DC offset DACs
def dc_offset_reset(self):
reg = self.fmc_adc_csr.rd_reg(self.R_CTL)
......@@ -523,6 +563,23 @@ class CFmcAdc100Ms:
addr = self.channel_addr(channel,self.R_CH1_VALUE)
return self.fmc_adc_csr.rd_reg(addr)
# Set channel gain and offset correction
def set_adc_gain_offset_corr(self, channel, gain, offset):
addr = self.channel_addr(channel,self.R_CH1_GAIN)
self.fmc_adc_csr.wr_reg(addr, gain)
addr = self.channel_addr(channel,self.R_CH1_OFFSET)
self.fmc_adc_csr.wr_reg(addr, offset)
# Get channel gain correction
def get_adc_gain_corr(self, channel):
addr = self.channel_addr(channel,self.R_CH1_GAIN)
return self.fmc_adc_csr.rd_reg(addr)
# Get channel offset correction
def get_adc_offset_corr(self, channel):
addr = self.channel_addr(channel,self.R_CH1_OFFSET)
return self.fmc_adc_csr.rd_reg(addr)
# Print ADC core config/status
def print_adc_core_config(self):
print("\nADC core configuration/status")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment