diff --git a/gateware/cal/cal.py b/gateware/cal/cal.py index e450d46..6aeb3fe 100755 --- a/gateware/cal/cal.py +++ b/gateware/cal/cal.py @@ -49,12 +49,18 @@ # Set this to False for the latter case. CAL_OPEN_LOAD = True -def twos_comp(val, bits): +def twos_comp(val, bits=16): """compute the 2's complement of int value val""" if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255 val = val - (1 << bits) # compute negative value return val # return positive value as is +def bit_not(n, numbits=16): + return (1 << numbits) - 1 - n + +def signed_to_twos_comp(n, numbits=16): + return n if n >= 0 else bit_not(-n, numbits) + 1 + ser = serial.Serial(SERIAL_PORT, 1000000) adc_avg = np.zeros(4) @@ -139,8 +145,8 @@ def decode_raw_samples(n, raw, array_avg): print("Average raw ADC counts converted to voltages using current input calibration") cal_mem = [int(x, 16) for x in input_cal_string.strip().split(' ')[1:]] for channel in range(4): - calibrated = ((-adc_avg[channel] - cal_mem[channel*2]) * - cal_mem[channel*2 + 1]) / (1 << MP_N_BITS) + calibrated = ((-adc_avg[channel] - twos_comp(cal_mem[channel*2])) * + twos_comp(cal_mem[channel*2 + 1])) / (1 << MP_N_BITS) adc_calibrated_avg[channel] = calibrated print(f"in{channel}",round(calibrated / COUNT_PER_VOLT, ndigits=3), "V") @@ -162,14 +168,17 @@ def decode_raw_samples(n, raw, array_avg): shift_constant = (n5v_dac_fb_avg + p5v_dac_fb_avg)/2. shift_constant = shift_constant * range_constant + def conv(constant): + return hex(signed_to_twos_comp(int(constant))).replace('0x','') + print() print("CALIBRATION MEMORY ('x' to exit, copy this to 'cal_mem.hex')\n") cal_string = None if np.isfinite(shift_constant).all() and np.isfinite(mp_constant).all(): cal_string = f"@0000000{'0' if input_cal else '8'} " for i in range(4): - cal_string = cal_string + hex(int(shift_constant[i])).replace('0x','') + ' ' - cal_string = cal_string + hex(int(mp_constant[i])).replace('0x','') + ' ' + cal_string = cal_string + conv(shift_constant[i]) + ' ' + cal_string = cal_string + conv(mp_constant[i]) + ' ' if input_cal: input_cal_string = cal_string else: diff --git a/gateware/cal/cal_mem.hex b/gateware/cal/cal_mem.hex index ea8c880..0559c26 100644 --- a/gateware/cal/cal_mem.hex +++ b/gateware/cal/cal_mem.hex @@ -1,4 +1,4 @@ // Input calibration constants -@00000000 11c0 650 10b2 64f 114a 650 1134 64f +@00000000 ff63 484 ff4a 485 40 48a ff74 485 // Output calibration constants -@00000008 f6e 3ed e53 3ef f3d 3ee fa0 3ef +@00000008 fd3f 3e5 fda5 3e8 fdc8 3ee fd15 3ec