class gpio_values_setter(gpio_values): def mask_f(num): return BoolField('gpio%d_mask' % num) set_mask_pattern = [ padding(1), mask_f(10), mask_f(9), mask_f(8), mask_f(7), mask_f(6), padding(1), mask_f(5), mask_f(4), mask_f(3), mask_f(2), mask_f(1), mask_f(0), padding(3) ] pattern = gpio_values.pattern + set_mask_pattern def set_level(self, num, level): attr = 'gpio%d_level' % num setattr(self, attr, level) def _set_mask(self, num): attr = 'gpio%d_mask' % num setattr(self, attr, True) def _set_field(self, index, value): super(gpio_values_setter, self)._set_field(index, value) self._set_mask(10 - index)
class gpio_values(Register): def level_f(num): return DictField('gpio%d_level' % num, 'uint:1', { LogicLevel.LOW: 0, LogicLevel.HIGH: 1 }) pattern = [ padding(1), level_f(10), level_f(9), level_f(8), level_f(7), level_f(6), padding(1), level_f(5), level_f(4), level_f(3), level_f(2), level_f(1), level_f(0), padding(3) ] def level(self, num): attr = 'gpio%d_level' % num return getattr(self, attr)
class usb_config_setter(usb_config): write_transfer_priority = BoolField('write_transfer_priority') write_release_version = BoolField('write_release_version') write_power_mode = BoolField('write_power_mode') write_max_power_2mA = BoolField('write_max_power_2mA') write_pid = BoolField('write_pid') write_vid = BoolField('write_vid') write_mask_pattern = [ write_transfer_priority, padding(2), write_release_version, write_power_mode, write_max_power_2mA, write_pid, write_vid ] pattern = usb_config.pattern + write_mask_pattern def _set_field(self, index, value): super(usb_config_setter, self)._set_field(index, value) if index == 0: self.write_vid = True elif index == 1: self.write_pid = True elif index == 2: self.write_max_power_2mA = True elif index == 3: self.write_power_mode = True elif index == 4: self.write_release_version = True elif index == 5: self.write_release_version = True elif index == 6: self.write_transfer_priority = True
class lock(Register): def lock_field(name): return DictField(name, 'uint:1', { LockState.LOCKED: 0, LockState.UNLOCKED: 1 }) transfer_priority = lock_field('transfer_priority') manufacturing_string1 = lock_field('manufacturing_string1') manufacturing_string2 = lock_field('manufacturing_string2') release_version = lock_field('release_version') power_mode = lock_field('power_mode') max_power = lock_field('max_power') pid = lock_field('pid') vid = lock_field('vid') pin_config = lock_field('pin_config') serial_string = lock_field('serial_string') product_string1 = lock_field('product_string1') product_string2 = lock_field('product_string2') pattern = [ transfer_priority, manufacturing_string1, manufacturing_string2, release_version, power_mode, max_power, pid, vid, padding(4), pin_config, serial_string, product_string2, product_string1 ]
class event_counter(Register): overflow = BoolField('overflow') mode = DictField( 'mode', 'uint:3', { EventCounterMode.RISING_EDGE: 4, EventCounterMode.FALLING_EDGE: 5, EventCounterMode.NEGATIVE_PULSE: 6, EventCounterMode.POSITIVE_PULSE: 7 }) count = Field('count', 'uint:8') pattern = [overflow, padding(4), mode, count]
class spi_delay(Register): cs_toggle = BoolField('cs_toggle') pre_deassert = BoolField('pre_deassert') post_assert = BoolField('post_assert') inter_byte = BoolField('inter_byte') inter_byte_delay_10us = IntField('inter_byte_delay_10us', 'uintbe:16') post_assert_delay_10us = IntField('post_assert_delay_10us', 'uintbe:16') pre_deassert_delay_10us = IntField('pre_deassert_delay_10us', 'uintbe:16') pattern = [ padding(4), cs_toggle, pre_deassert, post_assert, inter_byte, inter_byte_delay_10us, post_assert_delay_10us, pre_deassert_delay_10us ]
class all_gpio_chip_select(Register): def cs(num): return BoolField('channel%d_enable' % num) pattern = [ padding(8), padding(8), padding(1), cs(10), cs(9), cs(8), cs(7), cs(6), padding(1), cs(5), cs(4), cs(3), cs(2), cs(1), cs(0), padding(3) ]
class all_gpio_mode_and_level(Register): def mode_f(num): return DictField('gpio%d_mode' % num, 'uint:1', { OutputMode.OPEN_DRAIN: 0, OutputMode.PUSH_PULL: 1 }) def level_f(num): return DictField('gpio%d_level' % num, 'uint:1', { LogicLevel.LOW: 0, LogicLevel.HIGH: 1 }) pattern = [ level_f(4), level_f(3), level_f(2), level_f(1), level_f(0), padding(3), padding(1), level_f(10), level_f(9), level_f(8), level_f(7), level_f(6), padding(1), level_f(5), mode_f(4), mode_f(3), mode_f(2), mode_f(1), mode_f(0), padding(3), padding(1), mode_f(10), mode_f(9), mode_f(8), mode_f(7), mode_f(6), padding(1), mode_f(5) ] def mode(self, num): attr = 'gpio%d_mode' % num return getattr(self, attr) def level(self, num): attr = 'gpio%d_level' % num return getattr(self, attr)
def mask_pattern(func): return [ padding(1), func('gpio10'), func('gpio9'), func('gpio8'), func('gpio7'), func('gpio6'), func('vpp'), func('gpio5'), func('gpio4'), func('gpio3'), func('gpio2'), func('gpio1'), func('gpio0'), func('mosi'), func('miso'), func('sck') ]
class spi_word(Register): clock_phase = DictField('clock_phase', 'uint:1', { ClockPhase.LEADING_EDGE: 0, ClockPhase.TRAILING_EDGE: 1 }) clock_polarity = DictField('clock_polarity', 'uint:1', { ClockPolarity.IDLE_LOW: 0, ClockPolarity.IDLE_HIGH: 1 }) chip_select_mode = DictField('chip_select_mode', 'uint:1', { OutputMode.OPEN_DRAIN: 0, OutputMode.PUSH_PULL: 1 }) clock_frequency = DiscreteRangeField( 'clock_frequency', 'uint:3', operator.ge, [12000000, 6000000, 3000000, 1500000, 750000, 375000, 187500, 93800]) pattern = [ padding(2), clock_phase, clock_polarity, chip_select_mode, clock_frequency ]
class serial_string(Register): length = Field('length', 'uint:8') descriptor_type = ConstantField('descriptor_type', 'uint:8', 0x03) string = BytesField('string', 60) pattern = [length, descriptor_type, string, padding(8)]
class product_string2(Register): string = BytesField('string', 63) pattern = [string, padding(8)]
class manufacturing_string2(Register): string = BytesField('string', 63) pattern = [string, padding(8)]
class manufacturing_string1(Register): length = Field('length', 'uint:8') descriptor_type = ConstantField('descriptor_type', 'uint:8', 0x03) string = BytesField('string', 61) pattern = [length, descriptor_type, string, padding(8)]