def regSet(self, dev_type, addr, data): try: self._devAddrSet(dev_type) except Exception as e: self.logger.error("Set device address fail, error: " + str(e)) raise try: portio.iopl(3) except Exception as e: self.logger.error("LPC.regGet: iopl() enable failed") raise try: portio.ioperm(self.lpcAddr + addr, 4, 1) except Exception as e: self.logger.error("LPC.regGet: ioperm() enable failed") raise try: portio.outb(data, self.lpcAddr + addr) except Exception as e: self.logger.error("LPC.regGet: outb() failed") try: portio.ioperm(self.lpcAddr + addr, 4, 0) except Exception as e: self.logger.error("LPC.regGet: ioperm() disable failed") raise try: portio.iopl(0) except Exception as e: self.logger.error("LPC.regGet: iopl() disable failed") raise
def write(self, offset, bit, data): channel = 0 if offset == 1: channel = 1 elif offset != 0: raise Exception('offset in out of bounds') if bit < 0: # out whole byte self.do_state[channel] = 0x00ff & data portio.outb(0x00ff & data, self.base_addr + 16 + channel) else: # out bit only mask = 0x01 mask = mask << bit mask = mask & 0x00ff state = data << bit state = state & 0x00ff state = (state & mask) | (self.do_state[channel] & ~(mask)) portio.outb(state, self.base_addr + 16 + channel) self.do_state[channel] = state
def initializelocalports(): writetodebuglog("i","Initializing local I/O ports.") if hw == 0: GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(prt_i1,GPIO.IN) GPIO.setup(prt_i2,GPIO.IN) GPIO.setup(prt_i3,GPIO.IN) GPIO.setup(prt_i4,GPIO.IN) GPIO.setup(prt_ro1,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_ro2,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_ro3,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_ro4,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo1,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo2,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo3,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo4,GPIO.OUT,initial=GPIO.LOW) else: status = portio.ioperm(lptaddresses[lpt_prt],1,1) if status: writetodebuglog("e","ERROR #17: Cannot access I/O port:" + str(hex(lptaddresses[lpt_prt])) + "!") sys.exit(17) status = portio.ioperm(lptaddresses[lpt_prt] + 1,1,1) if status: writetodebuglog("e","ERROR #17: Cannot access I/O port:" + str(hex(lptaddresses[lpt_prt] + 1)) + "!") sys.exit(17) portio.outb(0,lptaddresses[lpt_prt])
def io_write(bar, offset, data): if not iopl_flag: acquire_io_access_permission() pass target = bar.addr + offset size = len(data) seek = 0 while seek < size: remains = size - seek if remains >= 4: val = struct.unpack('<I', data[seek:seek + 4])[0] portio.outl(val, target + seek) seek += 4 elif remains >= 2: val = struct.unpack('<H', data[seek:seek + 2])[0] portio.outw(val, target + seek) seek += 2 else: val = struct.unpack('<B', data[seek:seek + 1])[0] portio.outb(val, target + seek) seek += 1 pass continue return
def ec_read(self, port): try: self.ec_wait(self.EC_SC, self.IBF, 0) portio.outb(self.RD_EC, self.EC_SC) self.ec_wait(self.EC_SC, self.IBF, 0) portio.outb(port, self.EC_DATA) self.ec_wait(self.EC_SC, self.OBF, 1) result = portio.inb(self.EC_DATA) return result except TimeOutEcWait as terror: logger.error(terror) raise EcReadException("error reading port" + str(hex(port)))
def writelocalports(): if hw == 0: GPIO.output(prt_lo1,led_active) GPIO.output(prt_lo2,led_warning) GPIO.output(prt_lo3,led_error) GPIO.output(prt_ro1,relay_alarm) return 0 else: outdata = 64 * led_error + 32 * led_warning + 16 * led_active + relay_alarm portio.outb(outdata,lptaddresses[lpt_prt]) if (portio.inb(lptaddresses[lpt_prt]) == outdata): return 1 else: return 0
def write(self, offset, bit, data): select=0 if bit == 0: select=SELECT_COUNTER_0 elif bit == 1: select=SELECT_COUNTER_1 elif bit == 2: select=SELECT_COUNTER_2 self._write_counter_control_word(offset, select | RW_LSB_MSB | MODE_1 | BCD_16_BIT) chip = 0 if (offset >= 3): chip = 1 portio.outb(data, self.base_addr+8+offset+chip) portio.outb(data >> 8, self.base_addr+8+offset+chip)
def ec_write(self, port, value): try: self.ec_wait(self.EC_SC, self.IBF, 0) portio.outb(self.WR_EC, self.EC_SC) self.ec_wait(self.EC_SC, self.IBF, 0) portio.outb(port, self.EC_DATA) self.ec_wait(self.EC_SC, self.IBF, 0) portio.outb(value, self.EC_DATA) self.ec_wait(self.EC_SC, self.IBF, 0) except TimeOutEcWait as terror: logger.error(terror) raise EcWriteException("error write port" + str(hex(port)))
def get_azel(self): # for renishaw(El), for nikon(Az) byte_az = [0]*3 #dioOutputByte(CONTROLER_BASE0,0x03,0x04); #CONRROLER_BASE0 = 0xc000 portio.outb(0x04, (0x2050+0x03)) # Az time.sleep(3./1000) # need waiting #dioOutputByte(CONTROLER_BASE0,0x03,0x00); portio.outb(0x00, (0x2050+0x03)) # get data from board for i in range(3): # byte_az[i] = dioInputByte(CONTROLER_BASE0,i); byte_az[i] = portio.inb((0x2050+i)) # reverse byte # byte_az[i]=~byte_az[i];byte[2] is hugou 1keta+7keta suuji byte_az[i] = ~byte_az[i] & 0b11111111 self.Az = self.bin2dec_2s_complement(byte_az, 3) #for loop test f = open("enc_test_log.txt","w") f.write("get_az") f.close() portio.outb(2, 0x2006) cntEl = portio.inl(0x2000) b_num = bin(cntEl) b_num = b_num.lstrip("0b") if len(b_num) == 32: if int(b_num[0]) == 1: b_num = int(b_num, 2) cntEl = -(~b_num & 0b01111111111111111111111111111111) if cntEl > 0: encEl = int((324.*cntEl+295.)/590.) else: encEl = int((324.*cntEl-295.)/590.) self.El = encEl+45.*3600. #arcsecond #for loop test f = open("enc_test_log.txt","w") f.write("get_el") f.close() #print(self.Az/3600.) #print(self.El/3600.) return [self.Az, self.El]
def get_azel(self): # for renishaw(El), for nikon(Az) byte_az = [0] * 3 #dioOutputByte(CONTROLER_BASE0,0x03,0x04); #CONRROLER_BASE0 = 0xc000 portio.outb(0x04, (0x2050 + 0x03)) # Az time.sleep(3. / 1000) # need waiting #dioOutputByte(CONTROLER_BASE0,0x03,0x00); portio.outb(0x00, (0x2050 + 0x03)) # get data from board for i in range(3): # byte_az[i] = dioInputByte(CONTROLER_BASE0,i); byte_az[i] = portio.inb((0x2050 + i)) # reverse byte # byte_az[i]=~byte_az[i];byte[2] is hugou 1keta+7keta suuji byte_az[i] = ~byte_az[i] & 0b11111111 self.Az = self.bin2dec_2s_complement(byte_az, 3) #for loop test f = open("enc_test_log.txt", "w") f.write("get_az") f.close() portio.outb(2, 0x2006) cntEl = portio.inl(0x2000) b_num = bin(cntEl) b_num = b_num.lstrip("0b") if len(b_num) == 32: if int(b_num[0]) == 1: b_num = int(b_num, 2) cntEl = -(~b_num & 0b01111111111111111111111111111111) if cntEl > 0: encEl = int((324. * cntEl + 295.) / 590.) else: encEl = int((324. * cntEl - 295.) / 590.) self.El = encEl + 45. * 3600. #arcsecond #for loop test f = open("enc_test_log.txt", "w") f.write("get_el") f.close() #print(self.Az/3600.) #print(self.El/3600.) return [self.Az, self.El]
def SetPortByte(adr,data): if os.name == 'nt': ctypes.windll.inpout32.Out32(adr, data) if os.name == 'posix': if (not os.getuid()): portio.outb(data,adr)
def _set_counter_clock_input_mode(self, counter, mode): portio.outb(mode, self.base_addr+18+counter);
def closelocalports(): writetodebuglog("i","Close local I/O ports.") if hw == 0: GPIO.cleanup else: portio.outb(0,lptaddresses[lpt_prt])
# # Distributed under the terms of the GNU General Public License (GPL). # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import portio # init that stuff status_a = portio.ioperm(0x378, 1, 1) status_b = portio.ioperm(0x379, 1, 1) if status_a and status_b: print 'ioperm 0x378:', os.strerror(status_a) print 'ioperm 0x379:', os.strerror(status_b) sys.exit() portio.outb(0x0, 0x378) # output the button box to standard out while True: print portio.inb(0x379)
def __init__(self): if portio.ioperm(0x123, 1, 1): raise Exception("Could not gain IO permissions!") self.value = 0 portio.outb(self.value, 0x123)
GPIO.setup(prt_ro3,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_ro4,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo1,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo2,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo3,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(prt_lo4,GPIO.OUT,initial=GPIO.LOW) else: status = portio.ioperm(lptaddresses[lpt_prt],1,1) if status: print("ERROR #17: Cannot access I/O port:",hex(lptaddresses[lpt_prt])); sys.exit(17) status = portio.ioperm(lptaddresses[lpt_prt] + 1, 1, 1) if status: print("ERROR #17: Cannot access I/O port:",hex(lptaddresses[lpt_prt] + 1)); sys.exit(17) portio.outb(0,lptaddresses[lpt_prt]) while True: print(" * What do you like?") selection = input(" \ 1: Check I1-4 inputs\n \ 2: Check RO1-4 relay contact outputs\n \ 3: Check LO1-4 open collector outputs\n \ q: Quit\n") if selection is "Q" or selection is "q": print(" * Quitting.") if hw == 0: GPIO.cleanup() else: portio.outb(0,lptaddresses[lpt_prt]) sys.exit(0)
def low(self): self.value &= 0b10111111 portio.outb(self.value, 0x123)
def high(self): self.value |= 0b01000000 portio.outb(self.value, 0x123)
# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # .- import sys, time, os import portio # check for root privileges if os.getuid(): print 'You need to be root! Exiting.' sys.exit() # acquire permission for I/O on lp0 status = portio.ioperm(0x378, 1, 1) if status: print 'ioperm:',os.strerror(status) sys.exit() # toggle for ever the data lines of lp0 data = 0 while 1: lp0in = portio.inb(0x378) portio.outb(data,0x378) print 'read %x from lp0, written %x to lp0' % (lp0in,data) data = ~data & 0xff time.sleep(3) #### END
def on(self): self.value |= 0b00100000 portio.outb(self.value, 0x123)
def off(self): self.value &= 0b11011111 portio.outb(self.value, 0x123)
def _write_counter_control_word(self, counter, control_word): chip = 0; if counter >= 3: chip = 1 portio.outb(control_word, self.base_addr+11+chip*4)
def _write_fout_control_word(self, fout, control_word): chip = 0; if fout >= 3: chip = 1 portio.outb(control_word, self.base_addr+3+chip*4);