def create_simple_regs(): """Create siple reg structure with basic cases.""" regs = Registers(TEST_DEVICE_NAME) reg1 = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) reg2 = RegsRegister( TEST_REG_NAME + "_2", TEST_REG_OFFSET + 4, TEST_REG_WIDTH, TEST_REG_DESCR + "_2", TEST_REG_REV, TEST_REG_ACCESS, ) bitfield1 = RegsBitField( reg2, TEST_BITFIELD_NAME, TEST_BITFIELD_OFFSET, TEST_BITFIELD_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS, ) bitfield2 = RegsBitField( reg2, TEST_BITFIELD_NAME + "_2", TEST_BITFIELD_OFFSET + TEST_BITFIELD_WIDTH, 1, ".", 0, TEST_BITFIELD_ACCESS, ) enum1 = RegsEnum(TEST_ENUM_NAME, 0, TEST_ENUM_DESCR, 1) enum2 = RegsEnum(TEST_ENUM_NAME + "_2", 0, TEST_ENUM_DESCR + "_2", 1) bitfield2.add_enum(enum1) bitfield2.add_enum(enum2) reg2.add_bitfield(bitfield1) reg2.add_bitfield(bitfield2) regs.add_register(reg1) regs.add_register(reg2) return regs
def test_bitfield_has_enums(): """Test bitfield has enums function.""" parent_reg = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) bitfield = RegsBitField( parent_reg, TEST_BITFIELD_NAME, TEST_BITFIELD_OFFSET, TEST_BITFIELD_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS, ) parent_reg.add_bitfield(bitfield) assert bitfield.has_enums() is False enum = RegsEnum(TEST_ENUM_NAME, 0, TEST_ENUM_DESCR) bitfield.add_enum(enum) assert bitfield.has_enums() is True assert enum in bitfield.get_enums()
def _get_enums(self, bitfield: Any, excel_row: int, excel_row_cnt: int) -> None: """Tried to find and fill up all register bitfields enumerations.""" assert self.worksheet if excel_row_cnt <= 1: # There is no enums return enum_name_cr = cell_utils.coordinate_from_string( self.header_cells["Enum Name"]) desc_cr = cell_utils.coordinate_from_string( self.header_cells["Description"]) value_cr = cell_utils.coordinate_from_string( self.header_cells["Value"]) excel_row += 1 excel_row_cnt -= 1 for row in range(excel_row, excel_row + excel_row_cnt): cell = enum_name_cr[0] + str(row) if isinstance(self.worksheet[cell].value, str): enum_name = self.worksheet[cell].value enum_descr = self.worksheet[desc_cr[0] + str(row)].value or "N/A" enum_value: str = self.worksheet[value_cr[0] + str(row)].value enum_descr = enum_descr.replace("\n", " ") if enum_value is None: click.echo( f"Warning: The Enum {enum_name} is missing and it will be skipped." ) else: bitfield.add_enum( RegsEnum(enum_name, enum_value, enum_descr, bitfield.width))
def test_register(): parent_reg = RegsRegister(TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS) bitfield = RegsBitField(parent_reg, TEST_BITFIELD_NAME, TEST_BITFILED_OFFSET, TEST_BITFILED_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS) enum = RegsEnum(TEST_ENUM_NAME, 0, TEST_ENUM_DESCR) bitfield.add_enum(enum) parent_reg.add_bitfield(bitfield) printed_str = str(parent_reg) assert "Name:" in printed_str assert TEST_REG_NAME in printed_str assert TEST_REG_DESCR in printed_str assert "Width:" in printed_str assert "Access:" in printed_str assert "Bitfield" in printed_str assert TEST_BITFIELD_NAME in printed_str assert TEST_BITFIELD_DESCR in printed_str assert TEST_ENUM_NAME in printed_str assert TEST_ENUM_DESCR in printed_str
def test_bitfield_enums_invalid_name(): """Test bitfield enums with invalid enum name.""" parent_reg = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) bitfield = RegsBitField( parent_reg, TEST_BITFIELD_NAME, TEST_BITFIELD_OFFSET, TEST_BITFIELD_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS, ) parent_reg.add_bitfield(bitfield) bitfield.add_enum( RegsEnum(f"{TEST_ENUM_NAME}", 0, f"{TEST_ENUM_DESCR}", TEST_BITFIELD_WIDTH)) with pytest.raises(SPSDKError): bitfield.set_enum_value(f"Invalid Enum name")
def test_bitfield_find(): """Test bitfield find function.""" parent_reg = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) bitfield = RegsBitField( parent_reg, TEST_BITFIELD_NAME, TEST_BITFIELD_OFFSET, TEST_BITFIELD_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS, ) enum = RegsEnum(TEST_ENUM_NAME, 0, TEST_ENUM_DESCR) bitfield.add_enum(enum) parent_reg.add_bitfield(bitfield) assert bitfield == parent_reg.find_bitfield(TEST_BITFIELD_NAME) with pytest.raises(SPSDKRegsErrorBitfieldNotFound): parent_reg.find_bitfield("Invalid Name")
def test_bitfield(): """Basic bitfield test.""" parent_reg = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) bitfield = RegsBitField( parent_reg, TEST_BITFIELD_NAME, TEST_BITFIELD_OFFSET, TEST_BITFIELD_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS, ) enum = RegsEnum(TEST_ENUM_NAME, 0, TEST_ENUM_DESCR) bitfield.add_enum(enum) parent_reg.add_bitfield(bitfield) printed_str = str(bitfield) assert "Name:" in printed_str assert "Offset:" in printed_str assert "Width:" in printed_str assert "Access:" in printed_str assert "Reset val:" in printed_str assert "Description:" in printed_str assert "Enum" in printed_str
def test_enum_invalidval(): try: enum = RegsEnum(TEST_ENUM_NAME, "InvalidValue", TEST_ENUM_DESCR, TEST_ENUM_MAXWIDTH) printed_str = str(enum) assert "N/A" in printed_str except TypeError: assert 0
def test_enum(): enum = RegsEnum(TEST_ENUM_NAME, 0, TEST_ENUM_DESCR) printed_str = str(enum) assert "Name:" in printed_str assert "Value:" in printed_str assert "Description:" in printed_str assert TEST_ENUM_NAME in printed_str assert "0b0" in printed_str assert TEST_ENUM_DESCR in printed_str
def test_bitfield_enums(): """Test bitfield enums.""" parent_reg = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) bitfield = RegsBitField( parent_reg, TEST_BITFIELD_NAME, TEST_BITFIELD_OFFSET, TEST_BITFIELD_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS, ) parent_reg.add_bitfield(bitfield) enums = [] for index in range((1 << TEST_BITFIELD_WIDTH) - 1): enum = RegsEnum(f"{TEST_ENUM_NAME}{index}", index, f"{TEST_ENUM_DESCR}{index}", TEST_BITFIELD_WIDTH) enums.append(enum) bitfield.add_enum(enum) enum_names = bitfield.get_enum_names() for index in range((1 << TEST_BITFIELD_WIDTH) - 1): assert index == bitfield.get_enum_constant(f"{TEST_ENUM_NAME}{index}") assert enums[index].name in enum_names for index in range((1 << TEST_BITFIELD_WIDTH)): bitfield.set_value(index) if index < (1 << TEST_BITFIELD_WIDTH) - 1: assert f"{TEST_ENUM_NAME}{index}" == bitfield.get_enum_value() else: assert index == bitfield.get_enum_value() for index in range((1 << TEST_BITFIELD_WIDTH) - 1): bitfield.set_enum_value(f"{TEST_ENUM_NAME}{index}") assert index == bitfield.get_value() for index in range((1 << TEST_BITFIELD_WIDTH) - 1): bitfield.set_enum_value(f"{index}") assert index == bitfield.get_value() with pytest.raises(SPSDKRegsErrorEnumNotFound): bitfield.get_enum_constant("Invalid name")
def test_bitfield_enums(): parent_reg = RegsRegister(TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS) bitfield = RegsBitField(parent_reg, TEST_BITFIELD_NAME, TEST_BITFILED_OFFSET, TEST_BITFILED_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS) parent_reg.add_bitfield(bitfield) enums = [] for n in range((1 << TEST_BITFILED_WIDTH) - 1): enum = RegsEnum(f"{TEST_ENUM_NAME}{n}", n, f"{TEST_ENUM_DESCR}{n}", TEST_BITFILED_WIDTH) enums.append(enum) bitfield.add_enum(enum) enum_names = bitfield.get_enum_names() for n in range((1 << TEST_BITFILED_WIDTH) - 1): assert n == bitfield.get_enum_constant(f"{TEST_ENUM_NAME}{n}") assert enums[n].name in enum_names for n in range((1 << TEST_BITFILED_WIDTH)): bitfield.set_value(n) if n < (1 << TEST_BITFILED_WIDTH) - 1: assert f"{TEST_ENUM_NAME}{n}" == bitfield.get_enum_value() else: assert n == bitfield.get_enum_value() for n in range((1 << TEST_BITFILED_WIDTH) - 1): bitfield.set_enum_value(f"{TEST_ENUM_NAME}{n}") assert n == bitfield.get_value() with pytest.raises(EnumNotFound): bitfield.get_enum_constant("Invalid name") regs = Registers(TEST_DEVICE_NAME) regs.add_register(parent_reg)
def test_enum_bytes(): enum = RegsEnum(TEST_ENUM_NAME, TEST_ENUM_VALUE_BYTES, TEST_ENUM_DESCR, TEST_ENUM_MAXWIDTH) printed_str = str(enum) assert TEST_ENUM_RES_VAL in printed_str
def test_enum_hex(): """Enum test with hexadecimal value.""" enum = RegsEnum(TEST_ENUM_NAME, TEST_ENUM_VALUE_HEX, TEST_ENUM_DESCR, TEST_ENUM_MAXWIDTH) printed_str = str(enum) assert TEST_ENUM_RES_VAL in printed_str
def test_enum_bin(): """Enum test with binary value.""" enum = RegsEnum(TEST_ENUM_NAME, TEST_ENUM_VALUE_BIN, TEST_ENUM_DESCR, TEST_ENUM_MAXWIDTH) printed_str = str(enum) assert TEST_ENUM_RES_VAL in printed_str
def test_enum_invalidval(): """Enum test with INVALID value.""" with pytest.raises(SPSDKRegsError): RegsEnum(TEST_ENUM_NAME, "InvalidValue", TEST_ENUM_DESCR, TEST_ENUM_MAXWIDTH)
def test_enum_bytes(): """Enum test with bytes array value.""" enum = RegsEnum(TEST_ENUM_NAME, TEST_ENUM_VALUE_BYTES, TEST_ENUM_DESCR, TEST_ENUM_MAXWIDTH) printed_str = str(enum) assert TEST_ENUM_RES_VAL in printed_str
def test_enum_int(): """Enum test with integer value.""" enum = RegsEnum(TEST_ENUM_NAME, TEST_ENUM_VALUE_INT, TEST_ENUM_DESCR, TEST_ENUM_MAXWIDTH) printed_str = str(enum) assert TEST_ENUM_RES_VAL in printed_str