瀏覽代碼

Added backend code to write Bunch Shift register values
Added Bunch Shift register value offset handling to Timing Widget

Timo Dritschler 3 年之前
父節點
當前提交
868da6bb4f
共有 2 個文件被更改,包括 49 次插入8 次删除
  1. 27 3
      KCG/base/backend/board/board_config.py
  2. 22 5
      KCG/widgets/TimingWidget.py

+ 27 - 3
KCG/base/backend/board/board_config.py

@@ -73,6 +73,7 @@ class BoardConfiguration(QtGui.QWidget):
 
 
             'chip_delay_max': 31,
             'chip_delay_max': 31,
             'chip_delay' : [0,0,0,0,0,0,0,0],
             'chip_delay' : [0,0,0,0,0,0,0,0],
+            'bunch_shift' : [2,2,2,2,2,2,2,2], #Offset by +2 to encode -2 to +2 range
             'chip_delay_factor': 3,
             'chip_delay_factor': 3,
 
 
             
             
@@ -320,6 +321,7 @@ class BoardConfiguration(QtGui.QWidget):
 
 
             self.observe_write(self._set_chip_delay,   'chip_delay')
             self.observe_write(self._set_chip_delay,   'chip_delay')
             self.observe_write(self._set_samplingrate, 'samplingrate')
             self.observe_write(self._set_samplingrate, 'samplingrate')
+            self.observe_write(self._set_bunch_shift, 'bunch_shift')
 
 
             if self._config['adc_number'] > 4:
             if self._config['adc_number'] > 4:
                 #Setup Long delay 330ps. Value needs to be shifted to the left because first bit is used vor halfstep
                 #Setup Long delay 330ps. Value needs to be shifted to the left because first bit is used vor halfstep
@@ -581,7 +583,20 @@ class BoardConfiguration(QtGui.QWidget):
                 s += ' ADC_%i Fine Delay: %i,' % (adc+4, value)
                 s += ' ADC_%i Fine Delay: %i,' % (adc+4, value)
             s = s[:-1]  # cut away the last dangling ','
             s = s[:-1]  # cut away the last dangling ','
             logging.vinfo(s)
             logging.vinfo(s)
-            
+
+
+
+    def _set_bunch_shift(self, values):
+        #print("Setting bunch shifts: ", values)
+
+        base_reg = 0x9320
+
+        for i, val in enumerate(values):
+            reg = hex(base_reg+(i*4))
+            set = "0x{0:08x}".format(val)
+
+            logging.vinfo("Setting %s to %s"%(reg,set))
+            pci.write(self.identifier, set, reg)
 
 
 
 
 
 
@@ -663,7 +678,7 @@ class BoardConfiguration(QtGui.QWidget):
         if rate == 1: 
         if rate == 1: 
             #500 MHz
             #500 MHz
             self.update('delay_25_adc', 10)
             self.update('delay_25_adc', 10)
-            self.update('delay_330_adc', 1)#
+            self.update('delay_330_adc', 1)
             self.update('delay_25_adc_2', 10)
             self.update('delay_25_adc_2', 10)
             self.update('delay_330_adc_2', 1)
             self.update('delay_330_adc_2', 1)
         elif rate == 2:
         elif rate == 2:
@@ -687,13 +702,15 @@ class BoardConfiguration(QtGui.QWidget):
         sleep(0.1)
         sleep(0.1)
         self.update('chip_delay', [0,0,0,0, 0,0,0,0])
         self.update('chip_delay', [0,0,0,0, 0,0,0,0])
         sleep(0.1)
         sleep(0.1)
+        #Bunch Shifts are offset by +2 to encode -2 to +2 as 0x0 to 0x4 in hardware
+        self.update('bunch_shift', [2,2,2,2, 2,2,2,2])
+        sleep(0.1)
         self.update('adc_gain', [0,0,0,0, 0,0,0,0])
         self.update('adc_gain', [0,0,0,0, 0,0,0,0])
         sleep(0.1)
         sleep(0.1)
         self.update('header', 1)
         self.update('header', 1)
         sleep(0.1)
         sleep(0.1)
         self.update('pilot_bunch', 1)
         self.update('pilot_bunch', 1)
         logging.vinfo('Startup Config Set')
         logging.vinfo('Startup Config Set')
-
     def read_from_board(self):
     def read_from_board(self):
         """
         """
         Read values from board and update them in the configuration (Mainly used for skip init functionality)
         Read values from board and update them in the configuration (Mainly used for skip init functionality)
@@ -714,6 +731,13 @@ class BoardConfiguration(QtGui.QWidget):
                 tmp[selector[i]] = int(val[(adc_number-1-i)*2:(adc_number-i)*2], 16)
                 tmp[selector[i]] = int(val[(adc_number-1-i)*2:(adc_number-i)*2], 16)
             self.update('chip_delay', tmp , write=False)    
             self.update('chip_delay', tmp , write=False)    
 
 
+            # --[ read bunch shifts ] --
+            bunch_shifts = pci.read(self.identifier, reg='9320', amount=4, decimal=True)
+
+            if adc_number > 4:
+                bunch_shifts = bunch_shifts + pci.read(self.identifier, reg='9330', amount=4, decimal=True)
+            self.update('bunch_shift', bunch_shifts, write=False)    
+
             # --[ read and set th delay ]--
             # --[ read and set th delay ]--
             val = pci.read(self.identifier, reg='90B0')[0]
             val = pci.read(self.identifier, reg='90B0')[0]
             self.update('delay_330_th', self._config['delay_330_max'] - ((int(val, 16)>>1)-5), write=False)
             self.update('delay_330_th', self._config['delay_330_max'] - ((int(val, 16)>>1)-5), write=False)

+ 22 - 5
KCG/widgets/TimingWidget.py

@@ -187,6 +187,17 @@ class TimingPart(kcgw.KCGWidgets):
                 spinbox.setValue(value[adc])
                 spinbox.setValue(value[adc])
             spinbox.blockSignals(False)
             spinbox.blockSignals(False)
 
 
+
+        #Bunch Shift values are encoded 0x0 - 0x4 in Hardware
+        #but displayed as -2 to +2 in the GUI, so we need to offset
+        #the received register value by -2 when receiving an update
+        def setBunchShiftValuesSilent(values):
+            for i, v in enumerate(values):
+                self.bunchShiftInput[i].blockSignals(True)
+                self.bunchShiftInput[i].setValue(v-2)
+                self.bunchShiftInput[i].blockSignals(False)
+
+
         # --------[ Set observers ]------------
         # --------[ Set observers ]------------
         def obs(who, what, adc=None):
         def obs(who, what, adc=None):
             '''observe something'''
             '''observe something'''
@@ -197,8 +208,6 @@ class TimingPart(kcgw.KCGWidgets):
             )
             )
         for i, item in enumerate(self.fineAdcInput):
         for i, item in enumerate(self.fineAdcInput):
             obs(item, 'chip_delay', i)
             obs(item, 'chip_delay', i)
-        for i, item in enumerate(self.bunchShiftInput):
-            obs(item, 'bunch_shift', i)
         
         
         obs(self.coarseInputTh, 'delay_330_th')
         obs(self.coarseInputTh, 'delay_330_th')
         obs(self.coarseInputAdc, 'delay_330_adc')
         obs(self.coarseInputAdc, 'delay_330_adc')
@@ -222,6 +231,12 @@ class TimingPart(kcgw.KCGWidgets):
 
 
         #obs(self.adc1CoarseInput, 'adc_1_delay_individual')
         #obs(self.adc1CoarseInput, 'adc_1_delay_individual')
 
 
+        board.get_board_config(board_id).observe(
+                self.bunchShiftInput, 
+                setBunchShiftValuesSilent,
+                'bunch_shift'
+                )
+
         # -------[ Create outputs ]---------------
         # -------[ Create outputs ]---------------
         self.totalLabel = self.createLabel(tr("Label", "Total Delay"))
         self.totalLabel = self.createLabel(tr("Label", "Total Delay"))
         self.totalAdcBox = []
         self.totalAdcBox = []
@@ -440,8 +455,8 @@ class TimingPart(kcgw.KCGWidgets):
 
 
         for i, item in enumerate(self.fineAdcInput):
         for i, item in enumerate(self.fineAdcInput):
             self.board_config.unobserve(item, 'chip_delay')
             self.board_config.unobserve(item, 'chip_delay')
-        for i, item in enumerate(self.bunchShiftInput):
-            self.board_config.unobserve(item, 'bunch_shift')
+
+        self.board_config.unobserve(self.bunchShiftInput, 'bunch_shift')
 
 
 
 
         self.board_config.unobserve(self.coarseInputTh, 'delay_330_th')
         self.board_config.unobserve(self.coarseInputTh, 'delay_330_th')
@@ -520,7 +535,9 @@ class TimingPart(kcgw.KCGWidgets):
         try:
         try:
             factors = []
             factors = []
             for item in self.bunchShiftInput:
             for item in self.bunchShiftInput:
-                factors.extend([item.value()])
+                #Bunch Shifts are encoded 0x0 - 0x4 in Hardware
+                #but shown as -2 to +2 in GUI, so we need a +2 offset
+                factors.extend([item.value()+2])
             
             
             self.board_config.update('bunch_shift', factors)
             self.board_config.update('bunch_shift', factors)
         except board.BoardError as e:
         except board.BoardError as e: