Ejemplo n.º 1
0
class MIXBMUPWMSGR(object):
    '''
    Mix BMU PWM function class

    ClassType = MIXBMUPWMSGR

    Args:
        axi4_bus:    instance(AXI4LiteBus)/string, AXI4LiteBus class intance or device path.

    Examples:
        bmu_pwm = MIXBMUPWMSGR('/dev/MIX_SignalSource_SG')

    '''

    rpc_public_api = ['signal_output', 'open', 'close']

    def __init__(self, axi4_bus):
        if isinstance(axi4_bus, basestring):
            # device path; create axi4lite instance
            self.axi4_bus = AXI4LiteBus(axi4_bus, MIXBMUPWMSGRDef.REG_SIZE)
        else:
            self.axi4_bus = axi4_bus

        axi4_gpio = AXI4LiteSubBus(self.axi4_bus,
                                   MIXBMUPWMSGRDef.MIX_GPIO_IPCORE_ADDR,
                                   MIXBMUPWMSGRDef.GPIO_REG_SIZE)
        axi4_signalsource = AXI4LiteSubBus(self.axi4_bus,
                                           MIXBMUPWMSGRDef.MIX_SS_IPCORE_ADDR,
                                           MIXBMUPWMSGRDef.SS_REG_SIZE)

        self.gpio = MIXGPIOSG(axi4_gpio)
        self.signalsource = MIXSignalSourceSG(axi4_signalsource)
        self.gpio.set_pin_dir(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT0, 'output')
        self.gpio.set_pin_dir(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT1, 'output')

    def close(self):
        '''
        Disable mix BMU PWM function class

        Examples:
            bmu_pwm.close()

        '''
        self.signalsource.close()
        self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT0, 0)
        self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT1, 0)

    def open(self):
        '''
        Enable mix BMU PWM function class

        Examples:
            bmu_pwm.open()

        '''
        self.signalsource.open()
        self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT0, 0)
        self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT1, 0)

    def signal_output(self,
                      signal_frequency,
                      square_duty,
                      signal_time=0xffffffff):
        '''
        Set mix BMU PWM parameters and output.

        Args:
            signal_frequency:   int, unit Hz,              output signal frequency.
            square_duty:        float, [0.001~0.999],      duty of square.
            signal_time:        int, unit us, signal time of signal source.

        Return:
            "done"

        '''
        assert 1 >= square_duty >= 0
        self.signal_time = signal_time
        self.signal_frequency = signal_frequency
        self.square_duty = square_duty

        if square_duty == MIXBMUPWMSGRDef.PWM_OUTPUT_LOW:
            self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT0, 0)
            self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT1, 0)
        elif square_duty == MIXBMUPWMSGRDef.PWM_OUTPUT_HIGH:
            self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT0, 0)
            self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT1, 1)
        else:
            self.gpio.set_pin(MIXBMUPWMSGRDef.CHANNEL_SELECT_BIT0, 1)
            self.signalsource.set_signal_type('square')
            self.signalsource.set_signal_time(self.signal_time)
            self.signalsource.set_swg_paramter(
                sample_rate=125000000,
                signal_frequency=self.signal_frequency,
                vpp_scale=0.5,
                square_duty=self.square_duty)
            self.signalsource.output_signal()
        return "done"
Ejemplo n.º 2
0
class MIXBMUWidthSwitcherSGR(object):
    '''
    MIXBMUWidthSwitcherSGR function class to measure the time difference between edge signals

    ClassType = MIXBMUWidthSwitcherSGR

    Args:
        axi4_bus: instance(AXI4LiteBus)/string,   Class instance or dev path of AXI4 bus.

    Examples:
        width_measure = MIXBMUWidthSwitcherSGR('/dev/MIX_Signal_Meter_0')

    '''

    rpc_public_api = ['start_measure', 'stop_measure', 'open', 'close']

    def __init__(self, axi4_bus):
        if isinstance(axi4_bus, basestring):
            # device path; create axi4lite instance
            self.axi4_bus = AXI4LiteBus(axi4_bus,
                                        MIXBMUWidthSwitcherSGRDef.REG_SIZE)
        else:
            self.axi4_bus = axi4_bus

        axi4_gpio = AXI4LiteSubBus(
            self.axi4_bus, MIXBMUWidthSwitcherSGRDef.MIX_GPIO_IPCORE_ADDR,
            MIXBMUWidthSwitcherSGRDef.GPIO_REG_SIZE)
        axi4_widthmeasure = AXI4LiteSubBus(
            self.axi4_bus, MIXBMUWidthSwitcherSGRDef.MIX_WM_IPCORE_ADDR,
            MIXBMUWidthSwitcherSGRDef.WM_REG_SIZE)

        self.gpio = MIXGPIOSG(axi4_gpio)
        self.widthmeasure = MIXWidthMeasureSG(axi4_widthmeasure)
        self.gpio.set_pin_dir(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT0,
                              'output')
        self.gpio.set_pin_dir(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT1,
                              'output')
        self.gpio.set_pin_dir(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT2,
                              'output')

    def close(self):
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT0, 0)
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT1, 0)
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT2, 0)
        self.widthmeasure.stop_measure()

    def open(self):
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT0, 0)
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT1, 0)
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT2, 0)

    def start_measure(self, channel_select, start_status, stop_status):
        '''
        MIXBMUWidthSwitcherSGR module enable the corresponding register, then can get result

        Args:
            channel_select: int,  select the channel to measure.
            start_status:   int,  start trigger signal, details in class TriggarSignalDef.
            stop_status:    int,  stop trigger signal, details in class TriggarSignalDef.

        Returns:
            "done"

        '''
        assert isinstance(channel_select, int)

        a = bin(channel_select)
        a = a[2:]
        a = list(a)
        a.reverse()
        if 3 - len(a) == 0:
            a = a
        else:
            for i in range(3 - len(a)):
                a.append('0')
        a.reverse()
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT0,
                          int(a[2]))
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT1,
                          int(a[1]))
        self.gpio.set_pin(MIXBMUWidthSwitcherSGRDef.CHANNEL_SELECT_BIT2,
                          int(a[0]))

        self.widthmeasure.config(start_triggar_signal=start_status,
                                 stop_triggar_signal=stop_status)
        self.widthmeasure.start_measure()
        return "done"

    def stop_measure(self, time_out):
        '''
        MIXWidthMeasureSG module disable the corresponding register, and then get time measure result.

        Args:
            time_out: int,  overtime of measure, unit:ms.

        Returns:
            list,  list of width values.

        '''
        self.time_out = time_out * 1000

        start_time = time.time() * 1000
        while 1:
            try:
                wid = self.widthmeasure.get_width()
                result = []
                for i in range(len(wid)):
                    result.append(wid[i].width)
                return result
            except Exception as e:
                if time.time() * 1000 - start_time > self.time_out:
                    raise MIXBMUWidthSwitcherSGRException("Timeout: {}".format(
                        e.message))
            finally:
                self.widthmeasure.stop_measure()
Ejemplo n.º 3
0
class DM634(object):
    '''
    DM634 function class

    ClassType = DAC

    Args:
        axi4_bus:     instance(AXI4LiteBus)/string/None,  AXI4LiteBus class intance or device name;
                                                          if None, will create emulator
        gpio_device:  instance(GPIO)/string/None,  MIXGPIOSG device to control data and clock output;
                                                   if None, will create emulator.
        gpio_id:      int, gpio pin id to control data and clock output.

    Examples:
        dm634 = DM634('/dev/MIX_DM634_CTRL', '/dev/MIX_DUT1_GPIO0', 0)

    '''
    def __init__(self, axi4_bus=None, gpio=None, gpio_id=0):
        if gpio is None:
            self.gpio = MIXGPIOSGEmulator('dm634_gpio_emulator',
                                          PLGPIODef.REG_SIZE)
        elif isinstance(gpio, basestring):
            # device path; create MIXGPIOSG instance here.
            self.gpio = MIXGPIOSG(gpio)
        else:
            self.gpio = gpio

        self.gpio_id = gpio_id

        if axi4_bus is None:
            self.axi4_bus = AXI4LiteBusEmulator('dm634_axi4_emulator',
                                                DM634Def.REG_SIZE)
        elif isinstance(axi4_bus, basestring):
            # device path; create axi4litebus instance here.
            self.axi4_bus = AXI4LiteBus(axi4_bus, DM634Def.REG_SIZE)
        else:
            self.axi4_bus = axi4_bus

        self.channel_value_list = [[i, 0]
                                   for i in range(0, DM634Def.MAX_CHAN_NUM)]

        self.set_mode('normal')

    def write(self, wr_data):
        '''
        DM634 internal function to write data

        Args:
            wr_data:   list,   data format [sck, dai, latch, 0x00].

        Examples:
            dm634.write([0x55, 0x01, 0x01, 0x00])

        '''
        assert len(wr_data) > 0
        assert len(wr_data) % 4 == 0

        sent = 0
        while sent < len(wr_data):
            tx_buf_count = self.axi4_bus.read_16bit_inc(
                DM634Def.TXBUF_COUNT_REGISTER, 1)[0]
            if len(wr_data) - sent > tx_buf_count * DM634Def.DATA_WIDTH:
                wr_len = tx_buf_count * DM634Def.DATA_WIDTH
            else:
                wr_len = len(wr_data) - sent

            send_data = [
                wr_data[i] | (wr_data[i + 1] << 8) | (wr_data[i + 2] << 16) |
                (wr_data[i + 3] << 24) for i in range(sent, sent + wr_len, 4)
            ]
            self.axi4_bus.write_32bit_fix(DM634Def.TXBUF_REGISTER, send_data)
            sent += wr_len

    def _shift_channel(self, ch_value):
        '''
        DM634 shift channel value to port

        Args:
            ch_value:    hexmial, [0~0xffff], channel output value.

        Examples:
            dm634._shift_channel(0x01)

        '''
        # ch_value is 16 bit data, send 4 bits every write cycle
        assert ch_value >= 0 and ch_value <= 0xFFFF
        for i in range(4):
            ch_4bit_index = (ch_value >> (12 - i * 4)) & 0x0F
            dai = DM634Def.ch_wave_4bit_tab[ch_4bit_index]
            self.write([0x55, dai, 0x00, 0x00])

    def get_mode(self):
        '''
        DM634 get mode function

        Examples:
            mode = dm634.get_mode()
            print(mode)

        '''
        return self._mode

    def set_mode(self, mode):
        '''
        DM634 set mode function

        Args:
            mode:   string, ['normal', 'gck'], normal mode use internal clock,
                                               gck mode use external clock.

        Examples:
            dm634.set_mode('normal')

        '''
        assert mode in ['normal', 'gck']
        self._mode = mode
        self.gpio.set_pin(self.gpio_id, 0)

        if mode == 'normal':
            # set DCK high and LAT low
            self.write([0xFF, 0x00, 0x00, 0x00])

            # give 4 LAT
            self.write([0xFF, 0x00, 0x55, 0x00])

            # set DCK and LAT high
            self.write([0xFF, 0x00, 0xFF, 0x00])

            # give 3 DCK
            self.write([0x2A, 0x00, 0xFF, 0x00])

            # set LAT high and DCK low
            self.write([0x00, 0x00, 0xFF, 0x00])

            # set LAT and DCK low
            self.write([0x00, 0x00, 0x00, 0x00])
        else:
            # set DCK high and LAT low
            self.write([0xFF, 0x00, 0x00, 0x00])

            # give 2 LAT and then set LAT high
            self.write([0xFF, 0x00, 0x57, 0x00])

            # LAT high and give 2 DCK then set DCK low
            self.write([0x0A, 0x00, 0xFF, 0x00])

            # set LAT high
            self.write([0x00, 0x00, 0xFF, 0x00])

            # set DCK and LAT low
            self.write([0x00, 0x00, 0x00, 0x00])

        self.gpio.set_pin(self.gpio_id, 1)

    def set_channels(self, ch_list):
        '''
        DM634 set channels output value

        Args:
            ch_list:   list,    channel value list to output,eg
                                [(chX, valueY),...], X in [0,...,15].

        Examples:
            set channel 0 value 0xfff and channel 1 value 0xffff
            dm634.set_channels([(0, 0xfff), (1, 0xffff)])

        '''
        assert isinstance(ch_list, list)
        assert len(ch_list) > 0 and len(ch_list) <= DM634Def.MAX_CHAN_NUM

        for ch in ch_list:
            if ch[0] < 0 or ch[0] >= DM634Def.MAX_CHAN_NUM:
                raise DM634Exception("Channel index %d not in [0-15]." %
                                     (ch[0]))
            self.channel_value_list[ch[0]][1] = ch[1]

        self.gpio.set_pin(self.gpio_id, 0)

        # write data to DAI
        for i in range(16):
            self._shift_channel(self.channel_value_list[15 - i][1])

        # latch data
        self.write([0x00, 0x00, 0x01, 0x00])

        self.write([0x00, 0x00, 0x00, 0x00])

        self.gpio.set_pin(self.gpio_id, 1)

    def set_all_channel_brightness(self, value):
        '''
        DM634 set all channel brightness

        Args:
            value:   int, [0~127], global brightness.

        Examples:
            dm634.set_all_channel_brightness(100)

        '''
        assert value >= 0 and value <= 127

        self.gpio.set_pin(self.gpio_id, 0)

        # set DCK high and LAT low
        self.write([0xFF, 0x00, 0x00, 0x00])

        # give 4 LAT when DCK is high
        self.write([0xFF, 0x00, 0x55, 0x00])

        # set LAT and DCK low
        self.write([0x00, 0x00, 0x00, 0x00])

        # write brightness data
        brightness = value << 1
        dai = DM634Def.ch_wave_4bit_tab[(brightness >> 4) & 0x0F]
        self.write([0x55, dai, 0x00, 0x00])
        dai = DM634Def.ch_wave_4bit_tab[(brightness & 0x0F)]
        self.write([0x55, dai, 0x00, 0x00])

        # latch data
        self.write([0x00, 0x00, 0x01, 0x00])

        self.write([0x00, 0x00, 0x00, 0x00])

        self.gpio.set_pin(self.gpio_id, 1)