def rdwr(self, dev_addr, write_data_list, read_length): logger.debug( "rdwr i2c device %s addr:0x%x write length:%d data:%s read length:%d" % (self._name, dev_addr, len(write_data_list), logger.print_list2hex(write_data_list), read_length)) if not self.__open(): return False write_length = len(write_data_list) write_data = (ctypes.c_ubyte * write_length)(*write_data_list) read_data = (ctypes.c_char * read_length)() status = self._bus.sg_i2c_rdwr(self._fd, dev_addr, write_data, write_length, read_data, read_length) self.__close() if (status < 0): logger.warning( "rdwr i2c device %s addr:0x%x write length:%d data:%s read length:%d fail" % (self._name, dev_addr, len(write_data_list), logger.print_list2hex(write_data_list), read_length)) return False data = list(struct.unpack('%dB' % read_length, read_data.raw)) return data
def _net_to_uart(self): try: data = self._connect_socket.recv(2048) except socket.error as e: logger.warning("read %d socket error:%s" % (self._connect_socket.fileno(), repr(e))) self._del_connect_socket() return 0 if len(data) == 0: self._del_connect_socket() return 0 self._total_net_size += len(data) logger.info("recev %s net byte size %d total recev :%d"%(self._uartname ,len(data),\ self._total_net_size)) logger.info("net to uart data:%s" % (logger.print_list2hex( list(bytearray.fromhex(data.encode("hex")))))) try: self._uart_device.write((data)) except Exception as e: logger.warning("write %s device error %s" % (self._uartname, repr(e))) return 0 return 1
def _tcp_to_device(self): try: data = self._connect_socket.recv(2048) except socket.error as e: logger.warning("read %d socket error:%s" % (self._connect_socket.fileno(), repr(e))) self._del_connect_socket() return 0 if len(data) == 0: self._del_connect_socket() return 0 self._total_net_size += len(data) logger.info("recev tcp port %s byte size %d, total recev :%d"%(self._port ,len(data),\ self._total_net_size)) logger.info("tcp to device data:%s" % (logger.print_list2hex( list(bytearray.fromhex(data.encode("hex")))))) try: self._device.write((data)) except Exception as e: logger.warning("device write data byte size %d error :%s" % (len(data), repr(e))) return 0 return 1
def write(self, addr, data, length): if self._base_addr == 0: logger.error("fpga device:%s not opened" % (self._name)) return False logger.debug("write fpga device:%s addr:0x%x length:%d data: %s" % (self._name, addr, length, logger.print_list2hex(data))) write_data = (ctypes.c_ubyte * length)(*data) status = self._libaxi4lite.sg_fpga_write(self._base_addr + addr, write_data, length) if (status < 0): logger.warning( "write fpga device:%s addr:0x%x length:%d data: %s fail" % (self._name, addr, length, logger.print_list2hex(data))) return False return True
def write(self, dev_addr, data): logger.debug( "write i2c device %s addr:0x%x length:%d data:%s" % (self._name, dev_addr, len(data), logger.print_list2hex(data))) if not self.__open(): return False length = len(data) write_data = (ctypes.c_ubyte * length)(*data) status = self._bus.sg_i2c_write(self._fd, dev_addr, write_data, length) self.__close() if (status < 0): logger.warning( "write i2c device %s addr:0x%x length:%d data:%s fail" % (self._name, dev_addr, len(data), logger.print_list2hex(data))) return False return True
def _init_extendio(): ret_value = True extendios = Profile.get_extendio() initconfig = Profile.get_initconfig() for board_name, extendio_profile in extendios.iteritems(): for chip_name, bit in initconfig['extendio'][board_name].iteritems(): try: partno = extendio_profile[chip_name]['partno'] if Extendio.chip_dir_set(extendio_profile, chip_name, bit['dir']) is True: ret = Extendio.chip_set(extendio_profile, chip_name, bit['value']) if ret is False: logger.boot( 'error: set the %s board %s chip %s value:%s fail' % (board_name, partno, chip_name, logger.print_list2hex(bit['value']))) ret_value = False else: logger.boot( 'init the %s board extendio chip %s success, value is %s, direction is %s' % (board_name, chip_name, logger.print_list2hex(bit['value']), logger.print_list2hex(bit['dir']))) else: logger.boot( 'error: set the %s board %s chip %s direction:%s fail' % (board_name, partno, chip_name, logger.print_list2hex(bit['dir']))) ret_value = False except Exception: logger.boot("error: %s _init_extendio execute error:\n%s" % (chip_name, traceback.format_exc())) ret_value = False return ret_value
def read(self, addr, length): if self._base_addr == 0: logger.error("fpga device:%s not opened" % (self._name)) return False read_data = (ctypes.c_char * length)() status = self._libaxi4lite.sg_fpga_read(self._base_addr + addr, read_data, length) if (status < 0): logger.info("read fpga device:%s addr:0x%x length:%s fail" % (self._name, addr, length)) return False data = list(struct.unpack('%dB' % length, read_data.raw)) logger.debug("read fpga device:%s addr:0x%x length:%s data : %s " % (self._name, addr, length, logger.print_list2hex(data))) return data
def write(self, register_addr, data_list): """ Write data to register Args: register_addr: int 0x00-0x07 data_list :[data0,data1,...,datan] data_n (0x00-0xff) Returns: bool: True | False, True for success, False for adc read failed """ logger.debug("cat9555 device write addr :" + hex(register_addr) + " data:" + logger.print_list2hex(data_list)) write_data = [] write_data.append(register_addr) write_data += data_list[0:2] result = self.iic_bus.write(self._device_addr, write_data) return result
def _uart_to_net(self): uart_data = self._uart_device.read() if self._connect_socket is None: return 0 if not uart_data: return 0 else: self._total_uart_size += len(uart_data) logger.info(" read %s data byte size :%d total read size : %d"%\ (self._uartname,len(uart_data),self._total_uart_size)) logger.info("uart to net data:%s" % (logger.print_list2hex( list(bytearray.fromhex(uart_data.encode("hex")))))) try: self._connect_socket.send(bytes(uart_data)) logger.info("client %d send byte size : %d" % (self._connect_socket.fileno(), len(uart_data))) except socket.error as e: logger.warning("device %s net send error" % (self._uartname)) self._del_connect_socket() return 1
def _device_to_tcp(self): device_data = self._device.read() if self._connect_socket is None: return 0 if not device_data: return 0 else: self._total_device_size += len(device_data) logger.info("device read data byte size :%d, total read size : %d"%\ (len(device_data),self._total_device_size)) logger.info("device to tcp data:%s" % (logger.print_list2hex( list(bytearray.fromhex(device_data.encode("hex")))))) try: self._connect_socket.send(bytes(device_data)) logger.info("client %d send byte size : %d" % (self._connect_socket.fileno(), len(device_data))) except socket.error as e: logger.warning("device send to tcp port %s error" % (self._port)) self._del_connect_socket() return 1