Example #1
0
def io_read(bar, offset, size):
    if not iopl_flag:
        acquire_io_access_permission()
        pass

    target = bar.addr + offset

    ret = b''
    seek = 0
    while seek < size:
        remains = size - seek

        if remains >= 4:
            r = portio.inl(target + seek)
            ret += struct.pack('<I', r)
            seek += 4

        elif remains >= 2:
            r = portio.inw(target + seek)
            ret += struct.pack('<H', r)
            seek += 2

        else:
            r = portio.inb(target + seek)
            ret += struct.pack('<B', r)
            seek += 1
            pass
        continue

    return ret
Example #2
0
def readlocalports():
  global mainssensor
  global mainsbreaker1
  global mainsbreaker2
  global mainsbreaker3
  if hw == 0:
    GPIO.input(prt_i1,mainssensor)
    GPIO.input(prt_i2,mainsbreaker1)
    GPIO.input(prt_i3,mainsbreaker2)
    GPIO.input(prt_i4,mainsbreaker3)
  else:
    indata = portio.inb(lptaddresses[lpt_prt] + 1)
    mainssensor = indata & 8
    if mainssensor > 1:
      mainssensor = 1
    mainsbreaker1 = indata & 16
    if mainsbreaker1 > 1:
      mainsbreaker1 = 1
    mainsbreaker2 = indata & 32
    if mainsbreaker2 > 1:
      mainsbreaker2 = 1
    mainsbreaker3 = indata & 64
    if mainsbreaker3 > 1:
      mainsbreaker3 = 1
  return 1
Example #3
0
 def read(self, offset, bit):
     b = portio.inb(self.base_addr+16+offset)
     if bit < 0:
         return 0x00ff & b
     
     b = b << (7 - bit)
     b = b & 0x00ff
     b = b >> 7
     
     return b
Example #4
0
 def read(self, offset, bit):
     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
     low = portio.inb(base+8+offset+chip)
     hi  = portio.inb(base+8+offset+chip)
     
     self.write(offset, bit, MAX_COUNTER_VALUE)
     
     return low + (hi << 8)
Example #5
0
 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)))
Example #6
0
 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]
Example #7
0
    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]
Example #8
0
 def ec_wait(self, port, flag, value):
     logger.info("ec_wait")
     condition = False
     for i in range(100):
         data = portio.inb(port)
         if ((data >> flag) & 0x1) == value:
             condition = True
             break
         time.sleep(0.001)
     if not condition:
         logger.warning("timeout in ec_wait")
         raise TimeOutEcWait("timeout in ec_wait")
     else:
         logger.info("ec_wait correct")
Example #9
0
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
Example #10
0
    def regGet(self, dev_type, addr):
        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:
            getValue = portio.inb(self.lpcAddr + addr)
        except Exception as e:
            self.logger.error("LPC.regGet: inb() 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

        return getValue
Example #11
0
#!/usr/bin/env python

import sys, time, os
import portio

if os.getuid():
    print 'you need to be root! Exiting.'
    sys.exit()

status = portio.ioperm(0xEE0, 1, 1)
if status:
    print 'ioperm:', os.strerror(status)
    sys.exit()
while 1:
    ignition = portio.inb(0x0EE0) & 4

    if ignition == 4:
        print 'ignition ok', ignition
        time.sleep(60)
    else:
        os.system("shutdown now -h")
Example #12
0
File: test.py Project: S010/misc
import sys,portio

if portio.enable_io():
	print "Access to I/O ports successfully enabled"
else:
	print "Failed to enable access to I/O ports (try running as root)"
	sys.exit(1)

port = 0x378
print "Reading port 0x%x: 0x%x"%(port, portio.inb(port))
Example #13
0
#
# 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)
Example #14
0
            s = s + "0"
          print(s)
          time.sleep(1)
      except KeyboardInterrupt:
        print()
    else:
      print("   used lines of LPT port:")
      print('     LPT #',lpt_prt + 1,sep = '')
      print("     I1: -ERROR")
      print("     I2: SELECT")
      print("     I3: PE")
      print("     I4: -ACKNOLEDGE")
      print("   Press ^C to stop!")
      try:
        while True:
          indata = portio.inb(lptaddresses[lpt_prt] + 1)
          indata = indata << 1
          indata = indata >> 4
          print("    ",format(indata,'04b'))
          time.sleep(1)
      except KeyboardInterrupt:
        print()

  if selection is "2":
    print(" * Check RO1-4 relay contact outputs")
    if hw == 0:
      print("   used GPIO ports:")
      print("     RO1: GPIO", prt_ro1,sep = '')
      print("     RO2: GPIO", prt_ro2,sep = '')
      print("     RO3: GPIO", prt_ro3,sep = '')
      print("     RO4: GPIO", prt_ro4,sep = '')
Example #15
0
#
# 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
Example #16
0
#
# 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)