def spi_config(bus_name, clk_frequency, clk_type, wait_time_us=1, spi_clk_polarity='high'): logger.warning( "spi config -->bus_name:%s, clk_frequency:%s, clk_type:%s, wait_time_us:%s " % (bus_name, clk_frequency, clk_type, wait_time_us)) """ Set spi bus parameter Args: spi_clk_frequency(int): spi bus clock frequency, unit is Hz spi_clk_type(str): 'pos' --sclk posedge send data, sclk negedge receive data 'neg' --sclk negedge send data, sclk posedge receive data wait_time_us(float): the wait time for new spi access spi_clk_polarity(str): 'high' --when CS is high, the SCLK is high 'low' --when CS is high, the SCLK is low Returns: None """ try: profile = Profile.get_bus_by_name(bus_name) bus = SpiFactory.create_object(SpiFactory, profile["path"]) return bus.config(clk_frequency, clk_type, wait_time_us, spi_clk_polarity) except Exception as e: logger.error("execute module spi_config False:" + repr(e)) return False
def spi_slave_enable(bus_name): logger.debug("spi slave enable -->bus name:%s "%(bus_name)) try: profile = Profile.get_bus_by_name(bus_name) bus = SpiSlaveFactory.create_object(SpiSlaveFactory, profile["path"]) return bus.enable() except Exception as e: logger.error("execute module spi_slave_enable False:" + repr(e)) return False
def spi_disable(bus_name): logger.warning("spi disable -->bus name:%s " % (bus_name)) try: profile = Profile.get_bus_by_name(bus_name) bus = SpiFactory.create_object(SpiFactory, profile["path"]) return bus.disable() except Exception as e: logger.error("execute module spi_disable False:" + repr(e)) return False
def test_get_bus_by_name(): ps_i2c_0 = Profile.get_bus_by_name('ps_i2c_0') assert ps_i2c_0['bus'] == 'i2c' assert ps_i2c_0['path'] == '/dev/i2c-0' assert ps_i2c_0['rate'] == '100000' UUT_UART = Profile.get_bus_by_name('UUT_UART') assert UUT_UART['bus'] == 'uart' assert UUT_UART['path'] == '/dev/AXI4_UUT_UART' assert UUT_UART['baudrate'] == '115200' assert UUT_UART['ipcore'] == 'Axi4Uart' INSTRUMENT_IIC_2 = Profile.get_bus_by_name('INSTRUMENT_IIC_2') assert INSTRUMENT_IIC_2['bus'] == 'i2c' assert INSTRUMENT_IIC_2['path'] == '/dev/AXI4_INSTRUMENT_IIC_2' assert INSTRUMENT_IIC_2['rate'] == '400000' assert INSTRUMENT_IIC_2['ipcore'] == 'Axi4I2c' EEPROM_IIC = Profile.get_bus_by_name('EEPROM_IIC') assert EEPROM_IIC['bus'] == 'i2c' assert EEPROM_IIC['path'] == '/dev/AXI4_EEPROM_IIC' assert EEPROM_IIC['rate'] == '400000' assert EEPROM_IIC['ipcore'] == 'Axi4I2c'
def swd_freq_set(bus_id, freq_data): logger.warning("swd freq set ---> %s" % list((bus_id, freq_data))) """ Set swd clk freq parameter Args: freq_data(int): swd bus clock frequency, unit is Hz Returns: None """ try: profile = Profile.get_bus_by_name(bus_id) bus = SwdFactory.create_object(SwdFactory, profile["path"]) return bus.swd_freq_set(freq_data) except Exception as e: logger.error("execute module swd freq set False:" + repr(e)) return False
def swd_rst_set(bus_id, level): logger.warning("swd rst set ---> %s" % list((bus_id, level))) """ swd debug rst ctrl Args: level(string): 'L'--Low level,'H'--High level Returns: None """ try: profile = Profile.get_bus_by_name(bus_id) bus = SwdFactory.create_object(SwdFactory, profile["path"]) return bus.swd_rst_pin_ctrl(level) except Exception as e: logger.error("execute module swd rst set False:" + repr(e)) return False return False
def swd_timing_generate(bus_id, timing_data): logger.warning("swd timing generate ---> %s, len:%s" % ([('0x%02x' % i) for i in timing_data], len(timing_data))) """ swd_switch_timing_generate Args: timing_data(list): timing_data, bit order: first_byte_bit0-->bit7,second_byte_bit0-->bit7,...,last_byte_bit0-->bit7 Returns: False | True """ try: profile = Profile.get_bus_by_name(bus_id) bus = SwdFactory.create_object(SwdFactory, profile["path"]) return bus.swd_switch_timing_gen(timing_data) except Exception as e: logger.error("execute module swd rst set False:" + repr(e)) return False return False
def i2c_config(bus_id, rate): logger.debug("bus:%s, rate:%s" % (bus_id, rate)) """ Args: bus_id: str type, i2c bus id. rate: int type. Returns: return None """ try: profile = Profile.get_bus_by_name(bus_id) bus = I2cBus(profile["path"]) return bus.config(rate) except Exception as e: logger.error("execute module i2c config False:" + repr(e)) return False
def spi_slave_write(bus_name, address, write_data): logger.debug("spi slave write -->bus name:%s, address:%s, write_data:%s "%(bus_name,address,write_data)) """ write data in spi bus Args: bus_name: str type, spi bus id. address: int type write_datas: list type. Returns: bool: The return value. True for success, False otherwise. """ try: profile = Profile.get_bus_by_name(bus_name) bus = SpiSlaveFactory.create_object(SpiSlaveFactory, profile["path"]) return bus.register_write(address, write_data) except Exception as e: logger.error("execute module spi_slave_write False:" + repr(e)) return False
def spi_write(bus_name, write_datas, cs_extend): logger.warning("spi write -->bus_name:%s, write_datas:%s, cs_extend:%s " % (bus_name, write_datas, cs_extend)) """ write data in spi bus Args: bus_name: str type, spi bus id. write_datas: list type. cs_extend: switch channel, default is 0 Returns: bool: The return value. True for success, False otherwise. """ try: profile = Profile.get_bus_by_name(bus_name) bus = SpiFactory.create_object(SpiFactory, profile["path"]) return bus.write(write_datas, cs_extend) except Exception as e: logger.error("execute module spi_write False:" + repr(e)) return False
def spi_slave_read(bus_name, address, read_length): logger.debug("spi slave read -->bus name:%s, address:%s, read_length:%s "%(bus_name,address,read_length)) """ read data in spi bus Args: bus_name: str type, spi bus id. address: int type read_length: int type. Returns: success : return data of list type. if read fail, return False. """ try: profile = Profile.get_bus_by_name(bus_name) bus = SpiSlaveFactory.create_object(SpiSlaveFactory, profile["path"]) return bus.register_read(address, read_length) except Exception as e: logger.error("execute module spi_slave_read False:" + repr(e)) return False
def i2c_write(bus_id, dev_addr, data, channel=None): """ write data in i2c bus Args: bus_id: str type, i2c bus id. dev_addr: hex type, devixe addr. data: list type. channel: switch channel, default is None Returns: bool: The return value. True for success, False otherwise. """ try: profile = Profile.get_bus_by_name(bus_id) channel = 'none' if channel == None else channel bus = I2cBus(profile["path"], channel) return bus.write(dev_addr, data) except Exception as e: logger.error("execute module i2c write False:" + repr(e)) return False
def spi_read(bus_name, read_length, cs_extend): logger.warning("spi read -->bus_name:%s, read_length:%s, cs_extend:%s " % (bus_name, read_length, cs_extend)) """ read data in spi bus Args: bus_name: str type, spi bus id. read_length: int type. cs_extend: switch channel, default is 0 Returns: success : return data of list type. if read fail, return False. """ try: profile = Profile.get_bus_by_name(bus_name) bus = SpiFactory.create_object(SpiFactory, profile["path"]) return bus.read(read_length, cs_extend) except Exception as e: logger.error("execute module spi_read False:" + repr(e)) return False
def swd_write(bus_id, request_data, write_data): logger.warning("swd write ---> %s" % list( (bus_id, request_data, hex(write_data)))) """ swd_wr_operate Args: request_data(byte): 8 bits write_data(int): less than 32 bits Returns: False | SWD RETURN ACK DATA(byte) """ try: profile = Profile.get_bus_by_name(bus_id) bus = SwdFactory.create_object(SwdFactory, profile["path"]) logger.warning("swd write data list---> %s" % utility.int_convert_list(write_data, 4)) return bus.swd_wr_operate(request_data, (utility.int_convert_list(write_data, 4))) except Exception as e: logger.error("execute module swd write False:" + repr(e)) return False
def i2c_read(bus_id, dev_addr, length, channel=None): """ read data from i2c bus Args: bus_id: str type, i2c bus id. dev_addr: hex type, devixe addr. length: int type. channel: switch channel, default is None Returns: success : return data of list type. if read fail, return False. """ try: profile = Profile.get_bus_by_name(bus_id) channel = 'none' if channel == None else channel bus = I2cBus(profile["path"], channel) return bus.read(dev_addr, length) except Exception as e: logger.error("execute module i2c read False:" + repr(e)) return False
def spi_write_and_read(bus_name, write_datas, cs_extend): logger.warning( "spi write and read -->bus_name:%s, write_datas:%s, cs_extend:%s " % (bus_name, write_datas, cs_extend)) """ write data and read data at time in spi bus Args: bus_name: str type, spi bus id. write_datas: list type. cs_extend: switch channel, default is 0 Returns: success : return data of list type. if read fail, return False. """ try: profile = Profile.get_bus_by_name(bus_name) bus = SpiFactory.create_object(SpiFactory, profile["path"]) return bus.write_and_read(write_datas, cs_extend) except Exception as e: logger.error("execute module spi_write_and_read False:" + repr(e)) return False
def i2c_rdwr(bus_id, dev_addr, write_data, read_length, channel=None): """ write_data is a data list , read_lenght ,the size of your will read Args: bus_id: str type, i2c bus id. dev_addr: hex type, devixe addr. write_data: list type. read_length: int type. channel: switch channel, default is None Returns: success : return data of list type. if read fail, return False. """ try: profile = Profile.get_bus_by_name(bus_id) channel = 'none' if channel == None else channel bus = I2cBus(profile["path"], channel) return bus.rdwr(dev_addr, write_data, read_length) except Exception as e: logger.error("execute module i2c rdwr False:" + repr(e)) return False
def i2c_crol(bus_id, state): logger.debug("bus:%s, state:%s" % (bus_id, state)) """ Args: bus_id: str type, i2c bus id. state: (0,1),0:disable 1:enable. Returns: return None """ try: profile = Profile.get_bus_by_name(bus_id) #bus = I2cBus(profile["path"]) bus = I2cMasterFactory.create_object(I2cMasterFactory, profile["path"]) if state == 0: logger.error("i2c_crol disable") return bus.disable() else: return bus.enable() except Exception as e: logger.error("execute module i2c crol False:" + repr(e)) return False
def swd_read(bus_id, request_data): logger.warning("swd read ---> %s" % list((bus_id, hex(request_data)))) """ swd_rd_operate Args: request_data(byte): 8 bits Returns: False | swd_rd_data(tuple): (int read_data, char ack_data, char parity_bit) """ try: profile = Profile.get_bus_by_name(bus_id) bus = SwdFactory.create_object(SwdFactory, profile["path"]) frame_data = bus.swd_rd_operate(request_data) if frame_data is not False: read_data = frame_data[:4] ack_data = frame_data[4] & 0x07 parity_bit = (frame_data[4] >> 7) & 0x01 return (utility.list_convert_number(read_data), ack_data, parity_bit) return False except Exception as e: logger.error("execute module swd read False:" + repr(e)) return False
def spi_slave_config(bus_name, spi_clk_cpha_cpol = 'Mode_0',spi_byte_cfg = '1'): logger.debug("spi slave config -->bus name:%s, spi_clk_cpha_cpol:%s, spi_byte_cfg:%s "%(bus_name,spi_clk_cpha_cpol,spi_byte_cfg)) """ Set spi bus parameter Args: spi_byte_cfg(str): '1' --spi slave receive data or send data is 1byte '2' --spi slave receive data or send data is 2byte '3' --spi slave receive data or send data is 3byte '4' --spi slave receive data or send data is 4byte spi_clk_cpha_cpol(str): 'Mode_0' --CPHA=0, CPOL=0, when CS is high, the SCLK is low, first edge sample 'Mode_1' --CPHA=0, CPOL=1, when CS is high, the SCLK is high, first edge sample 'Mode_2' --CPHA=1, CPOL=0, when CS is high, the SCLK is low, second edge sample 'Mode_3' --CPHA=1, CPOL=1, when CS is high, the SCLK is high, second edge sample Returns: None """ try: profile = Profile.get_bus_by_name(bus_name) bus = SpiSlaveFactory.create_object(SpiSlaveFactory, profile["path"]) return bus.config(spi_clk_cpha_cpol, spi_byte_cfg) except Exception as e: logger.error("execute module spi_slave_config False:" + repr(e)) return False